Paste: AOC Day 5

Author: Kacarott
Mode: factor
Date: Mon, 5 Dec 2022 08:27:05
Plain Text |
! Copyright (C) 2022 Keldan Chapman.
! See http://factorcode.org/license.txt for BSD license.
USING: kernel AOC prettyprint splitting sequences sequences.extras
       strings math math.parser ;
IN: AOC.2022.5

: parse-table ( input -- result )
    "\n\n" split-subseq first >string split-lines harvest
    [ [ 1 swap length 4 ] keep <step-slice> >string ] map
    flip [ [ 32 > ] filter ] map ;

: parse-instructions ( input -- result )
    "\n\n" split-subseq second >string split-lines harvest
    [ split-words [ 1 swap length 2 ] keep <step-slice> [ dec> ] map ] map ;

: move-stack ( matrix n from to modifier -- )
    [ pick [ nth dupd snip ] 2keep set-nth ] prepose
    [ rot [ nth append ] 2keep set-nth ] bi* ; inline

: run-simulation ( input modifier -- result )
    [ [ parse-table ] [ parse-instructions ] bi ] dip
    '[ dupd first3 [ 1 - ] bi@ _ move-stack ] each
    [ first ] map >string ; inline

: part-1 ( input -- result ) [ reverse ] run-simulation ;
: part-2 ( input -- result ) [ ] run-simulation ;

MAIN: [ 5 read-day-input [ part-1 . ] [ part-2 . ] bi ]

New Annotation

Summary:
Author:
Mode:
Body: