Files
adventofcode/2025/day08/main.go
2025-12-08 13:28:53 -06:00

84 lines
1.6 KiB
Go

package main
import (
"cmp"
"fmt"
"maps"
"slices"
"strings"
h "git.bullercodeworks.com/brian/adventofcode/helpers"
)
func main() {
inp := h.StdinToStringSlice()
solve(inp)
}
func solve(inp []string) {
var part1, part2 int
pts := inputToMap(inp)
circuits := []map[h.Coordinate3d]struct{}{}
for _, b := range pts {
circuits = append(circuits, map[h.Coordinate3d]struct{}{b: {}})
}
pairs := [][2]h.Coordinate3d{}
for i, b1 := range pts {
for _, b2 := range pts[i+1:] {
pairs = append(pairs, [2]h.Coordinate3d{b1, b2})
}
}
slices.SortFunc(pairs, func(a, b [2]h.Coordinate3d) int {
return cmp.Compare(a[0].Distance(a[1]), b[0].Distance(b[1]))
})
for i, p := range pairs {
var c1, c2 int
for i, c := range circuits {
if _, ok := c[p[0]]; ok {
c1 = i
}
if _, ok := c[p[1]]; ok {
c2 = i
}
}
if c1 != c2 {
maps.Copy(circuits[c1], circuits[c2])
circuits = slices.Delete(circuits, c2, c2+1)
}
if i+1 == 1000 {
slices.SortFunc(circuits, func(a, b map[h.Coordinate3d]struct{}) int {
return -cmp.Compare(len(a), len(b))
})
part1 = len(circuits[0]) * len(circuits[1]) * len(circuits[2])
}
if len(circuits) == 1 {
part2 = (p[0].X * p[1].X)
break
}
}
fmt.Println("# Part 1")
fmt.Println(part1)
fmt.Println()
fmt.Println("# Part 2")
fmt.Println(part2)
}
func inputToMap(inp []string) []h.Coordinate3d {
var ret []h.Coordinate3d
for i := range inp {
pts := strings.Split(inp[i], ",")
ret = append(ret,
h.Coordinate3d{
X: h.Atoi(pts[0]),
Y: h.Atoi(pts[1]),
Z: h.Atoi(pts[2]),
},
)
}
return ret
}