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
+92
View File
@@ -0,0 +1,92 @@
// Package circuitMap provides a data structure to manage and track connected junction boxes.
package circuitMap
import "github.com/StevanFreeborn/advent-of-code-2025/cmd/08/box"
type CircuitMap interface {
Length() int
GetValueFor(b box.Box) (CircuitMapValue, bool)
UpdateValueFor(b box.Box, value CircuitMapValue)
FindRootBoxFor(b box.Box) box.Box
Entries() map[box.Box]CircuitMapValue
}
type CircuitMapValue interface {
Parent() box.Box
Size() int
UpdateParent(newParent box.Box)
IncreaseSize(by int)
}
type circuitMap map[box.Box]CircuitMapValue
func From(boxes []box.Box) CircuitMap {
circuitsMap := circuitMap{}
for _, b := range boxes {
circuitsMap[b] = &circuitMapValue{
parent: b,
size: 1,
}
}
return circuitsMap
}
func (c circuitMap) Length() int {
return len(c)
}
func (c circuitMap) GetValueFor(b box.Box) (CircuitMapValue, bool) {
v, exists := c[b]
return v, exists
}
func (c circuitMap) UpdateValueFor(b box.Box, value CircuitMapValue) {
c[b] = value
}
func (c circuitMap) FindRootBoxFor(b box.Box) box.Box {
root := b
for c[root].Parent() != root {
root = c[root].Parent()
}
current := b
for current != root {
item := c[current]
next := item.Parent()
item.UpdateParent(root)
c[current] = item
current = next
}
return root
}
func (c circuitMap) Entries() map[box.Box]CircuitMapValue {
return c
}
type circuitMapValue struct {
parent box.Box
size int
}
func (c *circuitMapValue) Parent() box.Box {
return c.parent
}
func (c *circuitMapValue) Size() int {
return c.size
}
func (c *circuitMapValue) UpdateParent(newParent box.Box) {
c.parent = newParent
}
func (c *circuitMapValue) IncreaseSize(by int) {
c.size += by
}
+101
View File
@@ -0,0 +1,101 @@
package circuitMap_test
import (
"testing"
"github.com/StevanFreeborn/advent-of-code-2025/cmd/08/box"
"github.com/StevanFreeborn/advent-of-code-2025/cmd/08/circuitMap"
)
func TestFrom(t *testing.T) {
boxes := []box.Box{
box.From("0,0,0"),
box.From("1,1,1"),
}
expectedNumOfEntries := len(boxes)
result := circuitMap.From(boxes)
if result.Length() != expectedNumOfEntries {
t.Errorf("CreateMap() returned map with %d entries; expected %d", result.Length(), expectedNumOfEntries)
}
for _, b := range boxes {
_, exists := result.GetValueFor(b)
if !exists {
t.Errorf("CreateMap() result missing entry for box %v", b)
}
}
}
func TestGetAndUpdateValueFor(t *testing.T) {
b := box.From("2,2,2")
cMap := circuitMap.From([]box.Box{b})
value, exists := cMap.GetValueFor(b)
if !exists {
t.Fatalf("GetValueFor() did not find value for box %v", b)
}
if value.Parent() != b {
t.Errorf("GetValueFor() returned value with Parent() = %v; expected %v", value.Parent(), b)
}
newParent := box.From("3,3,3")
value.UpdateParent(newParent)
cMap.UpdateValueFor(b, value)
updatedValue, exists := cMap.GetValueFor(b)
if !exists {
t.Fatalf("GetValueFor() after UpdateValueFor() did not find value for box %v", b)
}
if updatedValue.Parent() != newParent {
t.Errorf("After UpdateValueFor(), GetValueFor() returned value with Parent() = %v; expected %v", updatedValue.Parent(), newParent)
}
}
func TestFindRootBoxFor(t *testing.T) {
b1 := box.From("0,0,0")
b2 := box.From("1,1,1")
b3 := box.From("2,2,2")
cMap := circuitMap.From([]box.Box{b1, b2, b3})
// Manually create connections
value2, _ := cMap.GetValueFor(b2)
value2.UpdateParent(b1)
cMap.UpdateValueFor(b2, value2)
value3, _ := cMap.GetValueFor(b3)
value3.UpdateParent(b2)
cMap.UpdateValueFor(b3, value3)
root := cMap.FindRootBoxFor(b3)
if root != b1 {
t.Errorf("FindRootBoxFor(%v) = %v; expected %v", b3, root, b1)
}
}
func TestEntries(t *testing.T) {
b1 := box.From("0,0,0")
b2 := box.From("1,1,1")
cMap := circuitMap.From([]box.Box{b1, b2})
entries := cMap.Entries()
if len(entries) != 2 {
t.Errorf("Entries() returned %d entries; expected 2", len(entries))
}
for _, b := range []box.Box{b1, b2} {
if _, exists := entries[b]; !exists {
t.Errorf("Entries() missing entry for box %v", b)
}
}
}