From 78df933581f26c90e26563ef556914082a502326 Mon Sep 17 00:00:00 2001 From: monoid Date: Fri, 20 Dec 2024 14:22:41 +0900 Subject: [PATCH] Add solution day 17 on js impl --- day_17/solve_2.ts | 42 +++++++++++++++++++++++++++++++++++++++++ day_17/solve_2_print.ts | 4 ++-- 2 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 day_17/solve_2.ts diff --git a/day_17/solve_2.ts b/day_17/solve_2.ts new file mode 100644 index 0000000..39cc3b0 --- /dev/null +++ b/day_17/solve_2.ts @@ -0,0 +1,42 @@ +import { readData } from "./solve_1.ts"; + +function dfs( + a: bigint, + output: bigint[], +): bigint[] { + const [first, ...rest] = output; + if (output.length === 0) { + return [a]; + } + const candidates = new Array(8) + .fill(0n) + .map((_, i) => BigInt(i)); + return candidates.filter(cand => { + const na = ((a << 3n) | cand); + if (na < 0) { + console.log("na < 0", na, a, cand); + } + let b = na & 7n; // b == cand + b = b ^ 1n; + const c = (na >> b); + b = b ^ 5n; + b = b ^ c; + b = b & 7n; + return b === first; + }).map(cand => { + const na = (a << 3n) | cand; + return na; + }).flatMap(na => { + return dfs(na, rest); + }); +} + + +if (import.meta.main) { + const data = await readData("input.txt"); + console.log(data.registers); + const outputs = data.programs.toReversed().map(x => BigInt(x)); + // const outputs = [5,0,3,5,7,6,1,5,4].toReversed().map(x => BigInt(x)); + const solutions = dfs(0n, outputs); + console.log(solutions.sort((a, b) => Number(a - b))); +} diff --git a/day_17/solve_2_print.ts b/day_17/solve_2_print.ts index b9ca0e5..141060a 100644 --- a/day_17/solve_2_print.ts +++ b/day_17/solve_2_print.ts @@ -37,9 +37,9 @@ class VMCompiler extends VM { } if (import.meta.main) { - const data = await readData("example_3.txt"); + const data = await readData(Deno.args[0]); console.log(data.registers); - data.registers.A = 0b11100101011000000; + // data.registers.A = 0b11100101011000000; console.log(data.programs.join(",")); const vm = new VMCompiler(data); const text = vm.compile();