Files
advent-of-code-2025/cmd/07/main.go
T

71 lines
1.4 KiB
Go
Raw Normal View History

2025-12-13 06:08:15 -06:00
package main
import (
"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"
2025-12-13 06:08:15 -06:00
"github.com/StevanFreeborn/advent-of-code-2025/internal/position"
"github.com/StevanFreeborn/advent-of-code-2025/internal/queue"
2025-12-13 06:08:15 -06:00
)
const StartCharacter = "S"
const SplitterCharacter = "^"
func SolvePartOne(filePath string) int {
input := file.ReadAllLines(filePath)
grid := grid.From(input)
2025-12-13 06:08:15 -06:00
var startPosition position.Position
2025-12-13 06:08:15 -06:00
2025-12-13 07:49:01 -06:00
for p, v := range grid.Positions() {
if v == StartCharacter {
startPosition = p
break
2025-12-13 06:08:15 -06:00
}
}
if startPosition == nil {
2025-12-13 06:08:15 -06:00
panic("unable to find start location")
}
beamStart := startPosition.Move(move.Down)
beamsQueue := queue.New[position.Position]()
beamsQueue.Enqueue(beamStart)
2025-12-13 06:08:15 -06:00
visited := map[position.Position]bool{}
total := 0
for beamsQueue.IsEmpty() == false {
currentBeam, _ := beamsQueue.Dequeue()
2025-12-13 06:08:15 -06:00
if visited[currentBeam] {
continue
}
if grid.InBounds(currentBeam) == false {
2025-12-13 06:08:15 -06:00
continue
}
visited[currentBeam] = true
value := grid.GetValueAt(currentBeam)
2025-12-13 06:08:15 -06:00
if value == SplitterCharacter {
total++
rightBeam := currentBeam.Move(move.Right)
leftBeam := currentBeam.Move(move.Left)
beamsQueue.Enqueue(rightBeam)
beamsQueue.Enqueue(leftBeam)
2025-12-13 06:08:15 -06:00
continue
}
nextBeam := currentBeam.Move(move.Down)
beamsQueue.Enqueue(nextBeam)
2025-12-13 06:08:15 -06:00
}
return total
}