-
Notifications
You must be signed in to change notification settings - Fork 0
/
10b.ts
28 lines (24 loc) · 788 Bytes
/
10b.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
import { timer } from '~/util/Timer';
import { Simulation, getSimulations } from './10';
function countAdapterConfigurations({ adapters }: Simulation) {
const dp = [] as number[];
// we need to add the outlet of 0 jolts to it.
const sortedAdapters = [...adapters, 0].sort((a, b) => a - b);
for (let i = sortedAdapters.length - 1; i >= 0; i--) {
const curr = sortedAdapters[i];
// the prev one has to fewer than 3 jolts away.
dp[i] = dp[i + 1] ?? 1;
if (sortedAdapters[i + 2] - curr <= 3) {
dp[i] += dp[i + 2];
}
if (sortedAdapters[i + 3] - curr <= 3) {
dp[i] += dp[i + 3];
}
}
return dp[0];
}
export function run() {
getSimulations().forEach((sim) =>
timer.run(countAdapterConfigurations, `day 10b - ${sim.name}`, sim)
);
}