mirror of
https://github.com/onyx-and-iris/aoc2024.git
synced 2025-04-03 19:53:45 +01:00
Compare commits
2 Commits
1429ece4f4
...
6cb3fd1654
Author | SHA1 | Date | |
---|---|---|---|
6cb3fd1654 | |||
a0a25639cc |
1
day-15/.gitignore
vendored
1
day-15/.gitignore
vendored
@ -1 +0,0 @@
|
||||
internal/two
|
15
day-15/benchmark
Normal file
15
day-15/benchmark
Normal file
@ -0,0 +1,15 @@
|
||||
goos: linux
|
||||
goarch: amd64
|
||||
pkg: github.com/onyx-and-iris/aoc2024/day-15
|
||||
cpu: Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz
|
||||
BenchmarkSolve-12 1000000000 0.1255 ns/op
|
||||
BenchmarkSolve-12 1000000000 0.1184 ns/op
|
||||
BenchmarkSolve-12 1000000000 0.1247 ns/op
|
||||
BenchmarkSolve-12 1000000000 0.1178 ns/op
|
||||
BenchmarkSolve-12 1000000000 0.1214 ns/op
|
||||
BenchmarkSolve-12 1000000000 0.1201 ns/op
|
||||
BenchmarkSolve-12 1000000000 0.1196 ns/op
|
||||
BenchmarkSolve-12 1000000000 0.1199 ns/op
|
||||
BenchmarkSolve-12 1000000000 0.1192 ns/op
|
||||
BenchmarkSolve-12 1000000000 0.1230 ns/op
|
||||
ok github.com/onyx-and-iris/aoc2024/day-15 13.298s
|
@ -5,6 +5,11 @@ go 1.23.3
|
||||
require github.com/sirupsen/logrus v1.9.3
|
||||
|
||||
require (
|
||||
github.com/stretchr/testify v1.7.1 // indirect
|
||||
github.com/bahlo/generic-list-go v0.2.0 // indirect
|
||||
github.com/buger/jsonparser v1.1.1 // indirect
|
||||
github.com/mailru/easyjson v0.9.0 // indirect
|
||||
github.com/stretchr/testify v1.8.1 // indirect
|
||||
github.com/wk8/go-ordered-map/v2 v2.1.8 // indirect
|
||||
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
@ -1,16 +1,31 @@
|
||||
github.com/bahlo/generic-list-go v0.2.0 h1:5sz/EEAK+ls5wF+NeqDpk5+iNdMDXrh3z3nPnH1Wvgk=
|
||||
github.com/bahlo/generic-list-go v0.2.0/go.mod h1:2KvAjgMlE5NNynlg/5iLrrCCZ2+5xWbdbCW3pNTGyYg=
|
||||
github.com/buger/jsonparser v1.1.1 h1:2PnMjfWD7wBILjqQbt530v576A/cAbQvEW9gGIpYMUs=
|
||||
github.com/buger/jsonparser v1.1.1/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx27UK13J/0=
|
||||
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/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
|
||||
github.com/mailru/easyjson v0.9.0 h1:PrnmzHw7262yW8sTBwxi1PdJA3Iw/EKBa8psRf7d9a4=
|
||||
github.com/mailru/easyjson v0.9.0/go.mod h1:1+xMtQp2MRNVL/V1bOzuP3aP8VNwRW55fQUto+XFtTU=
|
||||
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/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||
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=
|
||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
||||
github.com/wk8/go-ordered-map/v2 v2.1.8 h1:5h/BUHu93oj4gIdvHHHGsScSTMijfx5PeYkE/fJgbpc=
|
||||
github.com/wk8/go-ordered-map/v2 v2.1.8/go.mod h1:5nJHM5DyteebpVlHnWMV0rPz6Zp7+xBAnxjb1X5vnTw=
|
||||
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=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
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)
|
||||
}
|
15
day-17/benchmark
Normal file
15
day-17/benchmark
Normal file
@ -0,0 +1,15 @@
|
||||
goos: linux
|
||||
goarch: amd64
|
||||
pkg: github.com/onyx-and-iris/aoc2024/day-17
|
||||
cpu: Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz
|
||||
BenchmarkSolve-12 1000000000 0.002182 ns/op
|
||||
BenchmarkSolve-12 1000000000 0.001786 ns/op
|
||||
BenchmarkSolve-12 1000000000 0.001724 ns/op
|
||||
BenchmarkSolve-12 1000000000 0.001759 ns/op
|
||||
BenchmarkSolve-12 1000000000 0.002150 ns/op
|
||||
BenchmarkSolve-12 1000000000 0.001757 ns/op
|
||||
BenchmarkSolve-12 1000000000 0.001747 ns/op
|
||||
BenchmarkSolve-12 1000000000 0.001807 ns/op
|
||||
BenchmarkSolve-12 1000000000 0.001720 ns/op
|
||||
BenchmarkSolve-12 1000000000 0.001692 ns/op
|
||||
ok github.com/onyx-and-iris/aoc2024/day-17 0.139s
|
41
day-17/cmd/cli/main.go
Normal file
41
day-17/cmd/cli/main.go
Normal file
@ -0,0 +1,41 @@
|
||||
/********************************************************************************
|
||||
Advent of Code 2024 - day-17
|
||||
********************************************************************************/
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"flag"
|
||||
"fmt"
|
||||
"slices"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
||||
problems "github.com/onyx-and-iris/aoc2024/day-17"
|
||||
)
|
||||
|
||||
//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: %s\nsolution two: %d\n", one, two)
|
||||
}
|
7
day-17/go.mod
Normal file
7
day-17/go.mod
Normal file
@ -0,0 +1,7 @@
|
||||
module github.com/onyx-and-iris/aoc2024/day-17
|
||||
|
||||
go 1.23.3
|
||||
|
||||
require github.com/sirupsen/logrus v1.9.3
|
||||
|
||||
require golang.org/x/sys v0.12.0 // indirect
|
16
day-17/go.sum
Normal file
16
day-17/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 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
|
||||
golang.org/x/sys v0.12.0/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=
|
6
day-17/internal/one/benchmark
Normal file
6
day-17/internal/one/benchmark
Normal file
@ -0,0 +1,6 @@
|
||||
goos: linux
|
||||
goarch: amd64
|
||||
pkg: github.com/onyx-and-iris/aoc2024/day-17/internal/one
|
||||
cpu: Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz
|
||||
BenchmarkSolve-12 1000000000 0.0000459 ns/op
|
||||
ok github.com/onyx-and-iris/aoc2024/day-17/internal/one 0.006s
|
108
day-17/internal/one/computer.go
Normal file
108
day-17/internal/one/computer.go
Normal file
@ -0,0 +1,108 @@
|
||||
package one
|
||||
|
||||
import (
|
||||
"math"
|
||||
)
|
||||
|
||||
type computer struct {
|
||||
registers map[string]int
|
||||
instructionMap map[int]instructionFn
|
||||
}
|
||||
|
||||
func newComputer(registers map[string]int) *computer {
|
||||
c := &computer{
|
||||
registers: registers,
|
||||
}
|
||||
|
||||
c.instructionMap = map[int]instructionFn{
|
||||
0: c.adv,
|
||||
1: c.bxl,
|
||||
2: c.bst,
|
||||
3: c.jnz,
|
||||
4: c.bxc,
|
||||
5: c.out,
|
||||
6: c.bdv,
|
||||
7: c.cdv,
|
||||
}
|
||||
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *computer) run(program []int) []int {
|
||||
results := []int{}
|
||||
var ptr int
|
||||
for ptr < len(program) {
|
||||
opcode := program[ptr]
|
||||
jump, doJump, out, doStore := c.performOperation(c.instructionMap[opcode], operand(program[ptr+1]))
|
||||
if doJump {
|
||||
ptr = jump
|
||||
} else {
|
||||
ptr += 2
|
||||
}
|
||||
if doStore {
|
||||
results = append(results, out)
|
||||
}
|
||||
}
|
||||
return results
|
||||
}
|
||||
|
||||
func (c *computer) performOperation(fn instructionFn, operand operand) (int, bool, int, bool) {
|
||||
return fn(operand)
|
||||
}
|
||||
|
||||
func (c *computer) adv(operand operand) (int, bool, int, bool) {
|
||||
c.registers["A"] /= int(math.Pow(2, float64(c.combo(operand))))
|
||||
return 0, false, 0, false
|
||||
}
|
||||
|
||||
func (c *computer) bxl(operand operand) (int, bool, int, bool) {
|
||||
c.registers["B"] ^= int(operand)
|
||||
return 0, false, 0, false
|
||||
}
|
||||
|
||||
func (c *computer) bst(operand operand) (int, bool, int, bool) {
|
||||
c.registers["B"] = c.combo(operand) % 8
|
||||
return 0, false, 0, false
|
||||
}
|
||||
|
||||
func (c *computer) jnz(operand operand) (int, bool, int, bool) {
|
||||
if c.registers["A"] == 0 {
|
||||
return 0, false, 0, false
|
||||
}
|
||||
|
||||
return int(operand), true, 0, false
|
||||
}
|
||||
|
||||
func (c *computer) bxc(operand operand) (int, bool, int, bool) {
|
||||
c.registers["B"] ^= c.registers["C"]
|
||||
return 0, false, 0, false
|
||||
}
|
||||
|
||||
func (c *computer) out(operand operand) (int, bool, int, bool) {
|
||||
return 0, false, c.combo(operand) % 8, true
|
||||
}
|
||||
|
||||
func (c *computer) bdv(operand operand) (int, bool, int, bool) {
|
||||
c.registers["B"] = c.registers["A"] / int(math.Pow(2, float64(c.combo(operand))))
|
||||
return 0, false, 0, false
|
||||
}
|
||||
|
||||
func (c *computer) cdv(operand operand) (int, bool, int, bool) {
|
||||
c.registers["C"] = c.registers["A"] / int(math.Pow(2, float64(c.combo(operand))))
|
||||
return 0, false, 0, false
|
||||
}
|
||||
|
||||
func (c *computer) combo(operand operand) int {
|
||||
switch operand {
|
||||
case 0, 1, 2, 3:
|
||||
return int(operand)
|
||||
case 4:
|
||||
return c.registers["A"]
|
||||
case 5:
|
||||
return c.registers["B"]
|
||||
case 6:
|
||||
return c.registers["C"]
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
}
|
94
day-17/internal/one/computer_internal_test.go
Normal file
94
day-17/internal/one/computer_internal_test.go
Normal file
@ -0,0 +1,94 @@
|
||||
package one
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestComputerRunProgram1(t *testing.T) {
|
||||
tt := map[string]struct {
|
||||
registers map[string]int
|
||||
program []int
|
||||
}{
|
||||
"register C contains 9 with program 2,6, should set register B to 1": {
|
||||
registers: map[string]int{
|
||||
"A": 0,
|
||||
"B": 0,
|
||||
"C": 9,
|
||||
},
|
||||
program: []int{2, 6},
|
||||
},
|
||||
}
|
||||
|
||||
c := newComputer(nil)
|
||||
for name, tc := range tt {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
c.registers = tc.registers
|
||||
c.run(tc.program)
|
||||
if c.registers["B"] != 1 {
|
||||
t.Errorf("register B got: %v, expected %v", c.registers["B"], 1)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestComputerRunProgram2(t *testing.T) {
|
||||
tt := map[string]struct {
|
||||
registers map[string]int
|
||||
program []int
|
||||
expected []int
|
||||
}{
|
||||
"register A contains 10 with program 5,0,5,1,5,4, should output 0,1,2": {
|
||||
registers: map[string]int{
|
||||
"A": 10,
|
||||
"B": 0,
|
||||
"C": 0,
|
||||
},
|
||||
program: []int{5, 0, 5, 1, 5, 4},
|
||||
expected: []int{0, 1, 2},
|
||||
},
|
||||
}
|
||||
|
||||
c := newComputer(nil)
|
||||
for name, tc := range tt {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
c.registers = tc.registers
|
||||
got := c.run(tc.program)
|
||||
if !slices.Equal(got, tc.expected) {
|
||||
t.Errorf("output: %q, expected %q", got, tc.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestComputerRunProgram3(t *testing.T) {
|
||||
tt := map[string]struct {
|
||||
registers map[string]int
|
||||
program []int
|
||||
expected []int
|
||||
}{
|
||||
"register A contains 2024 with program 0, 1, 5, 4, 3, 0, should leave 0 in register A and output 4, 2, 5, 6, 7, 7, 7, 7, 3, 1, 0": {
|
||||
registers: map[string]int{
|
||||
"A": 2024,
|
||||
"B": 0,
|
||||
"C": 0,
|
||||
},
|
||||
program: []int{0, 1, 5, 4, 3, 0},
|
||||
expected: []int{4, 2, 5, 6, 7, 7, 7, 7, 3, 1, 0},
|
||||
},
|
||||
}
|
||||
|
||||
c := newComputer(nil)
|
||||
for name, tc := range tt {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
c.registers = tc.registers
|
||||
got := c.run(tc.program)
|
||||
if c.registers["A"] != 0 {
|
||||
t.Errorf("register A got: %v, expected %v", c.registers["A"], 0)
|
||||
}
|
||||
if !slices.Equal(got, tc.expected) {
|
||||
t.Errorf("output got: %v, expected %v", got, tc.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
31
day-17/internal/one/solve.go
Normal file
31
day-17/internal/one/solve.go
Normal file
@ -0,0 +1,31 @@
|
||||
package one
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type operand int
|
||||
type instructionFn func(operand) (int, bool, int, bool)
|
||||
|
||||
func Solve(buf []byte) (string, error) {
|
||||
r := bytes.NewReader(buf)
|
||||
registers, program, err := parseLines(r)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
computer := newComputer(registers)
|
||||
results := computer.run(program)
|
||||
|
||||
var sb strings.Builder
|
||||
for i, n := range results {
|
||||
sb.WriteString(strconv.Itoa(n))
|
||||
if i < len(results)-1 {
|
||||
sb.WriteRune(',')
|
||||
}
|
||||
}
|
||||
|
||||
return sb.String(), nil
|
||||
}
|
15
day-17/internal/one/solve_internal_test.go
Normal file
15
day-17/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)
|
||||
}
|
49
day-17/internal/one/util.go
Normal file
49
day-17/internal/one/util.go
Normal file
@ -0,0 +1,49 @@
|
||||
package one
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"io"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
reRegister = regexp.MustCompile(`Register (?P<identifier>[A-Z]): (?P<value>[0-9]+)`)
|
||||
reProgram = regexp.MustCompile(`Program: (?P<instructions>[0-9,]+)`)
|
||||
)
|
||||
|
||||
func parseLines(r io.Reader) (map[string]int, []int, error) {
|
||||
registers := make(map[string]int)
|
||||
var instructions []int
|
||||
|
||||
scanner := bufio.NewScanner(r)
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
|
||||
switch line := line; {
|
||||
case reRegister.MatchString(line):
|
||||
m := reRegister.FindStringSubmatch(line)
|
||||
registers[m[1]] = mustConv(m[2])
|
||||
case reProgram.MatchString(line):
|
||||
m := reProgram.FindStringSubmatch(line)
|
||||
for _, n := range strings.Split(m[1], ",") {
|
||||
instructions = append(instructions, mustConv(n))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if err := scanner.Err(); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
return registers, instructions, nil
|
||||
}
|
||||
|
||||
func mustConv(s string) int {
|
||||
n, err := strconv.Atoi(s)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return n
|
||||
}
|
6
day-17/internal/two/benchmark
Normal file
6
day-17/internal/two/benchmark
Normal file
@ -0,0 +1,6 @@
|
||||
goos: linux
|
||||
goarch: amd64
|
||||
pkg: github.com/onyx-and-iris/aoc2024/day-17/internal/two
|
||||
cpu: Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz
|
||||
BenchmarkSolve-12 1000000000 0.001878 ns/op
|
||||
ok github.com/onyx-and-iris/aoc2024/day-17/internal/two 0.017s
|
114
day-17/internal/two/computer.go
Normal file
114
day-17/internal/two/computer.go
Normal file
@ -0,0 +1,114 @@
|
||||
package two
|
||||
|
||||
import (
|
||||
"math"
|
||||
)
|
||||
|
||||
type computer struct {
|
||||
registers map[string]int64
|
||||
instructionMap map[int64]instructionFn
|
||||
}
|
||||
|
||||
func newComputer(registers map[string]int64) *computer {
|
||||
c := &computer{
|
||||
registers: registers,
|
||||
}
|
||||
|
||||
c.instructionMap = map[int64]instructionFn{
|
||||
0: c.adv,
|
||||
1: c.bxl,
|
||||
2: c.bst,
|
||||
3: c.jnz,
|
||||
4: c.bxc,
|
||||
5: c.out,
|
||||
6: c.bdv,
|
||||
7: c.cdv,
|
||||
}
|
||||
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *computer) bumpRegisters(next int64) {
|
||||
c.registers["A"] = next
|
||||
c.registers["B"] = 0
|
||||
c.registers["C"] = 0
|
||||
}
|
||||
|
||||
func (c *computer) run(program []int64) []int64 {
|
||||
results := []int64{}
|
||||
var ptr int
|
||||
for ptr < len(program) {
|
||||
opcode := program[ptr]
|
||||
jump, doJump, out, doStore := c.performOperation(c.instructionMap[opcode], operand(program[ptr+1]))
|
||||
if doJump {
|
||||
ptr = jump
|
||||
} else {
|
||||
ptr += 2
|
||||
}
|
||||
if doStore {
|
||||
results = append(results, out)
|
||||
}
|
||||
}
|
||||
return results
|
||||
}
|
||||
|
||||
func (c *computer) performOperation(fn instructionFn, operand operand) (int, bool, int64, bool) {
|
||||
return fn(operand)
|
||||
}
|
||||
|
||||
func (c *computer) adv(operand operand) (int, bool, int64, bool) {
|
||||
c.registers["A"] /= int64(math.Pow(2, float64(c.combo(operand))))
|
||||
return 0, false, 0, false
|
||||
}
|
||||
|
||||
func (c *computer) bxl(operand operand) (int, bool, int64, bool) {
|
||||
c.registers["B"] ^= int64(operand)
|
||||
return 0, false, 0, false
|
||||
}
|
||||
|
||||
func (c *computer) bst(operand operand) (int, bool, int64, bool) {
|
||||
c.registers["B"] = c.combo(operand) % 8
|
||||
return 0, false, 0, false
|
||||
}
|
||||
|
||||
func (c *computer) jnz(operand operand) (int, bool, int64, bool) {
|
||||
if c.registers["A"] == 0 {
|
||||
return 0, false, 0, false
|
||||
}
|
||||
|
||||
return int(operand), true, 0, false
|
||||
}
|
||||
|
||||
func (c *computer) bxc(operand operand) (int, bool, int64, bool) {
|
||||
c.registers["B"] ^= c.registers["C"]
|
||||
return 0, false, 0, false
|
||||
}
|
||||
|
||||
func (c *computer) out(operand operand) (int, bool, int64, bool) {
|
||||
return 0, false, c.combo(operand) % 8, true
|
||||
}
|
||||
|
||||
func (c *computer) bdv(operand operand) (int, bool, int64, bool) {
|
||||
c.registers["B"] = c.registers["A"] / int64(math.Pow(2, float64(c.combo(operand))))
|
||||
return 0, false, 0, false
|
||||
}
|
||||
|
||||
func (c *computer) cdv(operand operand) (int, bool, int64, bool) {
|
||||
c.registers["C"] = c.registers["A"] / int64(math.Pow(2, float64(c.combo(operand))))
|
||||
return 0, false, 0, false
|
||||
}
|
||||
|
||||
func (c *computer) combo(operand operand) int64 {
|
||||
switch operand {
|
||||
case 0, 1, 2, 3:
|
||||
return int64(operand)
|
||||
case 4:
|
||||
return int64(c.registers["A"])
|
||||
case 5:
|
||||
return int64(c.registers["B"])
|
||||
case 6:
|
||||
return int64(c.registers["C"])
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
}
|
33
day-17/internal/two/computer_internal_test.go
Normal file
33
day-17/internal/two/computer_internal_test.go
Normal file
@ -0,0 +1,33 @@
|
||||
package two
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestComputerRunProgram1(t *testing.T) {
|
||||
tt := map[string]struct {
|
||||
registers map[string]int64
|
||||
program []int64
|
||||
}{
|
||||
"register C contains 9 with program 2,6, should set register B to 1": {
|
||||
registers: map[string]int64{
|
||||
"A": 117440,
|
||||
"B": 0,
|
||||
"C": 0,
|
||||
},
|
||||
program: []int64{0, 3, 5, 4, 3, 0},
|
||||
},
|
||||
}
|
||||
|
||||
c := newComputer(nil)
|
||||
for name, tc := range tt {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
c.registers = tc.registers
|
||||
got := c.run(tc.program)
|
||||
if !slices.Equal(got, tc.program) {
|
||||
t.Errorf("output: %q, expected %q", got, tc.program)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
57
day-17/internal/two/solve.go
Normal file
57
day-17/internal/two/solve.go
Normal file
@ -0,0 +1,57 @@
|
||||
package two
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"math"
|
||||
"slices"
|
||||
)
|
||||
|
||||
type operand int
|
||||
type instructionFn func(operand) (int, bool, int64, bool)
|
||||
|
||||
func Solve(buf []byte) (int64, error) {
|
||||
r := bytes.NewReader(buf)
|
||||
_, program, err := parseLines(r)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
computer := newComputer(make(map[string]int64))
|
||||
a := lowestA(program, computer)
|
||||
|
||||
return a, nil
|
||||
}
|
||||
|
||||
func lowestA(program []int64, computer *computer) int64 {
|
||||
var a int64 = 1
|
||||
computer.bumpRegisters(a)
|
||||
var results []int64
|
||||
for {
|
||||
results = computer.run(program)
|
||||
|
||||
if slices.Equal(results, program) {
|
||||
return a
|
||||
}
|
||||
|
||||
if len(program) > len(results) {
|
||||
a *= 2
|
||||
computer.bumpRegisters(a)
|
||||
continue
|
||||
}
|
||||
|
||||
if len(program) == len(results) {
|
||||
for j := len(program) - 1; j >= 0; j-- {
|
||||
if program[j] != results[j] {
|
||||
a += int64(math.Pow(8, float64(j)))
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(program) < len(results) {
|
||||
a /= 2
|
||||
}
|
||||
|
||||
computer.bumpRegisters(a)
|
||||
}
|
||||
}
|
15
day-17/internal/two/solve_internal_test.go
Normal file
15
day-17/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)
|
||||
}
|
49
day-17/internal/two/util.go
Normal file
49
day-17/internal/two/util.go
Normal file
@ -0,0 +1,49 @@
|
||||
package two
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"io"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
reRegister = regexp.MustCompile(`Register (?P<identifier>[A-Z]): (?P<value>[0-9]+)`)
|
||||
reProgram = regexp.MustCompile(`Program: (?P<instructions>[0-9,]+)`)
|
||||
)
|
||||
|
||||
func parseLines(r io.Reader) (map[string]int64, []int64, error) {
|
||||
registers := make(map[string]int64)
|
||||
var instructions []int64
|
||||
|
||||
scanner := bufio.NewScanner(r)
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
|
||||
switch line := line; {
|
||||
case reRegister.MatchString(line):
|
||||
m := reRegister.FindStringSubmatch(line)
|
||||
registers[m[1]] = mustConv(m[2])
|
||||
case reProgram.MatchString(line):
|
||||
m := reProgram.FindStringSubmatch(line)
|
||||
for _, n := range strings.Split(m[1], ",") {
|
||||
instructions = append(instructions, mustConv(n))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if err := scanner.Err(); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
return registers, instructions, nil
|
||||
}
|
||||
|
||||
func mustConv(s string) int64 {
|
||||
n, err := strconv.Atoi(s)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return int64(n)
|
||||
}
|
30
day-17/makefile
Normal file
30
day-17/makefile
Normal file
@ -0,0 +1,30 @@
|
||||
program = day-17
|
||||
|
||||
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-17/solve.go
Normal file
20
day-17/solve.go
Normal file
@ -0,0 +1,20 @@
|
||||
package dayseventeen
|
||||
|
||||
import (
|
||||
"github.com/onyx-and-iris/aoc2024/day-17/internal/one"
|
||||
"github.com/onyx-and-iris/aoc2024/day-17/internal/two"
|
||||
)
|
||||
|
||||
func Solve(buf []byte) (string, int64, error) {
|
||||
answerOne, err := one.Solve(buf)
|
||||
if err != nil {
|
||||
return "", 0, err
|
||||
}
|
||||
|
||||
answerTwo, err := two.Solve(buf)
|
||||
if err != nil {
|
||||
return "", 0, err
|
||||
}
|
||||
|
||||
return answerOne, answerTwo, nil
|
||||
}
|
15
day-17/solve_internal_test.go
Normal file
15
day-17/solve_internal_test.go
Normal file
@ -0,0 +1,15 @@
|
||||
package dayseventeen
|
||||
|
||||
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…
x
Reference in New Issue
Block a user