Skip to content

Commit 99da008

Browse files
committed
solved day 10
1 parent 83d4542 commit 99da008

File tree

3 files changed

+291
-0
lines changed

3 files changed

+291
-0
lines changed

src/2025/day10.js

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
function parse(input) {
2+
let machines = input.split("\n").map(line => {
3+
let [indicator, ...buttons] = line.split(" ");
4+
let joltage = buttons.pop();
5+
indicator = indicator
6+
.slice(1, -1)
7+
.split("")
8+
.map((x, i) => (x === "#" ? i : null))
9+
.filter(x => x !== null);
10+
buttons = buttons.map(button => {
11+
button = button
12+
.slice(1, -1)
13+
.split(",")
14+
.map(x => parseInt(x, 10));
15+
return new Set(button);
16+
});
17+
joltage = joltage
18+
.slice(1, -1)
19+
.split(",")
20+
.map(x => parseInt(x, 10));
21+
return { indicator: new Set(indicator), buttons: buttons, joltage };
22+
});
23+
return machines;
24+
}
25+
26+
function minimumPresses(machine) {
27+
let { indicator, buttons } = machine;
28+
let queue = [];
29+
let visited = new Set();
30+
buttons.forEach(button => {
31+
queue.push({ pressed: 1, state: button });
32+
});
33+
while (queue.length > 0) {
34+
let { pressed, state } = queue.shift();
35+
let stateKey = Array.from(state)
36+
.sort((a, b) => a - b)
37+
.join(",");
38+
if (visited.has(stateKey)) continue;
39+
visited.add(stateKey);
40+
if (indicator.symmetricDifference(state).size === 0) {
41+
return pressed;
42+
}
43+
buttons.forEach(button => {
44+
queue.push({
45+
pressed: pressed + 1,
46+
state: button.symmetricDifference(state),
47+
});
48+
});
49+
}
50+
}
51+
52+
function minimumPresses2(machine) {
53+
let { buttons, joltage } = machine;
54+
let queue = [];
55+
let visited = new Set();
56+
queue.push({ pressed: 0, state: [...joltage], pushes: [] });
57+
while (queue.length > 0) {
58+
let { pressed, state, pushes } = queue.shift();
59+
let stateKey = state.join(",");
60+
if (visited.has(stateKey)) continue;
61+
visited.add(stateKey);
62+
if (state.every(x => x === 0)) return pressed;
63+
if (state.some(x => x < 0)) continue;
64+
for (let i = 0; i < buttons.length; i++) {
65+
let button = buttons[i];
66+
let next = [...state];
67+
button.forEach(pos => next[pos]--);
68+
queue.push({
69+
pressed: pressed + 1,
70+
state: next,
71+
pushes: pushes.concat(i),
72+
});
73+
}
74+
}
75+
}
76+
77+
export function part1(input) {
78+
let machines = parse(input);
79+
let result = 0;
80+
machines.forEach(machine => (result += minimumPresses(machine)));
81+
return result;
82+
}
83+
84+
export function part2(input) {
85+
let machines = parse(input);
86+
let result = 0;
87+
machines.forEach(machine => (result += minimumPresses2(machine)));
88+
return result;
89+
}

src/2025/day10.test.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { part1, part2 } from "./day10.js";
2+
import { describe, test, expect } from "vitest";
3+
import readInput from "../utils/read-input.js";
4+
5+
let input = readInput(import.meta.url);
6+
7+
describe("day10 2025", () => {
8+
describe("part1", () => {
9+
test("it should work for part 1 examples", () => {
10+
expect(
11+
part1(
12+
[
13+
"[.##.] (3) (1,3) (2) (2,3) (0,2) (0,1) {3,5,4,7}",
14+
"[...#.] (0,2,3,4) (2,3) (0,4) (0,1,2) (1,2,3,4) {7,5,12,7,2}",
15+
"[.###.#] (0,1,2,3,4) (0,3,4) (0,1,2,4,5) (1,2) {10,11,11,5,10,5}",
16+
].join("\n"),
17+
),
18+
).toEqual(7);
19+
});
20+
21+
test("it should work for part 1 input", () => {
22+
expect(part1(input)).toEqual(409);
23+
});
24+
});
25+
26+
describe("part2", () => {
27+
test("it should work for part 2 examples", () => {
28+
expect(
29+
part2(
30+
[
31+
"[.##.] (3) (1,3) (2) (2,3) (0,2) (0,1) {3,5,4,7}",
32+
"[...#.] (0,2,3,4) (2,3) (0,4) (0,1,2) (1,2,3,4) {7,5,12,7,2}",
33+
"[.###.#] (0,1,2,3,4) (0,3,4) (0,1,2,4,5) (1,2) {10,11,11,5,10,5}",
34+
].join("\n"),
35+
),
36+
).toEqual(33);
37+
});
38+
39+
test.skip("it should work for part 2 input", () => {
40+
expect(part2(input)).toEqual(0); // solved with z3, will implement in code later
41+
});
42+
});
43+
});

0 commit comments

Comments
 (0)