Skip to content

Commit 843ae05

Browse files
committed
Some improvements and moved to shared code
1 parent e27123c commit 843ae05

File tree

3 files changed

+31
-25
lines changed

3 files changed

+31
-25
lines changed

2022/src/main/kotlin/day05/Day05.kt

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
package day05
22

3+
import shared.extractInts
34
import java.util.*
45

56
class Day05 {
67

78
companion object {
89

9-
private val MATCH_NUMBERS = Regex("[0-9]+")
10-
1110
enum class CraneType {
1211
CRATE_MOVER_9000,
1312
CRATE_MOVER_9001
@@ -71,10 +70,7 @@ class Day05 {
7170

7271
private fun parseInstructions(input: String): List<Instruction> {
7372
return input.split("\n").map {
74-
val (move, from, to) = MATCH_NUMBERS.findAll(it)
75-
.map(MatchResult::value)
76-
.map(String::toInt)
77-
.toList()
73+
val (move, from, to) = it.extractInts()
7874

7975
Instruction(move, from - 1, to - 1)
8076
}

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

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
11
package day11
22

3+
import shared.extractLongs
34
import java.lang.RuntimeException
45
import java.util.LinkedList
56

67
class Day11 {
78

89
companion object {
910

10-
private val MATCH_NUMBERS = Regex("[0-9]+")
11-
1211
fun part1(input: String): Long {
1312
val monkeys = parseInput(input).associateBy(Monkey::id)
14-
return startGame(monkeys, 20, 3)
13+
return startGame(monkeys, 20) { it / 3 }
1514
}
1615

1716
fun part2(input: String): Long {
1817
val monkeys = parseInput(input).associateBy(Monkey::id)
19-
return startGame(monkeys, 10_000, 1)
20-
}
2118

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 }
19+
val commonDenominator = monkeys.values
20+
.map(Monkey::divisibleValue)
21+
.reduce { acc, v -> acc * v }
22+
23+
return startGame(monkeys, 10_000) { it % commonDenominator }
24+
}
2625

26+
private fun startGame(monkeys: Map<Int, Monkey>, rounds: Int, reliefOperation: ReliefOperation): Long {
2727
for (round in 1..rounds) {
2828
monkeys.forEach { (_, monkey) ->
2929
monkey.items.forEach { item ->
30-
val newWorryLevel = (monkey.inspectItem(item) / reliefModifier) % commonDenominator
30+
val newWorryLevel = reliefOperation(monkey.inspectItem(item))
3131
val newMonkeyId = monkey.chooseTargetMonkey(newWorryLevel)
3232

3333
monkeys[newMonkeyId]?.items?.add(newWorryLevel)
@@ -73,7 +73,8 @@ class Day11 {
7373
input.split("\n\n").mapIndexed { i, value ->
7474
val lines = value.split("\n").map(String::trimIndent)
7575

76-
val items = parseItems(lines[1])
76+
// Parse: Starting items: 79, 98
77+
val items = lines[1].extractLongs()
7778
val operation = parseOperation(lines[2])
7879
val test = parseTest(lines[3], lines[4], lines[5])
7980
val divisibleValue = lines[3].split(" ").last().toInt()
@@ -84,13 +85,6 @@ class Day11 {
8485
return monkeys
8586
}
8687

87-
// Parse: Starting items: 79, 98
88-
private fun parseItems(input: String): List<Long> {
89-
return MATCH_NUMBERS.findAll(input).map {
90-
it.value.toLong()
91-
}.toList()
92-
}
93-
9488
// Parse: Operation: new = old * 19
9589
private fun parseOperation(input: String): MonkeyOperation {
9690
val parts = input.split(" ")
@@ -138,4 +132,5 @@ class Day11 {
138132
}
139133

140134
typealias MonkeyOperation = (value: Long) -> Long
141-
typealias MonkeyTest = (value: Long) -> Int
135+
typealias MonkeyTest = (value: Long) -> Int
136+
typealias ReliefOperation = (value: Long) -> Long
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package shared
2+
3+
private val MATCH_NUMBERS = Regex("[0-9]+")
4+
5+
fun String.extractInts(): List<Int> {
6+
return MATCH_NUMBERS.findAll(this).map {
7+
it.value.toInt()
8+
}.toList()
9+
}
10+
11+
fun String.extractLongs(): List<Long> {
12+
return MATCH_NUMBERS.findAll(this).map {
13+
it.value.toLong()
14+
}.toList()
15+
}

0 commit comments

Comments
 (0)