mirror of
https://github.com/onyx-and-iris/aoc2024.git
synced 2026-04-09 02:23:36 +00:00
add day-15 part1
This commit is contained in:
16
day-15/internal/one/direction.go
Normal file
16
day-15/internal/one/direction.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package one
|
||||
|
||||
import "slices"
|
||||
|
||||
type direction int
|
||||
|
||||
const (
|
||||
N direction = iota
|
||||
E
|
||||
S
|
||||
W
|
||||
)
|
||||
|
||||
func directions(dir rune) direction {
|
||||
return direction(slices.Index([]rune{'^', '>', 'v', '<'}, dir))
|
||||
}
|
||||
50
day-15/internal/one/graph.go
Normal file
50
day-15/internal/one/graph.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package one
|
||||
|
||||
import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
type graph struct {
|
||||
robot point
|
||||
boxes map[point]struct{}
|
||||
data []string
|
||||
}
|
||||
|
||||
func newGraph() *graph {
|
||||
return &graph{boxes: make(map[point]struct{})}
|
||||
}
|
||||
|
||||
func (g *graph) String() string {
|
||||
return strings.Join(g.data, "\n")
|
||||
}
|
||||
|
||||
func (g *graph) valueAt(point point) rune {
|
||||
return rune(g.data[point.y][point.x])
|
||||
}
|
||||
|
||||
func (g *graph) updateRobot(dir direction) {
|
||||
new := neighbours(g.robot)[dir]
|
||||
|
||||
g.updateEach(g.robot, '.')
|
||||
g.updateEach(new, '@')
|
||||
g.robot = new
|
||||
}
|
||||
|
||||
func (g *graph) updateBox(point point, dir direction) {
|
||||
g.deleteBox(point)
|
||||
g.addBox(neighbours(point)[dir])
|
||||
}
|
||||
|
||||
func (g *graph) deleteBox(point point) {
|
||||
delete(g.boxes, point)
|
||||
g.updateEach(point, '.')
|
||||
}
|
||||
|
||||
func (g *graph) addBox(point point) {
|
||||
g.boxes[point] = struct{}{}
|
||||
g.updateEach(point, 'O')
|
||||
}
|
||||
|
||||
func (g *graph) updateEach(point point, r rune) {
|
||||
g.data[point.y] = replaceAtIndex(g.data[point.y], r, point.x)
|
||||
}
|
||||
10
day-15/internal/one/neighbours.go
Normal file
10
day-15/internal/one/neighbours.go
Normal file
@@ -0,0 +1,10 @@
|
||||
package one
|
||||
|
||||
func neighbours(p point) [4]point {
|
||||
return [4]point{
|
||||
{p.x, p.y - 1}, // N
|
||||
{p.x + 1, p.y}, // E
|
||||
{p.x, p.y + 1}, // S
|
||||
{p.x - 1, p.y}, // W
|
||||
}
|
||||
}
|
||||
10
day-15/internal/one/point.go
Normal file
10
day-15/internal/one/point.go
Normal file
@@ -0,0 +1,10 @@
|
||||
package one
|
||||
|
||||
type point struct {
|
||||
x int
|
||||
y int
|
||||
}
|
||||
|
||||
func newPoint(x, y int) point {
|
||||
return point{x, y}
|
||||
}
|
||||
57
day-15/internal/one/solve.go
Normal file
57
day-15/internal/one/solve.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package one
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
for _, dir := range dirs {
|
||||
log.Debugf("about to explore '%s' direction from robot location %v", string(dir), graph.robot)
|
||||
|
||||
stack, ok := explore(graph, graph.robot, directions(dir), newStack())
|
||||
if !ok {
|
||||
log.Debug("path ends with '#', continuing...")
|
||||
continue
|
||||
}
|
||||
|
||||
for !stack.IsEmpty() {
|
||||
point := stack.Pop().(point)
|
||||
graph.updateBox(point, directions(dir))
|
||||
}
|
||||
graph.updateRobot(directions(dir))
|
||||
|
||||
log.Debugf("\n%s\n", graph.String())
|
||||
}
|
||||
|
||||
var sum int
|
||||
for box := range graph.boxes {
|
||||
sum += (100 * box.y) + box.x
|
||||
}
|
||||
|
||||
return sum, nil
|
||||
}
|
||||
|
||||
func explore(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 'O':
|
||||
stack.Push(ns[direction])
|
||||
}
|
||||
|
||||
return explore(graph, ns[direction], direction, stack)
|
||||
}
|
||||
15
day-15/internal/one/solve_internal_test.go
Normal file
15
day-15/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)
|
||||
}
|
||||
33
day-15/internal/one/stack.go
Normal file
33
day-15/internal/one/stack.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package one
|
||||
|
||||
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
|
||||
}
|
||||
59
day-15/internal/one/util.go
Normal file
59
day-15/internal/one/util.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package one
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"io"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
var reBoxes = regexp.MustCompile(`O`)
|
||||
|
||||
func parseLines(r io.Reader) (*graph, string, error) {
|
||||
graph := newGraph()
|
||||
var directions []string
|
||||
|
||||
var inDirections bool
|
||||
var linecount int
|
||||
scanner := bufio.NewScanner(r)
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
if len(line) == 0 {
|
||||
inDirections = true
|
||||
continue
|
||||
}
|
||||
|
||||
matches := reBoxes.FindAllStringIndex(line, -1)
|
||||
for _, m := range matches {
|
||||
graph.boxes[newPoint(m[0], linecount)] = struct{}{}
|
||||
}
|
||||
|
||||
if inDirections {
|
||||
directions = append(directions, line)
|
||||
} else {
|
||||
indx := strings.Index(line, "@")
|
||||
if indx != -1 {
|
||||
log.Debugf("adding start point at (%d, %d)", indx, linecount)
|
||||
graph.robot = newPoint(indx, linecount)
|
||||
}
|
||||
|
||||
graph.data = append(graph.data, line)
|
||||
}
|
||||
|
||||
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