-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday20.ts
121 lines (91 loc) · 3.88 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
import { readLines } from "./common";
type PulseValue = 0 | 1;
interface Pulse { loc: string, source: string, value: PulseValue };
const modulesByName: Record<string, { type: string, name: string, to: string[] }> = readLines('day20.txt')
.map(line => /(?<type>[%&]?)(?<name>\w+) -> (?<to>.*)/.exec(line).groups)
.map(({ type, name, to }) => ({ type, name, to }))
.reduce((acc, { type, name, to }) => ({ ...acc, [name]: { type, name, to: to.split(', ') } }), {});
const flipFlopState: Record<string, boolean> = Object
.values(modulesByName)
.filter(x => x.type === '%')
.reduce((acc, x) => ({ ...acc, [x.name]: false }), {});
const conjunctionStates: Record<string, Record<string, PulseValue>> = Object
.values(modulesByName)
.filter(x => x.type === '&')
.reduce((acc, x) => ({
...acc,
[x.name]: Object
.values(modulesByName)
.filter(y => y.to.some(z => z === x.name))
.reduce((acc, v) => ({ ...acc, [v.name]: 0 }), {})
}), {});
const createNext = (sourcePulse: Pulse, value: PulseValue): Pulse[] =>
modulesByName[sourcePulse.loc].to.map(l => ({ source: sourcePulse.loc, loc: l, value: value }))
const pulseCodeByType: Record<string, (pulse: Pulse) => Pulse[]> = {
'': p => createNext(p, 0),
'%': p => {
if (p.value === 1) return [];
flipFlopState[p.loc] = !flipFlopState[p.loc];
return createNext(p, flipFlopState[p.loc] ? 1 : 0);
},
'&': p => {
const state = conjunctionStates[p.loc];
state[p.source] = p.value;
const nextPulse = Object.values(state).every(x => x) ? 0 : 1;
return createNext(p, nextPulse);
}
};
let hiPulses = 0;
let loPulses = 0;
for (let i = 0; i < 1_000; i++) {
let pulses: Array<Pulse> = [ { loc: 'broadcaster', source: '', value: 0 } ];
while (pulses.length > 0) {
let pulsesToProcessPerTick = pulses.length;
for (let i = 0; i < pulsesToProcessPerTick; i++) {
const module = modulesByName[pulses[i].loc];
if (pulses[i].value === 0) loPulses++;
else hiPulses++;
if (module === undefined) continue;
pulseCodeByType[module.type](pulses[i]).forEach(p => pulses.push(p));
}
pulses = pulses.slice(pulsesToProcessPerTick);
}
}
console.log('Part 1', hiPulses * loPulses)
// clean state for part 2
Object.keys(flipFlopState).forEach(x => flipFlopState[x] = false);
Object.keys(conjunctionStates).forEach(x => Object.keys(conjunctionStates[x]).forEach(y => conjunctionStates[x][y] = 0))
let requiredKeyPresses = 0;
const rxSourceModule = Object.values(modulesByName).find(x => x.to.includes('rx'));
const watchedModules = new Set<string>(Object.values(modulesByName).filter(x => x.to.includes(rxSourceModule.name)).map(x => x.name));
const firedAtByModuleName = {};
const pulseCodeByTypeForPart2: Record<string, (pulse: Pulse) => Pulse[]> = {
...pulseCodeByType,
'&': p => {
const nextPulse = pulseCodeByType['&'](p);
if (watchedModules.has(p.loc)
&& nextPulse[0]?.value === 1
&& firedAtByModuleName[p.loc] === undefined) {
firedAtByModuleName[p.loc] = requiredKeyPresses;
}
return nextPulse;
}
};
while (Object.keys(firedAtByModuleName).length !== watchedModules.size) {
requiredKeyPresses++;
let pulses: Array<Pulse> = [ { loc: 'broadcaster', source: '', value: 0 } ];
while (pulses.length > 0) {
let pulsesToProcessPerTick = pulses.length;
for (let i = 0; i < pulsesToProcessPerTick; i++) {
const module = modulesByName[pulses[i].loc];
if (module === undefined) continue;
pulseCodeByTypeForPart2[module.type](pulses[i]).forEach(p => pulses.push(p));
}
pulses = pulses.slice(pulsesToProcessPerTick);
}
}
/// copied from day8
const gcd = (a: number, b: number): number => b ? gcd(b, a % b) : a;
const lcm = (a: number, b: number): number => (a * b) / gcd(a, b);
let firedAt: number[] = Object.values(firedAtByModuleName);
console.log('part 2', BigInt(firedAt.slice(1).reduce((acc, v) => lcm(acc, v), firedAt[0])));