-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
222 lines (203 loc) · 4.32 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
package main
import (
_ "embed"
"fmt"
"log"
"regexp"
"strconv"
. "github.com/beanz/advent/lib-go"
)
//go:embed input.txt
var input []byte
type Output struct {
num int
values []int
}
type Bot struct {
*Output
low *Output
high *Output
}
func (b *Bot) String() string {
return fmt.Sprintf("Bot with values %v\n", b.values)
}
func (o *Output) String() string {
return fmt.Sprintf("Output with values %v\n", o.values)
}
func (o *Output) AddValue(v int) {
o.values = append(o.values, v)
}
type Game struct {
bots map[int]*Bot
outputs map[int]*Output
debug bool
}
func (g *Game) Bot(i int) *Bot {
if b, ok := g.bots[i]; ok {
return b
}
if g.debug {
fmt.Printf("Creating bot %d\n", i)
}
b := &Bot{&Output{i, []int{}}, nil, nil}
g.bots[i] = b
return b
}
func (g *Game) Output(i int) *Output {
if o, ok := g.outputs[i]; ok {
return o
}
if g.debug {
fmt.Printf("Creating output %d\n", i)
}
o := &Output{i, []int{}}
g.outputs[i] = o
return o
}
func (g *Game) String() string {
s := ""
for k, v := range g.bots {
s += fmt.Sprintf("Bot %d: %s\n", k, v)
}
for k, v := range g.outputs {
s += fmt.Sprintf("Output %d: %s\n", k, v)
}
return s
}
func readGame(lines []string) *Game {
game := Game{map[int]*Bot{}, map[int]*Output{}, false}
for _, line := range lines {
valueRe := regexp.MustCompile(`value (-?\d+) goes to bot (-?\d+)`)
botRe := regexp.MustCompile(
`bot (-?\d+) gives low to (bot|output) (-?\d+) and high to (bot|output) (-?\d+)`)
m := botRe.FindStringSubmatch(line)
if m == nil {
m := valueRe.FindStringSubmatch(line)
if m == nil {
log.Fatalf("invalid line:\n%s\n", line)
}
value, err := strconv.Atoi(m[1])
if err != nil {
log.Fatalf("invalid value in line:\n%s\n", line)
}
botNum, err := strconv.Atoi(m[2])
if err != nil {
log.Fatalf("invalid bot in value line:\n%s\n", line)
}
if game.debug {
fmt.Printf("value line: bot %d <= %d\n", botNum, value)
}
bot := game.Bot(botNum)
bot.AddValue(value)
continue
}
botNum, err := strconv.Atoi(m[1])
if err != nil {
log.Fatalf("invalid bot in bot line:\n%s\n", line)
}
lowType := m[2]
highType := m[4]
lowNum, err := strconv.Atoi(m[3])
if err != nil {
log.Fatalf("invalid low bot in line:\n%s\n", line)
}
highNum, err := strconv.Atoi(m[5])
if err != nil {
log.Fatalf("invalid high bot in line:\n%s\n", line)
}
if game.debug {
fmt.Printf("bot line: %d => %d, %d\n", botNum, lowNum, highNum)
}
bot := game.Bot(botNum)
if lowType == "bot" {
bot.low = game.Bot(lowNum).Output
} else {
bot.low = game.Output(lowNum)
}
if highType == "bot" {
bot.high = game.Bot(highNum).Output
} else {
bot.high = game.Output(highNum)
}
}
return &game
}
func (g *Game) Part1() int {
for {
for i, bot := range g.bots {
if len(bot.values) != 2 {
continue
}
var min, max int
if bot.values[0] > bot.values[1] {
min = bot.values[1]
} else {
min = bot.values[0]
}
if bot.values[0] > bot.values[1] {
max = bot.values[0]
} else {
max = bot.values[1]
}
if min == 17 && max == 61 {
return i
}
if g.debug {
fmt.Printf("adding %d to bot %d\n", min, bot.low.num)
}
bot.low.AddValue(min)
if g.debug {
fmt.Printf("adding %d to bot %d\n", max, bot.high.num)
}
bot.high.AddValue(max)
bot.values = []int{}
}
}
}
func (g *Game) Part2() int {
for {
for _, bot := range g.bots {
if len(bot.values) != 2 {
continue
}
var min, max int
if bot.values[0] > bot.values[1] {
min = bot.values[1]
} else {
min = bot.values[0]
}
if bot.values[0] > bot.values[1] {
max = bot.values[0]
} else {
max = bot.values[1]
}
if g.debug {
fmt.Printf("adding %d to bot %d\n", min, bot.low.num)
}
bot.low.AddValue(min)
if g.debug {
fmt.Printf("adding %d to bot %d\n", max, bot.high.num)
}
bot.high.AddValue(max)
bot.values = []int{}
}
if len(g.outputs[0].values) > 0 &&
len(g.outputs[1].values) > 0 &&
len(g.outputs[2].values) > 0 {
return g.outputs[0].values[0] * g.outputs[1].values[0] *
g.outputs[2].values[0]
}
}
}
func main() {
game := readGame(InputLines(input))
res := game.Part1()
if !benchmark {
fmt.Printf("Part 1: %d\n", res)
}
res = game.Part2()
if !benchmark {
fmt.Printf("Part 2: %d\n", res)
}
}
var benchmark = false