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)) // );