Skip to content

Commit 27a91d6

Browse files
authored
Day 03 2025 (#297)
* Day 03 2025 * Optimize by not recursing n=0 * remove extraneous wrapper class * remove extraneous drop call * use shifting start index instead of sublists * formatting * Optimization, you only need the first index of the max value
1 parent 6656a3e commit 27a91d6

File tree

4 files changed

+68
-0
lines changed

4 files changed

+68
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package me.peckb.aoc._2025.calendar.day03
2+
3+
import javax.inject.Inject
4+
import me.peckb.aoc.generators.InputGenerator.InputGeneratorFactory
5+
6+
class Day03 @Inject constructor(
7+
private val generatorFactory: InputGeneratorFactory,
8+
) {
9+
fun partOne(filename: String) = generatorFactory.forFile(filename).readAs(::batteryArray) { batteryArrays ->
10+
batteryArrays.sumOf { batteryArray -> maxJoltage(batteryArray, numToEnable = 2) }
11+
}
12+
13+
fun partTwo(filename: String) = generatorFactory.forFile(filename).readAs(::batteryArray) { batteryArrays ->
14+
batteryArrays.sumOf { batteryArray -> maxJoltage(batteryArray, numToEnable = 12) }
15+
}
16+
17+
private fun maxJoltage(batteryArray: List<Long>, startIndex: Int = 0, numToEnable: Int) : Long {
18+
val indicesOfFlippableBatteries = startIndex .. ((batteryArray.size - 1) - (numToEnable - 1))
19+
val largestStartValue = indicesOfFlippableBatteries.maxOf { i -> batteryArray[i] }
20+
val firstIndexOfValue = indicesOfFlippableBatteries.first { i -> batteryArray[i] == largestStartValue }
21+
22+
// we can bottom out if our next recurse would enable zero batteries
23+
if (numToEnable == 1) { return largestStartValue }
24+
25+
// if we have more batteries to flip - recurse down!
26+
val maxSubJoltage = maxJoltage(batteryArray, startIndex = firstIndexOfValue + 1, numToEnable = numToEnable - 1)
27+
28+
// then add it to our value and return!
29+
return "$largestStartValue$maxSubJoltage".toLong()
30+
}
31+
32+
private fun batteryArray(line: String) = line.map { it.digitToInt().toLong() }
33+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
## [Day 3: Lobby](https://adventofcode.com/2025/day/3)

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package me.peckb.aoc._2025
22

33
import dagger.Component
44
import me.peckb.aoc._2025.calendar.day02.Day02Test
5+
import me.peckb.aoc._2025.calendar.day03.Day03Test
56
import javax.inject.Singleton
67
import me.peckb.aoc.DayComponent
78
import me.peckb.aoc.InputModule
@@ -12,4 +13,5 @@ import me.peckb.aoc._2025.calendar.day01.Day01Test
1213
internal interface TestDayComponent : DayComponent {
1314
fun inject(day01Test: Day01Test)
1415
fun inject(day02Test: Day02Test)
16+
fun inject(day03Test: Day03Test)
1517
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package me.peckb.aoc._2025.calendar.day03
2+
3+
import javax.inject.Inject
4+
5+
import me.peckb.aoc._2025.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 Day03Test {
11+
@Inject
12+
lateinit var day03: Day03
13+
14+
@BeforeEach
15+
fun setup() {
16+
DaggerTestDayComponent.create().inject(this)
17+
}
18+
19+
@Test
20+
fun testDay03PartOne() {
21+
assertEquals(17443, day03.partOne(DAY_03))
22+
}
23+
24+
@Test
25+
fun testDay03PartTwo() {
26+
assertEquals(172167155440541, day03.partTwo(DAY_03))
27+
}
28+
29+
companion object {
30+
private const val DAY_03: String = "advent-of-code-input/2025/day03.input"
31+
}
32+
}

0 commit comments

Comments
 (0)