day 08 part 2

This commit is contained in:
Simon Ziegler
2025-12-11 14:57:47 +01:00
parent 4e26c55eb7
commit aee88264c5

View File

@@ -76,13 +76,55 @@ func do(test bool) (int, int) {
// sort distances by length
sort.Slice(connections, func(i, j int) bool { return connections[i].Length < connections[j].Length })
threshold := 1000
if test {
connections = connections[:10]
} else {
connections = connections[:1000]
threshold = 10
}
var res1, res2 int
for ii := range connections {
if ii == threshold {
roots := []*Node{}
for ii := range points {
pp := &points[ii]
if pp.GetRoot() == pp {
roots = append(roots, pp)
}
}
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)
} else if ii > threshold {
cc := connections[ii-1]
// check wether it is one big circuit
root := points[0].GetRoot()
multipleCircuits := false
for jj := range points[1:] {
pp := &points[jj]
if pp.GetRoot() != root {
multipleCircuits = true
break
}
}
if !multipleCircuits {
res2 = cc.P1.X * cc.P2.X
fmt.Println(res2)
break
}
}
cc := &connections[ii]
root1 := cc.P1.GetRoot()
@@ -95,26 +137,11 @@ func do(test bool) (int, int) {
}
}
roots := []*Node{}
for ii := range points {
pp := &points[ii]
if pp.GetRoot() == pp {
roots = append(roots, pp)
}
}
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
return res1, res2
}
func main() {
if r1, r2 := do(true); r1 == 40 && r2 == 0 {
if r1, r2 := do(true); r1 == 40 && r2 == 25272 {
do(false)
}
}