From 17f2bc822385dc1331a29d1be168d6962b3f334b Mon Sep 17 00:00:00 2001 From: onyx-and-iris Date: Thu, 19 Dec 2024 01:27:45 +0000 Subject: [PATCH] add day-16 part2 + benchmarks --- day-16/.gitignore | 1 - day-16/benchmark | 15 ++++ day-16/go.mod | 5 +- day-16/go.sum | 10 ++- day-16/internal/one/benchmark | 6 ++ day-16/internal/one/graph.go | 3 +- day-16/internal/two/benchmark | 6 ++ day-16/internal/two/graph.go | 91 ++++++++++++++++++++++ day-16/internal/two/heap.go | 31 ++++++++ day-16/internal/two/move.go | 11 +++ day-16/internal/two/neighbours.go | 32 ++++++++ day-16/internal/two/node.go | 24 ++++++ day-16/internal/two/solve.go | 17 ++++ day-16/internal/two/solve_internal_test.go | 15 ++++ day-16/internal/two/util.go | 52 +++++++++++++ day-16/solve.go | 2 +- day-16/solve_internal_test.go | 15 ++++ 17 files changed, 329 insertions(+), 7 deletions(-) delete mode 100644 day-16/.gitignore create mode 100644 day-16/benchmark create mode 100644 day-16/internal/one/benchmark create mode 100644 day-16/internal/two/benchmark create mode 100644 day-16/internal/two/graph.go create mode 100644 day-16/internal/two/heap.go create mode 100644 day-16/internal/two/move.go create mode 100644 day-16/internal/two/neighbours.go create mode 100644 day-16/internal/two/node.go create mode 100644 day-16/internal/two/solve.go create mode 100644 day-16/internal/two/solve_internal_test.go create mode 100644 day-16/internal/two/util.go create mode 100644 day-16/solve_internal_test.go diff --git a/day-16/.gitignore b/day-16/.gitignore deleted file mode 100644 index 780f6b0..0000000 --- a/day-16/.gitignore +++ /dev/null @@ -1 +0,0 @@ -internal/two \ No newline at end of file diff --git a/day-16/benchmark b/day-16/benchmark new file mode 100644 index 0000000..1cbb01f --- /dev/null +++ b/day-16/benchmark @@ -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 diff --git a/day-16/go.mod b/day-16/go.mod index 390622e..a7005a0 100644 --- a/day-16/go.mod +++ b/day-16/go.mod @@ -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 +) diff --git a/day-16/go.sum b/day-16/go.sum index 21f9bfb..c8a8bfb 100644 --- a/day-16/go.sum +++ b/day-16/go.sum @@ -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= diff --git a/day-16/internal/one/benchmark b/day-16/internal/one/benchmark new file mode 100644 index 0000000..66dbd3a --- /dev/null +++ b/day-16/internal/one/benchmark @@ -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 diff --git a/day-16/internal/one/graph.go b/day-16/internal/one/graph.go index 69130c7..7af90c5 100644 --- a/day-16/internal/one/graph.go +++ b/day-16/internal/one/graph.go @@ -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) diff --git a/day-16/internal/two/benchmark b/day-16/internal/two/benchmark new file mode 100644 index 0000000..fd34487 --- /dev/null +++ b/day-16/internal/two/benchmark @@ -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 diff --git a/day-16/internal/two/graph.go b/day-16/internal/two/graph.go new file mode 100644 index 0000000..30519e2 --- /dev/null +++ b/day-16/internal/two/graph.go @@ -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") +} diff --git a/day-16/internal/two/heap.go b/day-16/internal/two/heap.go new file mode 100644 index 0000000..5c1bb1d --- /dev/null +++ b/day-16/internal/two/heap.go @@ -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 +} diff --git a/day-16/internal/two/move.go b/day-16/internal/two/move.go new file mode 100644 index 0000000..855fc9f --- /dev/null +++ b/day-16/internal/two/move.go @@ -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} +} diff --git a/day-16/internal/two/neighbours.go b/day-16/internal/two/neighbours.go new file mode 100644 index 0000000..60fdf68 --- /dev/null +++ b/day-16/internal/two/neighbours.go @@ -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{} + } +} diff --git a/day-16/internal/two/node.go b/day-16/internal/two/node.go new file mode 100644 index 0000000..70ec0a5 --- /dev/null +++ b/day-16/internal/two/node.go @@ -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} +} diff --git a/day-16/internal/two/solve.go b/day-16/internal/two/solve.go new file mode 100644 index 0000000..6727ca9 --- /dev/null +++ b/day-16/internal/two/solve.go @@ -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 +} diff --git a/day-16/internal/two/solve_internal_test.go b/day-16/internal/two/solve_internal_test.go new file mode 100644 index 0000000..ecb05d5 --- /dev/null +++ b/day-16/internal/two/solve_internal_test.go @@ -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) +} diff --git a/day-16/internal/two/util.go b/day-16/internal/two/util.go new file mode 100644 index 0000000..ef35f87 --- /dev/null +++ b/day-16/internal/two/util.go @@ -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) +} diff --git a/day-16/solve.go b/day-16/solve.go index 517e34b..e71d0f8 100644 --- a/day-16/solve.go +++ b/day-16/solve.go @@ -1,4 +1,4 @@ -package name +package daysixteen import ( "github.com/onyx-and-iris/aoc2024/day-16/internal/one" diff --git a/day-16/solve_internal_test.go b/day-16/solve_internal_test.go new file mode 100644 index 0000000..21283c1 --- /dev/null +++ b/day-16/solve_internal_test.go @@ -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) +}