refactor: update solution to 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
|
||||||
|
|||||||
Reference in New Issue
Block a user