Skip to content

Commit e27123c

Browse files
committed
Add 2022 day 11 part 2
1 parent ecc7eae commit e27123c

File tree

2 files changed

+24
-39
lines changed

2 files changed

+24
-39
lines changed

2022/src/main/kotlin/day11/Day11.kt

Lines changed: 22 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -11,36 +11,26 @@ class Day11 {
1111

1212
fun part1(input: String): Long {
1313
val monkeys = parseInput(input).associateBy(Monkey::id)
14-
15-
for (round in 1..20) {
16-
monkeys.forEach { (_, monkey) ->
17-
monkey.items.forEach { item ->
18-
item.worryLevel = monkey.inspectItem(item) / 3
19-
val newMonkeyId = monkey.chooseTargetMonkey(item.worryLevel)
20-
21-
monkeys[newMonkeyId]?.items?.add(item)
22-
}
23-
monkey.items.clear()
24-
}
25-
}
26-
27-
val (m1, m2) = monkeys.values
28-
.sortedByDescending(Monkey::totalItemsInspected)
29-
.take(2)
30-
31-
return m1.totalItemsInspected * m2.totalItemsInspected
14+
return startGame(monkeys, 20, 3)
3215
}
3316

3417
fun part2(input: String): Long {
3518
val monkeys = parseInput(input).associateBy(Monkey::id)
19+
return startGame(monkeys, 10_000, 1)
20+
}
3621

37-
for (round in 1..10_000) {
22+
private fun startGame(monkeys: Map<Int, Monkey>, rounds: Int, reliefModifier: Int): Long {
23+
val commonDenominator = monkeys.values
24+
.map(Monkey::divisibleValue)
25+
.reduce { acc, v -> acc * v }
26+
27+
for (round in 1..rounds) {
3828
monkeys.forEach { (_, monkey) ->
3929
monkey.items.forEach { item ->
40-
item.worryLevel = monkey.inspectItem(item)
41-
val newMonkeyId = monkey.chooseTargetMonkey(item.worryLevel)
30+
val newWorryLevel = (monkey.inspectItem(item) / reliefModifier) % commonDenominator
31+
val newMonkeyId = monkey.chooseTargetMonkey(newWorryLevel)
4232

43-
monkeys[newMonkeyId]?.items?.add(item)
33+
monkeys[newMonkeyId]?.items?.add(newWorryLevel)
4434
}
4535
monkey.items.clear()
4636
}
@@ -50,27 +40,22 @@ class Day11 {
5040
.sortedByDescending(Monkey::totalItemsInspected)
5141
.take(2)
5242

53-
// After 10000 rounds, the two most active monkeys inspected
54-
// items 52166 and 52013 times.
55-
// Multiplying these together, the level of monkey business in this situation
56-
// is now 2713310158.
5743
return m1.totalItemsInspected * m2.totalItemsInspected
5844
}
5945

60-
data class Item(val id: Int, var worryLevel: Long)
61-
6246
data class Monkey(
6347
val id: Int,
6448
val inspectOperation: MonkeyOperation,
6549
val chooseTargetMonkey: MonkeyTest,
66-
val items: LinkedList<Item>
50+
val items: MutableList<Long>,
51+
val divisibleValue: Int
6752
) {
6853
var totalItemsInspected = 0L
6954
private set
7055

71-
fun inspectItem(item: Item): Long {
56+
fun inspectItem(itemWorryLevel: Long): Long {
7257
totalItemsInspected++
73-
return inspectOperation(item.worryLevel)
58+
return inspectOperation(itemWorryLevel)
7459
}
7560
}
7661

@@ -82,27 +67,27 @@ class Day11 {
8267
* If true: throw to monkey 2
8368
* If false: throw to monkey 3
8469
*/
85-
fun parseInput(input: String): LinkedList<Monkey> {
70+
private fun parseInput(input: String): LinkedList<Monkey> {
8671
val monkeys = LinkedList<Monkey>()
87-
val itemIndexGenerator = generateSequence(0) { it + 1 }.iterator()
8872

8973
input.split("\n\n").mapIndexed { i, value ->
9074
val lines = value.split("\n").map(String::trimIndent)
9175

92-
val items = parseItems(lines[1], itemIndexGenerator)
76+
val items = parseItems(lines[1])
9377
val operation = parseOperation(lines[2])
9478
val test = parseTest(lines[3], lines[4], lines[5])
79+
val divisibleValue = lines[3].split(" ").last().toInt()
9580

96-
Monkey(i, operation, test, LinkedList<Item>(items))
81+
Monkey(i, operation, test, items.toMutableList(), divisibleValue)
9782
}.forEach(monkeys::add)
9883

9984
return monkeys
10085
}
10186

10287
// Parse: Starting items: 79, 98
103-
private fun parseItems(input: String, itemIndexGenerator: Iterator<Int>): List<Item> {
88+
private fun parseItems(input: String): List<Long> {
10489
return MATCH_NUMBERS.findAll(input).map {
105-
Item(itemIndexGenerator.next(), it.value.toLong())
90+
it.value.toLong()
10691
}.toList()
10792
}
10893

2022/src/test/kotlin/Day11Test.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ internal class Day11Test {
1919
}
2020
@Test
2121
fun `part2 example`() {
22-
assertEquals(2713310158L, Day11.part2(testInput))
22+
assertEquals(2713310158, Day11.part2(testInput))
2323
}
2424
@Test
2525
fun `part2 puzzel input`() {
26-
assertEquals(0, Day11.part2(input))
26+
assertEquals(16792940265, Day11.part2(input))
2727
}
2828
}

0 commit comments

Comments
 (0)