-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path12p2.go
60 lines (45 loc) · 1.01 KB
/
12p2.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 main
import (
"fmt"
"aoc2022/utils"
)
func D12P2() {
lines := utils.ReadLines("./inputs/12.txt")
hills := parseHills(lines)
queue := []nodeCoordinate{}
end := findStart(hills, true)
value, exists := hills[end]
if exists {
value.movesFromStart = 0
}
hills[end] = value
queue = append(queue, end)
bestRoute := int(^uint(0) >> 1)
for len(queue) > 0 {
if hills[queue[0]].visited {
queue = queue[1:]
continue
}
if hills[queue[0]].letter == "a" {
if hills[queue[0]].movesFromStart < bestRoute {
bestRoute = hills[queue[0]].movesFromStart
}
}
neighbours := findNeighbours(&hills, queue[0], true)
for _, n := range neighbours {
value, exists := hills[n]
if exists {
value.movesFromStart = updateMovesFromStart(value, hills[queue[0]])
hills[n] = value
}
queue = append(queue, n)
}
value, exists := hills[queue[0]]
if exists {
value.visited = true
hills[queue[0]] = value
}
queue = queue[1:]
}
fmt.Printf("Best route: %d\n", bestRoute)
}