Kotlin
Tried recursive for part1, didn't work. LUCKILY for once I was smart and committed anyway, as it was the right solution for part2! I was losing my mind for a bit though as I had originally written my method with Integers...
Solution
class Day07 : Puzzle {
val grid = mutableListOf<MutableList<Char>>()
val partTwoCache = mutableMapOf<Pair<Int, Int>, Long>()
override fun readFile() {
val input = readInputFromFile(2025, 7, false)
for (line in input.lines().filter { it.isNotBlank() }) {
grid.add(line.toCharArray().toMutableList())
}
}
override fun solvePartOne(): String {
grid[1][grid[0].indexOf('S')] = '|'
var splits = 0
for (r in 1..<grid.size - 1) {
for (c in 0..<grid[r].size) {
if (grid[r][c] == '|') {
if (grid[r+1][c] == '.') {
grid[r+1][c] = '|'
} else if (grid[r+1][c] == '^') {
grid[r+1][c-1] = '|'
grid[r+1][c+1] = '|'
splits++
}
}
}
}
return splits.toString()
}
override fun solvePartTwo(): String {
val start = grid[0].indexOf('S')
return (1 + processBeamPartTwo(1, start)).toString() // don't forget to count the original timeline
}
private fun processBeamPartTwo(row: Int, column: Int): Long {
if (partTwoCache.contains(Pair(row, column))) {
return partTwoCache[Pair(row, column)]!!
}
if (row == grid.size) return 0L
if (column == grid[row].size || column < 0) return 0L
val out = if (grid[row][column] == '^') { // splitter
1L + processBeamPartTwo(row, column - 1) + processBeamPartTwo(row, column + 1)
} else {
processBeamPartTwo(row + 1, column)
}
partTwoCache[Pair(row, column)] = out
return out
}
}
full code on Codeberg