move steps into tracker... it is afterall, the tracker

initializeTracker should be a factory function
This commit is contained in:
onyx-and-iris 2023-12-11 12:08:55 +00:00
parent f24a9085b3
commit 7cf383d9c9
3 changed files with 24 additions and 22 deletions

View File

@ -28,6 +28,25 @@ type tracker struct {
init point init point
point point point point
last []coords 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 { 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 // walk moves along the pipes storing poinst that mark loop locations
// it also keeps a track of last two moves // it also keeps a track of last two moves
// returns the number of steps to traverse all pipes // returns the number of steps to traverse all pipes
func walk(tracker tracker) int { func walk(tracker tracker) int {
var steps int for ; tracker.steps == 0 || !comparePoints(tracker.init, tracker.point); tracker.steps++ {
for steps := 0; steps == 0 || !comparePoints(tracker.init, tracker.point); steps++ {
next := checkSouth(tracker.point) next := checkSouth(tracker.point)
if !inLastMoves(tracker.last, next.coords) { if !inLastMoves(tracker.last, next.coords) {
log.Debug("moving south from ", string(tracker.point.identifier), " to ", string(next.identifier)) 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) tracker.last = append(tracker.last[1:], next.coords)
continue 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 // one returns the number of steps to reach the furthest point from the start
func one(lines []string) int { func one(lines []string) int {
mapPoints(lines) mapPoints(lines)
tracker := initializeTracker() tracker := newTracker()
return walk(tracker) / 2 return walk(tracker) / 2
} }

0
day-10/run.sh Normal file → Executable file
View File

View File

@ -22,7 +22,7 @@ func debugPrint() {
var loop []coords var loop []coords
// two // two returns the number of coords that sit inside the polygon
func two(lines []string) int { func two(lines []string) int {
if log.GetLevel() == log.DebugLevel { if log.GetLevel() == log.DebugLevel {
debugPrint() debugPrint()