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

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

View File

@@ -1,4 +1,4 @@
package one
package point
type Point struct {
X int

View File

@@ -2,5 +2,5 @@ goos: linux
goarch: amd64
pkg: github.com/onyx-and-iris/aoc2024/day-18/internal/two
cpu: Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz
BenchmarkSolve-12 1 3756699083 ns/op
ok github.com/onyx-and-iris/aoc2024/day-18/internal/two 3.760s
BenchmarkSolve-12 1 1414311557 ns/op
ok github.com/onyx-and-iris/aoc2024/day-18/internal/two 1.418s

View File

@@ -1,17 +1,17 @@
package two
import (
"errors"
"math"
"slices"
"strings"
"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"
)
type graph struct {
start point
end point
start point.Point
end point.Point
data []string
}
@@ -30,38 +30,35 @@ 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 {
return p.x < 0 || p.y < 0 || p.y >= len(g.data) || p.x >= len(g.data[p.y])
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 {
return rune(g.data[p.y][p.x])
func (g *graph) valueAt(p point.Point) rune {
return rune(g.data[p.Y][p.X])
}
func (g *graph) addCorruption(coords []int) {
g.data[coords[1]] = replaceAtIndex(g.data[coords[1]], '#', coords[0])
func (g *graph) addCorruption(p point.Point) {
g.data[p.Y] = replaceAtIndex(g.data[p.Y], '#', p.X)
}
func (g *graph) dijkstra(start, end point) ([]point, error) {
queue := queue.New[point]()
queue.Enqueue(start)
visited := make(map[point]struct{})
costs := make(map[point]int)
prev := make(map[point]point)
func (g *graph) bfs() bool {
queue := queue.New[point.Point]()
queue.Enqueue(g.start)
visited := make(map[point.Point]struct{})
for !queue.IsEmpty() {
current := queue.Dequeue()
// we found a shortest path
if current == end {
return g.generatePath(start, end, prev), nil
if current == g.end {
return true
}
_, ok := visited[current]
@@ -79,40 +76,21 @@ func (g *graph) dijkstra(start, end point) ([]point, error) {
continue
}
_, ok := costs[n]
if !ok {
costs[n] = math.MaxInt
}
new_cost := costs[current] + 1
if new_cost < costs[n] {
costs[n] = new_cost
prev[n] = current
queue.Enqueue(n)
}
queue.Enqueue(n)
}
}
log.Debugf("\n%s\n", g.debug(visited))
return nil, errors.New("unable to find a shortest path")
return false
}
func (g *graph) generatePath(start, end point, prev map[point]point) []point {
path := []point{end}
node := prev[end]
for node != start {
path = append(path, prev[node])
node = prev[node]
}
return path
}
func (g *graph) debug(path []point) string {
func (g *graph) debug(visited map[point.Point]struct{}) string {
temp := slices.Clone(g.data)
for _, p := range path {
for p := range visited {
if g.valueAt(p) == '#' {
continue
}
temp[p.y] = replaceAtIndex(temp[p.y], 'O', p.x)
temp[p.Y] = replaceAtIndex(temp[p.Y], 'O', p.X)
}
return strings.Join(temp, "\n")
}

View File

@@ -1,10 +1,12 @@
package two
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 two
type point struct {
x int
y int
}

View File

@@ -5,6 +5,7 @@ import (
"fmt"
"github.com/onyx-and-iris/aoc2024/day-18/internal/config"
"github.com/onyx-and-iris/aoc2024/day-18/internal/point"
log "github.com/sirupsen/logrus"
)
@@ -16,6 +17,7 @@ func Solve(buf []byte, config config.Config) (string, error) {
}
log.Debugf("start: %v end: %v", graph.start, graph.end)
log.Debug(corruptedCoords)
indx := runUntilNoPath(graph, corruptedCoords, config)
return fmt.Sprintf("%d,%d", corruptedCoords[indx][0], corruptedCoords[indx][1]), nil
@@ -23,17 +25,15 @@ func Solve(buf []byte, config config.Config) (string, error) {
func runUntilNoPath(graph *graph, corruptedCoords [][]int, config config.Config) int {
for i, coords := range corruptedCoords[config.NumCorruptions+1:] {
nextCorruption := point{coords[0], coords[1]}
nextCorruption := point.Point{X: coords[0], Y: coords[1]}
log.Debugf("adding corruption %v", nextCorruption)
graph.addCorruption(nextCorruption)
graph.addCorruption(coords)
path, err := graph.dijkstra(graph.start, graph.end)
if err != nil {
log.Debug(err)
ok := graph.bfs()
if !ok {
return config.NumCorruptions + i + 1
}
log.Debugf("\n%s\n", graph.debug(path))
}
return 0
}