48 lines
1.2 KiB
TypeScript
48 lines
1.2 KiB
TypeScript
|
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<number>, 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<number>).map(permute(curr));
|
||
|
}, [] as Array<number>);
|
||
|
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))
|
||
|
);
|