-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
47 lines (40 loc) · 825 Bytes
/
Copy pathmain.go
File metadata and controls
47 lines (40 loc) · 825 Bytes
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
package main
import (
"fmt"
"github.com/xlzior/aoc2024/utils"
)
func countTrails(grid utils.Grid, start utils.Pair) (int, int) {
peaks := make(map[utils.Pair]bool)
paths := 0
var nsew = []utils.Pair{{0, 1}, {0, -1}, {1, 0}, {-1, 0}}
var dfs func(utils.Pair)
dfs = func(p1 utils.Pair) {
curr := grid.GetCell(p1)
if curr == '9' {
peaks[p1] = true
paths++
}
for _, dir := range nsew {
p2 := p1.Plus(dir)
next := grid.GetCell(p2)
if next == curr+1 {
dfs(p2)
}
}
}
dfs(start)
return len(peaks), paths
}
func main() {
lines := utils.ReadLines()
grid := utils.Grid{Grid: lines}
part1 := 0
part2 := 0
for _, start := range grid.FindAllList('0') {
p1, p2 := countTrails(grid, start)
part1 += p1
part2 += p2
}
fmt.Println("Part 1:", part1)
fmt.Println("Part 2:", part2)
}