-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday11.go
151 lines (116 loc) Β· 2.8 KB
/
day11.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
package day11
import (
"math"
"sort"
"strconv"
"strings"
)
type Item struct {
worryLevel int
}
type WorryOperation struct {
operation string
argument int
}
type MonkeyTest struct {
divisor int
trueBranch int
falseBranch int
}
type Monkey struct {
items []Item
itemsInspected int
worryOperation *WorryOperation
test *MonkeyTest
}
func check(err error) {
if err != nil {
panic(err)
}
}
func (monkey *Monkey) inspectNextItem() (int, Item) {
if len(monkey.items) == 0 {
return 0, Item{}
}
nextItem := monkey.items[0]
monkey.items = monkey.items[1:]
monkey.itemsInspected++
switch monkey.worryOperation.operation {
case "+":
nextItem.worryLevel = nextItem.worryLevel + monkey.worryOperation.argument
case "*":
nextItem.worryLevel = nextItem.worryLevel * monkey.worryOperation.argument
case "^":
nextItem.worryLevel = int(math.Pow(float64(nextItem.worryLevel), 2))
}
nextItem.worryLevel /= 3
passesTest := nextItem.worryLevel%monkey.test.divisor == 0
if passesTest {
return monkey.test.trueBranch, nextItem
} else {
return monkey.test.falseBranch, nextItem
}
}
func parseMonkeys(input string) (parsedMonkeys []Monkey) {
rawMonkeys := strings.Split(input, "\n\n")
for _, rawMonkey := range rawMonkeys {
monkeyParts := strings.Split(rawMonkey, "\n")
items := strings.Split(monkeyParts[1][18:], ", ")
var parsedItems []Item
for _, item := range items {
numberValue, err := strconv.Atoi(item)
check(err)
parsedItems = append(parsedItems, Item{numberValue})
}
operator := monkeyParts[2][23:24]
argument := monkeyParts[2][25:]
if argument == "old" {
operator = "^"
argument = "2"
}
parsedArgument, err := strconv.Atoi(argument)
check(err)
divisor, err := strconv.Atoi(monkeyParts[3][21:])
check(err)
trueBranch, err := strconv.Atoi(monkeyParts[4][29:])
check(err)
falseBranch, err := strconv.Atoi(monkeyParts[5][30:])
check(err)
parsedMonkeys = append(parsedMonkeys,
Monkey{
parsedItems,
0,
&WorryOperation{
operator,
parsedArgument,
},
&MonkeyTest{
divisor,
trueBranch,
falseBranch,
},
},
)
}
return
}
func part1(input string, roundsToRun int) (monkeyBusiness int) {
monkeys := parseMonkeys(input)
for round := 0; round < roundsToRun; round++ {
for monkeyIndex := range monkeys {
for {
toMonkey, item := monkeys[monkeyIndex].inspectNextItem()
if item.worryLevel == 0 {
break
}
monkeys[toMonkey].items = append(monkeys[toMonkey].items, item)
}
}
}
var itemsInspectedByMonkey []int
for _, monkey := range monkeys {
itemsInspectedByMonkey = append(itemsInspectedByMonkey, monkey.itemsInspected)
}
sort.Sort(sort.Reverse(sort.IntSlice(itemsInspectedByMonkey)))
return itemsInspectedByMonkey[0] * itemsInspectedByMonkey[1]
}