feat: solve part seven day two
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
|||||||
"github.com/StevanFreeborn/advent-of-code-2025/internal/move"
|
"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"
|
"github.com/StevanFreeborn/advent-of-code-2025/internal/queue"
|
||||||
|
"github.com/StevanFreeborn/advent-of-code-2025/internal/stack"
|
||||||
)
|
)
|
||||||
|
|
||||||
const StartCharacter = "S"
|
const StartCharacter = "S"
|
||||||
@@ -68,3 +69,79 @@ func SolvePartOne(filePath string) int {
|
|||||||
|
|
||||||
return total
|
return total
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func SolvePartTwo(filePath string) int {
|
||||||
|
input := file.ReadAllLines(filePath)
|
||||||
|
grid := grid.From(input)
|
||||||
|
|
||||||
|
var startPosition position.Position
|
||||||
|
|
||||||
|
for p, v := range grid.Positions() {
|
||||||
|
if v == StartCharacter {
|
||||||
|
startPosition = p
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if startPosition == nil {
|
||||||
|
panic("unable to find start location")
|
||||||
|
}
|
||||||
|
|
||||||
|
beamStartPosition := startPosition.Move(move.Down)
|
||||||
|
beamsStack := stack.New[position.Position]()
|
||||||
|
beamsStack.Push(beamStartPosition)
|
||||||
|
|
||||||
|
positionsToTimelinesMap := map[position.Position]int{}
|
||||||
|
|
||||||
|
for beamsStack.IsEmpty() == false {
|
||||||
|
currentBeam, _ := beamsStack.Peek()
|
||||||
|
|
||||||
|
_, hasBeenProcessed := positionsToTimelinesMap[currentBeam]
|
||||||
|
|
||||||
|
if hasBeenProcessed {
|
||||||
|
beamsStack.Pop()
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
value := grid.GetValueAt(currentBeam)
|
||||||
|
nextTimelinePositions := []position.Position{}
|
||||||
|
|
||||||
|
if value == SplitterCharacter {
|
||||||
|
rightBeam := currentBeam.Move(move.Right)
|
||||||
|
leftBeam := currentBeam.Move(move.Left)
|
||||||
|
nextTimelinePositions = append(nextTimelinePositions, rightBeam, leftBeam)
|
||||||
|
} else {
|
||||||
|
nextBeam := currentBeam.Move(move.Down)
|
||||||
|
nextTimelinePositions = append(nextTimelinePositions, nextBeam)
|
||||||
|
}
|
||||||
|
|
||||||
|
numberOfTimelinesForPosition := 0
|
||||||
|
finalizePosition := true
|
||||||
|
|
||||||
|
for _, newTimelinePosition := range nextTimelinePositions {
|
||||||
|
if grid.InBounds(newTimelinePosition) == false {
|
||||||
|
numberOfTimelinesForPosition++
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
v, existing := positionsToTimelinesMap[newTimelinePosition]
|
||||||
|
|
||||||
|
if existing {
|
||||||
|
numberOfTimelinesForPosition += v
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
beamsStack.Push(newTimelinePosition)
|
||||||
|
finalizePosition = false
|
||||||
|
}
|
||||||
|
|
||||||
|
if finalizePosition == false {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
beamsStack.Pop()
|
||||||
|
positionsToTimelinesMap[currentBeam] = numberOfTimelinesForPosition
|
||||||
|
}
|
||||||
|
|
||||||
|
return positionsToTimelinesMap[beamStartPosition]
|
||||||
|
}
|
||||||
|
|||||||
@@ -25,3 +25,23 @@ func TestSolvePartOneWithInput(t *testing.T) {
|
|||||||
t.Errorf("SolvePartOne() = %d; want %d", result, expected)
|
t.Errorf("SolvePartOne() = %d; want %d", result, expected)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSolvePartTwoWithExampleInput(t *testing.T) {
|
||||||
|
expected := 40
|
||||||
|
|
||||||
|
result := solution.SolvePartTwo("EXAMPLE.txt")
|
||||||
|
|
||||||
|
if result != expected {
|
||||||
|
t.Errorf("SolveTwoOne() = %d; want %d", result, expected)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSolvePartTwoWithInput(t *testing.T) {
|
||||||
|
expected := 73_007_003_089_792
|
||||||
|
|
||||||
|
result := solution.SolvePartTwo("INPUT.txt")
|
||||||
|
|
||||||
|
if result != expected {
|
||||||
|
t.Errorf("SolveTwoOne() = %d; want %d", result, expected)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user