feat: solve day 8 part 2...we in this bitch
This commit is contained in:
@@ -9,6 +9,50 @@ import (
|
||||
"github.com/StevanFreeborn/advent-of-code-2025/internal/queue"
|
||||
)
|
||||
|
||||
func SolvePartTwo(filePath string) int {
|
||||
boxes := []box.Box{}
|
||||
|
||||
for line := range file.ReadLines(filePath) {
|
||||
boxes = append(boxes, box.From(line))
|
||||
}
|
||||
|
||||
estimator := estimator.From(boxes)
|
||||
circuitsMap := estimator.CreateMap()
|
||||
|
||||
numberOfSets := len(boxes)
|
||||
|
||||
for _, connection := range estimator.PossibleConnections() {
|
||||
startRoot := circuitsMap.FindRootBoxFor(connection.Start())
|
||||
endRoot := circuitsMap.FindRootBoxFor(connection.End())
|
||||
|
||||
if startRoot == endRoot {
|
||||
continue
|
||||
}
|
||||
|
||||
startRootValue, _ := circuitsMap.GetValueFor(startRoot)
|
||||
endRootValue, _ := circuitsMap.GetValueFor(endRoot)
|
||||
|
||||
if startRootValue.Size() < endRootValue.Size() {
|
||||
startRootValue.UpdateParent(endRoot)
|
||||
endRootValue.IncreaseSize(startRootValue.Size())
|
||||
} else {
|
||||
endRootValue.UpdateParent(startRoot)
|
||||
startRootValue.IncreaseSize(endRootValue.Size())
|
||||
}
|
||||
|
||||
circuitsMap.UpdateValueFor(startRoot, startRootValue)
|
||||
circuitsMap.UpdateValueFor(endRoot, endRootValue)
|
||||
|
||||
numberOfSets--
|
||||
|
||||
if numberOfSets == 1 {
|
||||
return connection.Start().X() * connection.End().X()
|
||||
}
|
||||
}
|
||||
|
||||
return -1
|
||||
}
|
||||
|
||||
func SolvePartOne(filePath string, numOfConnections int) int {
|
||||
boxes := []box.Box{}
|
||||
|
||||
|
||||
+22
-2
@@ -17,7 +17,7 @@ func TestSolvePartOneWithExampleInput(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestSolvePartOneWithInput(t *testing.T) {
|
||||
expected := 62186
|
||||
expected := 62_186
|
||||
|
||||
result := solution.SolvePartOne("INPUT.txt", 1000)
|
||||
|
||||
@@ -37,7 +37,7 @@ func TestSolvePartOneAgainWithExampleInput(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestSolvePartOneAgainWithInput(t *testing.T) {
|
||||
expected := 62186
|
||||
expected := 62_186
|
||||
|
||||
result := solution.SolvePartOneAgain("INPUT.txt", 1000)
|
||||
|
||||
@@ -45,3 +45,23 @@ func TestSolvePartOneAgainWithInput(t *testing.T) {
|
||||
t.Errorf("SolvePartOneAgain returned %d, expected %d", result, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSolvePartTwoWithExampleInput(t *testing.T) {
|
||||
expected := 25_272
|
||||
|
||||
result := solution.SolvePartTwo("EXAMPLE.txt")
|
||||
|
||||
if result != expected {
|
||||
t.Errorf("SolvePartTwo returned %d, expected %d", result, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSolvePartTwoWithInput(t *testing.T) {
|
||||
expected := 8_420_405_530
|
||||
|
||||
result := solution.SolvePartTwo("INPUT.txt")
|
||||
|
||||
if result != expected {
|
||||
t.Errorf("SolvePartTwo returned %d, expected %d", result, expected)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user