-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day11.kt
103 lines (82 loc) · 3.09 KB
/
Day11.kt
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
package aoc.years.year2022
import aoc.Day
import java.math.BigInteger
private val mod = BigInteger.valueOf(9699690) // = 13*3*7*2*19*5*11*17
@Year2022
class Day11 : Day() {
override fun solvePart1(input: List<String>): Any {
val keepAway = initializeKeepAwayGame(input)
keepAway.playRounds(20)
return keepAway.getLevelOfMonkeyBusiness()
}
override fun solvePart2(input: List<String>): Any {
val keepAway = initializeKeepAwayGame(input)
keepAway.worryAboutSomeMonkeysStealingYourShit = false
keepAway.playRounds(10000)
return keepAway.getLevelOfMonkeyBusiness()
}
}
private class KeepAway {
val monkeys = mutableListOf<Monkey>()
var worryAboutSomeMonkeysStealingYourShit = true
fun playRounds(rounds: Int) {
repeat(rounds) {
monkeys.forEach { monkey ->
monkey.items
.onEach { monkey.inspectionCount++ }
.map { item -> monkey.operation.invoke(item) }
.map { item -> if (worryAboutSomeMonkeysStealingYourShit) item.div(BigInteger.valueOf(3)) else item }
.map { item -> item.remainder(mod) }
.forEach { item -> monkeys[monkey.test.invoke(item)].items.add(item) }
monkey.items.clear()
}
}
}
fun getLevelOfMonkeyBusiness(): BigInteger {
return monkeys
.asSequence()
.map { it.inspectionCount }
.sortedDescending()
.chunked(2)
.map { (largest, secondLargest) -> largest * secondLargest }
.first()
}
}
private data class Monkey(
val items: MutableList<BigInteger>,
val operation: (BigInteger) -> BigInteger,
val test: (BigInteger) -> Int,
var inspectionCount: BigInteger = BigInteger.ZERO
)
private fun initializeKeepAwayGame(input: List<String>): KeepAway {
val keepAway = KeepAway()
input
.chunked(7)
.map { (_, startingItems, operation, test, ifTrue, ifFalse) ->
Monkey(
startingItems.substringAfter(": ").split(", ").map { it.toBigInteger() }.toMutableList(),
operation.toOperation(),
test.toTest(ifTrue.split(" ").last().toInt(), ifFalse.split(" ").last().toInt())
)
}
.forEach(keepAway.monkeys::add)
return keepAway
}
private fun String.toOperation(): (BigInteger) -> BigInteger {
val operand = this.split(" ").last()
if (this.contains("*")) {
return { worryLevel -> worryLevel * if (operand != "old") operand.toBigInteger() else worryLevel }
} else {
return { worryLevel -> worryLevel + if (operand != "old") operand.toBigInteger() else worryLevel }
}
}
private fun String.toTest(monkeyNrIfTestIsTrue: Int, monkeyNrIfTestIsFalse: Int): (BigInteger) -> Int {
return { worryLevel ->
if (worryLevel % this.split(" ").last().toBigInteger() == BigInteger.ZERO) {
monkeyNrIfTestIsTrue
} else {
monkeyNrIfTestIsFalse
}
}
}
private operator fun <T> List<T>.component6() = this[5]