Paste: aoc5

Author: erg
Mode: factor
Date: Mon, 5 Dec 2022 05:30:02
Plain Text |
! Copyright (C) 2022 Doug Coleman.
! See http://factorcode.org/license.txt for BSD license.
USING: grouping kernel math math.parser sequences splitting ;
IN: 2022-aoc5

: parse-crate-line ( string -- seq )
    4 group harvest
    [ [ "[] " member? ] trim ] { } map-as ;

: parse-move-line ( string -- count,from,to )
    " " split { 1 3 5 } swap nths [ dec> ] map first3 [ 1 - ] bi@ 3array ;

: parse-aoc5-lines ( string -- crates insns )
    "\n\n" split1
    [ split-lines but-last [ parse-crate-line ] map flip [ reverse >vector harvest ] V{ } map-as ]
    [ split-lines [ parse-move-line ] map ] bi* ;

:: do-move ( seqs n from to -- )
    seqs { from to } swap nths first2 '[ _ _ [ pop ] [ push ] bi* ] n swap times ;

:: do-move2 ( seqs n from to -- )
    V{ } clone :> temp
    from seqs nth '[ _ pop temp push ] n swap times
    temp reverse to seqs nth push-all ;

: aoc5-1 ( string -- seqs )
    parse-aoc5-lines
    [ dupd first3 do-move ] each
    [ last ] map concat ;

: aoc5-2 ( string -- seqs )
    parse-aoc5-lines
    [ dupd first3 do-move2 ] each
    [ last ] map concat ;

Annotation: gripes

Author: erg
Mode: factor
Date: Mon, 5 Dec 2022 05:41:12
Plain Text |
- puzzle input better as { from to n }
- swap nths is bad
- ntimes word ( quot n -- ) for frying in the from,to seqs
- i should have used map range 1 3

Annotation: no locals, nth-of word, ntimes

Author: erg
Mode: factor
Date: Mon, 5 Dec 2022 06:21:40
Plain Text |
! Copyright (C) 2022 Doug Coleman.
! See http://factorcode.org/license.txt for BSD license.
USING: arrays combinators.extras grouping kernel math
math.parser sequences shuffle splitting vectors ;
IN: 2022-aoc5

: ntimes ( quot n -- ) swap times ; inline
: nths-of ( seq indices -- elts ) swap nths ; inline
: nth-of ( seq n -- elt ) swap nth ; inline

: parse-crate-line ( string -- seq )
    4 group harvest
    [ [ "[] " member? ] trim ] { } map-as ;

: parse-move-line ( string -- count,from,to )
    " " split { 3 5 1 } nths-of
    [ dec> ] map
    first3 [ [ 1 - ] bi@ ] dip 3array ;

: parse-aoc5-lines ( string -- crates insns )
    "\n\n" split1 [
        split-lines but-last [ parse-crate-line ] map flip
        [ reverse >vector harvest ] V{ } map-as
    ] [ split-lines [ parse-move-line ] map ] bi* ;

: do-move ( seqs from to n -- )
    [
        2array nths-of first2
        '[ _ _ [ pop ] [ push ] bi* ]
    ] dip ntimes ;

: do-move2 ( seqs from to n -- )
    [ 2array nths-of first2 V{ } clone ] dip
    [ nipd [ '[ _ pop _ push ] ] dip ntimes ]
    [ drop nipd reverse swap push-all ] 4bi ;

: aoc5-1 ( string -- seqs )
    parse-aoc5-lines
    [ dupd first3 do-move ] each
    [ last ] map concat ;

: aoc5-2 ( string -- seqs )
    parse-aoc5-lines
    [ dupd first3 do-move2 ] each
    [ last ] map concat ;

Annotation: no need for map-as

Author: erg
Mode: factor
Date: Mon, 5 Dec 2022 06:24:47
Plain Text |
! Copyright (C) 2022 Doug Coleman.
! See http://factorcode.org/license.txt for BSD license.
USING: arrays combinators.extras grouping kernel math
math.parser sequences shuffle splitting vectors ;
IN: 2022-aoc5

: ntimes ( quot n -- ) swap times ; inline
: nths-of ( seq indices -- elts ) swap nths ; inline
: nth-of ( seq n -- elt ) swap nth ; inline

: parse-crate-line ( string -- seq )
    4 group harvest
    [ [ "[] " member? ] trim ] { } map-as ;

: parse-move-line ( string -- count,from,to )
    " " split { 3 5 1 } nths-of
    [ dec> ] map
    first3 [ [ 1 - ] bi@ ] dip 3array ;

: parse-aoc5-lines ( string -- crates insns )
    "\n\n" split1 [
        split-lines but-last [ parse-crate-line ] map flip
        [ reverse >vector harvest ] map
    ] [ split-lines [ parse-move-line ] map ] bi* ;

: do-move ( seqs from to n -- )
    [
        2array nths-of first2
        '[ _ _ [ pop ] [ push ] bi* ]
    ] dip ntimes ;

: do-move2 ( seqs from to n -- )
    [ 2array nths-of first2 V{ } clone ] dip
    [ nipd [ '[ _ pop _ push ] ] dip ntimes ]
    [ drop nipd reverse swap push-all ] 4bi ;

: aoc5-1 ( string -- seqs )
    parse-aoc5-lines
    [ dupd first3 do-move ] each
    [ last ] map concat ;

: aoc5-2 ( string -- seqs )
    parse-aoc5-lines
    [ dupd first3 do-move2 ] each
    [ last ] map concat ;

New Annotation

Summary:
Author:
Mode:
Body: