Paste: advent-of-code 24

Author: jon
Mode: factor
Date: Tue, 13 Feb 2018 14:43:07
Plain Text |
! Copyright (C) 2018 Your name.
! See http://factorcode.org/license.txt for BSD license.
USING: arrays combinators io.encodings.ascii io.files kernel
math.parser sequences splitting ;
IN: aoc.2017.24.1


: (extend-bridge) ( pieces last -- {piece,pieces}s )
    over [ swapd [ {
            { [ over first over = ] [ drop ] } 
            { [ over second over = ] [ drop reverse ] } 
            [ 2drop f ]
        } cond ] 2dip
        rot [ [ remove-nth ] dip swap 2array ] [ 2drop f ] if*
    ]
    curry curry map-index harvest ;
    
: extend-bridge ( bridge pieces -- {bridge,pieces}s )
    over last last
    (extend-bridge) [ { } 2array 1array ] [ [ first2 [ suffix ] dip 2array ] with map
    ] if-empty ;

: (valid-bridges) ( {bridge,pieces} -- {bridge,pieces}' )
    [ first2 [ 1array ] [ extend-bridge (valid-bridges) ] if-empty ] map concat ;

: valid-bridges ( pieces -- bridges )
    { { 0 0 } } swap 2array 1array (valid-bridges) ;

: p1 ( -- n )
"/tmp/input" ascii file-lines [ "/" split [ string>number ] map ] map
valid-bridges [ concat sum ] map supremum ;

: p2 ( -- n )
"/tmp/input" ascii file-lines [ "/" split [ string>number ] map ] map
valid-bridges [ [ length ] map supremum ] keep [ length = ] with filter 
[ concat sum ] map supremum ;

New Annotation

Summary:
Author:
Mode:
Body: