add day-14 + benchmarks

This commit is contained in:
2024-12-14 19:11:58 +00:00
parent 7b17f8edc1
commit d441f6a555
20 changed files with 578 additions and 0 deletions

View File

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

View File

@@ -0,0 +1,59 @@
package two
import (
"strings"
)
type dimension int
type graph struct {
x dimension
y dimension
data [][]int
}
func newGraph(x, y dimension) *graph {
var data [][]int
for range y {
data = append(data, make([]int, x))
}
return &graph{x: x, y: y, data: data}
}
func (g *graph) String() string {
temp := []string{}
for range len(g.data) {
temp = append(temp, string(make([]byte, len(g.data[0]))))
}
for i := 0; i < len(g.data); i++ {
for j := 0; j < len(g.data[0]); j++ {
if g.data[i][j] == 0 {
temp[i] = replaceAtIndex(temp[i], '.', j)
} else {
temp[i] = replaceAtIndex(temp[i], '*', j)
}
}
}
return strings.Join(temp, "\n")
}
func (g *graph) update(robots []*robot) {
for _, robot := range robots {
g.updateEach(robot.position)
}
}
func (g *graph) updateEach(p position) {
g.data[p.y][p.x]++
}
func (g *graph) isOutOfBounds(p position) bool {
return p.x < 0 || p.y < 0 || p.y >= len(g.data) || p.x >= len(g.data[p.y])
}
func (g *graph) valueAt(p position) int {
return g.data[p.y][p.x]
}

View File

@@ -0,0 +1,14 @@
package two
func neighbours(p position) [8]position {
return [8]position{
{p.x, p.y - 1}, // N
{p.x + 1, p.y - 1}, // NE
{p.x + 1, p.y}, // E
{p.x + 1, p.y + 1}, // SE
{p.x, p.y + 1}, // S
{p.x - 1, p.y + 1}, // Sw
{p.x - 1, p.y}, // W
{p.x - 1, p.y - 1}, // NW
}
}

View File

@@ -0,0 +1,51 @@
package two
import (
"fmt"
)
type position struct {
x int
y int
}
type velocity struct {
x int
y int
}
type robot struct {
position position
velocity velocity
}
func newRobot(px, py, vx, vy int) *robot {
return &robot{
position{x: px, y: py},
velocity{x: vx, y: vy},
}
}
func (r *robot) String() string {
return fmt.Sprintf("position: %+v velocity: %+v", r.position, r.velocity)
}
func (r *robot) update(width, height dimension) position {
oldPosition := r.position
r.position.x += r.velocity.x
if r.position.x < 0 {
r.position.x = int(width) + r.position.x
} else if r.position.x >= int(width) {
r.position.x = r.position.x - int(width)
}
r.position.y += r.velocity.y
if r.position.y < 0 {
r.position.y = int(height) + r.position.y
} else if r.position.y >= int(height) {
r.position.y = r.position.y - int(height)
}
return oldPosition
}

View File

@@ -0,0 +1,55 @@
package two
import (
"bytes"
log "github.com/sirupsen/logrus"
)
func Solve(buf []byte) (int, error) {
r := bytes.NewReader(buf)
robots, err := parseLines(r)
if err != nil {
return 0, err
}
const maxSeconds int = 10e3
const width, height dimension = 101, 103
var graph *graph
var max, maxAtSecond int
for i := 1; i <= maxSeconds; i++ {
graph = newGraph(width, height)
for _, robot := range robots {
robot.update(width, height)
}
graph.update(robots)
numNeighbours := evaluateNeighbours(graph, robots)
if numNeighbours > max {
max = numNeighbours
maxAtSecond = i
}
}
log.Debugf("\n%s\n", graph.String())
return maxAtSecond, nil
}
func evaluateNeighbours(graph *graph, robots []*robot) int {
var numNeighbours int
for _, robot := range robots {
for _, n := range neighbours(robot.position) {
if graph.isOutOfBounds(n) {
continue
}
if graph.valueAt(n) > 0 {
numNeighbours++
}
}
}
return numNeighbours
}

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,46 @@
package two
import (
"bufio"
"io"
"regexp"
"strconv"
)
var reRobot = regexp.MustCompile(`p=(?P<px>\d+),(?P<py>\d+) v=(?P<vx>-?\d+),(?P<vy>-?\d+)`)
func parseLines(r io.Reader) ([]*robot, error) {
var matches [][]string
scanner := bufio.NewScanner(r)
for scanner.Scan() {
line := scanner.Text()
m := reRobot.FindStringSubmatch(line)
matches = append(matches, m)
}
if err := scanner.Err(); err != nil {
return nil, err
}
var robots []*robot
for _, m := range matches {
robots = append(robots, newRobot(mustConv(m[1]), mustConv(m[2]), mustConv(m[3]), mustConv(m[4])))
}
return robots, nil
}
func mustConv(s string) int {
n, err := strconv.Atoi(s)
if err != nil {
panic(err)
}
return n
}
func replaceAtIndex(s string, r rune, i int) string {
out := []rune(s)
out[i] = r
return string(out)
}