-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
514 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { readFile } from "fs/promises"; | ||
import { taskOne, taskTwo } from "./task"; | ||
|
||
async function main() { | ||
let lastArgument = process.argv.pop() as string; | ||
let taskNumber = 1; | ||
let isTest = false; | ||
|
||
if (lastArgument === "test") { | ||
isTest = true; | ||
taskNumber = parseInt(process.argv.pop() as string); | ||
} else { | ||
taskNumber = parseInt(lastArgument) | ||
} | ||
|
||
const fileToLoad = isTest ? "test.in" : "solve.in"; | ||
const fileContents = await readFile(fileToLoad, "utf-8") | ||
|
||
const lines = fileContents.split("\n"); | ||
|
||
if (taskNumber === 1) { | ||
await taskOne(lines); | ||
} | ||
if (taskNumber === 2) { | ||
await taskTwo(lines); | ||
} | ||
} | ||
|
||
void main(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
const W = 150 | ||
|
||
export async function taskOne(input: string[]): Promise<void> { | ||
const w = input.map(Number); | ||
console.log(next(w, 0, 0)); | ||
|
||
function next(w: number[], i: number, s: number): number { | ||
if (s > W) return 0 | ||
if (i === w.length) return s == W ? 1 : 0; | ||
|
||
return next(w, i + 1, s + w[i]) + next(w, i + 1, s); | ||
} | ||
} | ||
|
||
export async function taskTwo(input: string[]): Promise<void> { | ||
const w = input.map(Number); | ||
console.log(next(w, 0, 0, 0)); | ||
|
||
function next(w: number[], i: number, s: number, c: number): number { | ||
if (s > W) return Infinity | ||
if (i === w.length) return s == W ? c : Infinity; | ||
|
||
return Math.min(next(w, i + 1, s + w[i], c+1), next(w, i + 1, s, c)); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { readFile } from "fs/promises"; | ||
import { taskOne, taskTwo } from "./task"; | ||
|
||
async function main() { | ||
let lastArgument = process.argv.pop() as string; | ||
let taskNumber = 1; | ||
let isTest = false; | ||
|
||
if (lastArgument === "test") { | ||
isTest = true; | ||
taskNumber = parseInt(process.argv.pop() as string); | ||
} else { | ||
taskNumber = parseInt(lastArgument) | ||
} | ||
|
||
const fileToLoad = isTest ? "test.in" : "solve.in"; | ||
const fileContents = await readFile(fileToLoad, "utf-8") | ||
|
||
const lines = fileContents.split("\n"); | ||
|
||
if (taskNumber === 1) { | ||
await taskOne(lines); | ||
} | ||
if (taskNumber === 2) { | ||
await taskTwo(lines); | ||
} | ||
} | ||
|
||
void main(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
const SIZE = 100; | ||
const g = Array.from({length:101}, () => Array.from({length: SIZE + 2}, () => Array.from({length: SIZE + 2}, () => false))) | ||
|
||
export async function taskOne(input: string[]): Promise<void> { | ||
parse(input) | ||
|
||
for (let s = 0; s < 100; s++) { | ||
for (let x = 1; x <= SIZE; x++) { | ||
for (let y = 1; y <= SIZE; y++) { | ||
step(x,y,s) | ||
} | ||
} | ||
} | ||
let c = 0 | ||
for (let x = 1; x <= SIZE; x++) { | ||
for (let y = 1; y <= SIZE; y++) { | ||
if (g[100][x][y]) c++ | ||
} | ||
} | ||
console.log(c) | ||
|
||
|
||
function step(x: number, y: number, t: number) { | ||
let c = 0 | ||
for (let dx = -1; dx <= 1; dx++) { | ||
for (let dy = -1; dy <= 1; dy++) { | ||
if (dx == 0 && dy == 0) continue | ||
if(g[t][x+dx][y+dy]) c++ | ||
} | ||
} | ||
g[t+1][x][y] = g[t][x][y] && (c==2 || c==3) || !g[t][x][y] && c==3 | ||
} | ||
} | ||
|
||
export async function taskTwo(input: string[]): Promise<void> { | ||
parse(input) | ||
|
||
for (let s = 0; s < 100; s++) { | ||
for (let x = 1; x <= SIZE; x++) { | ||
for (let y = 1; y <= SIZE; y++) { | ||
step(x,y,s) | ||
} | ||
} | ||
} | ||
let c = 0 | ||
for (let x = 1; x <= SIZE; x++) { | ||
for (let y = 1; y <= SIZE; y++) { | ||
if (g[100][x][y]) c++ | ||
} | ||
} | ||
console.log(c) | ||
|
||
|
||
function step(x: number, y: number, t: number) { | ||
if (x==1 || x == SIZE) { | ||
if(y == 1 || y == SIZE) { | ||
g[t+1][x][y] = true | ||
return | ||
} | ||
} | ||
let c = 0 | ||
for (let dx = -1; dx <= 1; dx++) { | ||
for (let dy = -1; dy <= 1; dy++) { | ||
if (dx == 0 && dy == 0) continue | ||
if(g[t][x+dx][y+dy]) c++ | ||
} | ||
} | ||
g[t+1][x][y] = g[t][x][y] && (c==2 || c==3) || !g[t][x][y] && c==3 | ||
} | ||
} | ||
|
||
function parse(input: string[]) { | ||
for (let x = 1; x <= SIZE; x++) { | ||
for (let y = 1; y <= SIZE; y++) { | ||
g[0][x][y] = input[x-1][y-1] == '#' | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { readFile } from "fs/promises"; | ||
import { taskOne, taskTwo } from "./task"; | ||
|
||
async function main() { | ||
let lastArgument = process.argv.pop() as string; | ||
let taskNumber = 1; | ||
let isTest = false; | ||
|
||
if (lastArgument === "test") { | ||
isTest = true; | ||
taskNumber = parseInt(process.argv.pop() as string); | ||
} else { | ||
taskNumber = parseInt(lastArgument) | ||
} | ||
|
||
const fileToLoad = isTest ? "test.in" : "solve.in"; | ||
const fileContents = await readFile(fileToLoad, "utf-8") | ||
|
||
const lines = fileContents.split("\n"); | ||
|
||
if (taskNumber === 1) { | ||
await taskOne(lines); | ||
} | ||
if (taskNumber === 2) { | ||
await taskTwo(lines); | ||
} | ||
} | ||
|
||
void main(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { Stack, Queue } from '../../base/simpleStructure' | ||
|
||
const M: Record<string, string[]> = {} | ||
const P: Prod[] = [] | ||
|
||
export async function taskOne(input: string[]): Promise<void> { | ||
parse(input) | ||
|
||
const initial = input[input.length-1] | ||
const S: Set<string> = new Set() | ||
for (let i = 0; i < initial.length; i++) { | ||
const o1 = initial[i] | ||
const o2 = initial[i] + initial[i+1] | ||
//console.log(i, o1, o2) | ||
if (M[o1]) for (let j = 0; j < M[o1].length; j++) S.add(rep(o1, M[o1][j], i, initial)) | ||
|
||
if (M[o2]) for (let j = 0; j < M[o2].length; j++) S.add(rep(o2, M[o2][j], i,initial)) | ||
//console.log(S) | ||
} | ||
console.log(S.size) | ||
|
||
|
||
} | ||
|
||
export async function taskTwo(input: string[]): Promise<void> { | ||
parse(input) | ||
const GOAL = input[input.length-1] | ||
|
||
let cur = GOAL | ||
let c = 0 | ||
while(cur != 'e') { | ||
c++ | ||
for (const p of P) { | ||
if (cur.includes(p.g)) { | ||
cur = cur.replace(p.g, p.s) | ||
break; | ||
} | ||
} | ||
} | ||
console.log(c) | ||
|
||
} | ||
|
||
function parse(input: string[]) { | ||
input.slice(0, input.length-2).forEach(i => { | ||
const r = /([A-Za-z]+) => ([A-Za-z]+)/.exec(i) | ||
if (!r) throw i | ||
if(!M[r[1]]) M[r[1]] = [] | ||
M[r[1]].push(r[2]) | ||
P.push({g:r[2], s: r[1]}) | ||
}) | ||
P.sort((a,b) => b.g.length-a.g.length) | ||
} | ||
|
||
interface Prod { g: string, s: string } | ||
|
||
function rep(o: string, r: string, i:number, rep: string) { | ||
return rep.substring(0,i) + rep.substring(i).replace(o, r) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { readFile } from "fs/promises"; | ||
import { taskOne, taskTwo } from "./task"; | ||
|
||
async function main() { | ||
let lastArgument = process.argv.pop() as string; | ||
let taskNumber = 1; | ||
let isTest = false; | ||
|
||
if (lastArgument === "test") { | ||
isTest = true; | ||
taskNumber = parseInt(process.argv.pop() as string); | ||
} else { | ||
taskNumber = parseInt(lastArgument) | ||
} | ||
|
||
const fileToLoad = isTest ? "test.in" : "solve.in"; | ||
const fileContents = await readFile(fileToLoad, "utf-8") | ||
|
||
const lines = fileContents.split("\n"); | ||
|
||
if (taskNumber === 1) { | ||
await taskOne(lines); | ||
} | ||
if (taskNumber === 2) { | ||
await taskTwo(lines); | ||
} | ||
} | ||
|
||
void main(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
export async function taskOne(input: string[]): Promise<void> { | ||
const GOAL = parseInt(input[0]) / 10 | ||
|
||
const primes: number[] = [2,3,5,7] | ||
let i = 2; | ||
console.log(val(4)) | ||
//for (let i = 2; i <= 120; i++) | ||
// console.log(i, val(i)) | ||
while(val(i) < GOAL) { | ||
if (i % 1000 == 0) { | ||
} | ||
i++; | ||
} | ||
console.log(i) | ||
|
||
function val(nu: number) { | ||
let n = nu | ||
let s = 1 | ||
for (const p of primes) { | ||
if (n % p == 0) { | ||
let i = 1 | ||
let j = 0 | ||
while(n % p == 0) { | ||
j += i | ||
i *= p | ||
n /= p | ||
} | ||
j += i | ||
s *= j | ||
} | ||
} | ||
if (s == 1) { | ||
primes.push(n) | ||
return (1+n) | ||
} | ||
return s | ||
} | ||
} | ||
|
||
export async function taskTwo(input: string[]): Promise<void> { | ||
const GOAL = parseInt(input[0]) / 11 | ||
const MAX = Math.ceil(GOAL) | ||
|
||
const A = Array.from({length: Math.ceil(MAX)}, ()=>0) | ||
|
||
for (let i = 1; i <= MAX; i++) { | ||
for (let j = 1; j <= 50; j++) { | ||
if (i * j >= MAX) break; | ||
A[i*j] += i | ||
} | ||
if(A[i] > GOAL) { | ||
console.log(i) | ||
return | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { readFile } from "fs/promises"; | ||
import { taskOne, taskTwo } from "./task"; | ||
|
||
async function main() { | ||
let lastArgument = process.argv.pop() as string; | ||
let taskNumber = 1; | ||
let isTest = false; | ||
|
||
if (lastArgument === "test") { | ||
isTest = true; | ||
taskNumber = parseInt(process.argv.pop() as string); | ||
} else { | ||
taskNumber = parseInt(lastArgument) | ||
} | ||
|
||
const fileToLoad = isTest ? "test.in" : "solve.in"; | ||
const fileContents = await readFile(fileToLoad, "utf-8") | ||
|
||
const lines = fileContents.split("\n"); | ||
|
||
if (taskNumber === 1) { | ||
await taskOne(lines); | ||
} | ||
if (taskNumber === 2) { | ||
await taskTwo(lines); | ||
} | ||
} | ||
|
||
void main(); |
Oops, something went wrong.