mirror of
https://github.com/onyx-and-iris/aoc2024.git
synced 2026-04-09 02:23:36 +00:00
add day-15 part2 + benchmarks
This commit is contained in:
6
day-15/internal/one/benchmark
Normal file
6
day-15/internal/one/benchmark
Normal file
@@ -0,0 +1,6 @@
|
||||
goos: linux
|
||||
goarch: amd64
|
||||
pkg: github.com/onyx-and-iris/aoc2024/day-15/internal/one
|
||||
cpu: Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz
|
||||
BenchmarkSolve-12 1000000000 0.03920 ns/op
|
||||
ok github.com/onyx-and-iris/aoc2024/day-15/internal/one 0.322s
|
||||
6
day-15/internal/two/benchmark
Normal file
6
day-15/internal/two/benchmark
Normal file
@@ -0,0 +1,6 @@
|
||||
goos: linux
|
||||
goarch: amd64
|
||||
pkg: github.com/onyx-and-iris/aoc2024/day-15/internal/two
|
||||
cpu: Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz
|
||||
BenchmarkSolve-12 1000000000 0.07903 ns/op
|
||||
ok github.com/onyx-and-iris/aoc2024/day-15/internal/two 0.728s
|
||||
20
day-15/internal/two/directions.go
Normal file
20
day-15/internal/two/directions.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package two
|
||||
|
||||
import "slices"
|
||||
|
||||
type direction int
|
||||
|
||||
const (
|
||||
N direction = iota
|
||||
NE
|
||||
E
|
||||
SE
|
||||
S
|
||||
SW
|
||||
W
|
||||
NW
|
||||
)
|
||||
|
||||
func directions(dir rune) direction {
|
||||
return direction(slices.Index([]rune{'^', '.', '>', '.', 'v', '.', '<', '.'}, dir))
|
||||
}
|
||||
102
day-15/internal/two/explore.go
Normal file
102
day-15/internal/two/explore.go
Normal file
@@ -0,0 +1,102 @@
|
||||
package two
|
||||
|
||||
import (
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
||||
orderedmap "github.com/wk8/go-ordered-map/v2"
|
||||
)
|
||||
|
||||
func exploreDFS(graph *graph, next point, direction direction, stack *stack) (*stack, bool) {
|
||||
ns := neighbours(next)
|
||||
|
||||
log.Debug(string(graph.valueAt(ns[direction])))
|
||||
|
||||
switch graph.valueAt(ns[direction]) {
|
||||
case '#':
|
||||
return nil, false
|
||||
case '.':
|
||||
return stack, true
|
||||
case '[', ']':
|
||||
stack.Push(ns[direction])
|
||||
}
|
||||
|
||||
return exploreDFS(graph, ns[direction], direction, stack)
|
||||
}
|
||||
|
||||
func exploreBFS(graph *graph, direction direction) (*orderedmap.OrderedMap[point, struct{}], bool) {
|
||||
queue := newQueue()
|
||||
queue.Enqueue(graph.robot)
|
||||
visited := make(map[point]struct{})
|
||||
om := orderedmap.New[point, struct{}]()
|
||||
|
||||
for !queue.IsEmpty() {
|
||||
current := queue.Dequeue().(point)
|
||||
|
||||
_, ok := visited[current]
|
||||
if ok {
|
||||
continue
|
||||
}
|
||||
visited[current] = struct{}{}
|
||||
|
||||
ns := neighbours(current)
|
||||
switch graph.valueAt(ns[direction]) {
|
||||
case '.':
|
||||
if graph.valueAt(current) == '@' {
|
||||
return om, true
|
||||
}
|
||||
case '#':
|
||||
return om, false
|
||||
case '[':
|
||||
log.Debugf("adding %s to the queue", string(graph.valueAt(ns[direction])))
|
||||
queue.Enqueue(ns[direction])
|
||||
_, ok := om.Get(ns[direction])
|
||||
if !ok {
|
||||
om.Set(ns[direction], struct{}{})
|
||||
}
|
||||
|
||||
switch direction {
|
||||
case N:
|
||||
log.Debugf("adding %s to the queue", string(graph.valueAt(ns[NE])))
|
||||
queue.Enqueue(ns[NE])
|
||||
_, ok := om.Get(ns[NE])
|
||||
if !ok {
|
||||
om.Set(ns[NE], struct{}{})
|
||||
}
|
||||
case S:
|
||||
log.Debugf("adding %s to the queue", string(graph.valueAt(ns[SE])))
|
||||
queue.Enqueue(ns[SE])
|
||||
_, ok := om.Get(ns[SE])
|
||||
if !ok {
|
||||
om.Set(ns[SE], struct{}{})
|
||||
}
|
||||
}
|
||||
|
||||
case ']':
|
||||
log.Debugf("adding %s to the queue", string(graph.valueAt(ns[direction])))
|
||||
queue.Enqueue(ns[direction])
|
||||
_, ok := om.Get(ns[direction])
|
||||
if !ok {
|
||||
om.Set(ns[direction], struct{}{})
|
||||
}
|
||||
|
||||
switch direction {
|
||||
case N:
|
||||
log.Debugf("adding %s to the queue", string(graph.valueAt(ns[NW])))
|
||||
queue.Enqueue(ns[NW])
|
||||
_, ok := om.Get(ns[NW])
|
||||
if !ok {
|
||||
om.Set(ns[NW], struct{}{})
|
||||
}
|
||||
case S:
|
||||
log.Debugf("adding %s to the queue", string(graph.valueAt(ns[SW])))
|
||||
queue.Enqueue(ns[SW])
|
||||
_, ok := om.Get(ns[SW])
|
||||
if !ok {
|
||||
om.Set(ns[SW], struct{}{})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return om, true
|
||||
}
|
||||
44
day-15/internal/two/graph.go
Normal file
44
day-15/internal/two/graph.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package two
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type graph struct {
|
||||
robot point
|
||||
data []string
|
||||
}
|
||||
|
||||
func newGraph() *graph {
|
||||
return &graph{}
|
||||
}
|
||||
|
||||
func (g *graph) String() string {
|
||||
return strings.Join(g.data, "\n")
|
||||
}
|
||||
|
||||
func (g *graph) valueAt(p point) rune {
|
||||
return rune(g.data[p.y][p.x])
|
||||
}
|
||||
|
||||
func (g *graph) updateRobot(dir direction) {
|
||||
new := neighbours(g.robot)[dir]
|
||||
log.Debugf("new robot point: %v", new)
|
||||
|
||||
g.updateEach(g.robot, '.')
|
||||
g.updateEach(new, '@')
|
||||
g.robot = new
|
||||
}
|
||||
|
||||
func (g *graph) updateBox(p point, dir direction) {
|
||||
new := neighbours(p)[dir]
|
||||
|
||||
g.updateEach(new, g.valueAt(p))
|
||||
g.updateEach(p, '.')
|
||||
}
|
||||
|
||||
func (g *graph) updateEach(p point, r rune) {
|
||||
g.data[p.y] = replaceAtIndex(g.data[p.y], r, p.x)
|
||||
}
|
||||
14
day-15/internal/two/neighbours.go
Normal file
14
day-15/internal/two/neighbours.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package two
|
||||
|
||||
func neighbours(p point) []point {
|
||||
return []point{
|
||||
{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
|
||||
}
|
||||
}
|
||||
16
day-15/internal/two/point.go
Normal file
16
day-15/internal/two/point.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package two
|
||||
|
||||
import "fmt"
|
||||
|
||||
type point struct {
|
||||
x int
|
||||
y int
|
||||
}
|
||||
|
||||
func newPoint(x, y int) point {
|
||||
return point{x, y}
|
||||
}
|
||||
|
||||
func (p *point) String() string {
|
||||
return fmt.Sprintf("x: %d y: %d", p.x, p.y)
|
||||
}
|
||||
47
day-15/internal/two/queue.go
Normal file
47
day-15/internal/two/queue.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package two
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type queue struct {
|
||||
elements []point
|
||||
}
|
||||
|
||||
func newQueue() *queue {
|
||||
return &queue{}
|
||||
}
|
||||
|
||||
func (q *queue) Enqueue(elem point) {
|
||||
q.elements = append(q.elements, elem)
|
||||
}
|
||||
|
||||
func (q *queue) Dequeue() interface{} {
|
||||
if q.IsEmpty() {
|
||||
fmt.Println("UnderFlow")
|
||||
return point{}
|
||||
}
|
||||
element := q.elements[0]
|
||||
if q.Len() == 1 {
|
||||
q.elements = nil
|
||||
return element
|
||||
}
|
||||
q.elements = q.elements[1:]
|
||||
return element
|
||||
}
|
||||
|
||||
func (q *queue) Len() int {
|
||||
return len(q.elements)
|
||||
}
|
||||
|
||||
func (q *queue) IsEmpty() bool {
|
||||
return len(q.elements) == 0
|
||||
}
|
||||
|
||||
func (q *queue) Peek() (point, error) {
|
||||
if q.IsEmpty() {
|
||||
return point{}, errors.New("empty queue")
|
||||
}
|
||||
return q.elements[0], nil
|
||||
}
|
||||
66
day-15/internal/two/solve.go
Normal file
66
day-15/internal/two/solve.go
Normal file
@@ -0,0 +1,66 @@
|
||||
package two
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func Solve(buf []byte) (int, error) {
|
||||
r := bytes.NewReader(buf)
|
||||
graph, dirs, err := parseLines(r)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
log.Debugf("\n%s\n", graph.String())
|
||||
|
||||
for _, dir := range dirs {
|
||||
log.Debugf("about to explore '%s' direction from robot location (%s)", string(dir), graph.robot.String())
|
||||
|
||||
switch directions(dir) {
|
||||
case N, S:
|
||||
exploreNorthSouth(graph, directions(dir))
|
||||
case W, E:
|
||||
exploreWestEast(graph, directions(dir))
|
||||
}
|
||||
|
||||
log.Debugf("\n%s\n", graph.String())
|
||||
}
|
||||
|
||||
var sum int
|
||||
for y, line := range graph.data {
|
||||
for x, r := range line {
|
||||
if r == '[' {
|
||||
sum += (100 * y) + x
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return sum, nil
|
||||
}
|
||||
|
||||
func exploreWestEast(graph *graph, direction direction) {
|
||||
stack, ok := exploreDFS(graph, graph.robot, direction, newStack())
|
||||
if !ok {
|
||||
log.Debug("path ends with '#', continuing...")
|
||||
return
|
||||
}
|
||||
|
||||
for !stack.IsEmpty() {
|
||||
point := stack.Pop().(point)
|
||||
graph.updateBox(point, direction)
|
||||
}
|
||||
graph.updateRobot(direction)
|
||||
}
|
||||
|
||||
func exploreNorthSouth(graph *graph, direction direction) {
|
||||
om, ok := exploreBFS(graph, direction)
|
||||
|
||||
if ok {
|
||||
for pair := om.Newest(); pair != nil; pair = pair.Prev() {
|
||||
graph.updateBox(pair.Key, direction)
|
||||
}
|
||||
graph.updateRobot(direction)
|
||||
}
|
||||
}
|
||||
15
day-15/internal/two/solve_internal_test.go
Normal file
15
day-15/internal/two/solve_internal_test.go
Normal 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)
|
||||
}
|
||||
33
day-15/internal/two/stack.go
Normal file
33
day-15/internal/two/stack.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package two
|
||||
|
||||
type stack struct {
|
||||
items []interface{}
|
||||
}
|
||||
|
||||
func newStack() *stack {
|
||||
return &stack{}
|
||||
}
|
||||
|
||||
func (s *stack) Push(element interface{}) interface{} {
|
||||
s.items = append(s.items, element)
|
||||
return element
|
||||
}
|
||||
|
||||
func (s *stack) Pop() interface{} {
|
||||
l := len(s.items)
|
||||
element := s.items[l-1]
|
||||
s.items = s.items[:l-1]
|
||||
return element
|
||||
}
|
||||
|
||||
func (s *stack) Peek() interface{} {
|
||||
return s.items[len(s.items)-1]
|
||||
}
|
||||
|
||||
func (s *stack) Len() int {
|
||||
return len(s.items)
|
||||
}
|
||||
|
||||
func (s *stack) IsEmpty() bool {
|
||||
return s.Len() == 0
|
||||
}
|
||||
70
day-15/internal/two/util.go
Normal file
70
day-15/internal/two/util.go
Normal file
@@ -0,0 +1,70 @@
|
||||
package two
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"io"
|
||||
"strings"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func parseLines(r io.Reader) (*graph, string, error) {
|
||||
graph := newGraph()
|
||||
var directions []string
|
||||
|
||||
var sb strings.Builder
|
||||
var inDirections bool
|
||||
var linecount int
|
||||
scanner := bufio.NewScanner(r)
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
if len(line) == 0 {
|
||||
inDirections = true
|
||||
continue
|
||||
}
|
||||
|
||||
if inDirections {
|
||||
directions = append(directions, line)
|
||||
} else {
|
||||
for _, r := range line {
|
||||
switch r {
|
||||
case '#':
|
||||
sb.WriteRune('#')
|
||||
sb.WriteRune('#')
|
||||
case 'O':
|
||||
sb.WriteRune('[')
|
||||
sb.WriteRune(']')
|
||||
case '@':
|
||||
sb.WriteRune('@')
|
||||
sb.WriteRune('.')
|
||||
default:
|
||||
sb.WriteRune('.')
|
||||
sb.WriteRune('.')
|
||||
}
|
||||
}
|
||||
|
||||
indx := strings.Index(sb.String(), "@")
|
||||
if indx != -1 {
|
||||
log.Debugf("adding start point at (%d, %d)", indx, linecount)
|
||||
graph.robot = newPoint(indx, linecount)
|
||||
}
|
||||
|
||||
graph.data = append(graph.data, sb.String())
|
||||
sb.Reset()
|
||||
}
|
||||
|
||||
linecount++
|
||||
}
|
||||
|
||||
if err := scanner.Err(); err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
|
||||
return graph, strings.Join(directions, ""), nil
|
||||
}
|
||||
|
||||
func replaceAtIndex(s string, r rune, i int) string {
|
||||
out := []rune(s)
|
||||
out[i] = r
|
||||
return string(out)
|
||||
}
|
||||
Reference in New Issue
Block a user