package two import ( "bytes" "math" "sync" log "github.com/sirupsen/logrus" ) func Solve(buf []byte) (int, error) { r := bytes.NewReader(buf) graph, err := parseLines(r) if err != nil { return 0, err } var wg sync.WaitGroup wg.Add(len(graph.antennae)) for i, a := range graph.antennae { go func() { defer wg.Done() for j, b := range graph.antennae { if i == j || a.identifier != b.identifier { continue } for _, coords := range calcAntiNodePos(a.coords, b.coords, []coords{a.coords, b.coords}, graph) { if !graph.antinodes.contains(coords) { graph.antinodes.insert(coords) } } } }() } wg.Wait() log.Debugf("\n%s\n", graph.debug()) return graph.antinodes.len(), nil } func calcAntiNodePos(a, b coords, all []coords, g *graph) []coords { xdiff := int(math.Abs(float64(a.x - b.x))) ydiff := int(math.Abs(float64(a.y - b.y))) var next coords if a.x < b.x && a.y < b.y { next = newCoords(b.x+xdiff, b.y+ydiff) } else if a.x < b.x && a.y > b.y { next = newCoords(b.x+xdiff, b.y-ydiff) } else if a.x > b.x && a.y < b.y { next = newCoords(b.x-xdiff, b.y+ydiff) } else { next = newCoords(b.x-xdiff, b.y-ydiff) } if g.isOutOfBounds(next) { return all } return calcAntiNodePos(b, next, append(all, next), g) }