@@ -3,13 +3,14 @@ package main
33import (
44 "fmt"
55
6+ "github.com/believer/aoc-utils/algorithms/bfs"
67 "github.com/believer/aoc-utils/files"
78 "github.com/believer/aoc-utils/grid"
89)
910
1011// I spent too much time trying to make a flawed solution work.
11- // I thought of BFS (I've had it in the back of my mind that
12- // it would appear at some point) but kept working on the path I had taken ':D
12+ // I thought of BFS (I've had it in the back of my mind that it would
13+ // appear at some point) but kept working on the path I had taken ':D
1314// Eventually, I gave up and looked at my solution from day 18 2024 to remember.
1415
1516func main () {
@@ -22,34 +23,30 @@ func part1(name string) (splits int) {
2223 manifold := grid .New (lines )
2324 start , _ := manifold .Find ('S' )
2425
25- queue := []grid.Point {start }
26- seen := map [grid.Point ]bool {}
26+ queue := bfs .New (start )
2727
28- for len (queue ) > 0 {
29- beam := queue [0 ]
30- queue = queue [1 :]
28+ for queue .Loop () {
29+ beam := queue .Pop ()
3130
3231 // Keep track of what we've seen since beams
3332 // can split into the same position
34- if _ , ok := seen [ beam ]; ok {
33+ if queue . HasVisited ( beam ) {
3534 continue
3635 }
3736
3837 if ! manifold .InBounds (beam ) {
3938 continue
4039 }
4140
42- seen [ beam ] = true
41+ queue . Visit ( beam )
4342
4443 if manifold .Get (beam ) == '^' {
4544 splits += 1
46- leftSplit := beam .Add (grid .LEFT )
47- rightSplit := beam .Add (grid .RIGHT )
4845
49- queue = append ( queue , leftSplit )
50- queue = append ( queue , rightSplit )
46+ queue . Push ( beam . Add ( grid . LEFT ) )
47+ queue . Push ( beam . Add ( grid . RIGHT ) )
5148 } else {
52- queue = append ( queue , beam .Add (grid .DOWN ))
49+ queue . Push ( beam .Add (grid .DOWN ))
5350 }
5451 }
5552
0 commit comments