Advent of Code 2025 Discussions

If it works, who's to complain? Well done on seeing the forest for the k-d trees.

Haha, I think the misdirect was the point of today. I got pretty frustrated due to my poor reading comprehension and came up with two different but equally wrong solutions before I erased everything and started over.

Spoilers
(define distance
  (match-lambda**
    [{(list x y z) (list a b c)}
     (+ (sqr (- x a))
        (sqr (- y b))
        (sqr (- z c)))]))

(define pairwise-distances
  (sort
   #:key car
   (let loop ([junctions junctions])
     (cond
       [(null? junctions) null]
       [else
        (define j₁ (car junctions))
        (define j* (cdr junctions))
        (append
         (for/list ([j₂ (in-list j*)])
           (list (distance j₁ j₂) j₁ j₂))
         (loop j*))]))
   <))

(define (connect-circuits [steps 1] [top-k 3])
  (define-values (end-pair connected)
    (for/fold ([end-pair #false]
               [circuits (map set junctions)]
               #:result
               (values end-pair (sort circuits > #:key set-count)))
              
              ([shortest (in-list pairwise-distances)]
               [step     (in-range steps)])
      (match-define (list j₁ j₂) (cdr shortest))
      (define c₁ (findf (lambda (c) (set-member? c j₁)) circuits))
      (define c₂ (findf (lambda (c) (set-member? c j₂)) circuits))
      (cond
        [(equal? c₁ c₂) (values end-pair circuits)]
        [else
         (define remainder (remove* `(,c₁ ,c₂) circuits))
         (values (cdr shortest) (cons (set-union c₁ c₂) remainder))])))
  
  (values
   end-pair
   (for/product ([circuit (in-list connected)]
                 [_       (in-range top-k)])
     (set-count circuit))))

; part 1
(let-values ([(_ product₃) (connect-circuits 1000 3)])
  product₃)
; part 2
(let-values ([(end-pair _) (connect-circuits +inf.0 1)])
  (apply * (map car end-pair)))
1 Like