Paste: AOC day 4

Author: chunes
Mode: factor
Date: Sat, 4 Dec 2021 06:26:36
Plain Text |
    USING: arrays grouping io io.encodings.ascii io.files kernel
    literals math math.parser prettyprint sequences sequences.extras
    splitting ;

    CONSTANT: numbers $[
        "input.txt" ascii [ readln ] with-file-reader
        "," split [ dec> ] map
    ]

    CONSTANT: boards $[
        "input.txt" ascii file-lines { "" } split rest
        [ [ " " split [ dec> ] map ] map-concat sift ] map
    ]

    : mark ( seq n -- newseq ) 1array { -1 } replace ;
    : mark-all ( seq n -- newseq ) [ mark ] curry map ;
    : row-filled? ( matrix -- ? ) [ [ -1 = ] all? ] any? ;
    : won? ( seq -- ? ) 5 group dup flip [ row-filled? ] either? ;
    : unmarked-sum ( seq -- newseq ) { -1 } { 0 } replace sum ;

    : part1 ( -- )
        boards numbers [ mark-all dup [ won? ] any? ] find nip swap
        [ won? ] find nip unmarked-sum * . ;

    : part2 ( -- )
        boards numbers [ mark-all [ won? ] reject dup length 1 = ]
        find drop 1 + numbers swap tail [ first ] dip
        [ mark dup won? ] find nip swap unmarked-sum * . ;

    part1 part2

New Annotation

Summary:
Author:
Mode:
Body: