Paste: aoc 2018 day 10

Author: Krenium
Mode: factor
Date: Mon, 10 Dec 2018 07:59:47
Plain Text |
TUPLE: point p v t ;
C: <point> point

: get-points ( -- seq )
    "input10.txt" ascii file-lines [
        R/ -{,1}\d+/ all-matching-subseqs [ parse-number ] map
        first4 [ 2array ] 2bi@ 1 <point>
    ] map ;

: move1 ( p -- p' )
    dup v>> [ v+ ] curry change-p [ 1 + ] change-t ;

: move-points ( seq -- seq' ) [ move1 ] map ;

:: movet ( p t -- p' )
    p dup v>> t 1 - v*n [ v+ ] curry change-p [ drop t ]
    change-t ;

: goto-time ( seq t -- seq' ) [ movet ] curry map ;

: bounds ( seq -- w h )
    [ p>> ] map unzip [ minmax swap - ] bi@ ;

: area ( seq -- n ) bounds * ;

: normalize ( seq -- seq' )
    dup [ [ p>> first ] map ] [ [ p>> second ] map ] bi
    [ infimum ] bi@ 2array [ [ v- ] curry change-p ] curry map ;

: find-msg ( seq -- seq' )
    1/0. [ over area 2dup > ] [ [ move-points ] 2dip nip ] while
    2drop first t>> 1 - [ get-points ] dip goto-time ;

: draw-msg ( seq -- )
    normalize [ [ p>> reverse ] map ] [
        bounds [ 1 + ] bi@ swap 32 <matrix>
    ] bi [ CHAR: # ] 2dip [ set-indices ] keep [ "" like print ]
    each ;

get-points find-msg [ draw-msg ] [
    first t>> 1 -
    "Message will take %d seconds to appear.\n" printf
] bi

New Annotation

Summary:
Author:
Mode:
Body: