-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
267 lines (217 loc) · 5.1 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
package main
import (
"fmt"
"strings"
"github.com/devkvlt/aoc"
)
type Pulse int8
const (
Low Pulse = 0
High Pulse = 1
)
// // DEBUG:
// func (p Pulse) String() string {
// if p == Low {
// return "Low"
// }
// return "High"
// }
type Signal struct {
pulse Pulse
src string
dst string
}
type Module interface {
ID() string
Dsts() []string
Process(src string, input Pulse)
}
type FlipFlop struct {
id string
dsts []string
on *bool
}
func (f FlipFlop) ID() string { return f.id }
func (f FlipFlop) Dsts() []string { return f.dsts }
func NewFlipFlop(id string, dsts []string) FlipFlop {
on := false
return FlipFlop{id: id, dsts: dsts, on: &on}
}
func (f FlipFlop) Process(src string, input Pulse) {
if input == Low {
*f.on = !(*f.on)
output := Low
if *f.on {
output = High
}
for _, dst := range f.dsts {
queue = append(queue, Signal{output, f.ID(), dst})
// fmt.Println(f.ID(), ": Send", output, "to", dst) // DEBUG:
}
}
}
type Conjunction struct {
id string
dsts []string
memory *map[string]Pulse
}
func NewConjunction(id string, dsts []string) Conjunction {
return Conjunction{id: id, dsts: dsts, memory: &map[string]Pulse{}}
}
func (c Conjunction) ID() string { return c.id }
func (c Conjunction) Dsts() []string { return c.dsts }
func (c Conjunction) Process(src string, input Pulse) {
(*c.memory)[src] = input
output := Low
for _, pulse := range *c.memory {
if pulse == Low {
output = High
break
}
}
for _, dst := range c.dsts {
queue = append(queue, Signal{output, c.ID(), dst})
// fmt.Println(c.ID(), ": Send", output, "to", dst) // DEBUG:
}
}
type Broadcaster struct {
id string
dsts []string
}
func NewBroadcaster(id string, dsts []string) Broadcaster {
return Broadcaster{id: id, dsts: dsts}
}
func (b Broadcaster) ID() string { return b.id }
func (b Broadcaster) Dsts() []string { return b.dsts }
func (c Broadcaster) Process(src string, input Pulse) {
for _, dst := range c.dsts {
queue = append(queue, Signal{input, c.ID(), dst})
// fmt.Println(c.ID(), ": Send", input, "to", dst) // DEBUG:
}
}
var queue []Signal
var modules = map[string]Module{}
func reset() {
lines := aoc.Lines("input")
for _, line := range lines {
var mod Module
fields := strings.Split(line, " -> ")
id := fields[0]
dsts := strings.Split(fields[1], ", ")
switch id[0] {
case '%':
id = id[1:]
mod = NewFlipFlop(id, dsts)
case '&':
id = id[1:]
mod = NewConjunction(id, dsts)
default:
mod = NewBroadcaster(id, dsts)
}
modules[id] = mod
}
// Populate the memory for Conjunctions.
for id, mod := range modules {
for _, dst := range mod.Dsts() {
if c, ok := modules[dst].(Conjunction); ok {
(*c.memory)[id] = Low
}
}
}
// // DEBUG:
// for name, mod := range modules {
// if m, ok := mod.(Conjunction); ok {
// fmt.Println(name, mod.Dsts(), (*m.memory))
// } else if m, ok := mod.(FlipFlop); ok {
// fmt.Println(name, mod.Dsts(), (*m.on))
// } else {
// fmt.Println(name, mod.Dsts())
// }
// }
}
func part1() {
reset()
lows := 0
highs := 0
for i := 0; i < 1000; i++ {
queue = []Signal{{Low, "button", "broadcaster"}} // Push da button
for len(queue) > 0 {
signal := queue[0]
queue = queue[1:]
pulse := signal.pulse
src := signal.src
dst := signal.dst
if pulse == High {
highs++
} else {
lows++
}
// // DEBUG:
// if dst == "output" {
// continue
// }
mod, ok := modules[dst]
if ok {
mod.Process(src, pulse)
}
}
}
fmt.Println(lows * highs)
}
// rx gets its input from a single module which happens to be a conjunction.
// For this module to produce a Low pulse, all the modules that feed into it
// must send a High pulse at the same time.
// If we assume that these modules periodically output a High pulse, then we can
// calculate their period and then calculate the LCM of these periods to find
// the number of presses it will take for all of them to produce a High pulse at
// the same time, and that will be the answer to part 2.
func part2() {
reset()
// The one conjunction that outputs to rx.
beforeRX := ""
for id, mod := range modules {
for _, dst := range mod.Dsts() {
if dst == "rx" {
beforeRX = id
break
}
}
}
// The number of modules that output to beforeRX. This is used to make a
// breaking condition in the loop below.
n := 0
for _, mod := range modules {
for _, dst := range mod.Dsts() {
if dst == beforeRX {
n++
}
}
}
// List of minimum presses required for each of the modules that output to
// beforeRX to produce a High pulse.
pressesList := []int{}
presses := 0
for len(pressesList) < n {
presses++
queue = []Signal{{Low, "button", "broadcaster"}} // Push da button
for len(queue) > 0 {
signal := queue[0]
queue = queue[1:]
pulse := signal.pulse
src := signal.src
dst := signal.dst
if dst == beforeRX && pulse == High {
pressesList = append(pressesList, presses)
}
mod, ok := modules[dst]
if ok {
mod.Process(src, pulse)
}
}
}
fmt.Println(aoc.LCM(pressesList))
}
func main() {
part1() // 896998430
part2() // 236095992539963
}