Skip to content

Commit 8a24c97

Browse files
committed
solved day 11
1 parent 1278502 commit 8a24c97

File tree

3 files changed

+655
-0
lines changed

3 files changed

+655
-0
lines changed

src/2025/day11.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
function memoize(fn) {
2+
let memo = {};
3+
return (...x) => {
4+
let s = JSON.stringify(x);
5+
return (memo[s] = memo[s] ?? fn(...x));
6+
};
7+
}
8+
9+
let solve = memoize((devices, curr, dac = true, fft = true) => {
10+
if (curr === "out") return dac && fft ? 1 : 0;
11+
if (curr === "dac") dac = true;
12+
if (curr === "fft") fft = true;
13+
return devices[curr].reduce(
14+
(sum, next) => sum + solve(devices, next, dac, fft),
15+
0,
16+
);
17+
});
18+
19+
function parse(input) {
20+
let devices = {};
21+
input.split("\n").forEach(line => {
22+
let [name, rest] = line.split(": ");
23+
let outputs = rest.split(" ");
24+
devices[name] = outputs;
25+
});
26+
return devices;
27+
}
28+
29+
export function part1(input) {
30+
return solve(parse(input), "you");
31+
}
32+
33+
export function part2(input) {
34+
return solve(parse(input), "svr", false, false);
35+
}

src/2025/day11.test.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { part1, part2 } from "./day11.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("day11 2025", () => {
8+
describe("part1", () => {
9+
test("it should work for part 1 examples", () => {
10+
expect(
11+
part1(
12+
[
13+
"aaa: you hhh",
14+
"you: bbb ccc",
15+
"bbb: ddd eee",
16+
"ccc: ddd eee fff",
17+
"ddd: ggg",
18+
"eee: out",
19+
"fff: out",
20+
"ggg: out",
21+
"hhh: ccc fff iii",
22+
"iii: out",
23+
].join("\n"),
24+
),
25+
).toEqual(5);
26+
});
27+
28+
test("it should work for part 1 input", () => {
29+
expect(part1(input)).toEqual(506);
30+
});
31+
});
32+
33+
describe("part2", () => {
34+
test("it should work for part 2 examples", () => {
35+
expect(
36+
part2(
37+
[
38+
"svr: aaa bbb",
39+
"aaa: fft",
40+
"fft: ccc",
41+
"bbb: tty",
42+
"tty: ccc",
43+
"ccc: ddd eee",
44+
"ddd: hub",
45+
"hub: fff",
46+
"eee: dac",
47+
"dac: fff",
48+
"fff: ggg hhh",
49+
"ggg: out",
50+
"hhh: out",
51+
].join("\n"),
52+
),
53+
).toEqual(2);
54+
});
55+
56+
test("it should work for part 2 input", () => {
57+
expect(part2(input)).toEqual(385912350172800);
58+
});
59+
});
60+
});

0 commit comments

Comments
 (0)