feat: solve day 8 part 1...twice

This commit is contained in:
Stevan Freeborn
2025-12-16 13:19:57 -06:00
parent d9be56da3e
commit 9c0f883bc5
11 changed files with 652 additions and 1 deletions
+59
View File
@@ -0,0 +1,59 @@
// Package box provides a model and methods for junction boxes.
package box
import (
"math"
"strconv"
"strings"
)
type Box interface {
Y() int
X() int
Z() int
DistanceFrom(neighbor Box) float64
}
type box struct {
x int
y int
z int
}
func (b box) X() int {
return b.x
}
func (b box) Y() int {
return b.y
}
func (b box) Z() int {
return b.z
}
func (b box) DistanceFrom(neighbor Box) float64 {
xDiff := math.Pow(float64(b.x-neighbor.X()), 2)
yDiff := math.Pow(float64(b.y-neighbor.Y()), 2)
zDiff := math.Pow(float64(b.z-neighbor.Z()), 2)
return math.Sqrt(xDiff + yDiff + zDiff)
}
func From(str string) Box {
parts := strings.Split(str, ",")
if len(parts) != 3 {
panic("invalid junction box")
}
x, _ := strconv.Atoi(parts[0])
y, _ := strconv.Atoi(parts[1])
z, _ := strconv.Atoi(parts[2])
return box{
x: x,
y: y,
z: z,
}
}
+46
View File
@@ -0,0 +1,46 @@
package box_test
import (
"testing"
"github.com/StevanFreeborn/advent-of-code-2025/cmd/08/box"
)
func TestFrom(t *testing.T) {
tests := []struct {
input string
expected box.Box
}{
{"1,2,3", box.From("1,2,3")},
{"0,0,0", box.From("0,0,0")},
{"-1,-2,-3", box.From("-1,-2,-3")},
}
for _, test := range tests {
result := box.From(test.input)
if result != test.expected {
t.Errorf("From(%q) = %v; want %v", test.input, result, test.expected)
}
}
}
func TestDistanceFrom(t *testing.T) {
tests := []struct {
box1 box.Box
box2 box.Box
expected float64
}{
{box.From("0,0,0"), box.From("1,1,1"), 1.7320508075688772},
{box.From("1,2,3"), box.From("4,5,6"), 5.196152422706632},
{box.From("-1,-2,-3"), box.From("1,2,3"), 7.483314773547883},
}
for _, test := range tests {
result := test.box1.DistanceFrom(test.box2)
if result != test.expected {
t.Errorf("DistanceFrom(%v, %v) = %v; want %v", test.box1, test.box2, result, test.expected)
}
}
}