Skip to content

Commit b7f4e88

Browse files
committed
[2025/7] Laboratories (Part 2)
1 parent d7bc5b1 commit b7f4e88

File tree

3 files changed

+34
-9
lines changed

3 files changed

+34
-9
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
| 2022 |||||||||||||| | | | | | | || | | || 28 |
3030
| 2023 |||||||| | | | | | | | | | | | | | | | | | | 12 |
3131
| 2024 ||||||||||||||| |||||| || | || 40 |
32-
| 2025 ||||||| | | | | | |||||||||||||| 13 |
32+
| 2025 ||||||| | | | | | |||||||||||||| 14 |
3333

3434
## 🛷 How to run
3535

@@ -212,7 +212,7 @@ e.g. `HandyHaversacks`)*
212212
| | 4 | [Printing Department](https://adventofcode.com/2025/day/4) | [[Code](src/main/kotlin/adventofcode/year2025/Day04PrintingDepartment.kt)] [[Test](src/test/kotlin/adventofcode/year2025/Day04PrintingDepartmentSpec.kt)] | `1411` | `8557` |
213213
| | 5 | [Cafeteria](https://adventofcode.com/2025/day/5) | [[Code](src/main/kotlin/adventofcode/year2025/Day05Cafeteria.kt)] [[Test](src/test/kotlin/adventofcode/year2025/Day05CafeteriaSpec.kt)] | `601` | `367899984917516` |
214214
| | 6 | [Trash Compactor](https://adventofcode.com/2025/day/6) | [[Code](src/main/kotlin/adventofcode/year2025/Day06TrashCompactor.kt)] [[Test](src/test/kotlin/adventofcode/year2025/Day06TrashCompactorSpec.kt)] | `5733696195703` | `10951882745757` |
215-
| | 7 | [Laboratories](https://adventofcode.com/2025/day/7) | [[Code](src/main/kotlin/adventofcode/year2025/Day07Laboratories.kt)] [[Test](src/test/kotlin/adventofcode/year2025/Day07LaboratoriesSpec.kt)] | `1711` | |
215+
| | 7 | [Laboratories](https://adventofcode.com/2025/day/7) | [[Code](src/main/kotlin/adventofcode/year2025/Day07Laboratories.kt)] [[Test](src/test/kotlin/adventofcode/year2025/Day07LaboratoriesSpec.kt)] | `1711` | `36706966158365` |
216216

217217
## 🕯️ Useful commands
218218

src/main/kotlin/adventofcode/year2025/Day07Laboratories.kt

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,56 @@ import adventofcode.common.spatial.Grid2d
77
class Day07Laboratories(
88
customInput: PuzzleInput? = null,
99
) : Puzzle(customInput) {
10-
private fun parseInput() = Grid2d(input)
11-
12-
override fun partOne(): Int {
13-
val grid = parseInput()
14-
val start = grid[START].x.toInt()
10+
private fun parseInput(): Pair<Long, List<Set<Long>>> {
11+
val grid = Grid2d(input)
12+
val start = grid[START].x
1513
val splittersByRow =
1614
grid
1715
.rows()
1816
.map { row ->
1917
row
2018
.mapIndexedNotNull { x, char ->
2119
when (char) {
22-
SPLITTER -> x
20+
SPLITTER -> x.toLong()
2321
else -> null
2422
}
2523
}.toSet()
2624
}
2725

26+
return Pair(start, splittersByRow)
27+
}
28+
29+
override fun partOne(): Int {
30+
val (start, splittersByRow) = parseInput()
31+
2832
return splittersByRow
2933
.fold(Pair(setOf(start), 0)) { (beam, splitCount), splittersInRow ->
3034
val reachedSplitters = beam intersect splittersInRow
3135
Pair(beam - reachedSplitters + reachedSplitters.flatMap { x -> setOf(x - 1, x + 1) }, splitCount + reachedSplitters.size)
3236
}.second
3337
}
3438

39+
override fun partTwo(): Long {
40+
val (start, splittersByRow) = parseInput()
41+
val beam = mutableMapOf(start to 1L)
42+
43+
for (splittersInRow in splittersByRow) {
44+
val reachedSplitters = splittersInRow intersect beam.keys
45+
46+
for (reachedSplitter in reachedSplitters) {
47+
val pathCount = beam[reachedSplitter] ?: 0
48+
49+
if (pathCount > 0) {
50+
beam[reachedSplitter - 1] = (beam[reachedSplitter - 1] ?: 0) + pathCount
51+
beam[reachedSplitter + 1] = (beam[reachedSplitter + 1] ?: 0) + pathCount
52+
beam[reachedSplitter] = 0
53+
}
54+
}
55+
}
56+
57+
return beam.values.sum()
58+
}
59+
3560
companion object {
3661
private const val START = 'S'
3762
private const val SPLITTER = '^'

src/test/kotlin/adventofcode/year2025/Day07LaboratoriesSpec.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ package adventofcode.year2025
22

33
import adventofcode.PuzzleBaseSpec
44

5-
class Day07LaboratoriesSpec : PuzzleBaseSpec(21)
5+
class Day07LaboratoriesSpec : PuzzleBaseSpec(21, 40)

0 commit comments

Comments
 (0)