-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
323 lines (274 loc) · 6.34 KB
/
main.go
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
package main
import (
"io"
aoc "github.com/teivah/advent-of-code"
)
type pulseType int
func (p pulseType) String() string {
if p == lowPulse {
return "low"
}
return "high"
}
const (
lowPulse pulseType = iota
highPulse
)
var pulsesSent map[pulseType]int
var res2 int
var rxParent string
type module interface {
pulseAction(it int, id string, p pulseType) []Pulse
}
type Pulse struct {
from string
destination module
p pulseType
}
type broadcaster struct {
id string
destinations []string
modules map[string]module
}
func (b *broadcaster) pulseAction(_ int, _ string, p pulseType) []Pulse {
pulses := make([]Pulse, 0, len(b.destinations))
for _, destination := range b.destinations {
destinationModule, contains := b.modules[destination]
if !contains {
pulsesSent[p]++
continue
}
pulses = append(pulses, Pulse{
destination: destinationModule,
from: b.id,
p: p,
})
pulsesSent[p]++
}
return pulses
}
type flipFlop struct {
id string
destinations []string
modules map[string]module
on bool
}
func (f *flipFlop) pulseAction(_ int, _ string, p pulseType) []Pulse {
if p == highPulse {
return nil
}
output := lowPulse
if !f.on {
output = highPulse
}
f.on = !f.on
pulses := make([]Pulse, 0, len(f.destinations))
for _, destination := range f.destinations {
destinationModule, contains := f.modules[destination]
if !contains {
pulsesSent[output]++
continue
}
pulses = append(pulses, Pulse{
destination: destinationModule,
from: f.id,
p: output,
})
pulsesSent[output]++
}
return pulses
}
type conjunction struct {
id string
inputs map[string]pulseType
destinations []string
modules map[string]module
latest map[string]int
multiples map[string]int
}
func (c *conjunction) setInputs(inputs []string) {
c.inputs = make(map[string]pulseType)
for _, input := range inputs {
c.inputs[input] = lowPulse
}
}
func (c *conjunction) pulseAction(it int, id string, p pulseType) []Pulse {
c.inputs[id] = p
if c.id == rxParent {
if c.inputs[id] == highPulse {
v, contains := c.latest[id]
if !contains {
c.latest[id] = it
} else {
if v != it {
c.multiples[id] = it - v
if len(c.multiples) == len(c.latest) {
var numbers []int
for _, n := range c.multiples {
numbers = append(numbers, n)
}
res2 = aoc.LeastCommonMultiple(numbers)
return nil
}
c.latest[id] = it
}
}
}
}
output := lowPulse
for _, input := range c.inputs {
if input == lowPulse {
output = highPulse
break
}
}
pulses := make([]Pulse, 0, len(c.destinations))
for _, destination := range c.destinations {
destinationModule, contains := c.modules[destination]
if !contains {
pulsesSent[output]++
continue
}
pulses = append(pulses, Pulse{
destination: destinationModule,
from: c.id,
p: output,
})
pulsesSent[output]++
}
return pulses
}
func fs1(input io.Reader, iterations int) int {
modules, _ := parse(input)
for i := 0; i < iterations; i++ {
actions := modules["broadcaster"].pulseAction(i, "broadcaster", lowPulse)
pulsesSent[lowPulse]++
q := actions
for len(q) != 0 {
pulse := q[0]
q = q[1:]
actions = pulse.destination.pulseAction(i, pulse.from, pulse.p)
q = append(q, actions...)
}
}
return pulsesSent[lowPulse] * pulsesSent[highPulse]
}
func parse(input io.Reader) (map[string]module, map[string][]string) {
pulsesSent = make(map[pulseType]int)
lines := aoc.ReaderToStrings(input)
graph := make(map[string][]string)
modules := make(map[string]module)
conjunctions := make(map[string]*conjunction)
inDegree := make(map[string][]string)
for _, line := range lines {
del := aoc.NewDelimiter(line, "->", aoc.WithTrimSpace())
name := del.GetString(0)
destinationsStr := del.GetString(1)
del2 := aoc.NewDelimiter(destinationsStr, ", ")
destinations := del2.GetStrings()
if name == "broadcaster" {
p := &broadcaster{
id: name,
destinations: destinations,
modules: modules,
}
modules[name] = p
} else if name[0] == '%' {
name = name[1:]
p := &flipFlop{
id: name,
destinations: destinations,
modules: modules,
}
modules[name] = p
} else if name[0] == '&' {
name = name[1:]
p := &conjunction{
id: name,
inputs: nil,
destinations: destinations,
modules: modules,
latest: make(map[string]int),
multiples: make(map[string]int),
}
conjunctions[name] = p
modules[name] = p
} else {
panic(name)
}
graph[name] = destinations
for _, destination := range destinations {
inDegree[destination] = append(inDegree[destination], name)
}
}
for name, c := range conjunctions {
c.setInputs(inDegree[name])
}
return modules, graph
}
func fs2(input io.Reader) int {
modules, graph := parse(input)
parent := dependency(graph, "broadcaster", nil, make(map[string]*Node))
target := find(parent, "rx")
for k := range target.parents {
rxParent = k
break
}
for i := 0; ; i++ {
actions := modules["broadcaster"].pulseAction(i, "broadcaster", lowPulse)
pulsesSent[lowPulse]++
q := actions
for len(q) != 0 {
pulse := q[0]
q = q[1:]
actions = pulse.destination.pulseAction(i, pulse.from, pulse.p)
q = append(q, actions...)
}
if res2 != 0 {
return res2
}
}
}
type Node struct {
id string
parents map[string]*Node
children []*Node
}
func dependency(graph map[string][]string, id string, currentParent *Node, nodes map[string]*Node) *Node {
if n, exists := nodes[id]; exists {
if currentParent != nil {
n.parents[currentParent.id] = currentParent
}
return n
}
parents := make(map[string]*Node)
if currentParent != nil {
parents[currentParent.id] = currentParent
}
node := &Node{
id: id,
parents: parents,
}
nodes[id] = node
destinations, exists := graph[id]
if !exists {
return node
}
var children []*Node
for _, destination := range destinations {
children = append(children, dependency(graph, destination, node, nodes))
}
node.children = children
return node
}
func find(node *Node, target string) *Node {
if node.id == target {
return node
}
for _, child := range node.children {
if n := find(child, target); n != nil {
return n
}
}
return nil
}