574 s on part 2, but it's tractable at least
This commit is contained in:
parent
18a47b0c9f
commit
f69f1e8deb
40
07/index.ts
40
07/index.ts
@ -9,8 +9,23 @@ const data = fs.readFileSync(
|
|||||||
);
|
);
|
||||||
|
|
||||||
const doubler = (t: Array<number>, c: number) => [...t, c, c];
|
const doubler = (t: Array<number>, c: number) => [...t, c, c];
|
||||||
|
// cut off the triplication if we've already outgrown the target
|
||||||
|
const tripler = (total: number) => (t: Array<number>, c: number) =>
|
||||||
|
total < c ? t : [...t, c, c, c];
|
||||||
|
|
||||||
const permute = (curr: number) => (v: number, i: number) =>
|
const permute = (curr: number) => (v: number, i: number) =>
|
||||||
i % 2 === 0 ? v * curr : v + curr;
|
i % 2 === 0 ? v * curr : v + curr;
|
||||||
|
const permuteHarder =
|
||||||
|
(curr: number) =>
|
||||||
|
(v: number, i: number): number => {
|
||||||
|
if (i % 3 === 0) {
|
||||||
|
return v * curr;
|
||||||
|
} else if (i % 3 === 1) {
|
||||||
|
return v + curr;
|
||||||
|
} else {
|
||||||
|
return parseInt(v.toString() + curr.toString(), 10);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
let timer = now.instant();
|
let timer = now.instant();
|
||||||
console.log(
|
console.log(
|
||||||
@ -42,6 +57,29 @@ console.log(
|
|||||||
timer = now.instant();
|
timer = now.instant();
|
||||||
console.log(
|
console.log(
|
||||||
"part two:",
|
"part two:",
|
||||||
data.split("\n").slice(0, -1),
|
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);
|
||||||
|
console.log(curr);
|
||||||
|
const res = inputs
|
||||||
|
.split(" ")
|
||||||
|
.map((v) => parseInt(v, 10))
|
||||||
|
.reduce((coll, curr, i) => {
|
||||||
|
if (i === 0) {
|
||||||
|
return [curr];
|
||||||
|
}
|
||||||
|
return coll
|
||||||
|
.reduce(tripler(total), [] as Array<number>)
|
||||||
|
.map(permuteHarder(curr));
|
||||||
|
}, [] as Array<number>);
|
||||||
|
if (res.includes(total)) {
|
||||||
|
return coll + total;
|
||||||
|
}
|
||||||
|
return coll;
|
||||||
|
}, 0),
|
||||||
printTime(now.instant().since(timer))
|
printTime(now.instant().since(timer))
|
||||||
);
|
);
|
||||||
|
Loading…
Reference in New Issue
Block a user