mirror of
https://github.com/onyx-and-iris/aoc2024.git
synced 2025-01-09 14:20:48 +00:00
52 lines
1.0 KiB
Go
52 lines
1.0 KiB
Go
package one
|
|
|
|
import (
|
|
"bytes"
|
|
"math"
|
|
|
|
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
|
|
}
|
|
|
|
for i, a := range graph.antennae {
|
|
for j, b := range graph.antennae {
|
|
if i == j || a.identifier != b.identifier {
|
|
continue
|
|
}
|
|
|
|
coords := calcAntiNodePos(a.coords, b.coords)
|
|
if !graph.isOutOfBounds(coords) {
|
|
_, ok := graph.antinodes[coords]
|
|
if !ok {
|
|
graph.antinodes[coords] = struct{}{}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
log.Debugf("\n%s\n", graph.debug())
|
|
|
|
return len(graph.antinodes), nil
|
|
}
|
|
|
|
func calcAntiNodePos(a, b coords) coords {
|
|
xdiff := int(math.Abs(float64(a.x - b.x)))
|
|
ydiff := int(math.Abs(float64(a.y - b.y)))
|
|
|
|
if a.x < b.x && a.y < b.y {
|
|
return newCoords(b.x+xdiff, b.y+ydiff)
|
|
} else if a.x < b.x && a.y > b.y {
|
|
return newCoords(b.x+xdiff, b.y-ydiff)
|
|
} else if a.x > b.x && a.y < b.y {
|
|
return newCoords(b.x-xdiff, b.y+ydiff)
|
|
} else {
|
|
return newCoords(b.x-xdiff, b.y-ydiff)
|
|
}
|
|
}
|