@@ -39,46 +39,61 @@ func NewMaze(lines []string) *Maze {
39
39
return & m
40
40
}
41
41
42
+ const (
43
+ stationary = iota
44
+ horizontal
45
+ vertical
46
+ )
47
+
42
48
type State struct {
43
- Position utils.Point
44
- Direction utils. Point
49
+ Position utils.Point
50
+ Motion int
45
51
}
46
52
47
53
var zero = utils.Point {X : 0 , Y : 0 }
48
54
49
55
func (m * Maze ) GetInitial () State {
50
56
// special starting direction from which we will go East to South
51
- return State {zero , zero }
57
+ return State {zero , stationary }
52
58
}
53
59
54
60
func (m * Maze ) GetEdges (s State ) []utils.Edge [State ] {
55
61
ret := []utils.Edge [State ]{}
56
62
57
63
nextDirections := make ([]utils.Point , 2 )
58
64
59
- switch s .Direction {
60
- case zero :
65
+ switch s .Motion {
66
+ case stationary :
61
67
// This is the special starting state
62
68
nextDirections [0 ] = utils .East
63
69
nextDirections [1 ] = utils .South
64
- case utils . East , utils . West :
70
+ case horizontal :
65
71
nextDirections [0 ] = utils .South
66
72
nextDirections [1 ] = utils .North
67
- case utils . North , utils . South :
73
+ case vertical :
68
74
nextDirections [0 ] = utils .East
69
75
nextDirections [1 ] = utils .West
70
76
}
71
77
72
78
// Left and Right
73
79
for _ , dir := range nextDirections {
80
+ var newMotion int
81
+ switch dir {
82
+ case utils .East , utils .West :
83
+ newMotion = horizontal
84
+ case utils .South , utils .North :
85
+ newMotion = vertical
86
+ }
87
+
74
88
p := s .Position
75
89
var length uint64
90
+
76
91
for step := 1 ; step <= 10 ; step ++ {
77
92
p = p .Add (dir )
78
93
if delta , ok := m .HeatLoss [p ]; ok {
79
94
length += delta
80
95
if step >= 4 {
81
- ret = append (ret , utils.Edge [State ]{Node : State {p , dir }, Distance : length })
96
+ ret = append (ret , utils.Edge [State ]{Node : State {p , newMotion }, Distance : length })
82
97
}
83
98
}
84
99
}
0 commit comments