Skip to content

Commit d7b46ec

Browse files
committed
feat: 2024 Day 24 Part 1
1 parent a479542 commit d7b46ec

File tree

3 files changed

+134
-0
lines changed

3 files changed

+134
-0
lines changed

2024/day/24/monitoringDevices.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
export type WireValues = ReturnType<typeof parseInitialWireValues>;
2+
export type GateConnections = ReturnType<typeof parseGateConnections>;
3+
4+
const gateRegExp = /^(?<a>\S+) (?<op>AND|OR|XOR) (?<b>\S+) -> (?<c>\S+)$/gm;
5+
6+
const ops: Record<string, (x: number, y: number) => number> = {
7+
AND: (a, b) => a & b,
8+
OR: (a, b) => a | b,
9+
XOR: (a, b) => a ^ b,
10+
};
11+
12+
function parseInitialWireValues(text: string) {
13+
return text.split("\n").reduce((map, line) => {
14+
const [wire, value] = line.split(": ");
15+
return map.set(wire, +value);
16+
}, new Map<string, number>());
17+
}
18+
19+
function parseGateConnections(text: string) {
20+
return text.matchAll(gateRegExp).reduce(
21+
(map, [, a, op, b, c]) => map.set({ a, op, b }, c),
22+
new Map<{ a: string; op: string; b: string }, string>(),
23+
);
24+
}
25+
26+
export function parseInput(input: string) {
27+
const [top, bottom] = input.split("\n\n");
28+
const initialWireValues = parseInitialWireValues(top);
29+
const gateConnections = parseGateConnections(bottom);
30+
return { initialWireValues, gateConnections };
31+
}
32+
33+
export function output(
34+
initialWireValues: WireValues,
35+
gateConnections: GateConnections,
36+
) {
37+
const wireValues = new Map(initialWireValues);
38+
const pendingGateConnections = new Map(gateConnections);
39+
while (pendingGateConnections.size) {
40+
for (const [gate, wire] of pendingGateConnections) {
41+
const { a, op, b } = gate;
42+
const x = wireValues.get(a), y = wireValues.get(b);
43+
if (x === undefined || y === undefined) continue;
44+
const z = ops[op](x, y);
45+
wireValues.set(wire, z);
46+
pendingGateConnections.delete(gate);
47+
}
48+
}
49+
let result = 0n;
50+
for (const [wire, value] of wireValues) {
51+
if (value === 0 || !wire.startsWith("z")) continue;
52+
const bit = BigInt(wire.slice(1));
53+
result ^= 2n ** bit;
54+
}
55+
return Number(result);
56+
}

2024/day/24/part/1/solve.test.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import solve from "./solve.ts";
2+
3+
import { assertEquals } from "@std/assert";
4+
5+
Deno.test("example", () => {
6+
const input = `\
7+
x00: 1
8+
x01: 1
9+
x02: 1
10+
y00: 0
11+
y01: 1
12+
y02: 0
13+
14+
x00 AND y00 -> z00
15+
x01 XOR y01 -> z01
16+
x02 OR y02 -> z02`;
17+
18+
assertEquals(solve(input), 4);
19+
});
20+
21+
Deno.test("larger example", () => {
22+
const input = `\
23+
x00: 1
24+
x01: 0
25+
x02: 1
26+
x03: 1
27+
x04: 0
28+
y00: 1
29+
y01: 1
30+
y02: 1
31+
y03: 1
32+
y04: 1
33+
34+
ntg XOR fgs -> mjb
35+
y02 OR x01 -> tnw
36+
kwq OR kpj -> z05
37+
x00 OR x03 -> fst
38+
tgd XOR rvg -> z01
39+
vdt OR tnw -> bfw
40+
bfw AND frj -> z10
41+
ffh OR nrd -> bqk
42+
y00 AND y03 -> djm
43+
y03 OR y00 -> psh
44+
bqk OR frj -> z08
45+
tnw OR fst -> frj
46+
gnj AND tgd -> z11
47+
bfw XOR mjb -> z00
48+
x03 OR x00 -> vdt
49+
gnj AND wpb -> z02
50+
x04 AND y00 -> kjc
51+
djm OR pbm -> qhw
52+
nrd AND vdt -> hwm
53+
kjc AND fst -> rvg
54+
y04 OR y02 -> fgs
55+
y01 AND x02 -> pbm
56+
ntg OR kjc -> kwq
57+
psh XOR fgs -> tgd
58+
qhw XOR tgd -> z09
59+
pbm OR djm -> kpj
60+
x03 XOR y03 -> ffh
61+
x00 XOR y04 -> ntg
62+
bfw OR bqk -> z06
63+
nrd XOR fgs -> wpb
64+
frj XOR qhw -> z04
65+
bqk OR frj -> z07
66+
y03 OR x01 -> nrd
67+
hwm AND bqk -> z03
68+
tgd XOR rvg -> z12
69+
tnw OR pbm -> gnj`;
70+
71+
assertEquals(solve(input), 2024);
72+
});

2024/day/24/part/1/solve.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { output, parseInput } from "../../monitoringDevices.ts";
2+
3+
export default function solve(input: string) {
4+
const { initialWireValues, gateConnections } = parseInput(input);
5+
return output(initialWireValues, gateConnections);
6+
}

0 commit comments

Comments
 (0)