Skip to content

Commit 86e3983

Browse files
committed
Half the number of states.
I don't need to track the direction for each state, just if it was moving vertically or horizontally. This cutes the time approximately in half.
1 parent 889e522 commit 86e3983

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

day17p2b/solution.go

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,46 +39,61 @@ func NewMaze(lines []string) *Maze {
3939
return &m
4040
}
4141

42+
const (
43+
stationary = iota
44+
horizontal
45+
vertical
46+
)
47+
4248
type State struct {
43-
Position utils.Point
44-
Direction utils.Point
49+
Position utils.Point
50+
Motion int
4551
}
4652

4753
var zero = utils.Point{X: 0, Y: 0}
4854

4955
func (m *Maze) GetInitial() State {
5056
// special starting direction from which we will go East to South
51-
return State{zero, zero}
57+
return State{zero, stationary}
5258
}
5359

5460
func (m *Maze) GetEdges(s State) []utils.Edge[State] {
5561
ret := []utils.Edge[State]{}
5662

5763
nextDirections := make([]utils.Point, 2)
5864

59-
switch s.Direction {
60-
case zero:
65+
switch s.Motion {
66+
case stationary:
6167
// This is the special starting state
6268
nextDirections[0] = utils.East
6369
nextDirections[1] = utils.South
64-
case utils.East, utils.West:
70+
case horizontal:
6571
nextDirections[0] = utils.South
6672
nextDirections[1] = utils.North
67-
case utils.North, utils.South:
73+
case vertical:
6874
nextDirections[0] = utils.East
6975
nextDirections[1] = utils.West
7076
}
7177

7278
// Left and Right
7379
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+
7488
p := s.Position
7589
var length uint64
90+
7691
for step := 1; step <= 10; step++ {
7792
p = p.Add(dir)
7893
if delta, ok := m.HeatLoss[p]; ok {
7994
length += delta
8095
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})
8297
}
8398
}
8499
}

0 commit comments

Comments
 (0)