-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
253 lines (223 loc) · 4.94 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
package main
import (
"bufio"
"fmt"
"io"
"strconv"
"strings"
"sync"
"time"
lib "github.com/teivah/advent-of-code"
)
func fs1(input io.Reader) int {
scanner := bufio.NewScanner(input)
var cmds []apply
for scanner.Scan() {
s := scanner.Text()
del := lib.NewDelimiter(s, " ")
if strings.HasPrefix(s, "snd") {
cmds = append(cmds, snd(toValueReg(del.GetString(1))))
} else if strings.HasPrefix(s, "set") {
cmds = append(cmds, set(toValueReg(del.GetString(1)), toValueReg(del.GetString(2))))
} else if strings.HasPrefix(s, "add") {
cmds = append(cmds, add(toValueReg(del.GetString(1)), toValueReg(del.GetString(2))))
} else if strings.HasPrefix(s, "mul") {
cmds = append(cmds, mul(toValueReg(del.GetString(1)), toValueReg(del.GetString(2))))
} else if strings.HasPrefix(s, "mod") {
cmds = append(cmds, mod(toValueReg(del.GetString(1)), toValueReg(del.GetString(2))))
} else if strings.HasPrefix(s, "rcv") {
cmds = append(cmds, rcv(toValueReg(del.GetString(1))))
} else if strings.HasPrefix(s, "jgz") {
cmds = append(cmds, jgz(toValueReg(del.GetString(1)), toValueReg(del.GetString(2))))
} else {
panic(s)
}
}
offset := 0
state := State{
reg: make(map[string]int),
}
for {
cmds[offset](&state)
if state.recovered {
return state.frequency
}
offset = state.offset
}
}
type State struct {
reg map[string]int
offset int
frequency int
// Part 1
recovered bool
// Part 2
id int
sent int
input <-chan int
output chan<- int
deadlock bool
}
func (s *State) print() {
fmt.Println(s.offset, s.reg)
}
type ValueReg struct {
i *int
s *string
}
func (vr ValueReg) get(state *State) int {
if vr.i != nil {
return *vr.i
}
return state.reg[*vr.s]
}
func toValueReg(s string) ValueReg {
i, err := strconv.Atoi(s)
if err != nil {
return ValueReg{s: &s}
}
return ValueReg{i: &i}
}
type apply func(state *State)
func snd(x ValueReg) apply {
return func(state *State) {
state.offset++
state.frequency = x.get(state)
}
}
func snd2(x ValueReg) apply {
return func(state *State) {
state.offset++
state.sent++
v := 0
if x.s != nil && *x.s == "p" {
v = state.id
} else {
v = x.get(state)
}
select {
case state.output <- v:
case <-time.After(time.Second):
state.deadlock = true
}
}
}
func set(x ValueReg, y ValueReg) apply {
return func(state *State) {
state.offset++
state.reg[*x.s] = y.get(state)
}
}
func add(x ValueReg, y ValueReg) apply {
return func(state *State) {
state.offset++
state.reg[*x.s] += y.get(state)
}
}
func mul(x ValueReg, y ValueReg) apply {
return func(state *State) {
state.offset++
state.reg[*x.s] *= y.get(state)
}
}
func mod(x ValueReg, y ValueReg) apply {
return func(state *State) {
state.offset++
state.reg[*x.s] = state.reg[*x.s] % y.get(state)
}
}
func rcv(x ValueReg) apply {
return func(state *State) {
state.offset++
if x.get(state) != 0 {
state.recovered = true
}
}
}
func rcv2(x ValueReg) apply {
return func(state *State) {
state.offset++
select {
case state.reg[*x.s] = <-state.input:
case <-time.After(time.Second):
state.deadlock = true
return
}
}
}
func jgz(x ValueReg, y ValueReg) apply {
return func(state *State) {
v := x.get(state)
if v > 0 {
state.offset += y.get(state)
} else {
state.offset++
}
}
}
func fs2(input io.Reader) int {
scanner := bufio.NewScanner(input)
var cmds []apply
for scanner.Scan() {
s := scanner.Text()
del := lib.NewDelimiter(s, " ")
if strings.HasPrefix(s, "snd") {
cmds = append(cmds, snd2(toValueReg(del.GetString(1))))
} else if strings.HasPrefix(s, "set") {
cmds = append(cmds, set(toValueReg(del.GetString(1)), toValueReg(del.GetString(2))))
} else if strings.HasPrefix(s, "add") {
cmds = append(cmds, add(toValueReg(del.GetString(1)), toValueReg(del.GetString(2))))
} else if strings.HasPrefix(s, "mul") {
cmds = append(cmds, mul(toValueReg(del.GetString(1)), toValueReg(del.GetString(2))))
} else if strings.HasPrefix(s, "mod") {
cmds = append(cmds, mod(toValueReg(del.GetString(1)), toValueReg(del.GetString(2))))
} else if strings.HasPrefix(s, "rcv") {
cmds = append(cmds, rcv2(toValueReg(del.GetString(1))))
} else if strings.HasPrefix(s, "jgz") {
cmds = append(cmds, jgz(toValueReg(del.GetString(1)), toValueReg(del.GetString(2))))
} else {
panic(s)
}
}
res := 0
ch0 := make(chan int, 1_000_000)
ch1 := make(chan int, 1_000_000)
go func() {
offset := 0
state := State{
reg: make(map[string]int),
id: 0,
input: ch0,
output: ch1,
}
for {
cmds[offset](&state)
offset = state.offset
if state.deadlock {
return
}
}
}()
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
offset := 0
state := State{
reg: map[string]int{"p": 1},
id: 1,
input: ch1,
output: ch0,
}
for {
cmds[offset](&state)
if state.deadlock {
res = state.sent
return
}
offset = state.offset
}
}()
wg.Wait()
return res
}