Skip to content

Commit d35e6a8

Browse files
committed
Add solution for Y2024D22.
1 parent 5bed9bf commit d35e6a8

File tree

6 files changed

+65
-1
lines changed

6 files changed

+65
-1
lines changed

Diff for: README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ _"Anything that's worth doing, is worth overdoing."_
1515
Table of contents to jump straight to the problem you're looking for.
1616

1717
<details open>
18-
<summary>2024 (42x⭐)</summary>
18+
<summary>2024 (44x⭐)</summary>
1919

2020
| Day | Title | Stars |
2121
|:---:|:------------------------------------------------------------|:-----:|
@@ -40,6 +40,7 @@ Table of contents to jump straight to the problem you're looking for.
4040
| 19 | [Linen Layout](solutions/aockt/y2024/Y2024D19.kt) | ⭐⭐ |
4141
| 20 | [Race Condition](solutions/aockt/y2024/Y2024D20.kt) | ⭐⭐ |
4242
| 21 | [Keypad Conundrum](solutions/aockt/y2024/Y2024D21.kt) | ⭐⭐ |
43+
| 22 | [Monkey Market](solutions/aockt/y2024/Y2024D22.kt) | ⭐⭐ |
4344

4445
</details>
4546

Diff for: inputs/aockt/y2024/d22/input.txt

19.4 KB
Binary file not shown.

Diff for: inputs/aockt/y2024/d22/solution_part1.txt

34 Bytes
Binary file not shown.

Diff for: inputs/aockt/y2024/d22/solution_part2.txt

27 Bytes
Binary file not shown.

Diff for: solutions/aockt/y2024/Y2024D22.kt

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package aockt.y2024
2+
3+
import aockt.util.parse
4+
import io.github.jadarma.aockt.core.Solution
5+
6+
object Y2024D22 : Solution {
7+
8+
/** Generate a pseudo-random sequence of numbers from the starting [seed]. */
9+
private fun pseudoRandom(seed: Long): Sequence<Long> = generateSequence(seed) {
10+
var result = it
11+
result = result xor (result shl 6 ) and 0xFFFFFF
12+
result = result xor (result shr 5 ) and 0xFFFFFF
13+
result = result xor (result shl 11) and 0xFFFFFF
14+
result
15+
}
16+
17+
/** Return the buyer prices from a given trader [seed], with a limit of how many secrets to generate [perDay]. */
18+
private fun buyerPrices(seed: Long, perDay: Int = 2000): Sequence<Int> =
19+
pseudoRandom(seed)
20+
.take(perDay + 1)
21+
.map { (it % 10).toInt() }
22+
23+
/** Parse the [input] and return the initial seed for each monkey buyer. */
24+
private fun parseInput(input: String): List<Long> = parse { input.lines().map(String::toLong) }
25+
26+
override fun partOne(input: String): Long = parseInput(input).sumOf { pseudoRandom(it).drop(2000).first() }
27+
28+
override fun partTwo(input: String): Int =
29+
buildMap {
30+
for (seed in parseInput(input)) {
31+
val seen = mutableSetOf<List<Int>>()
32+
for ((a, b, c, d, e) in buyerPrices(seed).windowed(size = 5)) {
33+
val sequence = listOf(b - a, c - b, d - c, e - d)
34+
.takeIf { it !in seen }
35+
?.also(seen::add)
36+
?: continue
37+
38+
compute(sequence) { _, v -> (v ?: 0) + e }
39+
}
40+
}
41+
}.maxOf { it.value }
42+
}

Diff for: tests/aockt/y2024/Y2024D22Test.kt

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package aockt.y2024
2+
3+
import io.github.jadarma.aockt.test.AdventDay
4+
import io.github.jadarma.aockt.test.AdventSpec
5+
6+
@AdventDay(2024, 22, "Monkey Market")
7+
class Y2024D22Test : AdventSpec<Y2024D22>({
8+
9+
val example1 = listOf(1, 10, 100, 2024).joinToString(separator = "\n")
10+
val example2 = listOf(1, 2, 3, 2024).joinToString(separator = "\n")
11+
12+
partOne {
13+
example1 shouldOutput 37_327_623
14+
example2 shouldOutput 37_990_510
15+
}
16+
17+
partTwo {
18+
example1 shouldOutput 24
19+
example2 shouldOutput 23
20+
}
21+
})

0 commit comments

Comments
 (0)