From 86e3983d4d8249fca948b15ecec8d8f01718686c Mon Sep 17 00:00:00 2001 From: Christopher De Vries Date: Sun, 17 Dec 2023 11:43:40 -0500 Subject: [PATCH] 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. --- day17p2b/solution.go | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/day17p2b/solution.go b/day17p2b/solution.go index 0d4b4ee..424efc5 100644 --- a/day17p2b/solution.go +++ b/day17p2b/solution.go @@ -39,16 +39,22 @@ func NewMaze(lines []string) *Maze { return &m } +const ( + stationary = iota + horizontal + vertical +) + type State struct { - Position utils.Point - Direction utils.Point + Position utils.Point + Motion int } var zero = utils.Point{X: 0, Y: 0} func (m *Maze) GetInitial() State { // special starting direction from which we will go East to South - return State{zero, zero} + return State{zero, stationary} } func (m *Maze) GetEdges(s State) []utils.Edge[State] { @@ -56,29 +62,38 @@ func (m *Maze) GetEdges(s State) []utils.Edge[State] { nextDirections := make([]utils.Point, 2) - switch s.Direction { - case zero: + switch s.Motion { + case stationary: // This is the special starting state nextDirections[0] = utils.East nextDirections[1] = utils.South - case utils.East, utils.West: + case horizontal: nextDirections[0] = utils.South nextDirections[1] = utils.North - case utils.North, utils.South: + case vertical: nextDirections[0] = utils.East nextDirections[1] = utils.West } // Left and Right for _, dir := range nextDirections { + var newMotion int + switch dir { + case utils.East, utils.West: + newMotion = horizontal + case utils.South, utils.North: + newMotion = vertical + } + p := s.Position var length uint64 + for step := 1; step <= 10; step++ { p = p.Add(dir) if delta, ok := m.HeatLoss[p]; ok { length += delta if step >= 4 { - ret = append(ret, utils.Edge[State]{Node: State{p, dir}, Distance: length}) + ret = append(ret, utils.Edge[State]{Node: State{p, newMotion}, Distance: length}) } } }