Skip to content

Commit 62ff811

Browse files
committed
refactor
1 parent 24f1504 commit 62ff811

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

src/2025/day10.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,18 @@ function parse(input) {
55
return input.split("\n").map(line => {
66
let parts = line.replaceAll(/[[\](){}]/g, "").split(" ");
77
let indicator = parts[0].replace(/./g, c => (c === "#" ? 1 : 0));
8-
let buttons = parts.map(x => x.split(",").map(Number));
8+
let buttons = parts.slice(1).map(x => x.split(",").map(Number));
99
let jolts = parts.at(-1).split(",").map(Number);
1010
return { indicator, buttons, jolts };
1111
});
1212
}
1313

1414
function producePatterns(buttons, length) {
1515
let patterns = {};
16-
for (let pressed of powerSet(buttons.map((_, i) => i))) {
17-
let lights = Array(length).fill(false);
18-
for (let i of pressed) {
19-
for (let j of buttons[i]) lights[j] = !lights[j];
20-
}
21-
let key = lights.map(x => (x ? 1 : 0)).join("");
16+
for (let pressed of powerSet(buttons)) {
17+
let lights = Array(length).fill(0);
18+
for (let button of pressed) for (let i of button) lights[i]++;
19+
let key = lights.map(x => x % 2).join("");
2220
patterns[key] = [...(patterns[key] || []), pressed];
2321
}
2422
return patterns;
@@ -46,8 +44,8 @@ export function part2(input) {
4644
let total = Infinity;
4745
let options = patterns[target.map(x => x % 2).join("")] || [];
4846
for (let pressed of options) {
49-
let jolt = Array(jolts.length).fill(0);
50-
for (let i of pressed) for (let j of buttons[i]) jolt[j]++;
47+
let jolt = Array(target.length).fill(0);
48+
for (let button of pressed) for (let i of button) jolt[i]++;
5149
let newTarget = jolt.map((a, i) => (target[i] - a) / 2);
5250
total = Math.min(total, pressed.length + 2 * presses(newTarget));
5351
}

0 commit comments

Comments
 (0)