feat: solve day 7 part 1

This commit is contained in:
Stevan Freeborn
2025-12-13 06:08:15 -06:00
parent c41ba29791
commit 41d26d1a23
3 changed files with 104 additions and 1 deletions
+76
View File
@@ -0,0 +1,76 @@
package main
import (
"github.com/StevanFreeborn/advent-of-code-2025/internal/file"
"github.com/StevanFreeborn/advent-of-code-2025/internal/position"
)
const StartCharacter = "S"
const SplitterCharacter = "^"
func SolvePartOne(filePath string) int {
input := file.ReadAllLines(filePath)
numOfRows := len(input)
numOfCols := len(input[0])
startRow := -1
startCol := -1
for row := range numOfRows - 1 {
for col := range numOfCols - 1 {
v := string(input[row][col])
if v == StartCharacter {
startRow = row
startCol = col
break
}
}
}
if startRow < 0 || startCol < 0 {
panic("unable to find start location")
}
beamStart := position.From(startRow+1, startCol)
beamsQueue := []position.Position{
beamStart,
}
visited := map[position.Position]bool{}
total := 0
for len(beamsQueue) > 0 {
currentBeam := beamsQueue[0]
beamsQueue = beamsQueue[1:]
if visited[currentBeam] {
continue
}
if currentBeam.Row() < 0 || currentBeam.Row() >= numOfRows {
continue
}
if currentBeam.Column() < 0 || currentBeam.Column() >= numOfCols {
continue
}
visited[currentBeam] = true
value := string(input[currentBeam.Row()][currentBeam.Column()])
if value == SplitterCharacter {
total++
rightBeam := position.From(currentBeam.Row(), currentBeam.Column()+1)
leftBeam := position.From(currentBeam.Row(), currentBeam.Column()-1)
beamsQueue = append(beamsQueue, rightBeam, leftBeam)
continue
}
nextBeam := position.From(currentBeam.Row()+1, currentBeam.Column())
beamsQueue = append(beamsQueue, nextBeam)
}
return total
}
+27
View File
@@ -0,0 +1,27 @@
package main_test
import (
"testing"
solution "github.com/StevanFreeborn/advent-of-code-2025/cmd/07"
)
func TestSolvePartOneWithExampleInput(t *testing.T) {
expected := 21
result := solution.SolvePartOne("EXAMPLE.txt")
if result != expected {
t.Errorf("SolvePartOne() = %d; want %d", result, expected)
}
}
func TestSolvePartOneWithInput(t *testing.T) {
expected := 1581
result := solution.SolvePartOne("INPUT.txt")
if result != expected {
t.Errorf("SolvePartOne() = %d; want %d", result, expected)
}
}