mirror of
https://github.com/onyx-and-iris/aoc2024.git
synced 2026-04-09 02:23:36 +00:00
add day-14 + benchmarks
This commit is contained in:
6
day-14/internal/one/benchmark
Normal file
6
day-14/internal/one/benchmark
Normal file
@@ -0,0 +1,6 @@
|
||||
goos: linux
|
||||
goarch: amd64
|
||||
pkg: github.com/onyx-and-iris/aoc2024/day-14/internal/one
|
||||
cpu: Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz
|
||||
BenchmarkSolve-12 1000000000 0.0007711 ns/op
|
||||
ok github.com/onyx-and-iris/aoc2024/day-14/internal/one 0.011s
|
||||
36
day-14/internal/one/graph.go
Normal file
36
day-14/internal/one/graph.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package one
|
||||
|
||||
type dimension int
|
||||
|
||||
type graph struct {
|
||||
data [][]int
|
||||
}
|
||||
|
||||
func newGraph(x, y dimension) *graph {
|
||||
var data [][]int
|
||||
for range y {
|
||||
data = append(data, make([]int, x))
|
||||
}
|
||||
|
||||
return &graph{data: data}
|
||||
}
|
||||
|
||||
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) quadrant(startx, endx, starty, endy int) int {
|
||||
var robotCount int
|
||||
for i := starty; i < endy; i++ {
|
||||
for j := startx; j < endx; j++ {
|
||||
robotCount += g.data[i][j]
|
||||
}
|
||||
}
|
||||
return robotCount
|
||||
}
|
||||
51
day-14/internal/one/robot.go
Normal file
51
day-14/internal/one/robot.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package one
|
||||
|
||||
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
|
||||
}
|
||||
41
day-14/internal/one/solve.go
Normal file
41
day-14/internal/one/solve.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package one
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
)
|
||||
|
||||
func Solve(buf []byte) (int, error) {
|
||||
r := bytes.NewReader(buf)
|
||||
robots, err := parseLines(r)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
const numSeconds int = 100
|
||||
const width, height dimension = 101, 103
|
||||
|
||||
graph := newGraph(width, height)
|
||||
|
||||
for range numSeconds {
|
||||
for _, robot := range robots {
|
||||
robot.update(width, height)
|
||||
}
|
||||
}
|
||||
graph.update(robots)
|
||||
|
||||
safetyFactor := 1
|
||||
for _, v := range calculateQuadrants(graph) {
|
||||
safetyFactor *= v
|
||||
}
|
||||
|
||||
return safetyFactor, nil
|
||||
}
|
||||
|
||||
func calculateQuadrants(graph *graph) []int {
|
||||
topLeft := graph.quadrant(0, len(graph.data[0])/2, 0, len(graph.data)/2)
|
||||
topRight := graph.quadrant(len(graph.data[0])/2+1, len(graph.data[0]), 0, len(graph.data)/2)
|
||||
bottomLeft := graph.quadrant(0, len(graph.data[0])/2, len(graph.data)/2+1, len(graph.data))
|
||||
bottomRight := graph.quadrant(len(graph.data[0])/2+1, len(graph.data[0]), len(graph.data)/2+1, len(graph.data))
|
||||
|
||||
return []int{topLeft, topRight, bottomLeft, bottomRight}
|
||||
}
|
||||
15
day-14/internal/one/solve_internal_test.go
Normal file
15
day-14/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)
|
||||
}
|
||||
40
day-14/internal/one/util.go
Normal file
40
day-14/internal/one/util.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package one
|
||||
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user