From be9e7619cdf2235be42b86fba0c59f07b8e26f04 Mon Sep 17 00:00:00 2001 From: David Ashby Date: Tue, 17 Dec 2024 20:50:33 -0500 Subject: [PATCH] day 13 has some issues --- 13/index.ts | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 13/index.ts diff --git a/13/index.ts b/13/index.ts new file mode 100644 index 0000000..c991d87 --- /dev/null +++ b/13/index.ts @@ -0,0 +1,79 @@ +import fs from "node:fs"; +import { printTime, now, toNum } from "../util"; + +const test = false; + +type coord = { x: bigint; y: bigint }; + +const data = fs.readFileSync( + test ? "./inputs/testinput" : "./inputs/input", + "utf8" +); + +let timer = now.instant(); +console.log( + "part one:", + data + .split("\n") + .slice(0, -1) + .reduce((machines, curr, index) => { + // parse this mess + const machine = Math.floor(index / 4); + switch (index % 4) { + case 0: + machines[machine] = { + ...machines[machine], + A: { x: BigInt(curr.slice(11, 14)), y: BigInt(curr.slice(17)) }, + }; + break; + case 1: + machines[machine] = { + ...machines[machine], + B: { x: BigInt(curr.slice(11, 14)), y: BigInt(curr.slice(17)) }, + }; + break; + case 2: + let coords = curr.slice(9).split(", "); + machines[machine] = { + ...machines[machine], + Prize: { x: BigInt(coords[0]), y: BigInt(coords[1].slice(2)) }, + }; + // skip processing the blank line + } + return machines; + }, [] as Array<{ A: coord; B: coord; Prize: coord }>) + .filter((machine) => { + // remove machines where it's impossible to reach the prize within 100 presses of each button + return !( + machine.A.x * BigInt(100) + machine.B.x * BigInt(100) < + machine.Prize.x || + machine.A.y * BigInt(100) + machine.B.y * BigInt(100) < machine.Prize.y + ); + }) + .map((m) => { + //94a/22 - 34a/67 = 8400/22 - 5400/67 + for (let a = BigInt(0); a < 101; a++) { + if ( + (m.A.x * a) / m.B.x - (m.A.y * a) / m.B.y == + m.Prize.x / m.B.x - m.Prize.y / m.B.y + ) { + // b = 8400/22 - 94(a)/22 + return a * BigInt(3) + m.Prize.x / m.B.x - (m.A.x * a) / m.B.x; + } + } + }) + .reduce((total: bigint, m) => { + if (m !== undefined) { + return total + m; + } + return total; + }, BigInt(0)), + printTime(now.instant().since(timer)) +); + +// timer = now.instant(); +// console.log( +// "part two:", +// data.split("\n").slice(0, -1), +// printTime(now.instant().since(timer)) +// );