Skip to content

Commit 5fd39f1

Browse files
committed
refactor: create bfs helper
1 parent ea4f8a7 commit 5fd39f1

File tree

2 files changed

+59
-14
lines changed

2 files changed

+59
-14
lines changed

go/2025/puzzles/day07/main.go

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ package main
33
import (
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

1516
func 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

go/utils/algorithms/bfs/bfs.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package bfs
2+
3+
import "github.com/believer/aoc-utils/grid"
4+
5+
type BFS struct {
6+
Queue []grid.Point
7+
Visited map[grid.Point]bool
8+
}
9+
10+
// Start a Breadth-first search (BFS) from a given start point
11+
func New(start grid.Point) BFS {
12+
queue := []grid.Point{start}
13+
14+
return BFS{
15+
Queue: queue,
16+
Visited: map[grid.Point]bool{},
17+
}
18+
}
19+
20+
// Report whether we still have items in the queue
21+
func (b *BFS) Loop() bool {
22+
return len(b.Queue) > 0
23+
}
24+
25+
// Grab (and remove) the first item in the queue and
26+
func (b *BFS) Pop() (current grid.Point) {
27+
current = b.Queue[0]
28+
b.Queue = b.Queue[1:]
29+
30+
return
31+
}
32+
33+
// Add a new point to the queue
34+
func (b *BFS) Push(p grid.Point) {
35+
b.Queue = append(b.Queue, p)
36+
}
37+
38+
// Report whether we've visited a given point
39+
func (b *BFS) HasVisited(p grid.Point) (ok bool) {
40+
_, ok = b.Visited[p]
41+
42+
return
43+
}
44+
45+
// Set a point as visited
46+
func (b *BFS) Visit(p grid.Point) {
47+
b.Visited[p] = true
48+
}

0 commit comments

Comments
 (0)