From 18a47b0c9f927b4d88ed4811f158ccab8623bf96 Mon Sep 17 00:00:00 2001 From: David Ashby Date: Sat, 7 Dec 2024 13:36:00 -0700 Subject: [PATCH] day 7 part 1 --- 07/index.ts | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 07/index.ts diff --git a/07/index.ts b/07/index.ts new file mode 100644 index 0000000..2028b6a --- /dev/null +++ b/07/index.ts @@ -0,0 +1,47 @@ +import fs from "node:fs"; +import { printTime, now } from "../util"; + +const test = false; + +const data = fs.readFileSync( + test ? "./inputs/testinput" : "./inputs/input", + "utf8" +); + +const doubler = (t: Array, c: number) => [...t, c, c]; +const permute = (curr: number) => (v: number, i: number) => + i % 2 === 0 ? v * curr : v + curr; + +let timer = now.instant(); +console.log( + "part one:", + data + .split("\n") + .slice(0, -1) + .reduce((coll, curr) => { + // there's probably a cleaner way to do this + let [stringtotal, inputs] = curr.split(": "); + let total = parseInt(stringtotal, 10); + const res = inputs + .split(" ") + .map((v) => parseInt(v, 10)) + .reduce((coll, curr, i) => { + if (i === 0) { + return [curr]; + } + return coll.reduce(doubler, [] as Array).map(permute(curr)); + }, [] as Array); + if (res.includes(total)) { + return coll + total; + } + return coll; + }, 0), + printTime(now.instant().since(timer)) +); + +timer = now.instant(); +console.log( + "part two:", + data.split("\n").slice(0, -1), + printTime(now.instant().since(timer)) +);