-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday16_1.go
70 lines (55 loc) · 2.12 KB
/
day16_1.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
61
62
63
64
65
66
67
68
69
70
package day16
import (
"container/heap"
"math"
"github.com/blfuentes/AdventOfCode_2024_Go/utilities"
)
func consumepath(queue *utilities.PriorityQueue[PositionDir],
graph *[][][]Cell, maxrows, maxcols int, themap [][]string, endnode Position) int {
if queue.Len() == 0 {
lowest := math.MaxInt32
for d := 0; d < 4; d++ {
if (*graph)[endnode.Row][endnode.Col][d].Distance <= lowest {
lowest = (*graph)[endnode.Row][endnode.Col][d].Distance
}
}
return lowest
}
current := heap.Pop(queue).(*utilities.Item[PositionDir]).Value
if alreadyVisited(current.Pos, current.Dir, *graph) {
return consumepath(queue, graph, maxrows, maxcols, themap, endnode)
}
setVisited(current.Pos, current.Dir, graph)
neighbours := Neighbours(current.Pos, current.Dir)
for _, n := range neighbours {
if isValidCoord(n.PDir.Pos, maxrows, maxcols, themap) &&
!(alreadyVisited(n.PDir.Pos, n.PDir.Dir, *graph)) {
currentDistance := (*graph)[current.Pos.Row][current.Pos.Col][current.Dir].Distance
n.Distance = currentDistance + n.Distance
if n.Distance <= (*graph)[n.PDir.Pos.Row][n.PDir.Pos.Col][n.PDir.Dir].Distance {
setDistance(n.PDir.Pos, n.PDir.Dir, n.Distance, graph)
heap.Push(queue, &utilities.Item[PositionDir]{Value: n.PDir, Priority: n.Distance, Index: -1})
}
}
}
return consumepath(queue, graph, maxrows, maxcols, themap, endnode)
}
func djikstraExplore(themap [][]string, startnode, endnode Position) int {
maxrows, maxcols := len(themap), len(themap[0])
graph := buildGraph(themap)
queue := make(utilities.PriorityQueue[PositionDir], 0)
heap.Init(&queue)
setDistance(startnode, 0, 0, &graph)
sn := PositionDir{Position{startnode.Row, startnode.Col}, 0}
heap.Push(&queue, &utilities.Item[PositionDir]{Value: sn, Priority: 0, Index: -1})
return consumepath(&queue, &graph, maxrows, maxcols, themap, endnode)
}
func Executepart1() int {
var result int = 0
var fileName string = "./day16/day16.txt"
if fileContent, err := utilities.ReadFileAsLines(fileName); err == nil {
themap, startnode, endnode := parseContent(fileContent)
result = djikstraExplore(themap, startnode, endnode)
}
return result
}