-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
124 lines (97 loc) · 2.85 KB
/
main.go
File metadata and controls
124 lines (97 loc) · 2.85 KB
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
package main
import (
"github.com/danvolchek/AdventOfCode/lib"
"golang.org/x/exp/slices"
"strings"
)
type Monkey struct {
// the items the monkey is currently holding
items []int
// the monkey operation to change your worry
operation func(int) int
// the monkey test operation to determine where to throw next
test func(int) bool
// the target monkeys based on the test outcome
target map[bool]int
// the total inspections this monkey has done
inspections int
}
func (m *Monkey) Inspections() int {
return m.inspections
}
func parse(chunk string) *Monkey {
lines := strings.Split(chunk, "\n")
var monkey Monkey
// Line 1:
// Starting items: 74, 87
monkey.items = lib.Ints(lines[1])
// Line 2:
// Operation: new = old + 2
// Operation: new = old * 2
// Operation: new = old * old
nums := lib.Ints(lines[2])
mul := strings.Contains(lines[2], "*")
monkey.operation = func(i int) int {
var arg int
if len(nums) == 0 {
arg = i
} else {
arg = nums[0]
}
if mul {
return i * arg
} else {
return i + arg
}
}
// Line 3:
// Test: divisible by 5
arg := lib.Int(lines[3])
monkey.test = func(i int) bool {
return i%arg == 0
}
// Line 4:
// If true: throw to monkey 7
// Line 5:
// If false: throw to monkey 4
monkey.target = map[bool]int{
true: lib.Int(lines[4]),
false: lib.Int(lines[5]),
}
return &monkey
}
// Add adds an item to this monkey's items.
func (m *Monkey) Add(item int) {
m.items = append(m.items, item)
}
// Inspect runs the monkey's inspect and throw algorithm.
func (m *Monkey) Inspect(monkeys []*Monkey) {
for _, item := range m.items {
item = m.operation(item) / 3
targetMonkeyIndex := m.target[m.test(item)]
monkeys[targetMonkeyIndex].Add(item)
}
m.inspections += len(m.items)
m.items = []int{}
}
const (
rounds = 20
)
func solve(monkeys []*Monkey) int {
for i := 0; i < rounds; i++ {
for _, monkey := range monkeys {
monkey.Inspect(monkeys)
}
}
inspections := lib.Map(monkeys, (*Monkey).Inspections)
slices.Sort(inspections)
return inspections[len(inspections)-1] * inspections[len(inspections)-2]
}
func main() {
solver := lib.Solver[[]*Monkey, int]{
ParseF: lib.ParseChunks(parse),
SolveF: solve,
}
solver.Expect("Monkey 0:\n Starting items: 79, 98\n Operation: new = old * 19\n Test: divisible by 23\n If true: throw to monkey 2\n If false: throw to monkey 3\n\nMonkey 1:\n Starting items: 54, 65, 75, 74\n Operation: new = old + 6\n Test: divisible by 19\n If true: throw to monkey 2\n If false: throw to monkey 0\n\nMonkey 2:\n Starting items: 79, 60, 97\n Operation: new = old * old\n Test: divisible by 13\n If true: throw to monkey 1\n If false: throw to monkey 3\n\nMonkey 3:\n Starting items: 74\n Operation: new = old + 3\n Test: divisible by 17\n If true: throw to monkey 0\n If false: throw to monkey 1", 10605)
solver.Verify(56350)
}