mirror of
https://github.com/onyx-and-iris/aoc2024.git
synced 2025-01-09 14:20:48 +00:00
add day-16 part2 + benchmarks
This commit is contained in:
parent
9d7a9d5791
commit
17f2bc8223
1
day-16/.gitignore
vendored
1
day-16/.gitignore
vendored
@ -1 +0,0 @@
|
||||
internal/two
|
15
day-16/benchmark
Normal file
15
day-16/benchmark
Normal file
@ -0,0 +1,15 @@
|
||||
goos: linux
|
||||
goarch: amd64
|
||||
pkg: github.com/onyx-and-iris/aoc2024/day-16
|
||||
cpu: Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz
|
||||
BenchmarkSolve-12 1000000000 0.4444 ns/op
|
||||
BenchmarkSolve-12 1000000000 0.4477 ns/op
|
||||
BenchmarkSolve-12 1000000000 0.4652 ns/op
|
||||
BenchmarkSolve-12 1000000000 0.4661 ns/op
|
||||
BenchmarkSolve-12 1000000000 0.4580 ns/op
|
||||
BenchmarkSolve-12 1000000000 0.4727 ns/op
|
||||
BenchmarkSolve-12 1000000000 0.4466 ns/op
|
||||
BenchmarkSolve-12 1000000000 0.4522 ns/op
|
||||
BenchmarkSolve-12 1000000000 0.4610 ns/op
|
||||
BenchmarkSolve-12 1000000000 0.4622 ns/op
|
||||
ok github.com/onyx-and-iris/aoc2024/day-16 110.890s
|
@ -4,4 +4,7 @@ go 1.23.3
|
||||
|
||||
require github.com/sirupsen/logrus v1.9.3
|
||||
|
||||
require golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
|
||||
require (
|
||||
github.com/stretchr/testify v1.8.1 // indirect
|
||||
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
|
||||
)
|
||||
|
@ -6,10 +6,16 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
|
||||
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/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/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 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
|
||||
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
||||
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-16/internal/one/benchmark
Normal file
6
day-16/internal/one/benchmark
Normal file
@ -0,0 +1,6 @@
|
||||
goos: linux
|
||||
goarch: amd64
|
||||
pkg: github.com/onyx-and-iris/aoc2024/day-16/internal/one
|
||||
cpu: Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz
|
||||
BenchmarkSolve-12 1000000000 0.01081 ns/op
|
||||
ok github.com/onyx-and-iris/aoc2024/day-16/internal/one 0.078s
|
@ -10,8 +10,6 @@ import (
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
const turnCost int = 1000
|
||||
|
||||
type graph struct {
|
||||
start node
|
||||
end node
|
||||
@ -36,6 +34,7 @@ func (g *graph) dijkstra() (int, error) {
|
||||
visited := make(map[node]struct{})
|
||||
costs := make(map[node]int)
|
||||
prev := make(map[node]node)
|
||||
const turnCost int = 1000
|
||||
|
||||
for heap.Len() > 0 {
|
||||
current := hp.Pop(heap).(move)
|
||||
|
6
day-16/internal/two/benchmark
Normal file
6
day-16/internal/two/benchmark
Normal file
@ -0,0 +1,6 @@
|
||||
goos: linux
|
||||
goarch: amd64
|
||||
pkg: github.com/onyx-and-iris/aoc2024/day-16/internal/two
|
||||
cpu: Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz
|
||||
BenchmarkSolve-12 1000000000 0.4555 ns/op
|
||||
ok github.com/onyx-and-iris/aoc2024/day-16/internal/two 10.170s
|
91
day-16/internal/two/graph.go
Normal file
91
day-16/internal/two/graph.go
Normal file
@ -0,0 +1,91 @@
|
||||
package two
|
||||
|
||||
import (
|
||||
hp "container/heap"
|
||||
"math"
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type graph struct {
|
||||
start node
|
||||
end node
|
||||
data []string
|
||||
}
|
||||
|
||||
func newGraph() *graph {
|
||||
return &graph{}
|
||||
}
|
||||
|
||||
func (g *graph) String() string {
|
||||
return strings.Join(g.data, "\n")
|
||||
}
|
||||
|
||||
func (g *graph) valueAt(n node) rune {
|
||||
return rune(g.data[n.y][n.x])
|
||||
}
|
||||
|
||||
func (g *graph) dijkstra() int {
|
||||
heap := newHeap()
|
||||
hp.Push(heap, move{g.start, 0, []node{g.start}})
|
||||
visited := make(map[node]int)
|
||||
const turnCost int = 1000
|
||||
lowestCost := math.MaxInt
|
||||
|
||||
bestPaths := [][]node{}
|
||||
for heap.Len() > 0 {
|
||||
current := hp.Pop(heap).(move)
|
||||
|
||||
// we're on a path that's already exceeded the lowest cost
|
||||
if current.cost > lowestCost {
|
||||
continue
|
||||
}
|
||||
|
||||
if g.valueAt(current.node) == 'E' {
|
||||
bestPaths = append(bestPaths, current.path)
|
||||
lowestCost = current.cost
|
||||
continue
|
||||
}
|
||||
|
||||
if v, ok := visited[current.node]; ok {
|
||||
if v < current.cost {
|
||||
continue
|
||||
}
|
||||
}
|
||||
visited[current.node] = current.cost
|
||||
|
||||
for _, n := range neighbours(current.node) {
|
||||
if g.valueAt(current.node) == '#' {
|
||||
continue
|
||||
}
|
||||
|
||||
next_cost := current.cost + 1
|
||||
if n.direction != current.node.direction {
|
||||
next_cost += turnCost
|
||||
}
|
||||
|
||||
hp.Push(heap, newMove(n, next_cost, append(slices.Clone(current.path), n)))
|
||||
}
|
||||
}
|
||||
|
||||
possibleSafe := make(map[coords]struct{})
|
||||
for _, path := range bestPaths {
|
||||
for _, n := range path {
|
||||
possibleSafe[n.coords] = struct{}{}
|
||||
}
|
||||
}
|
||||
|
||||
log.Debugf("\n%s\n", g.debug(possibleSafe))
|
||||
|
||||
return len(possibleSafe)
|
||||
}
|
||||
|
||||
func (g *graph) debug(possibleSafe map[coords]struct{}) string {
|
||||
temp := slices.Clone(g.data)
|
||||
for n := range possibleSafe {
|
||||
temp[n.y] = replaceAtIndex(temp[n.y], 'O', n.x)
|
||||
}
|
||||
return strings.Join(temp, "\n")
|
||||
}
|
31
day-16/internal/two/heap.go
Normal file
31
day-16/internal/two/heap.go
Normal file
@ -0,0 +1,31 @@
|
||||
package two
|
||||
|
||||
type minPath []move
|
||||
|
||||
func newHeap() *minPath {
|
||||
return &minPath{}
|
||||
}
|
||||
|
||||
func (h minPath) Len() int {
|
||||
return len(h)
|
||||
}
|
||||
|
||||
func (h minPath) Less(i, j int) bool {
|
||||
return h[i].cost < h[j].cost
|
||||
}
|
||||
|
||||
func (h minPath) Swap(i, j int) {
|
||||
h[i], h[j] = h[j], h[i]
|
||||
}
|
||||
|
||||
func (h *minPath) Push(x interface{}) {
|
||||
*h = append(*h, x.(move))
|
||||
}
|
||||
|
||||
func (h *minPath) Pop() interface{} {
|
||||
old := *h
|
||||
n := len(old)
|
||||
x := old[n-1]
|
||||
*h = old[:n-1]
|
||||
return x
|
||||
}
|
11
day-16/internal/two/move.go
Normal file
11
day-16/internal/two/move.go
Normal file
@ -0,0 +1,11 @@
|
||||
package two
|
||||
|
||||
type move struct {
|
||||
node node
|
||||
cost int
|
||||
path []node
|
||||
}
|
||||
|
||||
func newMove(n node, cost int, path []node) move {
|
||||
return move{n, cost, path}
|
||||
}
|
32
day-16/internal/two/neighbours.go
Normal file
32
day-16/internal/two/neighbours.go
Normal file
@ -0,0 +1,32 @@
|
||||
package two
|
||||
|
||||
func neighbours(n node) [3]node {
|
||||
switch n.direction {
|
||||
case N:
|
||||
return [3]node{
|
||||
newNode(n.x, n.y-1, N),
|
||||
newNode(n.x-1, n.y, W),
|
||||
newNode(n.x+1, n.y, E),
|
||||
}
|
||||
case E:
|
||||
return [3]node{
|
||||
newNode(n.x+1, n.y, E),
|
||||
newNode(n.x, n.y-1, N),
|
||||
newNode(n.x, n.y+1, S),
|
||||
}
|
||||
case S:
|
||||
return [3]node{
|
||||
newNode(n.x, n.y+1, S),
|
||||
newNode(n.x-1, n.y, W),
|
||||
newNode(n.x+1, n.y, E),
|
||||
}
|
||||
case W:
|
||||
return [3]node{
|
||||
newNode(n.x-1, n.y, W),
|
||||
newNode(n.x, n.y+1, S),
|
||||
newNode(n.x, n.y-1, N),
|
||||
}
|
||||
default:
|
||||
return [3]node{}
|
||||
}
|
||||
}
|
24
day-16/internal/two/node.go
Normal file
24
day-16/internal/two/node.go
Normal file
@ -0,0 +1,24 @@
|
||||
package two
|
||||
|
||||
type direction int
|
||||
|
||||
const (
|
||||
N direction = iota
|
||||
E
|
||||
S
|
||||
W
|
||||
)
|
||||
|
||||
type coords struct {
|
||||
x int
|
||||
y int
|
||||
}
|
||||
|
||||
type node struct {
|
||||
coords
|
||||
direction direction
|
||||
}
|
||||
|
||||
func newNode(x, y int, dir direction) node {
|
||||
return node{coords{x, y}, dir}
|
||||
}
|
17
day-16/internal/two/solve.go
Normal file
17
day-16/internal/two/solve.go
Normal file
@ -0,0 +1,17 @@
|
||||
package two
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
)
|
||||
|
||||
func Solve(buf []byte) (int, error) {
|
||||
r := bytes.NewReader(buf)
|
||||
graph, err := parseLines(r)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
possibleSafe := graph.dijkstra()
|
||||
|
||||
return possibleSafe, nil
|
||||
}
|
15
day-16/internal/two/solve_internal_test.go
Normal file
15
day-16/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)
|
||||
}
|
52
day-16/internal/two/util.go
Normal file
52
day-16/internal/two/util.go
Normal file
@ -0,0 +1,52 @@
|
||||
package two
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"io"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func parseLines(r io.Reader) (*graph, error) {
|
||||
graph := newGraph()
|
||||
|
||||
var linecount int
|
||||
scanner := bufio.NewScanner(r)
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
|
||||
r, indx := indxForAny(line, []rune{'S', 'E'})
|
||||
if indx != -1 {
|
||||
if r == 'S' {
|
||||
graph.start = newNode(indx, linecount, E)
|
||||
} else {
|
||||
graph.end = newNode(indx, linecount, 0)
|
||||
}
|
||||
}
|
||||
|
||||
graph.data = append(graph.data, line)
|
||||
|
||||
linecount++
|
||||
}
|
||||
|
||||
if err := scanner.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return graph, nil
|
||||
}
|
||||
|
||||
func indxForAny(s string, runes []rune) (rune, int) {
|
||||
for _, r := range runes {
|
||||
indx := strings.Index(s, string(r))
|
||||
if indx != -1 {
|
||||
return r, indx
|
||||
}
|
||||
}
|
||||
return 0, -1
|
||||
}
|
||||
|
||||
func replaceAtIndex(s string, r rune, i int) string {
|
||||
out := []rune(s)
|
||||
out[i] = r
|
||||
return string(out)
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package name
|
||||
package daysixteen
|
||||
|
||||
import (
|
||||
"github.com/onyx-and-iris/aoc2024/day-16/internal/one"
|
||||
|
15
day-16/solve_internal_test.go
Normal file
15
day-16/solve_internal_test.go
Normal file
@ -0,0 +1,15 @@
|
||||
package daysixteen
|
||||
|
||||
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