Day11
This commit is contained in:
parent
e43af3b8bc
commit
cacb123431
118
day11/day11.go
Normal file
118
day11/day11.go
Normal file
@ -0,0 +1,118 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"AOC2022/helper"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"sort"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Monkey struct {
|
||||||
|
items []int
|
||||||
|
newOperation func(item int) int
|
||||||
|
test func(item int) int
|
||||||
|
inspections int
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
args := os.Args[1:]
|
||||||
|
lines := helper.ReadTextFile(args[0])
|
||||||
|
runSimulation(lines, 20, true)
|
||||||
|
runSimulation(lines, 10000, false)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func runSimulation(lines []string, turns int, part1 bool) {
|
||||||
|
monkeys, worryLimiter := getMonkeys(lines, part1)
|
||||||
|
for i := 0; i < turns; i++ {
|
||||||
|
for i := 0; i < len(monkeys); i++ {
|
||||||
|
monkey := &monkeys[i]
|
||||||
|
for _, item := range monkey.items {
|
||||||
|
monkey.inspections++
|
||||||
|
item = monkey.newOperation(item) % worryLimiter
|
||||||
|
throwTo := monkey.test(item)
|
||||||
|
monkeys[throwTo].items = append(monkeys[throwTo].items, item)
|
||||||
|
}
|
||||||
|
monkey.items = []int{}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sort.Slice(monkeys, func(i, j int) bool {
|
||||||
|
return monkeys[i].inspections > monkeys[j].inspections
|
||||||
|
})
|
||||||
|
fmt.Println(monkeys[0].inspections * monkeys[1].inspections)
|
||||||
|
}
|
||||||
|
|
||||||
|
func getMonkeys(lines []string, part1 bool) ([]Monkey, int) {
|
||||||
|
var monkeys []Monkey
|
||||||
|
worryLimiter := 1
|
||||||
|
for i := 0; i < len(lines); i += 7 {
|
||||||
|
monkey := Monkey{}
|
||||||
|
startingItemsString := lines[i+1][18:]
|
||||||
|
for _, item := range strings.Split(startingItemsString, ", ") {
|
||||||
|
monkey.items = append(monkey.items, helper.RemoveError(strconv.Atoi(item)))
|
||||||
|
}
|
||||||
|
worryLimiter *= getTest(lines, i, &monkey)
|
||||||
|
getOperation(lines, i, &monkey, part1)
|
||||||
|
monkeys = append(monkeys, monkey)
|
||||||
|
}
|
||||||
|
return monkeys, worryLimiter
|
||||||
|
}
|
||||||
|
|
||||||
|
func getTest(lines []string, i int, monkey *Monkey) int {
|
||||||
|
divisbleby := helper.RemoveError(strconv.Atoi(lines[i+3][21:]))
|
||||||
|
returnTrue := helper.RemoveError(strconv.Atoi(lines[i+4][29:]))
|
||||||
|
returnFalse := helper.RemoveError(strconv.Atoi(lines[i+5][30:]))
|
||||||
|
monkey.test = func(item int) int {
|
||||||
|
if item%divisbleby == 0 {
|
||||||
|
return returnTrue
|
||||||
|
}
|
||||||
|
return returnFalse
|
||||||
|
}
|
||||||
|
return divisbleby
|
||||||
|
}
|
||||||
|
|
||||||
|
func getOperation(lines []string, i int, monkey *Monkey, part1 bool) {
|
||||||
|
opertionString := lines[i+2][23:]
|
||||||
|
value, err := strconv.Atoi(opertionString[2:])
|
||||||
|
if err == nil {
|
||||||
|
if opertionString[0] == '+' {
|
||||||
|
monkey.newOperation = func(item int) (returnVal int) {
|
||||||
|
returnVal = (item + value)
|
||||||
|
if part1 {
|
||||||
|
returnVal /= 3
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if opertionString[0] == '*' {
|
||||||
|
monkey.newOperation = func(item int) (returnVal int) {
|
||||||
|
returnVal = (item * value)
|
||||||
|
if part1 {
|
||||||
|
returnVal /= 3
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if opertionString[0] == '+' {
|
||||||
|
monkey.newOperation = func(item int) (returnVal int) {
|
||||||
|
returnVal = item + item
|
||||||
|
if part1 {
|
||||||
|
returnVal /= 3
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if opertionString[0] == '*' {
|
||||||
|
monkey.newOperation = func(item int) (returnVal int) {
|
||||||
|
returnVal = item * item
|
||||||
|
if part1 {
|
||||||
|
returnVal /= 3
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
55
day11/input
Normal file
55
day11/input
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
Monkey 0:
|
||||||
|
Starting items: 54, 61, 97, 63, 74
|
||||||
|
Operation: new = old * 7
|
||||||
|
Test: divisible by 17
|
||||||
|
If true: throw to monkey 5
|
||||||
|
If false: throw to monkey 3
|
||||||
|
|
||||||
|
Monkey 1:
|
||||||
|
Starting items: 61, 70, 97, 64, 99, 83, 52, 87
|
||||||
|
Operation: new = old + 8
|
||||||
|
Test: divisible by 2
|
||||||
|
If true: throw to monkey 7
|
||||||
|
If false: throw to monkey 6
|
||||||
|
|
||||||
|
Monkey 2:
|
||||||
|
Starting items: 60, 67, 80, 65
|
||||||
|
Operation: new = old * 13
|
||||||
|
Test: divisible by 5
|
||||||
|
If true: throw to monkey 1
|
||||||
|
If false: throw to monkey 6
|
||||||
|
|
||||||
|
Monkey 3:
|
||||||
|
Starting items: 61, 70, 76, 69, 82, 56
|
||||||
|
Operation: new = old + 7
|
||||||
|
Test: divisible by 3
|
||||||
|
If true: throw to monkey 5
|
||||||
|
If false: throw to monkey 2
|
||||||
|
|
||||||
|
Monkey 4:
|
||||||
|
Starting items: 79, 98
|
||||||
|
Operation: new = old + 2
|
||||||
|
Test: divisible by 7
|
||||||
|
If true: throw to monkey 0
|
||||||
|
If false: throw to monkey 3
|
||||||
|
|
||||||
|
Monkey 5:
|
||||||
|
Starting items: 72, 79, 55
|
||||||
|
Operation: new = old + 1
|
||||||
|
Test: divisible by 13
|
||||||
|
If true: throw to monkey 2
|
||||||
|
If false: throw to monkey 1
|
||||||
|
|
||||||
|
Monkey 6:
|
||||||
|
Starting items: 63
|
||||||
|
Operation: new = old + 4
|
||||||
|
Test: divisible by 19
|
||||||
|
If true: throw to monkey 7
|
||||||
|
If false: throw to monkey 4
|
||||||
|
|
||||||
|
Monkey 7:
|
||||||
|
Starting items: 72, 51, 93, 63, 80, 86, 81
|
||||||
|
Operation: new = old * old
|
||||||
|
Test: divisible by 11
|
||||||
|
If true: throw to monkey 0
|
||||||
|
If false: throw to monkey 4
|
27
day11/testinput
Normal file
27
day11/testinput
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
Monkey 0:
|
||||||
|
Starting items: 79, 98
|
||||||
|
Operation: new = old * 19
|
||||||
|
Test: divisible by 23
|
||||||
|
If true: throw to monkey 2
|
||||||
|
If false: throw to monkey 3
|
||||||
|
|
||||||
|
Monkey 1:
|
||||||
|
Starting items: 54, 65, 75, 74
|
||||||
|
Operation: new = old + 6
|
||||||
|
Test: divisible by 19
|
||||||
|
If true: throw to monkey 2
|
||||||
|
If false: throw to monkey 0
|
||||||
|
|
||||||
|
Monkey 2:
|
||||||
|
Starting items: 79, 60, 97
|
||||||
|
Operation: new = old * old
|
||||||
|
Test: divisible by 13
|
||||||
|
If true: throw to monkey 1
|
||||||
|
If false: throw to monkey 3
|
||||||
|
|
||||||
|
Monkey 3:
|
||||||
|
Starting items: 74
|
||||||
|
Operation: new = old + 3
|
||||||
|
Test: divisible by 17
|
||||||
|
If true: throw to monkey 0
|
||||||
|
If false: throw to monkey 1
|
Loading…
Reference in New Issue
Block a user