add day-16 part2 + benchmarks

This commit is contained in:
2024-12-19 01:27:45 +00:00
parent 9d7a9d5791
commit 17f2bc8223
17 changed files with 329 additions and 7 deletions

View 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

View File

@@ -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)

View 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

View 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")
}

View 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
}

View 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}
}

View 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{}
}
}

View 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}
}

View 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
}

View 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)
}

View 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)
}