-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path9b.ts
72 lines (65 loc) · 1.83 KB
/
9b.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { assert } from '~/util/assert';
import { timer } from '~/util/Timer';
import { pipe } from '~/util/util';
import { getSimulations, Simulation, getInvalidNumber } from './9';
interface SimulationPartB extends Simulation {
sumTo: number;
}
function toPartBSimulation(sim: Simulation): SimulationPartB {
return {
...sim,
sumTo: getInvalidNumber(sim),
};
}
function findContiguousSum({ sumTo, xmasData }: SimulationPartB): number[] {
// we don't need to iterate through the entire xmasData, just until the index
// where the invalidNumber (or sumTo) is.
const idx = assert(
xmasData.findIndex((d) => d === sumTo),
(idx) => idx > -1
);
const cumulativeSum = xmasData
.slice(0, idx + 1)
.reduce(
(cumSum, curr) => {
cumSum.push(cumSum[cumSum.length - 1] + curr);
return cumSum;
},
[0] // add an artificial 0, that we remove after the reduce.
)
.slice(1); // remove the first element, which is 0.
const workingSet = new Map<number, number>(); // cumSum[i] -> i
for (let i = 0; i < cumulativeSum.length; i++) {
if (workingSet.has(cumulativeSum[i] - sumTo)) {
return xmasData.slice(
workingSet.get(cumulativeSum[i] - sumTo)! + 1,
i + 1
);
}
workingSet.set(cumulativeSum[i], i);
}
throw new Error('No contiguous sum found.');
}
function getEncyptionWeakness(range: number[]) {
return Math.min(...range) + Math.max(...range);
}
function print<T, E>(sanitize?: (data: T) => E): (data: T) => T {
return (d) => {
console.log(sanitize?.(d) ?? d);
return d;
};
}
export function run() {
const sim = getSimulations()[1];
timer.run(
pipe(
toPartBSimulation,
print((s: SimulationPartB) => `sumTo ${s.sumTo}`),
findContiguousSum,
print(),
getEncyptionWeakness
),
'day 9b',
sim
);
}