-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday10.go
More file actions
107 lines (85 loc) · 2.29 KB
/
day10.go
File metadata and controls
107 lines (85 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package main
import (
"AdventOfCode-go/advent2024/utils"
"fmt"
)
var Debug bool
type Coord struct {
x int
y int
}
var Directions = []Coord{
{-1, 0}, {1, 0}, {0, -1}, {0, 1}, // Up, Down, Left, Right
}
// Check if the provided "pos" is a valid point to continue on
func lowEnough(pos Coord, previousHeight int, landscape [][]int, visited [][]bool) bool {
maxRows := len(landscape)
maxCols := len(landscape[0])
if (pos.x >= 0 && pos.x < maxRows) && (pos.y >= 0 && pos.y < maxCols) {
if !visited[pos.x][pos.y] && landscape[pos.x][pos.y] == previousHeight+1 {
return true
}
}
return false
}
// Depth First Search
func dfs(pos Coord, landscape [][]int, visited [][]bool, trailEnds *map[Coord]struct{}) {
visited[pos.x][pos.y] = true
currentHeight := landscape[pos.x][pos.y]
if currentHeight == 9 {
(*trailEnds)[pos] = struct{}{}
}
for _, dir := range Directions {
nextPos := Coord{pos.x + dir.x, pos.y + dir.y}
if lowEnough(nextPos, currentHeight, landscape, visited) {
dfs(nextPos, landscape, visited, trailEnds)
}
}
}
func calcTrailheadScores(landscape [][]int) int {
maxRows := len(landscape)
maxCols := len(landscape[0])
totalScore := 0
for i := 0; i < maxRows; i++ {
for j := 0; j < maxCols; j++ {
// Check for a trailhead (0)
if landscape[i][j] == 0 {
visited := make([][]bool, maxRows)
for k := range visited {
visited[k] = make([]bool, maxCols)
}
trailEnds := make(map[Coord]struct{})
dfs(Coord{i, j}, landscape, visited, &trailEnds)
totalScore += len(trailEnds)
}
}
}
return totalScore
}
func day10(filename string, part byte) int {
puzzleInput, _ := utils.ReadFile(filename)
landscape := make([][]int, len(puzzleInput))
for key, puzzleLine := range puzzleInput {
landscape[key] = make([]int, len(puzzleLine))
for lineKey, lineItem := range puzzleLine {
landscape[key][lineKey] = int(lineItem - '0')
}
}
if Debug {
utils.Print2DArrayInt(landscape)
}
if part == 'a' {
return calcTrailheadScores(landscape)
}
return calcTrailheadScoresPartB(landscape)
}
// Main routine
func main() {
filenamePtr, execPart, debug := utils.CatchUserInput()
Debug = debug
if execPart == 'z' {
fmt.Println("Bad part choice. Available choices are 'a' and 'b'")
} else {
fmt.Printf("Result is: %d\n", day10(filenamePtr, execPart))
}
}