Paste: AoC 2025 day 8

Author: annikapaprika
Mode: factor
Date: Mon, 8 Dec 2025 13:20:09
Plain Text |
USING: arrays assocs disjoint-sets kernel math
math.combinatorics math.parser math.statistics math.vectors
multiline peg.ebnf sets sequences sorting ;
IN: 2025.08

EBNF: parse [=[
    n = [0-9]+ => [[ dec> ]]
    line = n ","~ n ","~ n
]=]

: connections ( boxes -- boxes connections )
    dup 2 [ dup first2 v- l2-norm 2array ] map-combinations
    sort-values keys ;

: part-1 ( boxes -- n )
    connections 1000 head <disjoint-set>
    [ nip add-atoms ]
    [ nipd '[ _ equate ] assoc-each ]
    [ nip '[ _ representative ] map ] 3tri
    sorted-histogram values { 0 1 2 } kth-largests product ;

: part-2 ( boxes -- n )
    connections [ <disjoint-set> ] dip
    [ drop add-atoms ] 3keep [
        first2 pick equate 2dup
        '[ _ representative ] map cardinality 1 =
    ] find 3nip first2 [ first ] bi@ * ;

New Annotation

Summary:
Author:
Mode:
Body: