-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11p1.go
182 lines (152 loc) · 4.22 KB
/
11p1.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
package main
import (
"fmt"
"strconv"
"strings"
"aoc2022/utils"
)
type monkey struct {
id int
items []int
operation mokeyOperation
test monkeyTest
inspectCount int
}
type monkeyTest struct {
divisibleBy int
monkeyWhenTrue int
monkeyWhenFalse int
}
type mokeyOperation struct {
operationType byte
value int
}
var commonDivisor = 1
func D11P1() {
const rounds = 20
lines := utils.ReadLines("./inputs/11.txt")
monkeys := parseMonkeys(lines)
for i := 0; i < rounds; i++ {
simulateRound(&monkeys, true)
}
monkeyWithMostInspections := monkeys[0]
monkeyWithSecondMostInspections := monkeys[0]
for _, monkey := range monkeys {
if monkey.inspectCount > monkeyWithMostInspections.inspectCount {
monkeyWithSecondMostInspections = monkeyWithMostInspections
monkeyWithMostInspections = monkey
}
}
fmt.Printf(
"Product of top two monkey inspections: %d\n",
monkeyWithMostInspections.inspectCount*monkeyWithSecondMostInspections.inspectCount,
)
}
func parseMonkeys(lines []string) []monkey {
monkeys := []monkey{}
currentMonkey := monkey{}
for _, line := range lines {
if line == "" {
// Add completed monkey to array
monkeys = append(monkeys, currentMonkey)
continue
}
if line[0] == 'M' {
// Set ID
monkeyId := line[7 : len(line)-1]
currentMonkey.id, _ = strconv.Atoi(monkeyId)
continue
}
switch line[2] {
case 'S':
// Set Items
currentMonkey.items = []int{}
itemsString := line[18:]
itemsArray := strings.Split(itemsString, ", ")
for _, item := range itemsArray {
itemInt, _ := strconv.Atoi(item)
currentMonkey.items = append(currentMonkey.items, itemInt)
}
case 'O':
// Set Operation
currentMonkey.operation.operationType = line[23]
valueString := line[25:]
valueInt, err := strconv.Atoi(valueString)
if err != nil {
currentMonkey.operation.value = 0
continue
}
currentMonkey.operation.value = valueInt
case 'T':
// Set Test divisibleBy
divisibleByString := line[21:]
divisibleByInt, _ := strconv.Atoi(divisibleByString)
currentMonkey.test.divisibleBy = divisibleByInt
// Update common divisor
commonDivisor *= divisibleByInt
case ' ':
testCaseParts := strings.Split(line, " ")
testCaseMonkeyId, _ := strconv.Atoi(testCaseParts[len(testCaseParts)-1])
// Set Test monkeyWhenTrue
if line[7] == 't' {
currentMonkey.test.monkeyWhenTrue = testCaseMonkeyId
continue
}
// Set Test monkeyWhenFalse
currentMonkey.test.monkeyWhenFalse = testCaseMonkeyId
}
}
// Add last monkey
monkeys = append(monkeys, currentMonkey)
return monkeys
}
func simulateRound(monkeys *[]monkey, partOne bool) {
for i := 0; i < len(*monkeys); i++ {
monkey := (*monkeys)[i]
// Check if monkey has items
if len(monkey.items) == 0 {
continue
}
simulateMonkeyOperations(&monkey, monkeys, partOne)
// Remove all thrown items
(*monkeys)[i].items = []int{}
}
}
func simulateMonkeyOperations(selectedMonkey *monkey, allMonkeys *[]monkey, partOne bool) {
resetOperationValue := false
for i, item := range selectedMonkey.items {
(*allMonkeys)[selectedMonkey.id].inspectCount++
// If the monkey's operation references the item value, set to the value of the item
if selectedMonkey.operation.value == 0 {
selectedMonkey.operation.value = item
resetOperationValue = true
}
if selectedMonkey.operation.operationType == '*' {
selectedMonkey.items[i] = item * selectedMonkey.operation.value
} else {
selectedMonkey.items[i] = item + selectedMonkey.operation.value
}
if resetOperationValue {
selectedMonkey.operation.value = 0
}
if partOne {
selectedMonkey.items[i] = selectedMonkey.items[i] / 3
} else {
selectedMonkey.items[i] %= commonDivisor
}
simulateMonkeyTest(selectedMonkey, selectedMonkey.items[i], allMonkeys)
}
}
func simulateMonkeyTest(selectedMonkey *monkey, item int, allMonkeys *[]monkey) {
if item%selectedMonkey.test.divisibleBy == 0 {
(*allMonkeys)[selectedMonkey.test.monkeyWhenTrue].items = append(
(*allMonkeys)[selectedMonkey.test.monkeyWhenTrue].items,
item,
)
return
}
(*allMonkeys)[selectedMonkey.test.monkeyWhenFalse].items = append(
(*allMonkeys)[selectedMonkey.test.monkeyWhenFalse].items,
item,
)
}