diff --git a/2018/day10-1.go b/2018/day10-1.go index d271691..1a77038 100644 --- a/2018/day10-1.go +++ b/2018/day10-1.go @@ -1,8 +1,6 @@ package main import ( - "fmt" - "internal/day10" "internal/util" ) @@ -11,27 +9,21 @@ func main() { data := util.ReadInput() points := day10.ParseInput(data) - i := 0 // debug + lowest := day10.CalculateRange(points) for { for _, point := range points { point.Move() } - groups := day10.GroupPoints(points) - if groups != nil { - for _, group := range groups { - day10.DrawPoints(group) - fmt.Println() + newRange := day10.CalculateRange(points) + if newRange < lowest { + lowest = newRange + } else { + for _, point := range points { + point.Reverse() } + day10.DrawPoints(points) return } - - // debug - i++ - if i > 100000 { - fmt.Println("oops") - return - } - // end debug } } diff --git a/2018/day10-2.go b/2018/day10-2.go new file mode 100644 index 0000000..a64bf3b --- /dev/null +++ b/2018/day10-2.go @@ -0,0 +1,31 @@ +package main + +import ( + "fmt" + + "internal/day10" + "internal/util" +) + +func main() { + data := util.ReadInput() + points := day10.ParseInput(data) + + lowest := day10.CalculateRange(points) + i := 0 + for { + for _, point := range points { + point.Move() + } + + newRange := day10.CalculateRange(points) + if newRange < lowest { + lowest = newRange + } else { + fmt.Println(i) + return + } + + i++ + } +} diff --git a/2018/internal/day10/groups.go b/2018/internal/day10/groups.go index aa32c20..bb5852c 100644 --- a/2018/internal/day10/groups.go +++ b/2018/internal/day10/groups.go @@ -1,5 +1,11 @@ +// Unused solution: yes, this is a SECOND failed solution. Left here as a testament +// to... something. The actual solution uses CalculateRange from point.go package day10 +import ( + "sort" +) + // GroupPoints creates groups of points, then sorts them and returns the sorted list // A 'group' is a series of points that are adjacent to each other. // If this function finds any non-adjacent points, it returns nil. @@ -36,7 +42,12 @@ func GroupPoints(points []*Point) [][]*Point { groups = append(groups, group) } - // TODO: sort the groups + // sort the groups + sort.Slice(groups[:], func(i, j int) bool { + xMin1, _, _, _ := findBounds(groups[i]) + xMin2, _, _, _ := findBounds(groups[j]) + return xMin1 < xMin2 + }) return groups } diff --git a/2018/internal/day10/point.go b/2018/internal/day10/point.go index 0c9bd71..20e2ce3 100644 --- a/2018/internal/day10/point.go +++ b/2018/internal/day10/point.go @@ -14,6 +14,16 @@ func (p *Point) Move() { p.Y += p.Yv } +func (p *Point) Reverse() { + p.X -= p.Xv + p.Y -= p.Yv +} + +func CalculateRange(points []*Point) int { + xMin, xMax, yMin, yMax := findBounds(points) + return xMax - xMin + yMax - yMin +} + func DrawPoints(points []*Point) { pointArr := makeBuffer(points) @@ -33,7 +43,11 @@ func DrawPoints(points []*Point) { // findBounds returns xMin, xMax, yMin, and yMax from the provided points. func findBounds(points []*Point) (int, int, int, int) { // find min and max values - var xMin, xMax, yMin, yMax int + xMin := points[0].X + xMax := points[0].X + yMin := points[0].Y + yMax := points[0].Y + for _, point := range points { if point.X < xMin { xMin = point.X @@ -54,6 +68,7 @@ func findBounds(points []*Point) (int, int, int, int) { func makeBuffer(points []*Point) [][]byte { xMin, xMax, yMin, yMax := findBounds(points) + // get the total magnitude; we'll do adjustments later xRange := xMax - xMin yRange := yMax - yMin