Paste: AoC days 1-5

Author: yotes
Mode: factor
Date: Sat, 10 Dec 2022 01:24:19
Plain Text |
! DAY 5:
: get-stacks' ( lines -- stacks ) 
    but-last
    [ 4 <groups> >array [ >string [ { [ 32 = ] [ 91 = ] [ 93 = ] } 1|| ] trim ] map ] map
    flip [ reverse harvest >vector ] map >vector ;
: tokenize-instruction ( instruction -- amount from to )
    tokenize [ second ] [ fourth ] [ 5 swap nth ] tri [ string>number ] tri@ ;

: day5-pre ( i -- i' ) 
    split-chunks first2 [ get-stacks' ] dip 2array ;
:: day5-part1 ( input -- result )
    input first2 :> instructions :> stacks
    instructions [
        tokenize-instruction :> to :> from :> amount
        amount [
            from 1 - stacks nth :> source-stack
            to 1 - stacks nth :> target-stack
            source-stack pop
            target-stack push
        ] times
    ] each
    stacks [ last ] map "" join ;
:: day5-part2 ( input -- result )
    input first2 :> instructions :> stacks
    instructions [
        tokenize-instruction :> to :> from :> amount
        from 1- stacks nth :> source-stack
        to 1- stacks nth :> target-stack
        source-stack amount tail* :> source-slice
        source-stack length dup amount - swap source-stack delete-slice
        target-stack source-slice append! drop
    ] each
    stacks [ last ] map "" join ;

New Annotation

Summary:
Author:
Mode:
Body: