Skip to content

Commit 18391bc

Browse files
committed
Day 22 2024
1 parent af9e41c commit 18391bc

File tree

4 files changed

+105
-0
lines changed

4 files changed

+105
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package me.peckb.aoc._2024.calendar.day22
2+
3+
import arrow.core.Tuple4
4+
import javax.inject.Inject
5+
import me.peckb.aoc.generators.InputGenerator.InputGeneratorFactory
6+
7+
class Day22 @Inject constructor(
8+
private val generatorFactory: InputGeneratorFactory,
9+
) {
10+
fun partOne(filename: String) = generatorFactory.forFile(filename).readAs(::secretNumber) { input ->
11+
input.sumOf { number ->
12+
(0 until 2000).fold(number) { n, _ -> nextNumber(n) }
13+
}
14+
}
15+
16+
fun partTwo(filename: String) = generatorFactory.forFile(filename).readAs(::secretNumber) { input ->
17+
val data = input.toList()
18+
19+
val solutions = mutableMapOf<Tuple4<Long, Long, Long, Long>, Long>()
20+
21+
data.forEach { number->
22+
val buyerNumbers = sequence {
23+
var n = number
24+
while (true) {
25+
yield(n)
26+
n = nextNumber(n)
27+
}
28+
}
29+
30+
val mySequences = mutableSetOf<Tuple4<Long, Long, Long, Long>>()
31+
32+
buyerNumbers
33+
.map { it % 10 }
34+
.windowed(5)
35+
.take(2000)
36+
.forEach { (a, b, c, d, e) ->
37+
val k = Tuple4(b - a, c - b, d - c, e - d)
38+
if (k !in mySequences) {
39+
solutions.merge(k, e, Long::plus)
40+
mySequences.add(k)
41+
}
42+
}
43+
}
44+
45+
solutions.values.max()
46+
}
47+
48+
private fun nextNumber(number: Long): Long {
49+
var n = number
50+
51+
// step one
52+
val a = n * 64 // Calculate the result of multiplying the secret number by 64.
53+
n = n xor a // Then, mix this result into the secret number.
54+
n %= 16_777_216 // Finally, prune the secret number.
55+
56+
// step two
57+
val b = n / 32 // Calculate the result of dividing the secret number by 32.
58+
n = n xor b // Then, mix this result into the secret number.
59+
n %= 16_777_216 // Finally, prune the secret number.
60+
61+
// step three
62+
val c = n * 2048 // Calculate the result of multiplying the secret number by 2048.
63+
n = n xor c // Then, mix this result into the secret number.
64+
n %= 16_777_216 // Finally, prune the secret number.
65+
66+
return n
67+
}
68+
69+
private fun secretNumber(line: String) = line.toLong()
70+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
## [Day 22: Monkey Market](https://adventofcode.com/2024/day/22)

src/test/kotlin/me/peckb/aoc/_2024/TestDayComponent.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import me.peckb.aoc._2024.calendar.day18.Day18Test
2121
import me.peckb.aoc._2024.calendar.day19.Day19Test
2222
import me.peckb.aoc._2024.calendar.day20.Day20Test
2323
import me.peckb.aoc._2024.calendar.day21.Day21Test
24+
import me.peckb.aoc._2024.calendar.day22.Day22Test
2425
import javax.inject.Singleton
2526
import me.peckb.aoc.DayComponent
2627
import me.peckb.aoc.InputModule
@@ -50,4 +51,5 @@ internal interface TestDayComponent : DayComponent {
5051
fun inject(day19Test: Day19Test)
5152
fun inject(day20Test: Day20Test)
5253
fun inject(day21Test: Day21Test)
54+
fun inject(day22Test: Day22Test)
5355
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package me.peckb.aoc._2024.calendar.day22
2+
3+
import javax.inject.Inject
4+
5+
import me.peckb.aoc._2024.DaggerTestDayComponent
6+
import org.junit.jupiter.api.Assertions.assertEquals
7+
import org.junit.jupiter.api.BeforeEach
8+
import org.junit.jupiter.api.Test
9+
10+
internal class Day22Test {
11+
@Inject
12+
lateinit var day22: Day22
13+
14+
@BeforeEach
15+
fun setup() {
16+
DaggerTestDayComponent.create().inject(this)
17+
}
18+
19+
@Test
20+
fun testDay22PartOne() {
21+
assertEquals(16039090236, day22.partOne(DAY_22))
22+
}
23+
24+
@Test
25+
fun testDay22PartTwo() {
26+
assertEquals(1808, day22.partTwo(DAY_22))
27+
}
28+
29+
companion object {
30+
private const val DAY_22: String = "advent-of-code-input/2024/day22.input"
31+
}
32+
}

0 commit comments

Comments
 (0)