Skip to content

Commit a40529a

Browse files
committed
Day -07 2025
1 parent 99a8e55 commit a40529a

File tree

4 files changed

+121
-0
lines changed

4 files changed

+121
-0
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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+
var 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 updateBeamIndices = mutableSetOf<Int>()
24+
val line = manifold[depth]
25+
26+
beamIndices.forEach { beamIndex ->
27+
if (line[beamIndex] == '^') {
28+
splits++
29+
updateBeamIndices.add(beamIndex - 1)
30+
updateBeamIndices.add(beamIndex + 1)
31+
} else {
32+
updateBeamIndices.add(beamIndex)
33+
}
34+
}
35+
36+
beamIndices = updateBeamIndices
37+
depth++
38+
}
39+
40+
splits
41+
}
42+
43+
fun partTwo(filename: String) = generatorFactory.forFile(filename).read { input ->
44+
var beams = mutableMapOf<Location, Long>()
45+
val manifold = mutableListOf<MutableList<Char>>()
46+
47+
input.forEachIndexed { index, line ->
48+
if (index == 0) {
49+
beams[Location(0, line.indexOf('S'))] = 1
50+
}
51+
manifold.add(line.toCharArray().toMutableList())
52+
}
53+
54+
var depth = 1
55+
while (depth < manifold.size) {
56+
val newBeams = mutableMapOf<Location, Long>().withDefault { 0 }
57+
val line = manifold[depth]
58+
59+
beams.forEach { (location, count) ->
60+
if (location.depth != depth - 1) { throw IllegalStateException("We should only be tracking the last row.") }
61+
62+
if (line[location.index] == '^') {
63+
val left = Location(depth, location.index - 1)
64+
val right = Location(depth, location.index + 1)
65+
66+
newBeams[left] = newBeams.getValue(left) + count
67+
newBeams[right] = newBeams.getValue(right) + count
68+
} else {
69+
val below = Location(depth, location.index)
70+
newBeams[below] = newBeams.getValue(below) + count
71+
}
72+
}
73+
74+
beams = newBeams
75+
depth++
76+
}
77+
78+
beams.values.sum()
79+
}
80+
}
81+
82+
data class Location(val depth: Int, val index: Int) {
83+
override fun toString(): String {
84+
return "($depth,$index)"
85+
}
86+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
## [](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)