worked on day 02 part2

This commit is contained in:
Simon Ziegler
2025-12-10 16:47:48 +01:00
parent a05cb9d41f
commit 46c27fd8a6

View File

@@ -13,6 +13,7 @@ type Point struct {
X, Y, Z int X, Y, Z int
Parent *Point Parent *Point
Size int Size int
id int
} }
func (p *Point) DistanceTo(p2 *Point) float64 { func (p *Point) DistanceTo(p2 *Point) float64 {
@@ -23,7 +24,7 @@ func (p *Point) GetRoot() *Point {
if p.Parent == nil || p.Parent == p { if p.Parent == nil || p.Parent == p {
return p return p
} else { } else {
return p.GetRoot() return p.Parent.GetRoot()
} }
} }
@@ -50,16 +51,20 @@ func do(test bool) (int, int) {
for ii, rr := range rows { for ii, rr := range rows {
points[ii] = PointFromString(rr) points[ii] = PointFromString(rr)
points[ii].id = ii
} }
connections := []Connection{} connections := []Connection{}
for ii, p1 := range points { for ii := range points {
for _, p2 := range points[ii+1:] { for jj := range points[ii+1:] {
p1 := &points[ii]
p2 := &points[jj]
connections = append(connections, Connection{ connections = append(connections, Connection{
Length: p1.DistanceTo(&p2), Length: p1.DistanceTo(p2),
P1: &p1, P1: &points[ii],
P2: &p2, P2: &points[ii+1+jj],
}) })
} }
} }
@@ -67,10 +72,8 @@ 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 _, cc := range connections[:10] { for ii := range connections[:10] {
fmt.Println(*cc.P1) cc := &connections[ii]
fmt.Println(*cc.P2)
// if both have no parent, make the first one as the parent // if both have no parent, make the first one as the parent
if cc.P1.Parent == nil && cc.P2.Parent == nil { if cc.P1.Parent == nil && cc.P2.Parent == nil {
cc.P1.Parent = cc.P1 cc.P1.Parent = cc.P1
@@ -78,8 +81,6 @@ func do(test bool) (int, int) {
cc.P1.Size++ cc.P1.Size++
// fmt.Println(cc.P2.GetRoot().Size)
// if the second one has no parent, connect it to the first one // if the second one has no parent, connect it to the first one
} else if cc.P2.Parent == nil { } else if cc.P2.Parent == nil {
cc.P2.Parent = cc.P1 cc.P2.Parent = cc.P1
@@ -92,23 +93,27 @@ func do(test bool) (int, int) {
// both have a parent -> make the parent of p2's root p1 // both have a parent -> make the parent of p2's root p1
} else { } else {
p2OldSize := cc.P2.GetRoot().Size p2Root := cc.P2.GetRoot()
p2OldSize := p2Root.Size
cc.P2.GetRoot().Parent = cc.P1 cc.P2.GetRoot().Parent = cc.P1.GetRoot()
cc.P1.GetRoot().Size += p2OldSize cc.P1.GetRoot().Size += p2OldSize
} }
} }
roots := []Point{} roots := []*Point{}
for _, pp := range points { for ii := range points {
if pp.GetRoot() == &pp { pp := &points[ii]
if pp.GetRoot() == pp {
roots = append(roots, pp) roots = append(roots, pp)
fmt.Println(pp.Size)
} }
} }
sort.Slice(roots, func(i, j int) bool { return roots[i].Size < roots[j].Size }) sort.Slice(roots, func(i, j int) bool { return roots[i].Size > roots[j].Size })
res1 := roots[0].Size * roots[1].Size * roots[2].Size res1 := roots[0].Size * roots[1].Size * roots[2].Size