diff --git a/08/main.go b/08/main.go index a2e1e75..3115761 100644 --- a/08/main.go +++ b/08/main.go @@ -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) } }