add day-08 + benchmarks

This commit is contained in:
2024-12-08 19:55:23 +00:00
parent 9bfd45aee4
commit c1b8cb3f18
21 changed files with 520 additions and 0 deletions

View File

@@ -0,0 +1,20 @@
package two
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}
}

View File

@@ -0,0 +1,6 @@
goos: linux
goarch: amd64
pkg: github.com/onyx-and-iris/aoc2024/day-08/internal/two
cpu: Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz
BenchmarkSolve-12 1000000000 0.001471 ns/op
ok github.com/onyx-and-iris/aoc2024/day-08/internal/two 0.016s

View File

@@ -0,0 +1,33 @@
package two
import "sync"
type antiNodeCache struct {
mu sync.RWMutex
data map[coords]struct{}
}
func newAntiNodeCache() antiNodeCache {
return antiNodeCache{
data: make(map[coords]struct{}),
}
}
func (c *antiNodeCache) len() int {
return len(c.data)
}
func (c *antiNodeCache) contains(coords coords) bool {
c.mu.RLock()
defer c.mu.RUnlock()
_, ok := c.data[coords]
return ok
}
func (c *antiNodeCache) insert(coords coords) {
c.mu.Lock()
defer c.mu.Unlock()
c.data[coords] = struct{}{}
}

View File

@@ -0,0 +1,36 @@
package two
import (
"strings"
"github.com/onyx-and-iris/aoc2024/day-08/internal/util"
)
type graph struct {
data []string
antennae []antenna
antinodes antiNodeCache
}
func newGraph() *graph {
return &graph{[]string{}, []antenna{}, newAntiNodeCache()}
}
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.data {
g.data[antinode.y] = util.ReplaceAtIndex(g.data[antinode.y], '#', antinode.x)
}
return g.String()
}

View File

@@ -0,0 +1,66 @@
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
}
all := []coords{a.coords, b.coords}
for _, coords := range calcAntiNodePos(a.coords, b.coords, graph, all) {
if !graph.isOutOfBounds(coords) && !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, graph *graph, all []coords) []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 graph.isOutOfBounds(next) {
return all
}
return calcAntiNodePos(b, next, graph, append(all, next))
}

View File

@@ -0,0 +1,15 @@
package two
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)
}

View File

@@ -0,0 +1,34 @@
package two
import (
"bufio"
"io"
"regexp"
)
var reMatchAntennae = regexp.MustCompile(`[a-zA-Z0-9]`)
func parseLines(r io.Reader) (*graph, error) {
graph := newGraph()
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
}