diff --git a/day-10/one.go b/day-10/one.go index 9223d6c..14b7576 100644 --- a/day-10/one.go +++ b/day-10/one.go @@ -28,6 +28,25 @@ type tracker struct { init point point point last []coords + steps int +} + +// newTracker stores the starting point +// it creates a queue for storing past 2 moves +// returns a tracker struct for counting steps +func newTracker() tracker { + tracker := tracker{last: make([]coords, 0)} + for _, each := range pointsArray { + for _, point := range each.points { + if point.identifier == 'S' { + tracker.init = point + tracker.point = tracker.init + tracker.last = make([]coords, 1) + tracker.last = append(tracker.last, tracker.point.coords) + } + } + } + return tracker } func (t tracker) X() int { @@ -73,28 +92,11 @@ func mapPoints(lines []string) { } } -// initializeTracker stores the starting point, creates coords array for tracking last moves -func initializeTracker() tracker { - tracker := tracker{last: make([]coords, 0)} - for _, each := range pointsArray { - for _, point := range each.points { - if point.identifier == 'S' { - tracker.init = point - tracker.point = tracker.init - tracker.last = make([]coords, 1) - tracker.last = append(tracker.last, tracker.point.coords) - } - } - } - return tracker -} - // walk moves along the pipes storing poinst that mark loop locations // it also keeps a track of last two moves // returns the number of steps to traverse all pipes func walk(tracker tracker) int { - var steps int - for steps := 0; steps == 0 || !comparePoints(tracker.init, tracker.point); steps++ { + for ; tracker.steps == 0 || !comparePoints(tracker.init, tracker.point); tracker.steps++ { next := checkSouth(tracker.point) if !inLastMoves(tracker.last, next.coords) { log.Debug("moving south from ", string(tracker.point.identifier), " to ", string(next.identifier)) @@ -127,16 +129,16 @@ func walk(tracker tracker) int { tracker.last = append(tracker.last[1:], next.coords) continue } - steps++ + tracker.steps++ // the very last move will be back at start } - return steps + return tracker.steps } // one returns the number of steps to reach the furthest point from the start func one(lines []string) int { mapPoints(lines) - tracker := initializeTracker() + tracker := newTracker() return walk(tracker) / 2 } diff --git a/day-10/run.sh b/day-10/run.sh old mode 100644 new mode 100755 diff --git a/day-10/two.go b/day-10/two.go index 66f4402..9349bae 100644 --- a/day-10/two.go +++ b/day-10/two.go @@ -22,7 +22,7 @@ func debugPrint() { var loop []coords -// two +// two returns the number of coords that sit inside the polygon func two(lines []string) int { if log.GetLevel() == log.DebugLevel { debugPrint()