mirror of
https://github.com/onyx-and-iris/aoc2024.git
synced 2025-01-09 14:20:48 +00:00
add day-15 part1
This commit is contained in:
parent
415a41788b
commit
090530ce72
1
day-15/.gitignore
vendored
Normal file
1
day-15/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
internal/two
|
41
day-15/cmd/cli/main.go
Normal file
41
day-15/cmd/cli/main.go
Normal file
@ -0,0 +1,41 @@
|
||||
/********************************************************************************
|
||||
Advent of Code 2024 - day-15
|
||||
********************************************************************************/
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"flag"
|
||||
"fmt"
|
||||
"slices"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
||||
problems "github.com/onyx-and-iris/aoc2024/day-15"
|
||||
)
|
||||
|
||||
//go:embed testdata
|
||||
var files embed.FS
|
||||
|
||||
func main() {
|
||||
filename := flag.String("f", "input.txt", "input file")
|
||||
loglevel := flag.Int("l", int(log.InfoLevel), "log level")
|
||||
flag.Parse()
|
||||
|
||||
if slices.Contains(log.AllLevels, log.Level(*loglevel)) {
|
||||
log.SetLevel(log.Level(*loglevel))
|
||||
}
|
||||
|
||||
data, err := files.ReadFile(fmt.Sprintf("testdata/%s", *filename))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
one, two, err := problems.Solve(data)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
fmt.Printf("solution one: %d\nsolution two: %d\n", one, two)
|
||||
}
|
10
day-15/go.mod
Normal file
10
day-15/go.mod
Normal file
@ -0,0 +1,10 @@
|
||||
module github.com/onyx-and-iris/aoc2024/day-15
|
||||
|
||||
go 1.23.3
|
||||
|
||||
require github.com/sirupsen/logrus v1.9.3
|
||||
|
||||
require (
|
||||
github.com/stretchr/testify v1.7.1 // indirect
|
||||
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
|
||||
)
|
16
day-15/go.sum
Normal file
16
day-15/go.sum
Normal file
@ -0,0 +1,16 @@
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
|
||||
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
|
||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
|
||||
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
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)
|
||||
}
|
30
day-15/makefile
Normal file
30
day-15/makefile
Normal file
@ -0,0 +1,30 @@
|
||||
program = day-15
|
||||
|
||||
GO = go
|
||||
SRC_DIR := src
|
||||
BIN_DIR := bin
|
||||
|
||||
EXE := $(BIN_DIR)/$(program)
|
||||
|
||||
.DEFAULT_GOAL := build
|
||||
|
||||
.PHONY: fmt vet build bench clean
|
||||
fmt:
|
||||
$(GO) fmt ./...
|
||||
|
||||
vet: fmt
|
||||
$(GO) vet ./...
|
||||
|
||||
build: vet | $(BIN_DIR)
|
||||
$(GO) build -o $(EXE) ./$(SRC_DIR)
|
||||
|
||||
bench:
|
||||
$(GO) test ./internal/one/ -bench=. > internal/one/benchmark
|
||||
$(GO) test ./internal/two/ -bench=. > internal/two/benchmark
|
||||
$(GO) test . -count=10 -bench=. > benchmark
|
||||
|
||||
$(BIN_DIR):
|
||||
@mkdir -p $@
|
||||
|
||||
clean:
|
||||
@rm -rv $(BIN_DIR)
|
20
day-15/solve.go
Normal file
20
day-15/solve.go
Normal file
@ -0,0 +1,20 @@
|
||||
package dayfifteen
|
||||
|
||||
import (
|
||||
"github.com/onyx-and-iris/aoc2024/day-15/internal/one"
|
||||
"github.com/onyx-and-iris/aoc2024/day-15/internal/two"
|
||||
)
|
||||
|
||||
func Solve(buf []byte) (int, int, error) {
|
||||
answerOne, err := one.Solve(buf)
|
||||
if err != nil {
|
||||
return 0, 0, err
|
||||
}
|
||||
|
||||
answerTwo, err := two.Solve(buf)
|
||||
if err != nil {
|
||||
return 0, 0, err
|
||||
}
|
||||
|
||||
return answerOne, answerTwo, nil
|
||||
}
|
15
day-15/solve_internal_test.go
Normal file
15
day-15/solve_internal_test.go
Normal file
@ -0,0 +1,15 @@
|
||||
package dayfifteen
|
||||
|
||||
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)
|
||||
}
|
Loading…
Reference in New Issue
Block a user