Skip to content

Commit

Permalink
2024 day 10 part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
timredband committed Dec 24, 2024
1 parent 4c0dba5 commit 3c56c5e
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 2 deletions.
2 changes: 1 addition & 1 deletion 2024/go/inputs/day10/example1.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
0123
1234
8765
9976
9876
7 changes: 7 additions & 0 deletions 2024/go/inputs/day10/example4.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.....0.
..4321.
..5..2.
..6543.
..7..4.
..8765.
..9....
7 changes: 7 additions & 0 deletions 2024/go/inputs/day10/example5.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
..90..9
...1.98
...2..7
6543456
765.987
876....
987....
6 changes: 6 additions & 0 deletions 2024/go/inputs/day10/example6.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
012345
123456
234567
345678
4.6789
56789.
68 changes: 67 additions & 1 deletion 2024/go/pkg/day10/part2.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,72 @@ package day10

import (
"os"
"strconv"

"github.com/timredband/advent-of-code/pkg/utils"
)

func Part2(file *os.File) int { return 0 }
func Part2(file *os.File) int {
input := utils.ReadFile(file)
board := make([][]int, 0, len(input))
DP := make([][]int, len(input))

for i := range input {
row := make([]int, len(input[i]))
DP[i] = make([]int, len(input[i]))

for j := range input[i] {
if string(input[i][j]) == "." {
row[j] = -1
} else {
row[j], _ = strconv.Atoi(string(input[i][j]))
}

if row[j] == 9 {
DP[i][j] = 1
}
}

board = append(board, row)
}

for score := 8; score > -1; score -= 1 {
for i := range board {
for j := range board[i] {
if board[i][j] == score {
// up
if i-1 > -1 && board[i-1][j] == score+1 {
DP[i][j] += DP[i-1][j]
}

// down
if i+1 < len(board) && board[i+1][j] == score+1 {
DP[i][j] += DP[i+1][j]
}

// left
if j-1 > -1 && board[i][j-1] == score+1 {
DP[i][j] += DP[i][j-1]
}

// right
if j+1 < len(board[i]) && board[i][j+1] == score+1 {
DP[i][j] += DP[i][j+1]
}
}
}
}
}

result := 0

for i := range DP {
for j := range DP[i] {
if board[i][j] == 0 {
result += DP[i][j]
}
}
}

return result
}

0 comments on commit 3c56c5e

Please sign in to comment.