generated from devries/aoc_template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.go
80 lines (61 loc) · 1.48 KB
/
solution.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
71
72
73
74
75
76
77
78
79
80
package day17p1
import (
"io"
"aoc/utils"
)
func Solve(r io.Reader) any {
lines := utils.ReadLines(r)
m := NewMaze(lines)
dij := utils.NewDijkstra[State]()
finalState, err := dij.Run(m)
utils.Check(err, "error in search")
return dij.Distance[finalState]
}
type Maze struct {
HeatLoss map[utils.Point]uint64
BottomRight utils.Point
}
func NewMaze(lines []string) *Maze {
m := Maze{HeatLoss: make(map[utils.Point]uint64)}
for j, ln := range lines {
for i, r := range ln {
p := utils.Point{X: i, Y: -j}
v := uint64(r - '0')
m.HeatLoss[p] = v
m.BottomRight = p
}
}
return &m
}
type State struct {
Position utils.Point
Direction utils.Point
Steps int
}
func (m *Maze) GetInitial() State {
return State{utils.Point{X: 0, Y: 0}, utils.East, 0}
}
func (m *Maze) GetEdges(s State) []utils.Edge[State] {
ret := []utils.Edge[State]{}
// forward
np := s.Position.Add(s.Direction)
if hl, ok := m.HeatLoss[s.Position.Add(s.Direction)]; ok && s.Steps < 3 {
newState := State{np, s.Direction, s.Steps + 1}
ret = append(ret, utils.Edge[State]{Node: newState, Distance: hl})
}
// Left and Right
for _, dir := range []utils.Point{s.Direction.Right(), s.Direction.Left()} {
np := s.Position.Add(dir)
if hl, ok := m.HeatLoss[np]; ok {
newState := State{np, dir, 1}
ret = append(ret, utils.Edge[State]{Node: newState, Distance: hl})
}
}
return ret
}
func (m *Maze) IsFinal(s State) bool {
if s.Position == m.BottomRight {
return true
}
return false
}