diff --git a/2017/10/runner.ts b/2017/10/runner.ts new file mode 100644 index 0000000..b72c86b --- /dev/null +++ b/2017/10/runner.ts @@ -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(); \ No newline at end of file diff --git a/2017/10/task.ts b/2017/10/task.ts new file mode 100644 index 0000000..d0f8212 --- /dev/null +++ b/2017/10/task.ts @@ -0,0 +1,66 @@ +const HEX = [ + '0', '1', '2', '3','4','5','6','7','8','9','a','b','c','d','e','f' +] + +export async function taskOne(input: string[]): Promise { + const LENGTH = 256 + let list = Array.from({length: LENGTH}, (_, i: number) => i) + let currentIndex = 0; + let skipSize = 0; + + const instrcutions = input[0].split(',').map(Number) + for (const i of instrcutions) { + reverse(i) + currentIndex += i + skipSize + skipSize++ + } + console.log(list[0] * list[1]) + + function reverse(len: number) { + for (let i = 0; i < len / 2; i++) { + const temp = list[(currentIndex + i) % LENGTH] + list[(currentIndex + i) % LENGTH] = list[(currentIndex + len - i - 1) % LENGTH] + list[(currentIndex + len - i - 1) % LENGTH] = temp + } + } +} + +export async function taskTwo(input: string[]): Promise { + const LENGTH = 256 + let list = Array.from({length: LENGTH}, (_, i: number) => i) + let currentIndex = 0; + let skipSize = 0; + + const instrcutions = input[0].split('').map((s: string) => s.charCodeAt(0)) + instrcutions.push(...[17, 31, 73, 47, 23]) + for (let c = 0; c < 64; c++) { + for (const i of instrcutions) { + reverse(i) + currentIndex += i + skipSize + currentIndex %= LENGTH + skipSize++ + } + } + + let dense = Array.from({length: 16}, () => 0) + for (let i = 0; i < 16; i++) { + for (let j = 0; j < 16; j++) { + dense[i] = dense[i] ^ list[i * 16 + j] + } + } + + let result = '' + for (const d of dense) { + result += HEX[Math.floor(d/16)] + result += HEX[d%16] + } + console.log(result) + + function reverse(len: number) { + for (let i = 0; i < len / 2; i++) { + const temp = list[(currentIndex + i) % LENGTH] + list[(currentIndex + i) % LENGTH] = list[(currentIndex + len - i - 1) % LENGTH] + list[(currentIndex + len - i - 1) % LENGTH] = temp + } + } +} \ No newline at end of file diff --git a/2017/11/runner.ts b/2017/11/runner.ts new file mode 100644 index 0000000..b72c86b --- /dev/null +++ b/2017/11/runner.ts @@ -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(); \ No newline at end of file diff --git a/2017/11/task.ts b/2017/11/task.ts new file mode 100644 index 0000000..8f6b607 --- /dev/null +++ b/2017/11/task.ts @@ -0,0 +1,104 @@ +import {Queue} from '../../base/simpleStructure' + +export async function taskOne(input: string[]): Promise { + + console.log(distance(input[0].split(','))) +} + +export async function taskTwo(input: string[]): Promise { + const all = input[0].split(',') + const temp = [] as string[] + let max = 0; + for (const a of all) { + temp.push(a) + const d = distance(temp) + if (d > max) max = d + } + console.log(max) +} + +function distance(moves: string[]) { + let n = 0; + let s = 0; + let sw = 0; + let se = 0; + let nw = 0; + let ne = 0; + moves.forEach(i => { + switch(i) { + case 'n': + n++ + break; + case 's': + s++; + break; + case 'ne': + ne++ + break; + case 'se': + se++; + break; + case 'nw': + nw++ + break; + case 'sw': + sw++; + break; + } + }) + let didSomething = false; + do { + didSomething = false + if (n != 0 && s != 0) { + didSomething = true + const m = Math.min(n,s) + n -= m + s -= m + } + + if (ne != 0 && nw != 0) { + didSomething = true + const m = Math.min(ne,nw) + ne -= m + nw -= m + n += m + } + if (se != 0 && sw != 0) { + didSomething = true + const m = Math.min(se,sw) + se -= m + sw -= m + s += m + } + + if (se != 0 && n != 0) { + didSomething = true + const m = Math.min(se,n) + se -= m + n -= m + ne += m + } + if (sw != 0 && n != 0) { + didSomething = true + const m = Math.min(sw,n) + sw -= m + n -= m + nw += m + } + if (ne != 0 && s != 0) { + didSomething = true + const m = Math.min(ne,s) + ne -= m + s -= m + se += m + } + if (nw != 0 && s != 0) { + didSomething = true + const m = Math.min(nw,s) + nw -= m + s -= m + sw += m + } + } while(didSomething) + return n+s+ne+nw+se+sw +} diff --git a/2017/12/runner.ts b/2017/12/runner.ts new file mode 100644 index 0000000..b72c86b --- /dev/null +++ b/2017/12/runner.ts @@ -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(); \ No newline at end of file diff --git a/2017/12/task.ts b/2017/12/task.ts new file mode 100644 index 0000000..3365d9c --- /dev/null +++ b/2017/12/task.ts @@ -0,0 +1,43 @@ +export async function taskOne(input: string[]): Promise { + const graph: Record = {} + input.forEach(i => { + const r = /([0-9]+) <-> (.*)/.exec(i)! + graph[r[1]] = r[2].split(', ') + }) + + const V = new Set() + const Q = ['0'] + while(Q.length > 0) { + const q = Q.pop() as string + if (V.has(q)) continue; + V.add(q) + Q.push(...graph[q]) + } + console.log(V.size) +} + +export async function taskTwo(input: string[]): Promise { + const graph: Record = {} + input.forEach(i => { + const r = /([0-9]+) <-> (.*)/.exec(i)! + graph[r[1]] = r[2].split(', ') + }) + + const V = new Set() + const Q = [] as string[] + const keys = Object.keys(graph) + let groupCount = 0; + for (const k of keys) { + if (V.has(k)) continue + groupCount++ + Q.push(k) + while(Q.length > 0) { + const q = Q.pop() as string + if (V.has(q)) continue; + V.add(q) + Q.push(...graph[q]) + } + } + + console.log(groupCount) +} \ No newline at end of file diff --git a/2017/13/runner.ts b/2017/13/runner.ts new file mode 100644 index 0000000..b72c86b --- /dev/null +++ b/2017/13/runner.ts @@ -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(); \ No newline at end of file diff --git a/2017/13/task.ts b/2017/13/task.ts new file mode 100644 index 0000000..f342ba3 --- /dev/null +++ b/2017/13/task.ts @@ -0,0 +1,29 @@ +export async function taskOne(input: string[]): Promise { + const nums = input.map(i => i.split(': ').map(Number)) + let sum = 0; + for (const n of nums) { + if ((n[0] % (n[1]*2-2)) == 0) { + sum += n[1] * n[0] + } + } + console.log(sum) +} + +export async function taskTwo(input: string[]): Promise { + const nums = input.map(i => i.split(': ').map(Number)) + let i = 0 + while (true) { + let passed = true + for (const n of nums) { + if (((n[0] + i) % (n[1]*2-2)) == 0) { + passed = false + break; + } + } + if (passed) break + i++ + continue + } + console.log(i) + +} diff --git a/2017/14/runner.ts b/2017/14/runner.ts new file mode 100644 index 0000000..b72c86b --- /dev/null +++ b/2017/14/runner.ts @@ -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(); \ No newline at end of file diff --git a/2017/14/task.ts b/2017/14/task.ts new file mode 100644 index 0000000..78dfc8b --- /dev/null +++ b/2017/14/task.ts @@ -0,0 +1,126 @@ +export async function taskOne(input: string[]): Promise { + const FREE_MAP: Record = { + 0: 4, + 1: 3, + 2: 3, + 3: 2, + 4: 3, + 5: 2, + 6: 2, + 7: 1, + 8: 3, + 9: 2, + 10: 2, + 11: 1, + 12: 2, + 13: 1, + 14: 1, + 15: 0 + } + let sum = 0; + for (let i = 0; i < 128; i++) { + const h = hash(`${input[0]}-${i}`) + for (let j of h) { + sum += 4-FREE_MAP[j] + } + } + console.log(sum) + +} + +export async function taskTwo(input: string[]): Promise { + const USED_MAP: Record = { + 0: [false, false, false, false], + 1: [false, false, false, true], + 2: [false, false, true, false], + 3: [false, false, true, true], + 4: [false, true, false, false], + 5: [false, true, false, true], + 6: [false, true, true, false], + 7: [false, true, true, true], + 8: [true, false, false, false], + 9: [true, false, false, true], + 10: [true, false, true, false], + 11: [true, false, true, true], + 12: [true, true, false, false], + 13: [true, true, false, true], + 14: [true, true, true, false], + 15: [true, true, true, true] + } + let grid: boolean[][] = [] + for (let i = 0; i < 128; i++) { + grid.push([]) + const h = hash(`${input[0]}-${i}`) + for (let j of h) { + grid[i].push(...USED_MAP[j]) + } + } + + let regions = 0 + const V = new Set() + const Q = [] as number[][] + + for (let x = 0; x < 128; x++) { + for (let y = 0; y < 128; y++) { + if (!grid[x][y]) continue + const k = `${x}-${y}` + if (V.has(k)) continue + regions++ + Q.push([x,y]) + while(Q.length > 0) { + const q = Q.pop()! + const k = `${q[0]}-${q[1]}` + if (q[0] < 0 || q[1] < 0 || q[0] >= 128 || q[1] >= 128) continue + if (!grid[q[0]][q[1]]) continue + if (V.has(k)) continue + V.add(k) + Q.push([q[0]+1,q[1]]) + Q.push([q[0]-1,q[1]]) + Q.push([q[0],q[1]+1]) + Q.push([q[0],q[1]-1]) + } + } + } + console.log(regions) +} + + +function hash(s: string) { + const LENGTH = 256 + let list = Array.from({length: LENGTH}, (_, i: number) => i) + let currentIndex = 0; + let skipSize = 0; + + const instrcutions = s.split('').map((s: string) => s.charCodeAt(0)) + instrcutions.push(...[17, 31, 73, 47, 23]) + for (let c = 0; c < 64; c++) { + for (const i of instrcutions) { + reverse(i) + currentIndex += i + skipSize + currentIndex %= LENGTH + skipSize++ + } + } + + let dense = Array.from({length: 16}, () => 0) + for (let i = 0; i < 16; i++) { + for (let j = 0; j < 16; j++) { + dense[i] = dense[i] ^ list[i * 16 + j] + } + } + + let result: number[] = [] + for (const d of dense) { + result.push(Math.floor(d/16)) + result.push(d%16) + } + return result + + function reverse(len: number) { + for (let i = 0; i < len / 2; i++) { + const temp = list[(currentIndex + i) % LENGTH] + list[(currentIndex + i) % LENGTH] = list[(currentIndex + len - i - 1) % LENGTH] + list[(currentIndex + len - i - 1) % LENGTH] = temp + } + } +} \ No newline at end of file diff --git a/2017/15/runner.ts b/2017/15/runner.ts new file mode 100644 index 0000000..b72c86b --- /dev/null +++ b/2017/15/runner.ts @@ -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(); \ No newline at end of file diff --git a/2017/15/task.ts b/2017/15/task.ts new file mode 100644 index 0000000..e37cd79 --- /dev/null +++ b/2017/15/task.ts @@ -0,0 +1,32 @@ +export async function taskOne(input: string[]): Promise { + let vals = input.map(i => i.split('with ')[1]).map(Number) + let n = 0 + for (let i = 0; i < 40*1000*1000; i++) { + vals[0] = (vals[0] * 16807) % 2147483647 + vals[1] = (vals[1] * 48271) % 2147483647 + + const s = vals.map(i => i.toString(2)).map(i => { + const l = i.length; + return i.substring(l-16) + }) + if (s[0] == s[1]) n++ + } + console.log(n) +} + +export async function taskTwo(input: string[]): Promise { + let vals = input.map(i => i.split('with ')[1]).map(Number) + let n = 0 + for (let i = 0; i < 5*1000*1000; i++) { + do {vals[0] = (vals[0] * 16807) % 2147483647} while(vals[0] % 4 != 0) + do {vals[1] = (vals[1] * 48271) % 2147483647} while(vals[1] % 8 != 0) + + + const s = vals.map(i => i.toString(2)).map(i => { + const l = i.length; + return i.substring(l-16) + }) + if (s[0] == s[1]) n++ + } + console.log(n) +} \ No newline at end of file diff --git a/2017/16/runner.ts b/2017/16/runner.ts new file mode 100644 index 0000000..b72c86b --- /dev/null +++ b/2017/16/runner.ts @@ -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(); \ No newline at end of file diff --git a/2017/16/task.ts b/2017/16/task.ts new file mode 100644 index 0000000..374e00b --- /dev/null +++ b/2017/16/task.ts @@ -0,0 +1,59 @@ +const letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p'] + const LENGTH = 16 + +export async function taskOne(input: string[]): Promise { + let row = Array.from({length: LENGTH}, (_, i) => letters[i]) + const ins = input[0].split(',') + console.log(dance(row, ins).join('')) +} + +const memo: Record = {} + +export async function taskTwo(input: string[]): Promise { + let row = Array.from({length: LENGTH}, (_, i) => letters[i]) + const ins = input[0].split(',') + let loopStart = -1; + let loopEnd = -1; + const TOTAL = 1000000000 + for (let i = 0; i < TOTAL; i++) { + row = dance(row, ins); + const k = row.join('') + if (memo[k] != undefined) { + loopStart = memo[k] + loopEnd = i + break + } + memo[k] = i + } + const doneCycles = Math.floor((TOTAL-loopStart) / (loopEnd-loopStart)) + const remaining = TOTAL - loopStart - (loopEnd-loopStart) * doneCycles + console.log(loopStart, loopEnd, doneCycles, remaining) + for (let i = 0; i < remaining - 1; i++) { + row = dance(row, ins); + } + console.log(row.join('')) +} + +function dance(row: string[], ins: string[]) { + for (let j = 0; j < ins.length; j++) { + const i = ins[j] + if (i.startsWith('s')) { + const n = parseInt(i.substring(1)) + const newRow = row.map((_,idx)=>row[(LENGTH+idx-n)%LENGTH]) + row = newRow + } else if (i.startsWith('x')) { + const n = i.substring(1).split('/').map(Number) + const t = row[n[0]] + row[n[0]] = row[n[1]] + row[n[1]] = t + } else if (i.startsWith('p')) { + const n = i.substring(1).split('/').map(l => row.indexOf(l)).map(Number) + const t = row[n[0]] + row[n[0]] = row[n[1]] + row[n[1]] = t + } else { + throw i + } + } + return row +} \ No newline at end of file diff --git a/2017/17/runner.ts b/2017/17/runner.ts new file mode 100644 index 0000000..b72c86b --- /dev/null +++ b/2017/17/runner.ts @@ -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(); \ No newline at end of file diff --git a/2017/17/task.ts b/2017/17/task.ts new file mode 100644 index 0000000..4aa43a3 --- /dev/null +++ b/2017/17/task.ts @@ -0,0 +1,33 @@ +export async function taskOne(input: string[]): Promise { + interface Node { + i: number, + next: Node + } + + const zero = {i:0} as Node + zero.next = zero + let cur = zero + let len = 1 + const stepSize = parseInt(input[0]) + for (let i = 1; i <= 2017; i++) { + for (let j = 0; j < stepSize; j++) { + cur = cur.next; + } + let ne = {i, next: cur.next} + cur.next = ne + cur = cur.next + } + console.log(cur.next.i) +} + +export async function taskTwo(input: string[]): Promise { + let pos = 0 + let last = 0 + const stepSize = parseInt(input[0]) + let curPos = 1; + for (let i = 1; i <= 50000000; i++) { + pos = ((pos + stepSize) % i) + 1 + if (pos == 1) last = i + } + console.log(last) +} \ No newline at end of file diff --git a/2017/18/runner.ts b/2017/18/runner.ts new file mode 100644 index 0000000..b72c86b --- /dev/null +++ b/2017/18/runner.ts @@ -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(); \ No newline at end of file diff --git a/2017/18/task.ts b/2017/18/task.ts new file mode 100644 index 0000000..2bbafa7 --- /dev/null +++ b/2017/18/task.ts @@ -0,0 +1,244 @@ +export async function taskOne(input: string[]): Promise { + let i = 0; + const registers: Record = {} + function get(s: string) { + return registers[s] ?? 0 + } + let lastSend = 0 + while(i < input.length) { + let r = /snd ([a-zA-Z])$/.exec(input[i]) + if (r) { + lastSend = get(r[1]) + i++ + continue + } + r = /set ([a-zA-Z]) ([a-zA-Z]|-?[0-9]+)$/.exec(input[i]) + if (r) { + if (/-?[0-9]+/.test(r[2])) registers[r[1]] = parseInt(r[2]) + else registers[r[1]] = get(r[2]) + i++ + continue + } + r = /add ([a-zA-Z]) ([a-zA-Z]|-?[0-9]+)$/.exec(input[i]) + if (r) { + let b = get(r[1]) + if (/-?[0-9]+/.test(r[2])) registers[r[1]] = b + parseInt(r[2]) + else registers[r[1]] = b + get(r[2]) + i++ + continue + } + r = /mul ([a-zA-Z]) ([a-zA-Z]|-?[0-9]+)$/.exec(input[i]) + if (r) { + let b = get(r[1]) + if (/-?[0-9]+/.test(r[2])) registers[r[1]] = b * parseInt(r[2]) + else registers[r[1]] = b * get(r[2]) + i++ + continue + } + r = /mod ([a-zA-Z]) ([a-zA-Z]|[0-9]+)$/.exec(input[i]) + if (r) { + let b = get(r[1]) + if (/[0-9]+/.test(r[2])) registers[r[1]] = b % parseInt(r[2]) + else registers[r[1]] = b % get(r[2]) + i++ + continue + } + r = /rcv ([a-zA-Z]|[0-9]+)$/.exec(input[i]) + if (r) { + let check = 0 + if (/[0-9]+/.test(r[1])) check = parseInt(r[1]) + else check = get(r[1]) + if (check != 0) { + console.log(lastSend) + return + } + i++ + continue + } + r = /jgz ([a-zA-Z]|[0-9]+) ([a-zA-Z]|-?[0-9]+)$/.exec(input[i]) + if (r) { + let check = 0 + if (/[0-9]+/.test(r[1])) check = parseInt(r[1]) + else check = get(r[1]) + if (check <= 0) { + i++ + continue + } + let jump = 0 + if (/-?[0-9]+/.test(r[2])) jump = parseInt(r[2]) + else jump = get(r[2]) + i += jump + continue + } + throw input[i] + } +} + +export async function taskTwo(input: string[]): Promise { + let i = 0; + let j = 0; + const registersI: Record = {} + function getI(s: string) { + return registersI[s] ?? 0 + } + const registersJ: Record = {} + function getJ(s: string) { + return registersJ[s] ?? 0 + } + registersI['p'] = 0 + registersJ['p'] = 1 + let queueI = [] as number[] + let queueJ = [] as number[] + let iWaits = false + let jWaits = false + let jSends = 0 + while(j < input.length) { + while (i < input.length && !iWaits) { + iWaits = false + let r = /snd ([a-zA-Z]|-?[0-9]+)$/.exec(input[i]) + if (r) { + if (/-?[0-9]+/.test(r[1])) queueJ.push(parseInt(r[1])) + else queueJ.push(getI(r[1])) + i++ + jWaits = false + continue + } + r = /set ([a-zA-Z]) ([a-zA-Z]|-?[0-9]+)$/.exec(input[i]) + if (r) { + if (/-?[0-9]+/.test(r[2])) registersI[r[1]] = parseInt(r[2]) + else registersI[r[1]] = getI(r[2]) + i++ + continue + } + r = /add ([a-zA-Z]) ([a-zA-Z]|-?[0-9]+)$/.exec(input[i]) + if (r) { + let b = getI(r[1]) + if (/-?[0-9]+/.test(r[2])) registersI[r[1]] = b + parseInt(r[2]) + else registersI[r[1]] = b + getI(r[2]) + i++ + continue + } + r = /mul ([a-zA-Z]) ([a-zA-Z]|-?[0-9]+)$/.exec(input[i]) + if (r) { + let b = getI(r[1]) + if (/-?[0-9]+/.test(r[2])) registersI[r[1]] = b * parseInt(r[2]) + else registersI[r[1]] = b * getI(r[2]) + i++ + continue + } + r = /mod ([a-zA-Z]) ([a-zA-Z]|[0-9]+)$/.exec(input[i]) + if (r) { + let b = getI(r[1]) + if (/[0-9]+/.test(r[2])) registersI[r[1]] = b % parseInt(r[2]) + else registersI[r[1]] = b % getI(r[2]) + i++ + continue + } + r = /rcv ([a-zA-Z])$/.exec(input[i]) + if (r) { + if (queueI.length > 0) { + registersI[r[1]] = queueI[0] + queueI.shift() + } else { + iWaits = true; + break + } + + i++ + continue + } + r = /jgz ([a-zA-Z]|[0-9]+) ([a-zA-Z]|-?[0-9]+)$/.exec(input[i]) + if (r) { + let check = 0 + if (/[0-9]+/.test(r[1])) check = parseInt(r[1]) + else check = getI(r[1]) + if (check <= 0) { + i++ + continue + } + let jump = 0 + if (/-?[0-9]+/.test(r[2])) jump = parseInt(r[2]) + else jump = getI(r[2]) + i += jump + continue + } + throw input[i] + } + if (jWaits && queueJ.length == 0) break + + while (j < input.length && !jWaits) { + jWaits = false + let r = /snd ([a-zA-Z]|-?[0-9]+)$/.exec(input[j]) + if (r) { + if (/-?[0-9]+/.test(r[1])) queueI.push(parseInt(r[1])) + else queueI.push(getJ(r[1])) + j++ + jSends++ + iWaits = false + continue + } + r = /set ([a-zA-Z]) ([a-zA-Z]|-?[0-9]+)$/.exec(input[j]) + if (r) { + if (/-?[0-9]+/.test(r[2])) registersJ[r[1]] = parseInt(r[2]) + else registersJ[r[1]] = getJ(r[2]) + j++ + continue + } + r = /add ([a-zA-Z]) ([a-zA-Z]|-?[0-9]+)$/.exec(input[j]) + if (r) { + let b = getJ(r[1]) + if (/-?[0-9]+/.test(r[2])) registersJ[r[1]] = b + parseInt(r[2]) + else registersJ[r[1]] = b + getJ(r[2]) + j++ + continue + } + r = /mul ([a-zA-Z]) ([a-zA-Z]|-?[0-9]+)$/.exec(input[j]) + if (r) { + let b = getJ(r[1]) + if (/-?[0-9]+/.test(r[2])) registersJ[r[1]] = b * parseInt(r[2]) + else registersJ[r[1]] = b * getJ(r[2]) + j++ + continue + } + r = /mod ([a-zA-Z]) ([a-zA-Z]|[0-9]+)$/.exec(input[j]) + if (r) { + let b = getJ(r[1]) + if (/[0-9]+/.test(r[2])) registersJ[r[1]] = b % parseInt(r[2]) + else registersJ[r[1]] = b % getJ(r[2]) + j++ + continue + } + r = /rcv ([a-zA-Z])$/.exec(input[j]) + if (r) { + if (queueJ.length > 0) { + registersJ[r[1]] = queueJ[0] + queueJ.shift() + } else { + jWaits = true; + break + } + + j++ + continue + } + r = /jgz ([a-zA-Z]|[0-9]+) ([a-zA-Z]|-?[0-9]+)$/.exec(input[j]) + if (r) { + let check = 0 + if (/[0-9]+/.test(r[1])) check = parseInt(r[1]) + else check = getJ(r[1]) + if (check <= 0) { + j++ + continue + } + let jump = 0 + if (/-?[0-9]+/.test(r[2])) jump = parseInt(r[2]) + else jump = getJ(r[2]) + j += jump + continue + } + throw input[j] + } + if (iWaits && queueI.length == 0) break + } + console.log(jSends) +} \ No newline at end of file