add point subpackage

build ShortestPath as ordered map

replace dijkstra in part two with a bfs
This commit is contained in:
2024-12-19 21:32:21 +00:00
parent 62665d4d10
commit 22b442171b
14 changed files with 111 additions and 105 deletions

View File

@@ -2,5 +2,5 @@ goos: linux
goarch: amd64
pkg: github.com/onyx-and-iris/aoc2024/day-18/internal/one
cpu: Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz
BenchmarkSolve-12 1000000000 0.004050 ns/op
ok github.com/onyx-and-iris/aoc2024/day-18/internal/one 0.029s
BenchmarkSolve-12 1000000000 0.003678 ns/op
ok github.com/onyx-and-iris/aoc2024/day-18/internal/one 0.030s

View File

@@ -3,11 +3,14 @@ package one
import (
"slices"
"strings"
"github.com/onyx-and-iris/aoc2024/day-18/internal/point"
orderedmap "github.com/wk8/go-ordered-map/v2"
)
type graph struct {
start Point
end Point
start point.Point
end point.Point
data []string
}
@@ -26,25 +29,26 @@ func newGraph(width, height, numCorruptions int, corruptedCoords [][]int) *graph
data[coords[1]] = replaceAtIndex(data[coords[1]], '#', coords[0])
}
return &graph{Point{0, 0}, Point{len(data[0]) - 1, len(data) - 1}, data}
return &graph{point.Point{X: 0, Y: 0}, point.Point{X: len(data[0]) - 1, Y: len(data) - 1}, data}
}
func (g *graph) String() string {
return strings.Join(g.data, "\n")
}
func (g *graph) isOutOfBounds(p Point) bool {
func (g *graph) isOutOfBounds(p point.Point) bool {
return p.X < 0 || p.Y < 0 || p.Y >= len(g.data) || p.X >= len(g.data[p.Y])
}
func (g *graph) valueAt(p Point) rune {
func (g *graph) valueAt(p point.Point) rune {
return rune(g.data[p.Y][p.X])
}
func (g *graph) debug(path []Point) string {
func (g *graph) debug(path *orderedmap.OrderedMap[point.Point, struct{}]) string {
temp := slices.Clone(g.data)
for _, p := range path {
temp[p.Y] = replaceAtIndex(temp[p.Y], 'O', p.X)
temp[g.start.Y] = replaceAtIndex(temp[g.start.Y], 'O', g.start.X)
for pair := path.Oldest(); pair != nil; pair = pair.Next() {
temp[pair.Key.Y] = replaceAtIndex(temp[pair.Key.Y], 'O', pair.Key.X)
}
return strings.Join(temp, "\n")
}

View File

@@ -1,10 +1,12 @@
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
import "github.com/onyx-and-iris/aoc2024/day-18/internal/point"
func neighbours(p point.Point) [4]point.Point {
return [4]point.Point{
{X: p.X, Y: p.Y - 1}, // N
{X: p.X + 1, Y: p.Y}, // E
{X: p.X, Y: p.Y + 1}, // S
{X: p.X - 1, Y: p.Y}, // W
}
}

View File

@@ -1,6 +0,0 @@
package one
type Point struct {
X int
Y int
}

View File

@@ -5,11 +5,14 @@ import (
"math"
"github.com/onyx-and-iris/aoc2024/day-18/internal/config"
"github.com/onyx-and-iris/aoc2024/day-18/internal/point"
"github.com/onyx-and-iris/aoc2024/day-18/internal/queue"
log "github.com/sirupsen/logrus"
orderedmap "github.com/wk8/go-ordered-map/v2"
)
var ShortestPath []Point
var ShortestPath *orderedmap.OrderedMap[point.Point, struct{}]
func Solve(buf []byte, config config.Config) (int, error) {
r := bytes.NewReader(buf)
@@ -21,11 +24,11 @@ func Solve(buf []byte, config config.Config) (int, error) {
log.Debugf("start: %v end: %v", graph.start, graph.end)
log.Debugf("\n%s\n", graph.String())
queue := queue.New[Point]()
queue := queue.New[point.Point]()
queue.Enqueue(graph.start)
visited := make(map[Point]struct{})
costs := make(map[Point]int)
prev := make(map[Point]Point)
visited := make(map[point.Point]struct{})
costs := make(map[point.Point]int)
prev := make(map[point.Point]point.Point)
for !queue.IsEmpty() {
current := queue.Dequeue()
@@ -63,14 +66,15 @@ func Solve(buf []byte, config config.Config) (int, error) {
}
}
ShortestPath = []Point{graph.end}
ShortestPath = orderedmap.New[point.Point, struct{}]()
ShortestPath.Set(graph.end, struct{}{})
node := prev[graph.end]
for node != graph.start {
ShortestPath = append(ShortestPath, prev[node])
ShortestPath.Set(node, struct{}{})
node = prev[node]
}
log.Debugf("\n%s\n", graph.debug(ShortestPath))
return len(ShortestPath), nil
return ShortestPath.Len(), nil
}