package main import ( "fmt" "sort" helpers "git.bullercodeworks.com/brian/adventofcode/helpers" ) const MaxInt = int(^uint(0) >> 1) var field []helpers.Coordinate func main() { inp := helpers.StdinToStringSlice() initField(inp) solve() } func initField(inp []string) { for y, yb := range inp { for x, xb := range yb { if xb != '#' { continue } field = append(field, helpers.Coordinate{X: x, Y: y}) } } } func solve() { // Figure out the best place to put the station var bestCount int var best helpers.Coordinate var bestAngles map[float64][]helpers.Coordinate for _, wrk := range field { angles := make(map[float64][]helpers.Coordinate) var count int for _, tgt := range field { if wrk == tgt { continue } a := wrk.Angle(tgt) if _, ok := angles[a]; !ok { count++ } angles[a] = append(angles[a], tgt) } if count > bestCount { bestCount = count best = wrk bestAngles = angles } } fmt.Println("Part 1:", best, bestCount) var angleSlice []float64 for k := range bestAngles { angleSlice = append(angleSlice, k) } sort.Float64s(angleSlice) // Now start vaporizing var hitCount int var c200 helpers.Coordinate for k := range bestAngles { sort.Slice(bestAngles[k], func(i, j int) bool { return bestAngles[k][i].Distance(best) < bestAngles[k][j].Distance(best) }) } for len(bestAngles) > 0 { for _, v := range angleSlice { if roids, ok := bestAngles[v]; ok { // Destroy the first asteroid in the list hitCount++ if hitCount == 200 { c200 = roids[0] res := (c200.X*100 + c200.Y) fmt.Println("Part 2:", c200, res) return } if len(roids) > 1 { bestAngles[v] = roids[1:] } else { delete(bestAngles, v) } } } } }