Paste: AoC 2026 03

Author: itohmaki
Mode: factor
Date: Fri, 4 Dec 2026 08:31:32
Plain Text |
USING: arrays hash-sets kernel math math.combinatorics sequences sets
sorting ;
IN: aoc.2026.03

: parse-row ( str -- row )
    [ swap CHAR: # = [ ] [ drop f ] if ] { } map-index-as sift ;

: parse-map ( strs -- marks )
    [ parse-row ] map ;

: map>rows ( marks -- rows )
    [ swap [ dupd rect> ] map >hash-set nip ] map-index ;

: rows>set ( rows -- set ) HS{ } [ union ] reduce ; inline

: both-in? ( x y set -- ? ) '[ _ in? ] bi@ and ; inline

: complex-key ( z -- key ) >rect 2array ; inline 

: has-rect? ( set pair -- set ? )
    ! set set point1 point2
    [ dup ] dip [ complex-key ] sort-by first2
    ! set set point3 point4
    2dup - >rect neg swap rect> '[ _ + ] bi@
    rot both-in? ;

: part1-row ( set row -- set count )
    members 2 <combinations> [ has-rect? ] count ;

: part1 ( set rows -- count )
    0 [ swap [ part1-row ] dip + ] reduce nip ;

: part2 ( set _ -- count )
    drop dup members 2 <combinations> [ has-rect? ] count nip 2 / ;

! "input.txt" ascii file-lines parse-map map>rows [ rows>set ] keep
! [ part1 ] [ part2 ] 2bi

New Annotation

Summary:
Author:
Mode:
Body: