-
Notifications
You must be signed in to change notification settings - Fork 0
/
day10_2.go
60 lines (51 loc) · 1.35 KB
/
day10_2.go
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
package day10
import (
"github.com/blfuentes/AdventOfCode_2024_Go/utilities"
)
func FindPath2(initnodes []*Node) int {
found := make([]Coord, 0) //make(map[Coord]bool)
var FindPathRecursive func(node Node)
FindPathRecursive = func(node Node) {
if node.Name == 9 {
found = append(found, node.Position)
} else {
for _, neighbor := range node.Neighbors {
if neighbor.Name-node.Name == 1 {
FindPathRecursive(*neighbor)
}
}
}
}
for _, node := range initnodes {
FindPathRecursive(*node)
}
return len(found)
}
func Executepart2() int {
var result int = 0
var fileName string = "./day10/day10.txt"
if fileContent, err := utilities.ReadFileAsLines(fileName); err == nil {
nodes := make(map[Coord]*Node)
initnodes := make([]*Node, 0)
for i, line := range fileContent {
for j, char := range line {
coord := Coord{i, j}
nodes[coord] = &Node{int(char - '0'), coord, make([]*Node, 0)}
}
}
movs := []Coord{{-1, 0}, {1, 0}, {0, -1}, {0, 1}}
for _, node := range nodes {
for _, mov := range movs {
neighbor := Coord{node.Position.Row + mov.Row, node.Position.Col + mov.Col}
if neighborNode, ok := nodes[neighbor]; ok {
node.Neighbors = append(node.Neighbors, neighborNode)
}
}
if node.Name == 0 {
initnodes = append(initnodes, node)
}
}
result = FindPath2(initnodes)
}
return result
}