feat: solve day 8 part 1...twice
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
// Package estimator provides model and methods
|
||||
// for answering questions about cable needs
|
||||
// for connecting junction boxes.
|
||||
package estimator
|
||||
|
||||
import (
|
||||
"slices"
|
||||
|
||||
"github.com/StevanFreeborn/advent-of-code-2025/cmd/08/box"
|
||||
"github.com/StevanFreeborn/advent-of-code-2025/cmd/08/circuitMap"
|
||||
"github.com/StevanFreeborn/advent-of-code-2025/cmd/08/connection"
|
||||
)
|
||||
|
||||
type Estimator interface {
|
||||
PossibleConnections() []connection.Connection
|
||||
CreateMap() circuitMap.CircuitMap
|
||||
}
|
||||
|
||||
type estimator struct {
|
||||
possibleConnections []connection.Connection
|
||||
boxes []box.Box
|
||||
}
|
||||
|
||||
func From(boxes []box.Box) Estimator {
|
||||
return estimator{
|
||||
boxes: boxes,
|
||||
possibleConnections: createAllPossibleConnections(boxes),
|
||||
}
|
||||
}
|
||||
|
||||
func (e estimator) PossibleConnections() []connection.Connection {
|
||||
return e.possibleConnections
|
||||
}
|
||||
|
||||
func (e estimator) CreateMap() circuitMap.CircuitMap {
|
||||
return circuitMap.From(e.boxes)
|
||||
}
|
||||
|
||||
func createAllPossibleConnections(boxes []box.Box) []connection.Connection {
|
||||
connections := []connection.Connection{}
|
||||
numOfBoxes := len(boxes)
|
||||
|
||||
for i := range numOfBoxes {
|
||||
for j := i + 1; j < numOfBoxes; j++ {
|
||||
start := boxes[i]
|
||||
end := boxes[j]
|
||||
conn := connection.From(start, end)
|
||||
connections = append(connections, conn)
|
||||
}
|
||||
}
|
||||
|
||||
slices.SortFunc(connections, func(a connection.Connection, b connection.Connection) int {
|
||||
return int(a.Distance() - b.Distance())
|
||||
})
|
||||
|
||||
return connections
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package estimator_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/StevanFreeborn/advent-of-code-2025/cmd/08/box"
|
||||
"github.com/StevanFreeborn/advent-of-code-2025/cmd/08/estimator"
|
||||
)
|
||||
|
||||
func TestFrom(t *testing.T) {
|
||||
result := estimator.From([]box.Box{})
|
||||
|
||||
if result == nil {
|
||||
t.Errorf("estimator.From returned nil; expected non-nil value")
|
||||
}
|
||||
|
||||
if len(result.PossibleConnections()) != 0 {
|
||||
t.Errorf("estimator.From returned estimator with PossibleConnections() = %d; expected 0", result.PossibleConnections())
|
||||
}
|
||||
}
|
||||
|
||||
func TestPossibleConnections(t *testing.T) {
|
||||
expectedNumOfConnections := 1
|
||||
|
||||
est := estimator.From([]box.Box{
|
||||
box.From("0,0,0"),
|
||||
box.From("1,1,1"),
|
||||
})
|
||||
|
||||
result := est.PossibleConnections()
|
||||
|
||||
if len(result) != expectedNumOfConnections {
|
||||
t.Errorf("PossibleConnections() returned %d connections; expected %d", len(result), expectedNumOfConnections)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user