Skip to content

Commit 2c27124

Browse files
committed
Solve day 22
1 parent 636c3b8 commit 2c27124

File tree

3 files changed

+125
-1
lines changed

3 files changed

+125
-1
lines changed

src/main/kotlin/day22/Day22.kt

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package day22
2+
3+
import helper.Debug
4+
5+
fun solveA(text: String, debug: Debug = Debug.Disabled): Long {
6+
return text.lines().sumOf {
7+
val seed = it.toLong()
8+
val last = sequenceA(seed).last()
9+
debug {
10+
println("$seed -> $last")
11+
}
12+
last
13+
}
14+
}
15+
16+
fun Long.mix(next: Long): Long = xor(next)
17+
fun Long.prune() = this % 16777216
18+
19+
fun sequenceA(start: Long): Sequence<Long> {
20+
return generateSequence(start) { current ->
21+
val first = (current * 64).mix(current).prune()
22+
val second = (first / 32).mix(first).prune()
23+
val third = (second * 2048).mix(second).prune()
24+
third
25+
}.take(2001)
26+
}
27+
28+
fun sequenceB(start: Long) = sequenceA(start)
29+
.map { "$it".last().digitToInt() }
30+
.zipWithNext { a, b -> b to (b - a) }
31+
.windowed(4) { list -> list.map { it.second } to list.last().first }
32+
33+
fun solveB(text: String, debug: Debug = Debug.Disabled): Int {
34+
val (bestSequence, bestValue) = text.lines().asSequence()
35+
.flatMap { sequenceB(it.toLong()).distinctBy { (d, _) -> d } }
36+
.groupingBy { (d, _) -> d }.fold(
37+
initialValueSelector = { _, _ -> 0 },
38+
operation = { _, acc, (_, count) -> acc + count }
39+
).maxBy { it.value }
40+
41+
debug {
42+
println(bestSequence)
43+
}
44+
return bestValue
45+
}

src/main/resources/inputs

src/test/kotlin/day22/Day22KtTest.kt

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package day22
2+
3+
import helper.Debug
4+
import helper.readDayFile
5+
import kotlin.test.Test
6+
import kotlin.test.assertEquals
7+
8+
9+
internal class Day22KtTest {
10+
11+
private val day = 22
12+
13+
@Test
14+
fun sample1() {
15+
val text = """
16+
1
17+
10
18+
100
19+
2024
20+
""".trimIndent().trimEnd()
21+
22+
assertEquals(37327623, solveA(text, Debug.Enabled))
23+
}
24+
25+
@Test
26+
fun sample3() {
27+
val text = """
28+
1
29+
2
30+
3
31+
2024
32+
""".trimIndent().trimEnd()
33+
34+
assertEquals(23, solveB(text, Debug.Enabled))
35+
}
36+
37+
@Test
38+
fun sample2() {
39+
40+
val expectedSequence = """
41+
15887950
42+
16495136
43+
527345
44+
704524
45+
1553684
46+
12683156
47+
11100544
48+
12249484
49+
7753432
50+
5908254""".trimIndent()
51+
val expectedA = expectedSequence.lines().map { it.toLong() }
52+
assertEquals(expectedA, sequenceA(123L).drop(1).take(10).toList())
53+
54+
val expectedB = listOf(
55+
listOf(-3, 6, -1, -1) to 4,
56+
listOf(6, -1, -1, 0) to 4,
57+
listOf(-1, -1, 0, 2) to 6,
58+
listOf(-1, 0, 2, -2) to 4,
59+
listOf(0, 2, -2, 0) to 4,
60+
listOf(2, -2, 0, -2) to 2,
61+
)
62+
63+
assertEquals(expectedB, sequenceB(123L).take(6).toList())
64+
}
65+
66+
@Test
67+
fun solve() {
68+
val lines = readDayFile(day, "input").readText().trimEnd()
69+
70+
val solveA = solveA(lines)
71+
println("A: $solveA")
72+
assertEquals(17262627539, solveA)
73+
74+
val solveB = solveB(lines)
75+
println("B: $solveB")
76+
//not 2121
77+
assertEquals(1986, solveB)
78+
}
79+
}

0 commit comments

Comments
 (0)