Files
advent-of-code-2025/08/main.go
2025-12-10 16:47:48 +01:00

130 lines
2.4 KiB
Go

package main
import (
"fmt"
"math"
"sort"
"strings"
"git.z1glr.de/advent-of-code-2025/pkg/aoc"
)
type Point struct {
X, Y, Z int
Parent *Point
Size int
id int
}
func (p *Point) DistanceTo(p2 *Point) 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)))
}
func (p *Point) GetRoot() *Point {
if p.Parent == nil || p.Parent == p {
return p
} else {
return p.Parent.GetRoot()
}
}
func PointFromString(s string) Point {
parts := strings.Split(s, ",")
return Point{
X: aoc.ParseInt(parts[0]),
Y: aoc.ParseInt(parts[1]),
Z: aoc.ParseInt(parts[2]),
Size: 1,
}
}
type Connection struct {
Length float64
P1, P2 *Point
}
func do(test bool) (int, int) {
rows := aoc.ReadFileRows(test)
points := make([]Point, len(rows))
for ii, rr := range rows {
points[ii] = PointFromString(rr)
points[ii].id = ii
}
connections := []Connection{}
for ii := range points {
for jj := range points[ii+1:] {
p1 := &points[ii]
p2 := &points[jj]
connections = append(connections, Connection{
Length: p1.DistanceTo(p2),
P1: &points[ii],
P2: &points[ii+1+jj],
})
}
}
// sort distances by length
sort.Slice(connections, func(i, j int) bool { return connections[i].Length < connections[j].Length })
for ii := range connections[:10] {
cc := &connections[ii]
// 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 {
p2Root := cc.P2.GetRoot()
p2OldSize := p2Root.Size
cc.P2.GetRoot().Parent = cc.P1.GetRoot()
cc.P1.GetRoot().Size += p2OldSize
}
}
roots := []*Point{}
for ii := range points {
pp := &points[ii]
if pp.GetRoot() == pp {
roots = append(roots, pp)
fmt.Println(pp.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
fmt.Println(res1)
return res1, 0
}
func main() {
if r1, r2 := do(true); r1 == 40 && r2 == 0 {
do(false)
}
}