-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart1.ts
82 lines (72 loc) · 2.16 KB
/
part1.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
import * as fs from 'fs';
const input = fs.readFileSync('input', 'utf8').split('\n');
type Module = { destinations: string[]; type: string; pulseSent: boolean; pulseReceived: boolean };
const modules: Module[] = [];
const result = [0, 0];
for (const line of input) {
const type = line[0];
const moduleName = type === 'b' ? 'broadcaster' : line.split(' -> ')[0].slice(1);
const destinations = line.split(' -> ')[1].split(', ');
modules[moduleName] = {
destinations,
type: type,
pulseSent: false,
pulseReceived: false,
};
if (type === '%') {
modules[moduleName].on = false;
} else if (type === '&') {
modules[moduleName].inputs = {};
}
}
for (const module of Object.values(modules)) {
module.destinations = module.destinations.map((x) => modules[x]);
}
for (const [moduleName, module] of Object.entries(modules)) {
for (const destination of module.destinations) {
if (typeof destination != 'undefined' && destination.type === '&') {
destination.inputs[moduleName] = module;
}
}
}
function receivePulse(module: Module) {
return (destination) => {
if (typeof destination === 'undefined') {
result[Number(module.pulseSent)]++;
} else if (destination.type === '%' && module.pulseSent) {
result[1]++;
} else {
result[Number(module.pulseSent)]++;
destination.pulseReceived = module.pulseSent;
}
};
}
function filterDestinations(module) {
return (destination) =>
typeof destination != 'undefined' && !(destination.type === '%' && module.pulseSent);
}
for (let i = 0; i < 1000; i++) {
const queue = [Object.values(modules).find((x) => x.type === 'b')];
result[0]++;
while (queue.length > 0) {
const module = queue.shift();
if (typeof module == 'undefined') {
continue;
}
switch (module.type) {
case '%':
if (!module.pulseReceived) {
module.pulseSent = module.on = !module.on;
}
break;
case '&':
module.pulseSent = !Object.values(module.inputs).every((x) => x.pulseSent);
break;
}
if (module.type === '&' || !module.pulseReceived) {
module.destinations.forEach(receivePulse(module));
queue.push(...module.destinations.filter(filterDestinations(module)));
}
}
}
console.log(result[0] * result[1]);