day 08 part2

This commit is contained in:
Simon Ziegler
2025-12-11 14:21:09 +01:00
parent 46c27fd8a6
commit f7949a3594

View File

@@ -9,29 +9,31 @@ import (
"git.z1glr.de/advent-of-code-2025/pkg/aoc" "git.z1glr.de/advent-of-code-2025/pkg/aoc"
) )
type Point struct { type Node struct {
X, Y, Z int X, Y, Z int
Parent *Point root *Node
Size int Size int
id int ID int
} }
func (p *Point) DistanceTo(p2 *Point) float64 { func (p *Node) DistanceTo(p2 *Node) float64 {
return math.Sqrt(float64((p.X-p2.X)*(p.X-p2.X) + (p.Y-p2.Y)*(p.Y-p2.Y) + (p.Z-p2.Z)*(p.Z-p2.Z))) return math.Sqrt(float64((p.X-p2.X)*(p.X-p2.X) + (p.Y-p2.Y)*(p.Y-p2.Y) + (p.Z-p2.Z)*(p.Z-p2.Z)))
} }
func (p *Point) GetRoot() *Point { func (p *Node) GetRoot() *Node {
if p.Parent == nil || p.Parent == p { if p.root == nil {
return p return p
} else { } else {
return p.Parent.GetRoot() root := p.root.GetRoot()
p.root = root
return root
} }
} }
func PointFromString(s string) Point { func PointFromString(s string) Node {
parts := strings.Split(s, ",") parts := strings.Split(s, ",")
return Point{ return Node{
X: aoc.ParseInt(parts[0]), X: aoc.ParseInt(parts[0]),
Y: aoc.ParseInt(parts[1]), Y: aoc.ParseInt(parts[1]),
Z: aoc.ParseInt(parts[2]), Z: aoc.ParseInt(parts[2]),
@@ -41,30 +43,32 @@ func PointFromString(s string) Point {
type Connection struct { type Connection struct {
Length float64 Length float64
P1, P2 *Point P1, P2 *Node
} }
func do(test bool) (int, int) { func do(test bool) (int, int) {
rows := aoc.ReadFileRows(test) rows := aoc.ReadFileRows(test)
points := make([]Point, len(rows)) points := make([]Node, len(rows))
for ii, rr := range rows { for ii, rr := range rows {
points[ii] = PointFromString(rr) points[ii] = PointFromString(rr)
points[ii].id = ii points[ii].ID = ii + 1
} }
connections := []Connection{} connections := []Connection{}
for ii := range points { for ii := range points[:len(points)-1] {
for jj := range points[ii+1:] { for jj := ii + 1; jj < len(points); jj++ {
// for jj := range points[ii+1:] {
p1 := &points[ii] p1 := &points[ii]
p2 := &points[jj] p2 := &points[jj]
// p2 := &points[ii+1+jj]
connections = append(connections, Connection{ connections = append(connections, Connection{
Length: p1.DistanceTo(p2), Length: p1.DistanceTo(p2),
P1: &points[ii], P1: p1,
P2: &points[ii+1+jj], P2: p2,
}) })
} }
} }
@@ -72,44 +76,31 @@ func do(test bool) (int, int) {
// sort distances by length // sort distances by length
sort.Slice(connections, func(i, j int) bool { return connections[i].Length < connections[j].Length }) sort.Slice(connections, func(i, j int) bool { return connections[i].Length < connections[j].Length })
for ii := range connections[:10] { if test {
cc := &connections[ii] connections = connections[:10]
// if both have no parent, make the first one as the parent
if cc.P1.Parent == nil && cc.P2.Parent == nil {
cc.P1.Parent = cc.P1
cc.P2.Parent = cc.P1
cc.P1.Size++
// if the second one has no parent, connect it to the first one
} else if cc.P2.Parent == nil {
cc.P2.Parent = cc.P1
cc.P1.GetRoot().Size++
} else if cc.P1.Parent == nil {
cc.P1.Parent = cc.P2
cc.P2.GetRoot().Size++
// both have a parent -> make the parent of p2's root p1
} else { } else {
p2Root := cc.P2.GetRoot() connections = connections[:1000]
p2OldSize := p2Root.Size }
cc.P2.GetRoot().Parent = cc.P1.GetRoot() for ii := range connections {
cc := &connections[ii]
cc.P1.GetRoot().Size += p2OldSize root1 := cc.P1.GetRoot()
root2 := cc.P2.GetRoot()
if root1 != root2 {
root2.root = root1
root1.Size += root2.Size
} }
} }
roots := []*Point{} roots := []*Node{}
for ii := range points { for ii := range points {
pp := &points[ii] pp := &points[ii]
if pp.GetRoot() == pp { if pp.GetRoot() == pp {
roots = append(roots, pp) roots = append(roots, pp)
fmt.Println(pp.Size)
} }
} }