From a3fb7ac3531f222c495a51eb8f66df2da2d92ccb Mon Sep 17 00:00:00 2001 From: Kharec Date: Sun, 7 Dec 2025 10:31:15 +0100 Subject: [PATCH] feat: solve part two using recursive dfs + memoization --- internal/2025/DaySeven/code.go | 38 +++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/internal/2025/DaySeven/code.go b/internal/2025/DaySeven/code.go index 6e87bcb..69026c8 100644 --- a/internal/2025/DaySeven/code.go +++ b/internal/2025/DaySeven/code.go @@ -31,7 +31,6 @@ func findStart(input []string) (int, int) { func PartOne(input []string) int { rows, columns := len(input), len(input[0]) - startRow, startColumn := findStart(input) queue := []Position{{startRow, startColumn}} @@ -68,6 +67,39 @@ func PartOne(input []string) int { return splits } -func PartTwo(data []string) int { - return 0 +func PartTwo(input []string) int { + rows, columns := len(input), len(input[0]) + startRow, startColumn := findStart(input) + + cache := make(map[Position]int) + + var countTimelines func(row, column int) int + countTimelines = func(row, column int) int { + position := Position{row, column} + if count, exists := cache[position]; exists { + return count + } + + r, c := row+1, column + for r < rows && input[r][c] != '^' { + r++ + } + + if r == rows { + return 1 + } + + total := 0 + if c > 0 { + total += countTimelines(r, c-1) + } + if c < columns-1 { + total += countTimelines(r, c+1) + } + + cache[position] = total + return total + } + + return countTimelines(startRow, startColumn) }