Skip to content

Commit 2d9174c

Browse files
committed
Improve solution 2025-03 (Lobby)
Use the generalized joltage function for both parts.
1 parent 754d55d commit 2d9174c

File tree

2 files changed

+8
-15
lines changed
  • src
    • main/kotlin/de/ronny_h/aoc/year2025/day03
    • test/kotlin/de/ronny_h/aoc/year2025/day03

2 files changed

+8
-15
lines changed

src/main/kotlin/de/ronny_h/aoc/year2025/day03/Lobby.kt

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,17 @@ fun main() = Lobby().run(17324, 171846613143331)
66

77
class Lobby : AdventOfCode<Long>(2025, 3) {
88
override fun part1(input: List<String>): Long = input
9-
.parse().sumOf(List<Int>::joltage)
9+
.parse().sumOf { it.joltage(2) }
1010

1111
override fun part2(input: List<String>): Long = input
12-
.parse().sumOf(List<Int>::joltageWithSafetyOverride)
12+
.parse().sumOf { it.joltage(12) }
1313
}
1414

15-
fun List<Int>.joltage(): Long {
16-
val first = subList(0, lastIndex).max()
17-
val firstIndex = indexOf(first)
18-
val second = subList(firstIndex + 1, size).max()
19-
return "$first$second".toLong()
20-
}
21-
22-
fun List<Int>.joltageWithSafetyOverride(): Long {
23-
var index = -1
15+
fun List<Int>.joltage(numberOfBatteries: Int): Long {
16+
var index = 0
2417
val batteriesSwitchedOn = mutableListOf<Int>()
25-
for (remaining in 11 downTo 0) {
26-
val remainingBatteries = subList(index + 1, size - remaining)
18+
for (remaining in numberOfBatteries - 1 downTo 0) {
19+
val remainingBatteries = subList(index, size - remaining)
2720
val chosenBattery = remainingBatteries.max()
2821
batteriesSwitchedOn.add(chosenBattery)
2922
index += 1 + remainingBatteries.indexOf(chosenBattery)

src/test/kotlin/de/ronny_h/aoc/year2025/day03/LobbyTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class LobbyTest : StringSpec({
2222
row(listOf(2, 3, 4, 2, 3, 4, 2, 3, 4, 2, 3, 4, 2, 7, 8), 78),
2323
row(listOf(8, 1, 8, 1, 8, 1, 9, 1, 1, 1, 1, 2, 1, 1, 1), 92),
2424
) { bank, expected ->
25-
bank.joltage() shouldBe expected
25+
bank.joltage(2) shouldBe expected
2626
}
2727
}
2828

@@ -33,7 +33,7 @@ class LobbyTest : StringSpec({
3333
row(listOf(2, 3, 4, 2, 3, 4, 2, 3, 4, 2, 3, 4, 2, 7, 8), 434234234278),
3434
row(listOf(8, 1, 8, 1, 8, 1, 9, 1, 1, 1, 1, 2, 1, 1, 1), 888911112111),
3535
) { bank, expected ->
36-
bank.joltageWithSafetyOverride() shouldBe expected
36+
bank.joltage(12) shouldBe expected
3737
}
3838
}
3939

0 commit comments

Comments
 (0)