Skip to content

Commit 19a8aba

Browse files
committed
Day 11 2024
1 parent 01e08e7 commit 19a8aba

File tree

4 files changed

+83
-0
lines changed

4 files changed

+83
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package me.peckb.aoc._2024.calendar.day11
2+
3+
import javax.inject.Inject
4+
import me.peckb.aoc.generators.InputGenerator.InputGeneratorFactory
5+
6+
class Day11 @Inject constructor(
7+
private val generatorFactory: InputGeneratorFactory,
8+
) {
9+
fun partOne(filename: String) = generatorFactory.forFile(filename).readOne { line ->
10+
val stonesMap = createStonesMap(line)
11+
12+
repeat(25) { blink(stonesMap) }
13+
14+
stonesMap.entries.sumOf { it.value }
15+
}
16+
17+
fun partTwo(filename: String) = generatorFactory.forFile(filename).readOne { line ->
18+
val stonesMap = createStonesMap(line)
19+
20+
repeat(75) { blink(stonesMap) }
21+
22+
stonesMap.entries.sumOf { it.value }
23+
}
24+
25+
private fun createStonesMap(line: String) = mutableMapOf<Long, Long>().apply {
26+
line.split(" ").forEach { merge(it.toLong(), 1, Long::plus) }
27+
}
28+
29+
private fun blink(stones: MutableMap<Long, Long>) {
30+
val entries = stones.entries.toList()
31+
32+
stones.clear()
33+
34+
entries.forEach { (stone, count) ->
35+
if (stone == 0L) {
36+
stones.merge(1, count, Long::plus)
37+
} else if ("$stone".length % 2 == 0) {
38+
val stoneStr = "$stone"
39+
val half = stoneStr.length / 2
40+
41+
stones.merge(stoneStr.take(half).toLong(), count, Long::plus)
42+
stones.merge(stoneStr.takeLast(half).toLong(), count, Long::plus)
43+
} else {
44+
stones.merge(stone * 2024, count, Long::plus)
45+
}
46+
}
47+
}
48+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
## [Day 11: Plutonian Pebbles](https://adventofcode.com/2024/day/11)

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import me.peckb.aoc._2024.calendar.day07.Day07Test
1010
import me.peckb.aoc._2024.calendar.day08.Day08Test
1111
import me.peckb.aoc._2024.calendar.day09.Day09Test
1212
import me.peckb.aoc._2024.calendar.day10.Day10Test
13+
import me.peckb.aoc._2024.calendar.day11.Day11Test
1314
import javax.inject.Singleton
1415
import me.peckb.aoc.DayComponent
1516
import me.peckb.aoc.InputModule
@@ -28,4 +29,5 @@ internal interface TestDayComponent : DayComponent {
2829
fun inject(day08Test: Day08Test)
2930
fun inject(day09Test: Day09Test)
3031
fun inject(day10Test: Day10Test)
32+
fun inject(day11Test: Day11Test)
3133
}
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.day11
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 Day11Test {
11+
@Inject
12+
lateinit var day11: Day11
13+
14+
@BeforeEach
15+
fun setup() {
16+
DaggerTestDayComponent.create().inject(this)
17+
}
18+
19+
@Test
20+
fun testDay11PartOne() {
21+
assertEquals(185205, day11.partOne(DAY_11))
22+
}
23+
24+
@Test
25+
fun testDay11PartTwo() {
26+
assertEquals(221280540398419, day11.partTwo(DAY_11))
27+
}
28+
29+
companion object {
30+
private const val DAY_11: String = "advent-of-code-input/2024/day11.input"
31+
}
32+
}

0 commit comments

Comments
 (0)