-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask18.go
183 lines (168 loc) · 3.89 KB
/
task18.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
package main
import (
"fmt"
"os"
"strconv"
"github.com/LaurenceUsas/advent-of-code-2017/helpers"
)
//Task18 Solution
func Task18() {
pwd, _ := os.Getwd()
instructions := helpers.InputFile(pwd + "/input/input18.txt")
//fmt.Printf("[Part 1 answer] - %v\n", task18PartOne(instructions))
fmt.Printf("[Part 2 answer] - %v\n", task18PartTwo(instructions))
}
type Instruction struct {
Command string //command
Register string //register
Value string //value
}
type Duet struct {
Instructions map[int]*Instruction
RegMemory map[string]int // Register memory
RegDefault int // Default value for empty register
K int // Instruction progress
Frequency int // Frequency of last sound
SentValues int
IsWaiting bool
Finished bool
}
func (d *Duet) GetValue(v string) int {
// accept both letter and
val, err := strconv.Atoi(v)
if err != nil {
if value, ok := d.RegMemory[v]; !ok {
val = d.RegDefault
} else {
val = value
}
}
return val
}
func NewDuet(instruction []string, regDefault int) *Duet {
d := &Duet{}
d.Instructions = make(map[int]*Instruction)
d.RegMemory = make(map[string]int)
for i, v := range instruction {
inst := helpers.SplitBySpace(v)
ti := &Instruction{
Command: inst[0],
Register: inst[1],
}
if len(inst) == 3 {
ti.Value = inst[2]
}
d.Instructions[i] = ti
}
d.RegDefault = regDefault
d.K = 0
d.SentValues = 0
d.IsWaiting = false
d.Finished = false
return d
}
func (d *Duet) Run() int {
for {
inst := d.Instructions[d.K]
switch inst.Command {
case "snd":
d.Frequency = d.RegMemory[inst.Register]
case "set":
val := d.GetValue(inst.Value)
d.RegMemory[inst.Register] = val
case "add":
//if Int add value or String - get int from register.
add := d.GetValue(inst.Value)
d.RegMemory[inst.Register] += add
case "mul":
val := d.GetValue(inst.Value)
d.RegMemory[inst.Register] *= val
case "mod":
val := d.GetValue(inst.Value)
d.RegMemory[inst.Register] %= val
case "rcv":
val := d.GetValue(inst.Register)
if val != 0 {
return d.Frequency
}
case "jgz":
val := d.GetValue(inst.Register)
if val != 0 {
add, _ := strconv.Atoi(inst.Value)
d.K += add
d.K--
}
}
d.K++
}
}
func task18PartOne(instructions []string) int {
d := NewDuet(instructions, 0)
return d.Run()
}
func task18PartTwo(instructions []string) int {
d0 := NewDuet(instructions, 0)
d1 := NewDuet(instructions, 1)
in0 := make(chan int, 1000)
in1 := make(chan int, 1000)
// w1 := false
// w2 := false
go d0.RunSync(in1, in0)
go d1.RunSync(in0, in1)
// all cases except. d1 and d2 is waiting.
for {
if d1.Finished {
return d1.SentValues
} else if d0.IsWaiting && d1.IsWaiting {
fmt.Printf("Both Waiting [%v][%v]\n", len(in0), len(in1))
if len(in0) == 0 || len(in1) == 0 {
return d1.SentValues
}
}
}
// Deadlock when both trying to receive from the empty channel.
// When both waiting.
}
func (d *Duet) RunSync(out chan<- int, in chan int) {
for {
if d.K >= len(d.Instructions) || d.K < 0 {
fmt.Println("Finished!")
d.Finished = true
break
}
inst := d.Instructions[d.K]
switch inst.Command {
case "snd":
d.SentValues++
out <- d.RegMemory[inst.Register]
case "set":
val := d.GetValue(inst.Value)
d.RegMemory[inst.Register] = val
case "add":
//if Int add value or String - get int from register.
add := d.GetValue(inst.Value)
d.RegMemory[inst.Register] += add
case "mul":
val := d.GetValue(inst.Value)
d.RegMemory[inst.Register] *= val
case "mod":
val := d.GetValue(inst.Value)
d.RegMemory[inst.Register] %= val
case "rcv":
if len(in) == 0 {
d.IsWaiting = true
}
val := <-in
d.IsWaiting = false
d.RegMemory[inst.Register] = val
case "jgz":
val := d.GetValue(inst.Register)
if val != 0 {
add, _ := strconv.Atoi(inst.Value)
d.K += add
d.K--
}
}
d.K++
}
}