Skip to content

Commit 3f321d5

Browse files
committed
solved day 23
1 parent 254b8df commit 3f321d5

File tree

10 files changed

+5296
-1918
lines changed

10 files changed

+5296
-1918
lines changed

src/2024/day23.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
function addOneToNetwork(network, connections = network) {
2+
let set = new Set();
3+
network.forEach(connection => {
4+
let as = connection
5+
.map(c => {
6+
return connections
7+
.filter(other => other.includes(c))
8+
.map(a => a.find(x => x !== c));
9+
})
10+
.map(a => new Set(a));
11+
let result = as.reduce((a, b) => a.intersection(b));
12+
result.forEach(x => set.add([...connection, x].sort().join(",")));
13+
});
14+
return set;
15+
}
16+
17+
export function part1(input) {
18+
let connections = input.split("\n").map(line => {
19+
let [a, b] = line.split("-").sort();
20+
return [a, b];
21+
});
22+
let set = addOneToNetwork(connections);
23+
return [...set].filter(x => x.match(/(^t|,t)/)).length;
24+
}
25+
26+
export function part2(input) {
27+
let connections = input.split("\n").map(line => {
28+
let [a, b] = line.split("-").sort();
29+
return [a, b];
30+
});
31+
let network = connections;
32+
while (network.length > 1) {
33+
let next = addOneToNetwork(network, connections);
34+
network = [...next].map(x => x.split(","));
35+
}
36+
return network[0].sort().join(",");
37+
}

src/2024/day23.spec.js

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import { part1, part2 } from "./day23.js";
2+
import readInput from "../utils/read-input.js";
3+
4+
const input = readInput(import.meta.url);
5+
6+
describe("day23 2024", () => {
7+
describe("part1", () => {
8+
test("it should work for part 1 examples", () => {
9+
expect(
10+
part1(
11+
[
12+
"kh-tc",
13+
"qp-kh",
14+
"de-cg",
15+
"ka-co",
16+
"yn-aq",
17+
"qp-ub",
18+
"cg-tb",
19+
"vc-aq",
20+
"tb-ka",
21+
"wh-tc",
22+
"yn-cg",
23+
"kh-ub",
24+
"ta-co",
25+
"de-co",
26+
"tc-td",
27+
"tb-wq",
28+
"wh-td",
29+
"ta-ka",
30+
"td-qp",
31+
"aq-cg",
32+
"wq-ub",
33+
"ub-vc",
34+
"de-ta",
35+
"wq-aq",
36+
"wq-vc",
37+
"wh-yn",
38+
"ka-de",
39+
"kh-ta",
40+
"co-tc",
41+
"wh-qp",
42+
"tb-vc",
43+
"td-yn",
44+
].join("\n"),
45+
),
46+
).toEqual(7);
47+
});
48+
49+
test("it should work for part 1 input", () => {
50+
expect(part1(input)).toEqual(1175);
51+
});
52+
});
53+
54+
describe("part2", () => {
55+
test("it should work for part 2 examples", () => {
56+
expect(
57+
part2(
58+
[
59+
"kh-tc",
60+
"qp-kh",
61+
"de-cg",
62+
"ka-co",
63+
"yn-aq",
64+
"qp-ub",
65+
"cg-tb",
66+
"vc-aq",
67+
"tb-ka",
68+
"wh-tc",
69+
"yn-cg",
70+
"kh-ub",
71+
"ta-co",
72+
"de-co",
73+
"tc-td",
74+
"tb-wq",
75+
"wh-td",
76+
"ta-ka",
77+
"td-qp",
78+
"aq-cg",
79+
"wq-ub",
80+
"ub-vc",
81+
"de-ta",
82+
"wq-aq",
83+
"wq-vc",
84+
"wh-yn",
85+
"ka-de",
86+
"kh-ta",
87+
"co-tc",
88+
"wh-qp",
89+
"tb-vc",
90+
"td-yn",
91+
].join("\n"),
92+
),
93+
).toEqual("co,de,ka,ta");
94+
});
95+
96+
test("it should work for part 2 input", () => {
97+
expect(part2(input)).toEqual("bw,dr,du,ha,mm,ov,pj,qh,tz,uv,vq,wq,xw");
98+
});
99+
});
100+
});

0 commit comments

Comments
 (0)