-
Notifications
You must be signed in to change notification settings - Fork 0
/
a.test.ts
133 lines (105 loc) · 3.61 KB
/
a.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import { expect, test } from "bun:test";
class Module {
name: string;
type: "%" | "&";
outputs: string[];
memory: "off" | "on" | Record<string, "low" | "high">;
constructor(name: string, type: "%" | "&", outputs: string[]) {
this.name = name;
this.type = type;
this.outputs = outputs;
if (type === "%") {
this.memory = "off";
} else {
this.memory = {};
}
}
}
function solution(input: string) {
const lines = input.split("\n");
const modules: Record<string, Module> = {};
const broadcastTargets: string[] = [];
for (const line of lines) {
const [left, right] = line.split(" -> ");
const outputs = right.split(", ");
if (left === "broadcaster") {
broadcastTargets.push(...outputs);
} else {
const type = left[0] as "%" | "&";
const name = left.slice(1);
modules[name] = new Module(name, type, outputs);
}
}
for (const modulesKey in modules) {
const module = modules[modulesKey];
for (const output of module.outputs) {
const outputModule = modules[output];
if (output in modules && outputModule.type === "&") {
const mem = outputModule.memory;
if (typeof mem === "string") throw new Error("Unexpected");
mem[modulesKey] = "low";
}
}
}
let low = 0;
let high = 0;
for (let i = 0; i < 1000; i++) {
low += 1;
const queue = broadcastTargets.map(
(target) => ["broadcaster", target, "low"] as [string, string, "low" | "high"],
);
while (queue.length > 0) {
const cur = queue.shift();
if (!cur) throw new Error("Unexpected queue");
const [from, to, pulse] = cur;
if (pulse === "low") {
low += 1;
} else {
high += 1;
}
if (!(to in modules)) {
continue;
}
const module = modules[to];
if (module.type === "%") {
if (pulse === "low") {
module.memory = module.memory === "off" ? "on" : "off";
const out = module.memory === "on" ? "high" : "low";
for (const output of module.outputs) {
queue.push([module.name, output, out]);
}
}
} else {
const mem = module.memory;
if (typeof mem === "string") throw new Error("Unexpected");
mem[from] = pulse;
const out = Object.values(module.memory).every((m) => m === "high") ? "low" : "high";
for (const output of module.outputs) {
queue.push([module.name, output, out]);
}
}
}
}
return low * high;
}
test("example-1", async () => {
const file = Bun.file(`${import.meta.dir}/example-1.txt`);
const input = await file.text();
const actual = solution(input);
const expected = 32000000;
expect(actual).toBe(expected);
});
test("example-2", async () => {
const file = Bun.file(`${import.meta.dir}/example-2.txt`);
const input = await file.text();
const actual = solution(input);
const expected = 11687500;
expect(actual).toBe(expected);
});
test("puzzle input", async () => {
const file = Bun.file(`${import.meta.dir}/input.txt`);
const input = await file.text();
const actual = solution(input);
const expected = 919383692;
expect(actual).toBe(expected);
});