-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart2.ts
119 lines (106 loc) · 3.44 KB
/
part2.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
import * as fs from 'fs';
const input = fs.readFileSync('input', 'utf8').split('\n');
const modules = {};
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) {
return (destination) => {
if (typeof destination === 'undefined') {
return;
}
if (destination.type === '%' && module.pulseSent) {
return;
}
destination.pulseReceived = module.pulseSent;
};
}
function filterDestinations(module) {
return (destination) =>
typeof destination != 'undefined' && !(destination.type === '%' && module.pulseSent);
}
// we can use brute-force but will takes forever, so we analyze the flow as below:
// rx inputs from &ql, receives low pulse only when ql receive all high pulses
// ql inputs from &fh, &mf, &fz, &ss
// fh, mf, fz, ss are inverters, only have 1 input each from &sn, &hl, &tf, &lr
// when all fh, mf, fz, ss receive low pulse, ql receives all high pulses
// sn, hl, tf, lr all receive pulse from multiplie flip-flops
// sn, hl, tf, lr send low pulses, only when all inputs of them sent high pulses
// all flip-flops inputs of sn, hl, tf, lr must be off and receive low pulse, to send high pulses
// keep going forward and it shows rx is contolled by FOUR separated sub-groups from boardcaster
// which means each sub-group need to be in a complete reset-state, to get its coresponding sn, hl, tf, lr conjunction to send low pulse
// therfore, rx only receive low pulse when all FOUR sub-groups receive do a complete reset-state at the same time
// which is the lcm of pulses of all destinations from boardcaster
const broadcaster = Object.values(modules).find((x) => x.type === 'b');
const pulses = [];
pulses.push(...Array(broadcaster.destinations.length).fill(0));
for (let i = 0; i < broadcaster.destinations.length; i++) {
while (true) {
const queue = [broadcaster.destinations[i]];
pulses[i]++;
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)));
}
}
if (
Object.values(modules)
.filter((x) => x.type === '%')
.every((x) => !x.on)
) {
break;
}
}
}
function lcm(...numbers) {
return numbers.reduce((a, b) => (a * b) / gcd(a, b));
}
function gcd(...numbers) {
return numbers.reduce((a, b) => {
while (b) {
const t = b;
b = a % b;
a = t;
}
return a;
});
}
console.log(lcm(...pulses));