Merge pull request #17 from StevanFreeborn/stevanfreeborn/refactor/use-grid-in-day-seven-part-one
refactor: use internal packages
This commit is contained in:
+27
-28
@@ -2,7 +2,10 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/StevanFreeborn/advent-of-code-2025/internal/file"
|
"github.com/StevanFreeborn/advent-of-code-2025/internal/file"
|
||||||
|
"github.com/StevanFreeborn/advent-of-code-2025/internal/grid"
|
||||||
|
"github.com/StevanFreeborn/advent-of-code-2025/internal/move"
|
||||||
"github.com/StevanFreeborn/advent-of-code-2025/internal/position"
|
"github.com/StevanFreeborn/advent-of-code-2025/internal/position"
|
||||||
|
"github.com/StevanFreeborn/advent-of-code-2025/internal/queue"
|
||||||
)
|
)
|
||||||
|
|
||||||
const StartCharacter = "S"
|
const StartCharacter = "S"
|
||||||
@@ -10,66 +13,62 @@ const SplitterCharacter = "^"
|
|||||||
|
|
||||||
func SolvePartOne(filePath string) int {
|
func SolvePartOne(filePath string) int {
|
||||||
input := file.ReadAllLines(filePath)
|
input := file.ReadAllLines(filePath)
|
||||||
numOfRows := len(input)
|
grid := grid.From(input)
|
||||||
numOfCols := len(input[0])
|
|
||||||
|
|
||||||
startRow := -1
|
var startPosition position.Position
|
||||||
startCol := -1
|
|
||||||
|
|
||||||
for row := range numOfRows - 1 {
|
for row := range grid.NumberOfRows() - 1 {
|
||||||
for col := range numOfCols - 1 {
|
for col := range grid.NumberOfColumns() - 1 {
|
||||||
v := string(input[row][col])
|
current := position.From(row, col)
|
||||||
|
v := grid.GetValueAt(current)
|
||||||
|
|
||||||
if v == StartCharacter {
|
if v == StartCharacter {
|
||||||
startRow = row
|
startPosition = current
|
||||||
startCol = col
|
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if startRow < 0 || startCol < 0 {
|
if startPosition == nil {
|
||||||
panic("unable to find start location")
|
panic("unable to find start location")
|
||||||
}
|
}
|
||||||
|
|
||||||
beamStart := position.From(startRow+1, startCol)
|
beamStart := startPosition.Move(move.Down)
|
||||||
beamsQueue := []position.Position{
|
beamsQueue := queue.New[position.Position]()
|
||||||
beamStart,
|
beamsQueue.Enqueue(beamStart)
|
||||||
}
|
|
||||||
visited := map[position.Position]bool{}
|
visited := map[position.Position]bool{}
|
||||||
|
|
||||||
total := 0
|
total := 0
|
||||||
|
|
||||||
for len(beamsQueue) > 0 {
|
for beamsQueue.IsEmpty() == false {
|
||||||
currentBeam := beamsQueue[0]
|
currentBeam, _ := beamsQueue.Dequeue()
|
||||||
beamsQueue = beamsQueue[1:]
|
|
||||||
|
|
||||||
if visited[currentBeam] {
|
if visited[currentBeam] {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if currentBeam.Row() < 0 || currentBeam.Row() >= numOfRows {
|
if grid.InBounds(currentBeam) == false {
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if currentBeam.Column() < 0 || currentBeam.Column() >= numOfCols {
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
visited[currentBeam] = true
|
visited[currentBeam] = true
|
||||||
|
|
||||||
value := string(input[currentBeam.Row()][currentBeam.Column()])
|
value := grid.GetValueAt(currentBeam)
|
||||||
|
|
||||||
if value == SplitterCharacter {
|
if value == SplitterCharacter {
|
||||||
total++
|
total++
|
||||||
rightBeam := position.From(currentBeam.Row(), currentBeam.Column()+1)
|
|
||||||
leftBeam := position.From(currentBeam.Row(), currentBeam.Column()-1)
|
rightBeam := currentBeam.Move(move.Right)
|
||||||
beamsQueue = append(beamsQueue, rightBeam, leftBeam)
|
leftBeam := currentBeam.Move(move.Left)
|
||||||
|
|
||||||
|
beamsQueue.Enqueue(rightBeam)
|
||||||
|
beamsQueue.Enqueue(leftBeam)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
nextBeam := position.From(currentBeam.Row()+1, currentBeam.Column())
|
nextBeam := currentBeam.Move(move.Down)
|
||||||
beamsQueue = append(beamsQueue, nextBeam)
|
beamsQueue.Enqueue(nextBeam)
|
||||||
}
|
}
|
||||||
|
|
||||||
return total
|
return total
|
||||||
|
|||||||
@@ -1,9 +1,12 @@
|
|||||||
// Package position provides types and functions to represent and manipulate positions and moves on a 2D grid.
|
// Package position provides types and functions to represent and manipulate positions and moves on a 2D grid.
|
||||||
package position
|
package position
|
||||||
|
|
||||||
|
import "github.com/StevanFreeborn/advent-of-code-2025/internal/move"
|
||||||
|
|
||||||
type Position interface {
|
type Position interface {
|
||||||
Row() int
|
Row() int
|
||||||
Column() int
|
Column() int
|
||||||
|
Move(move.Move) Position
|
||||||
}
|
}
|
||||||
|
|
||||||
type position struct {
|
type position struct {
|
||||||
@@ -25,3 +28,7 @@ func (p position) Row() int {
|
|||||||
func (p position) Column() int {
|
func (p position) Column() int {
|
||||||
return p.column
|
return p.column
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p position) Move(move move.Move) Position {
|
||||||
|
return From(p.row+move.NumberOfRows(), p.column+move.NumberOfColumns())
|
||||||
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package position_test
|
|||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/StevanFreeborn/advent-of-code-2025/internal/move"
|
||||||
"github.com/StevanFreeborn/advent-of-code-2025/internal/position"
|
"github.com/StevanFreeborn/advent-of-code-2025/internal/position"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -20,3 +21,14 @@ func TestFrom(t *testing.T) {
|
|||||||
t.Errorf("expected column to be %d, got %d", column, pos.Column())
|
t.Errorf("expected column to be %d, got %d", column, pos.Column())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestMove(t *testing.T) {
|
||||||
|
expectedPos := position.From(3, 4)
|
||||||
|
startPos := position.From(2, 4)
|
||||||
|
|
||||||
|
result := startPos.Move(move.Down)
|
||||||
|
|
||||||
|
if result != expectedPos {
|
||||||
|
t.Errorf("expected position to be %+v, got %+v", expectedPos, result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,55 @@
|
|||||||
|
// Package queue provides a thread-safe FIFO queue implementation.
|
||||||
|
package queue
|
||||||
|
|
||||||
|
import "sync"
|
||||||
|
|
||||||
|
type Queue[T any] interface {
|
||||||
|
Enqueue(item T)
|
||||||
|
Dequeue() (T, bool)
|
||||||
|
IsEmpty() bool
|
||||||
|
Size() int
|
||||||
|
}
|
||||||
|
|
||||||
|
type queue[T any] struct {
|
||||||
|
items []T
|
||||||
|
lock sync.Mutex
|
||||||
|
}
|
||||||
|
|
||||||
|
func New[T any]() Queue[T] {
|
||||||
|
return &queue[T]{items: []T{}}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *queue[T]) Enqueue(item T) {
|
||||||
|
q.lock.Lock()
|
||||||
|
defer q.lock.Unlock()
|
||||||
|
|
||||||
|
q.items = append(q.items, item)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *queue[T]) Dequeue() (T, bool) {
|
||||||
|
q.lock.Lock()
|
||||||
|
defer q.lock.Unlock()
|
||||||
|
|
||||||
|
if len(q.items) == 0 {
|
||||||
|
var zero T
|
||||||
|
return zero, false
|
||||||
|
}
|
||||||
|
|
||||||
|
item := q.items[0]
|
||||||
|
q.items = q.items[1:]
|
||||||
|
return item, true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *queue[T]) IsEmpty() bool {
|
||||||
|
q.lock.Lock()
|
||||||
|
defer q.lock.Unlock()
|
||||||
|
|
||||||
|
return len(q.items) == 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *queue[T]) Size() int {
|
||||||
|
q.lock.Lock()
|
||||||
|
defer q.lock.Unlock()
|
||||||
|
|
||||||
|
return len(q.items)
|
||||||
|
}
|
||||||
@@ -0,0 +1,87 @@
|
|||||||
|
package queue_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/StevanFreeborn/advent-of-code-2025/internal/queue"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestQueue_EnqueueDequeue(t *testing.T) {
|
||||||
|
q := queue.New[int]()
|
||||||
|
|
||||||
|
q.Enqueue(1)
|
||||||
|
q.Enqueue(2)
|
||||||
|
q.Enqueue(3)
|
||||||
|
|
||||||
|
expectedValues := []int{1, 2, 3}
|
||||||
|
|
||||||
|
for _, expected := range expectedValues {
|
||||||
|
value, ok := q.Dequeue()
|
||||||
|
|
||||||
|
if !ok {
|
||||||
|
t.Errorf("expected dequeue to be successful, but it failed")
|
||||||
|
}
|
||||||
|
|
||||||
|
if value != expected {
|
||||||
|
t.Errorf("expected value to be %d, got %d", expected, value)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
_, ok := q.Dequeue()
|
||||||
|
|
||||||
|
if ok {
|
||||||
|
t.Errorf("expected dequeue to fail on empty queue, but it succeeded")
|
||||||
|
}
|
||||||
|
|
||||||
|
if !q.IsEmpty() {
|
||||||
|
t.Errorf("expected queue to be empty, but it is not")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestQueue_Size(t *testing.T) {
|
||||||
|
q := queue.New[string]()
|
||||||
|
|
||||||
|
if q.Size() != 0 {
|
||||||
|
t.Errorf("expected size to be 0, got %d", q.Size())
|
||||||
|
}
|
||||||
|
|
||||||
|
q.Enqueue("a")
|
||||||
|
q.Enqueue("b")
|
||||||
|
|
||||||
|
if q.Size() != 2 {
|
||||||
|
t.Errorf("expected size to be 2, got %d", q.Size())
|
||||||
|
}
|
||||||
|
|
||||||
|
q.Dequeue()
|
||||||
|
|
||||||
|
if q.Size() != 1 {
|
||||||
|
t.Errorf("expected size to be 1, got %d", q.Size())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestQueue_ConcurrentAccess(t *testing.T) {
|
||||||
|
q := queue.New[int]()
|
||||||
|
done := make(chan bool)
|
||||||
|
numGoroutines := 100
|
||||||
|
numItemsPerGoroutine := 100
|
||||||
|
|
||||||
|
for i := range numGoroutines {
|
||||||
|
go func(start int) {
|
||||||
|
for j := range numItemsPerGoroutine {
|
||||||
|
q.Enqueue(start + j)
|
||||||
|
}
|
||||||
|
done <- true
|
||||||
|
}(i * numItemsPerGoroutine)
|
||||||
|
}
|
||||||
|
|
||||||
|
for range numGoroutines {
|
||||||
|
<-done
|
||||||
|
}
|
||||||
|
|
||||||
|
expectedSize := numGoroutines * numItemsPerGoroutine
|
||||||
|
|
||||||
|
if q.Size() != expectedSize {
|
||||||
|
t.Errorf("expected size to be %d, got %d", expectedSize, q.Size())
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user