-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day18.go
211 lines (177 loc) · 4.57 KB
/
Day18.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
package main
import (
"os"
"bufio"
"strings"
"strconv"
"fmt"
)
type Instruction struct {
instruction string
param1 string
param2 string
}
func main() {
if file, err := os.Open("Day18_t.txt"); err == nil {
fmt.Println(Day18P1(file))
defer file.Close()
}
if file, err := os.Open("Day18.txt"); err == nil {
fmt.Println(Day18P1(file))
defer file.Close()
}
if file, err := os.Open("Day18.txt"); err == nil {
Day18P2(file)
defer file.Close()
}
}
func Day18P2(input *os.File) int {
instructions, numInstructions := ParseInstruction(input)
zeroToOne := make(chan int64, 500)
oneToZero := make(chan int64, 500)
monitor0 := make(chan bool)
monitor1 := make(chan bool)
doneZero := make(chan bool)
doneOne := make(chan bool)
go func() {
recv0 := false
recv1 := false
for {
select {
case res := <-monitor0:
recv0 = res
case res := <-monitor1:
recv1 = res
}
if recv0 && recv1 && len(zeroToOne) == 0 && len(oneToZero) == 0 {
close(zeroToOne)
close(oneToZero)
break
}
}
}()
go InstructionRunner(0, instructions, numInstructions, zeroToOne, oneToZero, monitor0, doneZero)
go InstructionRunner(1, instructions, numInstructions, oneToZero, zeroToOne, monitor1, doneOne)
<-doneZero
<-doneOne
return 0
}
func InstructionRunner(pid int, instructions []Instruction, numIns int, recv <-chan int64, send chan<- int64, monitor chan bool, doneChannel chan<- bool) {
registers := make(map[string]int64, 26)
registers["p"] = int64(pid)
pcounter := 0
msgSent := 0
channelClosed := false
for pcounter < numIns {
nextInst := instructions[pcounter]
pcounter ++
var num int64
var err error
if num, err = strconv.ParseInt(nextInst.param2, 10, 0); err != nil {
num = registers[nextInst.param2]
}
if nextInst.instruction == "set" {
registers[nextInst.param1] = num
} else if nextInst.instruction == "add" {
registers[nextInst.param1] += num
} else if nextInst.instruction == "mul" {
registers[nextInst.param1] *= num
} else if nextInst.instruction == "mod" {
registers[nextInst.param1] %= num
} else if nextInst.instruction == "snd" {
var toSend int64
if toSend, err = strconv.ParseInt(nextInst.param1, 10, 0); err != nil {
toSend = registers[nextInst.param1]
}
msgSent ++
fmt.Printf("PID %d sending msg %d - %d\n", pid, msgSent, toSend)
send <- toSend
} else if nextInst.instruction == "rcv" {
monitor <- true
fmt.Printf("PID %d waiting\n", pid)
recvd, hasMore := <-recv
if !hasMore {
channelClosed = true
break
}
fmt.Printf("PID %d received %d\n", pid, recvd)
registers[nextInst.param1] = recvd
monitor <- false
} else if nextInst.instruction == "jgz" {
var val int64
if val, err = strconv.ParseInt(nextInst.param1, 10, 0); err != nil {
val = registers[nextInst.param1]
}
if val > 0 {
pcounter --
pcounter += int(num)
}
}
}
if !channelClosed {
monitor <- true
}
fmt.Printf("PID %d sent %d messages\n", pid, msgSent)
doneChannel <- true
}
func ParseInstruction(input *os.File) ([]Instruction, int) {
instructions := make([]Instruction, 1)
scanner := bufio.NewScanner(input)
counter := 0
for scanner.Scan() {
line := scanner.Text()
words := strings.Split(line, " ")
cmd := words[0]
reg := words[1]
var target string
if len(words) > 2 {
target = words[2]
} else {
target = ""
}
if counter == 0 {
instructions[0] = Instruction{cmd, reg, target}
} else {
instructions = append(instructions, Instruction{cmd, reg, target})
}
counter ++
}
return instructions, counter
}
func Day18P1(input *os.File) int {
registers := make(map[string]int, 26)
var lastPlayed int
//var lastRecovered int
instructions, numIns := ParseInstruction(input)
pcounter := 0
for pcounter < numIns {
nextIns := instructions[pcounter]
pcounter ++
var num int
var err error
if num, err = strconv.Atoi(nextIns.param2); err != nil {
num = registers[nextIns.param2]
}
if nextIns.instruction == "set" {
registers[nextIns.param1] = num
} else if nextIns.instruction == "add" {
registers[nextIns.param1] += num
} else if nextIns.instruction == "mul" {
registers[nextIns.param1] *= num
} else if nextIns.instruction == "mod" {
registers[nextIns.param1] %= num
} else if nextIns.instruction == "snd" {
lastPlayed = registers[nextIns.param1]
} else if nextIns.instruction == "rcv" {
if registers[nextIns.param1] != 0 {
return lastPlayed
}
} else if nextIns.instruction == "jgz" {
if registers[nextIns.param1] > 0 {
pcounter --
pcounter += num
}
}
}
return lastPlayed
}