Paste: AoC 2022 day 5
Author: | xr |
Mode: | factor |
Date: | Mon, 5 Dec 2022 14:21:39 |
Plain Text |
───────┬──────────────────────────────────────────────────────────────────────────────────────────────────
│ File: aoc2022-5.factor
───────┼──────────────────────────────────────────────────────────────────────────────────────────────────
1 │
2 │
3 │ USING: accessors splitting aoc math.matrices sequences kernel
4 │ peg.parsers peg.search ;
5 │
6 │ IN: aoc2022-5
7 │
8 │ : split-input ( seqstring -- seqstring seqstring )
9 │ { "" } split1 ;
10 │
11 │ : not-space ( x -- ? ) 32 = not ;
12 │
13 │ : parse-crates ( seqstring -- seqseq )
14 │ transpose
15 │
16 │ [ last not-space ] filter
17 │
18 │ [ [ not-space ] filter but-last ] map ;
19 │
20 │ TUPLE: inst { move read-only } { from read-only } { to read-only } ;
21 │
22 │ C: <inst> inst
23 │
24 │ : parse-instruction ( seqstring -- seqvec )
25 │ [ integer-parser search first3 <inst> ] map ;
26 │
27 │ : parse-input ( seqstr -- seqses seqinst )
28 │ split-input
29 │ [ parse-crates ] [ parse-instruction ] bi*
30 │ ;
Author: | xr |
Mode: | factor |
Date: | Mon, 5 Dec 2022 19:23:42 |
Plain Text |
USING: strings vectors accessors splitting aoc math math.matrices sequences kernel
peg.parsers peg.search ;
IN: aoc2022-5
: split-input ( seqstring -- seqstring seqstring )
{ "" } split1 ;
: not-space ( x -- ? ) 32 = not ;
: parse-crates ( seqstring -- seqseq )
transpose
[ last not-space ] filter
[ [ not-space ] filter but-last >vector ] map
;
TUPLE: inst { move read-only } { from read-only } { to read-only } ;
C: <inst> inst
: parse-instruction ( seqstring -- seqvec )
[ integer-parser search first3 <inst> ] map ;
: parse-input ( seqstr -- seqses seqinst )
split-input
[ parse-crates ] [ parse-instruction ] bi*
;
: popk ( i seq -- elt )
nth pop ;
: pushk ( elt i seq -- )
nth push ;
: movek ( i j seq -- )
[ nip popk ] 2keep pushk ;
: nmovek ( n i j seq -- )
'[ _ _ _ movek ] times ;
: inst>nmovek ( inst seq -- )
[ [ move>> ] [ from>> 1 - ] [ to>> 1 - ] tri ] dip
nmovek ;
: tops ( seq -- seq )
[ pop ] map ;
: part1 ( -- x )
"resource:work/aoc2022-5/input.txt" load-file
parse-input
swap
[ reverse ] map
[ '[ _ inst>nmovek ] each ] keep
tops
>string
;
: popk2 ( n i seq -- elts )
[ swap cut ] change-nth ;
: pushk2 ( elts i seq -- )
[ append ] change-nth ;
: movek2 ( n i j seq -- )
[ nip popk2 ] 2keep pushk2 ;
: inst>nmovek2 ( inst seq -- )
[ [ move>> ] [ from>> 1 - ] [ to>> 1 - ] tri ] dip
movek2 ;
: tops2 ( seq -- seq )
[ first ] map ;
: part2 ( -- x )
"resource:work/aoc2022-5/input.txt" load-file
parse-input
swap
[ '[ _ inst>nmovek2 ] each ] keep
tops2
>string
;
Author: | xr |
Mode: | factor |
Date: | Mon, 5 Dec 2022 19:49:33 |
Plain Text |
USING: strings vectors accessors splitting aoc math math.matrices sequences kernel
peg.parsers peg.search ;
IN: aoc2022-5
: split-input ( seqstring -- seqstring seqstring )
{ "" } split1 ;
: not-space ( x -- ? ) 32 = not ;
: parse-crates ( seqstring -- seqseq )
transpose
[ last not-space ] filter
[ [ not-space ] filter but-last >vector ] map
;
TUPLE: inst { move read-only } { from read-only } { to read-only } ;
C: <inst> inst
: parse-instruction ( seqstring -- seqvec )
[ integer-parser search first3 <inst> ] map ;
: parse-input ( seqstr -- seqses seqinst )
split-input
[ parse-crates ] [ parse-instruction ] bi*
swap
;
: popk ( n i seq -- elts )
[ swap cut ] change-nth ;
: pushk ( elts i seq -- )
[ append ] change-nth ;
: movek ( n i j seq quot -- )
'[ nip popk @ ] 2keep pushk ; inline
: inst>movek ( inst seq quot -- )
[ [ move>> ] [ from>> 1 - ] [ to>> 1 - ] tri ] 2dip
movek ; inline
: tops ( seq -- seq )
[ first ] map ;
: part1 ( -- x )
"resource:work/aoc2022-5/input.txt" load-file
parse-input
[ '[ _ [ reverse ] inst>movek ] each ] keep
tops
>string
;
: part2 ( -- x )
"resource:work/aoc2022-5/input.txt" load-file
parse-input
[ '[ _ [ ] inst>movek ] each ] keep
tops
>string
;
New Annotation