Skip to content

Commit f894da8

Browse files
authored
Day 07 2025 (#301)
* Day 07 2025
1 parent 99a8e55 commit f894da8

File tree

4 files changed

+116
-0
lines changed

4 files changed

+116
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package me.peckb.aoc._2025.calendar.day07
2+
3+
import javax.inject.Inject
4+
import me.peckb.aoc.generators.InputGenerator.InputGeneratorFactory
5+
import kotlin.text.indexOf
6+
import kotlin.text.toCharArray
7+
8+
class Day07 @Inject constructor(
9+
private val generatorFactory: InputGeneratorFactory,
10+
) {
11+
fun partOne(filename: String) = generatorFactory.forFile(filename).read { input ->
12+
val beamIndices = mutableSetOf<Int>()
13+
val manifold = mutableListOf<MutableList<Char>>()
14+
15+
input.forEachIndexed { index, line ->
16+
if (index == 0) { beamIndices.add(line.indexOf('S')) }
17+
manifold.add(line.toCharArray().toMutableList())
18+
}
19+
20+
var depth = 1
21+
var splits = 0
22+
while (depth < manifold.size) {
23+
val line = manifold[depth]
24+
25+
beamIndices.toList().also { beamIndices.clear() }.forEach { beamIndex ->
26+
if (line[beamIndex] == '^') {
27+
splits++
28+
beamIndices.add(beamIndex - 1)
29+
beamIndices.add(beamIndex + 1)
30+
} else {
31+
beamIndices.add(beamIndex)
32+
}
33+
}
34+
35+
depth++
36+
}
37+
38+
splits
39+
}
40+
41+
fun partTwo(filename: String) = generatorFactory.forFile(filename).read { input ->
42+
val beams = mutableMapOf<Location, Long>().withDefault { 0 }
43+
val manifold = mutableListOf<MutableList<Char>>()
44+
45+
input.forEachIndexed { index, line ->
46+
if (index == 0) { beams[Location(0, line.indexOf('S'))] = 1 }
47+
manifold.add(line.toCharArray().toMutableList())
48+
}
49+
50+
var depth = 1
51+
while (depth < manifold.size) {
52+
val line = manifold[depth]
53+
54+
beams.toList().also { beams.clear() }.forEach { (location, count) ->
55+
if (location.depth != depth - 1) {
56+
throw IllegalStateException("We should only be tracking the last row.")
57+
}
58+
59+
// if we need to split then the indices of our location need to shift and split
60+
// otherwise we just stay at the same index we're at
61+
val indexDeltas = if (line[location.index] == '^') listOf(-1, 1) else listOf(0)
62+
63+
indexDeltas.forEach { delta ->
64+
Location(depth, location.index + delta).let { loc ->
65+
beams[loc] = beams.getValue(loc) + count
66+
}
67+
}
68+
}
69+
70+
depth++
71+
}
72+
73+
beams.values.sum()
74+
}
75+
}
76+
77+
data class Location(val depth: Int, val index: Int) {
78+
override fun toString(): String {
79+
return "($depth,$index)"
80+
}
81+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
## [Day 7: Laboratories](https://adventofcode.com/2025/day/7)

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import me.peckb.aoc._2025.calendar.day03.Day03Test
66
import me.peckb.aoc._2025.calendar.day04.Day04Test
77
import me.peckb.aoc._2025.calendar.day05.Day05Test
88
import me.peckb.aoc._2025.calendar.day06.Day06Test
9+
import me.peckb.aoc._2025.calendar.day07.Day07Test
910
import javax.inject.Singleton
1011
import me.peckb.aoc.DayComponent
1112
import me.peckb.aoc.InputModule
@@ -20,4 +21,5 @@ internal interface TestDayComponent : DayComponent {
2021
fun inject(day04Test: Day04Test)
2122
fun inject(day05Test: Day05Test)
2223
fun inject(day06Test: Day06Test)
24+
fun inject(day07Test: Day07Test)
2325
}
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.day07
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 Day07Test {
11+
@Inject
12+
lateinit var day07: Day07
13+
14+
@BeforeEach
15+
fun setup() {
16+
DaggerTestDayComponent.create().inject(this)
17+
}
18+
19+
@Test
20+
fun testDay07PartOne() {
21+
assertEquals(1550, day07.partOne(DAY_07))
22+
}
23+
24+
@Test
25+
fun testDay07PartTwo() {
26+
assertEquals(9897897326778, day07.partTwo(DAY_07))
27+
}
28+
29+
companion object {
30+
private const val DAY_07: String = "advent-of-code-input/2025/day07.input"
31+
}
32+
}

0 commit comments

Comments
 (0)