Skip to content

Commit c5edbc8

Browse files
committed
Solution Day 22, part two (Monkey Market)
1 parent 6dbe4c2 commit c5edbc8

File tree

2 files changed

+81
-6
lines changed

2 files changed

+81
-6
lines changed

src/main/kotlin/de/ronny_h/aoc/year24/day22/MonkeyMarket.kt

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package de.ronny_h.aoc.year24.day22
22

33
import de.ronny_h.aoc.AdventOfCode
4+
import de.ronny_h.aoc.extensions.onesDigit
45

5-
fun main() = MonkeyMarket().run(17965282217, 0)
6+
fun main() = MonkeyMarket().run(17965282217, 2152)
67

78
class MonkeyMarket : AdventOfCode<Long>(2024, 22) {
89

@@ -26,7 +27,51 @@ class MonkeyMarket : AdventOfCode<Long>(2024, 22) {
2627
secret
2728
}
2829

30+
data class Buyer(val prices: List<Int>, val priceChanges: List<Int>)
31+
2932
override fun part2(input: List<String>): Long {
30-
TODO("Not yet implemented")
33+
val buyers = input
34+
.map(String::toLong)
35+
.map {
36+
val prices = listPrices(it, 2000)
37+
Buyer(prices, prices.changes())
38+
}
39+
println("${buyers.size} buyers")
40+
41+
val bananas = sumUpBananasForSequences(buyers)
42+
43+
val (sequence, maxBananas) = bananas.maxBy { it.value }
44+
println("sequence with max value: $sequence")
45+
val numberOfMax = bananas.filter { it.value == maxBananas }
46+
println("sequences with the same value: $numberOfMax from ${bananas.size} sequences total")
47+
return maxBananas
48+
}
49+
50+
fun sumUpBananasForSequences(buyers: List<Buyer>): Map<List<Int>, Long> {
51+
val bananas = mutableMapOf<List<Int>, Long>()
52+
buyers.forEach {
53+
var index = 4
54+
val seenForThatBuyer = mutableSetOf<List<Int>>()
55+
it.priceChanges.windowed(4) { ephemeralSequence ->
56+
val sequence = ephemeralSequence.toList()
57+
// the hiding spot is sold on the first occurrence of a sequence -> don't add up values for later ones
58+
if (seenForThatBuyer.add(sequence)) {
59+
bananas[sequence] = bananas.getOrElse(sequence) { 0 } + it.prices[index]
60+
}
61+
index++
62+
}
63+
}
64+
return bananas
65+
}
66+
67+
fun listPrices(seed: Long, n: Int): List<Int> {
68+
var secret = seed
69+
return listOf(seed.onesDigit()) + List(n) {
70+
secret = nextSecretNumber(secret)
71+
secret.onesDigit()
72+
}
3173
}
3274
}
75+
76+
fun List<Int>.changes(): List<Int> = windowed(2)
77+
.map { (a, b) -> b - a }

src/test/kotlin/de/ronny_h/aoc/year24/day22/MonkeyMarketTest.kt

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
11
package de.ronny_h.aoc.year24.day22
22

33
import de.ronny_h.aoc.extensions.asList
4+
import de.ronny_h.aoc.year24.day22.MonkeyMarket.Buyer
45
import io.kotest.core.spec.style.StringSpec
56
import io.kotest.matchers.shouldBe
67

78
class MonkeyMarketTest : StringSpec({
8-
9-
val input = """
9+
val input1 = """
1010
1
1111
10
1212
100
1313
2024
1414
""".asList()
15+
val input2 = """
16+
1
17+
2
18+
3
19+
2024
20+
""".asList()
21+
1522
val market = MonkeyMarket()
1623

1724
"next secret number of 123" {
@@ -39,7 +46,30 @@ class MonkeyMarketTest : StringSpec({
3946
)
4047
}
4148

42-
"part 1" {
43-
market.part1(input) shouldBe 37327623
49+
"part 1: The sum of the 2000th secret number generated by each buyer" {
50+
market.part1(input1) shouldBe 37327623
51+
}
52+
53+
"list the first 10 prices with a see of 123" {
54+
market.listPrices(123, 9) shouldBe listOf(3, 0, 6, 5, 4, 4, 6, 4, 4, 2)
55+
}
56+
57+
"the changes of consecutive prices" {
58+
listOf(3, 0, 6, 5, 4, 4, 6, 4, 4, 2).changes() shouldBe listOf(-3, 6, -1, -1, 0, 2, -2, 0, -2)
59+
}
60+
61+
"sumUpBananasForSequences() for the example buyer yields the right highest price" {
62+
val buyer = Buyer(
63+
prices = listOf(3, 0, 6, 5, 4, 4, 6, 4, 4, 2),
64+
priceChanges = listOf( -3, 6, -1, -1, 0, 2, -2, 0, -2))
65+
val entryWithHighestPrice = market
66+
.sumUpBananasForSequences(listOf(buyer))
67+
.maxBy { it.value }
68+
entryWithHighestPrice.key shouldBe listOf(-1, -1, 0, 2)
69+
entryWithHighestPrice.value shouldBe 6
70+
}
71+
72+
"part 2: The most bananas one can get" {
73+
market.part2(input2) shouldBe 23
4474
}
4575
})

0 commit comments

Comments
 (0)