aoc2024/day-08/internal/one/solve.go

52 lines
1.0 KiB
Go
Raw Normal View History

2024-12-08 19:55:23 +00:00
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)
}
}