Paste: AoC 2022 day 3

Author: zip
Mode: factor
Date: Sat, 3 Dec 2022 12:56:53
Plain Text |
IN: dec-3

USING: grouping kernel math sequences sets splitting ;
USING: io.encodings.ascii io.encodings.string math ;

: common-item ( string -- string )
    dup length 2 / group [ 0 swap nth ] [ 1 swap nth ] bi
    intersect ;

: score-letter ( string -- number )
    ascii encode first 64 bitxor dup 32 > [ 32 - ] [ 26 + ] if ;

: find-badge ( array -- string )
    f [ swap dup f = [ drop ] [ intersect ] if ] reduce ;

: part1 ( string -- number )
    "\n" split "" swap remove [ common-item score-letter ] map
    sum ;

: part2 ( string -- number )
    "\n" split "" swap remove 3 group
    [ find-badge score-letter ] map sum ;

New Annotation

Summary:
Author:
Mode:
Body: