Paste: aoc19

Author: jon
Mode: factor
Date: Fri, 30 Mar 2018 09:57:15
Plain Text |
: Mij ( {i,j} grid -- e )
    [ first2 ] dip nth nth ;
: find-start ( grid -- {i,j} dir )
    first [ CHAR: space = not ] find drop 0 2array { 0 1 } ;

: turns ( dir -- {left,right} )
    [ [ second neg ] [ first ] bi 2array ]
    [ [ second ] [ first neg ] bi 2array ] bi 2array ;

: can-walk? ( grid pos dir -- ? )
    v+ swap Mij CHAR: space = not ;

: choose-new-dir ( grid pos dir -- dir' )
    turns [ can-walk? ] with with find nip ;

: do-walk ( grid pos dir -- pos' dir' )
    [ drop ] 2dip [ v+ ] keep ;
: walk ( grid pos dir -- pos' dir' )
    3dup can-walk? [ do-walk ]
    [ [ 2dup ] dip choose-new-dir [ do-walk ] [ 2drop f f ] if* ] if ;

: walkall ( grid -- )
    [ find-start ] keep
    [ -rot walk dup ] [ pick swap Mij ] bi-curry produce 2nip
    [ [ LETTER? ] filter >string print ] [ length 1 + . ] bi ;

New Annotation

Summary:
Author:
Mode:
Body: