-
Notifications
You must be signed in to change notification settings - Fork 0
/
day11.go
144 lines (116 loc) · 3.18 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
package main
import (
"io"
"strconv"
"strings"
"github.com/armsnyder/aoc2022/aocutil"
"github.com/samber/lo"
)
var _ = declareDay(11, func(part2 bool, inputReader io.Reader) any {
if part2 {
return day11Part2(inputReader)
}
return day11Part1(inputReader)
})
func day11Part1(inputReader io.Reader) any {
return day11(inputReader, 20, func(_ []*day11Monkey) func(int) int {
return func(worry int) int {
return worry / 3
}
})
}
func day11Part2(inputReader io.Reader) any {
return day11(inputReader, 10000, func(monkeys []*day11Monkey) func(int) int {
modulus := lo.Reduce(monkeys, func(agg int, m *day11Monkey, _ int) int {
return agg * m.divisor
}, 1)
return func(worry int) int {
return worry % modulus
}
})
}
func day11(inputReader io.Reader, rounds int, makeWorryFunc func(monkeys []*day11Monkey) func(int) int) int {
monkeys := day11Parse(inputReader)
manageWorry := makeWorryFunc(monkeys)
inspections := day11CountInspections(monkeys, rounds, manageWorry)
monkeyBusiness := day11CalculateMonkeyBusiness(inspections)
return monkeyBusiness
}
func day11CountInspections(monkeys []*day11Monkey, rounds int, manageWorry func(int) int) []int {
inspections := make([]int, len(monkeys))
for round := 0; round < rounds; round++ {
for i, monkey := range monkeys {
inspections[i] += len(monkey.items)
for _, item := range monkey.items {
item = monkey.operation(item)
item = manageWorry(item)
if item%monkey.divisor == 0 {
monkeys[monkey.ifTrue].items = append(monkeys[monkey.ifTrue].items, item)
} else {
monkeys[monkey.ifFalse].items = append(monkeys[monkey.ifFalse].items, item)
}
}
monkey.items = monkey.items[:0]
}
}
return inspections
}
func day11CalculateMonkeyBusiness(inspections []int) int {
var top2 [2]int
for _, v := range inspections {
switch {
case v > top2[1]:
top2[0], top2[1] = top2[1], v
case v > top2[0]:
top2[0] = v
}
}
return top2[0] * top2[1]
}
type day11Monkey struct {
items []int
operation func(int) int
divisor int
ifTrue int
ifFalse int
}
func day11Parse(inputReader io.Reader) []*day11Monkey {
var monkeys []*day11Monkey
aocutil.VisitStringGroups(inputReader, func(v []string) {
var monkey day11Monkey
monkey.items = lo.Map(strings.Split(v[1][strings.LastIndexByte(v[1], ':')+2:], ", "), func(s string, _ int) int {
item, _ := strconv.Atoi(s)
return item
})
lastSpace := strings.LastIndexByte(v[2], ' ')
op := v[2][lastSpace-1]
operand := v[2][lastSpace+1:]
var opFn func(a, b int) int
switch op {
case '+':
opFn = func(a, b int) int {
return a + b
}
case '*':
opFn = func(a, b int) int {
return a * b
}
}
switch operand {
case "old":
monkey.operation = func(i int) int {
return opFn(i, i)
}
default:
operandInt, _ := strconv.Atoi(operand)
monkey.operation = func(i int) int {
return opFn(i, operandInt)
}
}
monkey.divisor, _ = strconv.Atoi(v[3][strings.LastIndexByte(v[3], ' ')+1:])
monkey.ifTrue, _ = strconv.Atoi(v[4][strings.LastIndexByte(v[4], ' ')+1:])
monkey.ifFalse, _ = strconv.Atoi(v[5][strings.LastIndexByte(v[5], ' ')+1:])
monkeys = append(monkeys, &monkey)
})
return monkeys
}