For me today was a big lesson in keeping it simple. I did a lot of thinking about k-d trees and grids before staring at the ceiling for a few minutes, going "wait a second... this isn't that much" and just brute-forcing it (with some pretty ugly code).
Part 2 full solution
I'm so sorry about this code ![]()
#lang racket
(define (dist x y)
(let ([delta (for/list ([nth `(,first ,second ,third)])
(- (nth y) (nth x)))])
(apply + (map (curryr expt 2) delta))))
(define
coords
(map (λ (v) (map string->number v))
(map (curryr string-split ",")
(file->lines "test"))))
(define distances
(sort #:key first
(for*/list ([i (combinations coords 2)])
(list (dist (first i) (second i)) (first i) (second i))) <))
(define circuits (for/list ([coord coords]) (set coord)))
(for ([distance distances])
(define new-set (set))
(set! circuits
(filter identity
(for/list ([circuit circuits])
(if (or (set-member? circuit (second distance)) (set-member? circuit (third distance)))
(begin
(set! new-set (set-union new-set circuit))
(when (= (length coords) (length (set->list new-set)))
(display (* (first (second distance)) (first (third distance))))
(exit))
#f)
circuit))))
(set! circuits (cons new-set circuits)))