mirror of
https://github.com/onyx-and-iris/aoc2024.git
synced 2026-04-08 18:13:36 +00:00
add day-08 + benchmarks
This commit is contained in:
20
day-08/internal/one/antenna.go
Normal file
20
day-08/internal/one/antenna.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package one
|
||||
|
||||
type coords struct {
|
||||
x int
|
||||
y int
|
||||
}
|
||||
|
||||
func newCoords(x, y int) coords {
|
||||
return coords{x, y}
|
||||
}
|
||||
|
||||
type antenna struct {
|
||||
coords
|
||||
identifier rune
|
||||
}
|
||||
|
||||
func newAntenna(x, y int, identifier rune) antenna {
|
||||
coords := newCoords(x, y)
|
||||
return antenna{coords: coords, identifier: identifier}
|
||||
}
|
||||
6
day-08/internal/one/benchmark
Normal file
6
day-08/internal/one/benchmark
Normal file
@@ -0,0 +1,6 @@
|
||||
goos: linux
|
||||
goarch: amd64
|
||||
pkg: github.com/onyx-and-iris/aoc2024/day-08/internal/one
|
||||
cpu: Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz
|
||||
BenchmarkSolve-12 1000000000 0.0004909 ns/op
|
||||
ok github.com/onyx-and-iris/aoc2024/day-08/internal/one 0.009s
|
||||
32
day-08/internal/one/graph.go
Normal file
32
day-08/internal/one/graph.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package one
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/onyx-and-iris/aoc2024/day-08/internal/util"
|
||||
)
|
||||
|
||||
type graph struct {
|
||||
data []string
|
||||
antennae []antenna
|
||||
antinodes map[coords]struct{}
|
||||
}
|
||||
|
||||
func (g *graph) String() string {
|
||||
return strings.Join(g.data, "\n")
|
||||
}
|
||||
|
||||
func (g *graph) isOutOfBounds(c coords) bool {
|
||||
return c.x < 0 || c.y < 0 || c.y >= len(g.data) || c.x >= len(g.data[0])
|
||||
}
|
||||
|
||||
func (g *graph) debug() string {
|
||||
for _, antenna := range g.antennae {
|
||||
g.data[antenna.y] = util.ReplaceAtIndex(g.data[antenna.y], antenna.identifier, antenna.x)
|
||||
}
|
||||
for antinode := range g.antinodes {
|
||||
g.data[antinode.y] = util.ReplaceAtIndex(g.data[antinode.y], '#', antinode.x)
|
||||
}
|
||||
|
||||
return g.String()
|
||||
}
|
||||
53
day-08/internal/one/solve.go
Normal file
53
day-08/internal/one/solve.go
Normal file
@@ -0,0 +1,53 @@
|
||||
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
|
||||
}
|
||||
|
||||
graph.antinodes = make(map[coords]struct{})
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
15
day-08/internal/one/solve_internal_test.go
Normal file
15
day-08/internal/one/solve_internal_test.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package one
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
//go:embed testdata/input.txt
|
||||
var data []byte
|
||||
|
||||
func BenchmarkSolve(b *testing.B) {
|
||||
os.Stdout, _ = os.Open(os.DevNull)
|
||||
Solve(data)
|
||||
}
|
||||
34
day-08/internal/one/util.go
Normal file
34
day-08/internal/one/util.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package one
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"io"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
var reMatchAntennae = regexp.MustCompile(`[a-zA-Z0-9]`)
|
||||
|
||||
func parseLines(r io.Reader) (*graph, error) {
|
||||
var graph graph
|
||||
|
||||
var linecount int
|
||||
scanner := bufio.NewScanner(r)
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
graph.data = append(graph.data, line)
|
||||
|
||||
for _, m := range reMatchAntennae.FindAllStringIndex(line, -1) {
|
||||
graph.antennae = append(
|
||||
graph.antennae,
|
||||
newAntenna(m[0], linecount, rune(line[m[0]])),
|
||||
)
|
||||
}
|
||||
linecount++
|
||||
}
|
||||
|
||||
if err := scanner.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &graph, nil
|
||||
}
|
||||
Reference in New Issue
Block a user