-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
176 lines (142 loc) · 3.46 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
package main
import (
"fmt"
"io"
"os"
"regexp"
"sort"
"strconv"
"github.com/devries/advent_of_code_2022/utils"
"github.com/spf13/pflag"
)
const inputfile = "../inputs/day11.txt"
func main() {
pflag.Parse()
f, err := os.Open(inputfile)
utils.Check(err, "error opening input")
defer f.Close()
r := solve(f)
fmt.Println(r)
}
func solve(r io.Reader) int64 {
lines := utils.ReadLines(r)
monkeys := parse(lines)
// rounds
for i := 0; i < 20; i++ {
for j := 0; j < len(monkeys); j++ {
turn(monkeys, j)
}
}
sort.Sort(ByCount(monkeys))
var ret int64 = 1
for j := len(monkeys) - 2; j < len(monkeys); j++ {
ret *= monkeys[j].Counter
}
return ret
}
type Monkey struct {
Items []int64
Operation func(int64) int64
Test int64
True int
False int
Counter int64
}
func parse(lines []string) []Monkey {
ret := []Monkey{}
monkeyRe := regexp.MustCompile(`^Monkey\s(\d+)`)
startingRe := regexp.MustCompile(`Starting\sitems:\s`)
operationRe := regexp.MustCompile(`Operation:\snew\s=\s(\w+)\s([*+])\s(\w+)`)
testRe := regexp.MustCompile(`Test:\sdivisible\sby\s(\d+)`)
ifRe := regexp.MustCompile(`If\s(true|false):\sthrow\sto\smonkey\s(\d+)`)
csnRe := regexp.MustCompile(`(\d+)`)
var m Monkey
first := true
for _, ln := range lines {
switch {
case monkeyRe.MatchString(ln):
if !first {
ret = append(ret, m)
} else {
first = false
}
m = Monkey{}
case startingRe.MatchString(ln):
for _, v := range csnRe.FindAllString(ln, -1) {
n, err := strconv.ParseInt(v, 10, 64)
utils.Check(err, "unable to parse %s to integer", v)
m.Items = append(m.Items, n)
}
case operationRe.MatchString(ln):
matches := operationRe.FindStringSubmatch(ln)
switch matches[2] {
case "+":
if matches[3] == "old" {
m.Operation = func(x int64) int64 {
return x + x
}
} else {
n, err := strconv.ParseInt(matches[3], 10, 64)
utils.Check(err, "unable to parse %s to integer", matches[3])
m.Operation = func(x int64) int64 {
return x + n
}
}
case "*":
if matches[3] == "old" {
m.Operation = func(x int64) int64 {
return x * x
}
} else {
n, err := strconv.ParseInt(matches[3], 10, 64)
utils.Check(err, "unable to parse %s to integer", matches[3])
m.Operation = func(x int64) int64 {
return x * n
}
}
}
case testRe.MatchString(ln):
matches := testRe.FindStringSubmatch(ln)
n, err := strconv.ParseInt(matches[1], 10, 64)
utils.Check(err, "unable to parse %s to integer", matches[1])
m.Test = n
case ifRe.MatchString(ln):
matches := ifRe.FindStringSubmatch(ln)
n, err := strconv.Atoi(matches[2])
utils.Check(err, "unable to parse %s to integer", matches[2])
switch matches[1] {
case "true":
m.True = n
case "false":
m.False = n
}
}
}
ret = append(ret, m)
return ret
}
func turn(monkeys []Monkey, n int) {
m := monkeys[n]
for {
if len(m.Items) == 0 {
break
}
i := m.Items[0]
m.Items = m.Items[1:]
m.Counter++
// Perform operation
i = m.Operation(i)
i = i / 3
// Test
if i%m.Test == 0 {
monkeys[m.True].Items = append(monkeys[m.True].Items, i)
} else {
monkeys[m.False].Items = append(monkeys[m.False].Items, i)
}
}
monkeys[n] = m
}
type ByCount []Monkey
func (a ByCount) Len() int { return len(a) }
func (a ByCount) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByCount) Less(i, j int) bool { return a[i].Counter < a[j].Counter }