42 lines
1.1 KiB
JavaScript
42 lines
1.1 KiB
JavaScript
const { Glib, BNMath: Math, fn } = require('../../lib');
|
|
|
|
const solve2 = fn.memo((number, inputs) => {
|
|
let paths = 0n;
|
|
const end = Math.max(number - 3n, 0n);
|
|
for (let next = number - 1n; next >= end; next--) {
|
|
if (inputs.has(next)) {
|
|
paths += solve2(next, inputs);
|
|
} else if (next === 0n) {
|
|
paths += 1n;
|
|
}
|
|
}
|
|
return paths;
|
|
});
|
|
|
|
module.exports = {
|
|
'1': (input) => {
|
|
let previous = 0n;
|
|
const differences = new Map([[3n, 1n]]);
|
|
input = Glib.fromLines(input)
|
|
.toInts()
|
|
.array.safeSort((a, b) => (a - b > 0 ? 1 : -1))
|
|
.glib.forEach((joltage) => {
|
|
const difference = joltage - previous;
|
|
if (difference > 3n) {
|
|
throw new Error('invalid');
|
|
}
|
|
differences.set(difference, (differences.get(difference) || 0n) + 1n);
|
|
previous = joltage;
|
|
});
|
|
return (differences.get(1n) || 0n) * (differences.get(3n) || 0n);
|
|
},
|
|
'2': (input) => {
|
|
const target =
|
|
Glib.fromLines(input)
|
|
.toInts()
|
|
.max() + 3n;
|
|
|
|
return solve2(target, Glib.fromLines(input).toInts().set);
|
|
},
|
|
};
|