-
Notifications
You must be signed in to change notification settings - Fork 0
/
day20.ts
236 lines (197 loc) · 6.03 KB
/
day20.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import { describe } from 'node:test';
function log(u: unknown) {
console.log(JSON.stringify(u, undefined, 2));
}
function lcm(arr: number[]): number {
return arr.reduce((acc, n) => (acc * n) / gcd(acc, n));
}
function gcd(a: number, b: number): number {
return b === 0 ? a : gcd(b, a % b);
}
const test_input1 = `broadcaster -> a, b, c
%a -> b
%b -> c
%c -> inv
&inv -> a`.split('\n');
const test_input2 = `broadcaster -> a
%a -> inv, con
&inv -> b
%b -> con
&con -> output`.split('\n');
const puzzle_input = `<get from aoc website>`.split('\n');
type NodeType = 'Broadcaster' | 'FlipFlop' | 'Conjunction' | 'Output';
type Pulse = { source: string; value: 'h' | 'l' };
interface NodeBase {
name: string;
type: NodeType;
destinations: string[];
}
interface Broadcaster extends NodeBase {
type: 'Broadcaster';
}
interface Output extends NodeBase {
type: 'Output';
}
interface FlipFLop extends NodeBase {
type: 'FlipFlop';
state?: boolean;
}
interface Conjunction extends NodeBase {
type: 'Conjunction';
inputs?: Map<string, 'l' | 'h'>;
}
type Node = FlipFLop | Conjunction | Broadcaster | Output;
const rsSources = new Map<string, number>();
let rsSourcesCnt = 0;
function getDestinations(
node: Node,
pulse: Pulse,
cnt: number,
): [string, Pulse][] | null {
switch (node.type) {
// Flip - flop modules(prefix %) are either on or off; they are initially off.
// If a flip - flop module receives a high pulse, it is ignored and nothing happens.
// However, if a flip - flop module receives a low pulse, it flips between on and off.
// If it was off, it turns on and sends a high pulse.
// If it was on, it turns off and sends a low pulse.
case 'FlipFlop': {
if (pulse.value === 'l') {
const response = node.state ? 'l' : 'h';
node.state = !node.state;
return node.destinations.map((d) => [
d,
{ source: node.name, value: response },
]);
}
return [];
}
// Conjunction modules(prefix &) remember the type of the most recent pulse received from each of their connected input modules;
// they initially default to remembering a low pulse for each input.When a pulse is received,
// the conjunction module first updates its memory for that input.
// Then, if it remembers high pulses for all inputs, it sends a low pulse; otherwise, it sends a high pulse.
case 'Conjunction': {
// rx has 1 input Conjunction 'rs' which has 4 different inputs
// so keep track when they hit 'h' for the first time,
// and hope the cycle starts at the first button press
if (node.name === 'rs' && pulse.value === 'h') {
log(`${pulse.source} @ ${cnt}`);
if (!rsSources.has(pulse.source)) {
rsSources.set(pulse.source, cnt);
rsSourcesCnt++;
}
if (rsSourcesCnt === 4) {
const values = Array.from(rsSources.values());
log({ values });
log(lcm(values));
return null;
}
}
node.inputs?.set(pulse.source, pulse.value);
let allHigh = false;
for (let s of node.inputs ?? []) {
if (s[1] === 'l') {
allHigh = false;
break;
}
allHigh = true;
}
return node.destinations.map((d) => [
d,
{ source: node.name, value: allHigh ? 'l' : 'h' },
]);
}
case 'Broadcaster': {
const response = { source: node.name, value: pulse.value };
return node.destinations.map((d) => [d, response]);
}
case 'Output': {
return [];
}
}
}
function getNameType(s: string): [string, NodeType] {
const t = s.trim();
if (t === 'broadcaster') {
return ['broadcaster', 'Broadcaster'];
}
if (t === 'output') {
return ['output', 'Output'];
}
if (t[0] === '%') {
return [t.slice(1), 'FlipFlop'];
}
if (t[0] === '&') {
return [t.slice(1), 'Conjunction'];
}
log(`invalid ${s}`);
throw new Error();
}
function nodeFactory(
line: string,
nodes: Map<string, Node>,
inputs: Map<string, string[]>,
): void {
const [n, d] = line.split('->');
const [name, type] = getNameType(n);
const destinations = d.split(',').map((s) => s.trim());
destinations.forEach((d) => {
const i = inputs.get(d) ?? [];
i.push(name);
inputs.set(d, i);
});
nodes.set(name, {
name,
type,
destinations,
});
}
function part1(input: readonly string[], max = 1000) {
const nodes: Map<string, Node> = new Map();
const inputs: Map<string, string[]> = new Map();
const getNode: (s: string) => Node | undefined = (s: string) => {
const n = nodes.get(s);
return n;
};
// add output node, since it's only a destination
nodes.set('output', { name: 'output', type: 'Output', destinations: [] });
nodes.set('rx', { name: 'rx', type: 'Output', destinations: [] });
input.forEach((i) => nodeFactory(i, nodes, inputs));
for (const [name, sources] of inputs) {
const n = getNode(name);
if (n != null && n.type === 'Conjunction') {
n.inputs = new Map(sources.map((s) => [s, 'l']));
}
}
const pulsesInSystem: [string, Pulse][] = [];
let lowCnt = 0;
let highCnt = 0;
for (let i = 0; i < max; i++) {
lowCnt += 1; // push the button
pulsesInSystem.push(['broadcaster', { source: 'button', value: 'l' }]);
while (pulsesInSystem.length > 0) {
const c = pulsesInSystem.splice(0, 1)[0];
if (c == null) {
break;
}
// log(`#${lowCnt + highCnt} ${c[1].source}:${c[1].value}>>${c[0]}`);
const node = getNode(c[0]);
if (node == null) {
throw new Error(c[0]);
}
const destinations = getDestinations(node, c[1], i + 1);
if (destinations === null) {
// short cut for part 2
return;
}
for (let d of destinations) {
d[1].value === 'l' ? lowCnt++ : highCnt++;
pulsesInSystem.push([d[0], d[1]]);
}
}
}
log({ lowCnt, highCnt, multiple: lowCnt * highCnt });
}
// part1(test_input1);
// part1(test_input2);
part1(puzzle_input); // part 1
part1(puzzle_input, Number.MAX_SAFE_INTEGER); // part 2