───────┬────────────────────────────────────────────────────────────────────────────────────────────────── │ File: aoc2022-5.factor ───────┼────────────────────────────────────────────────────────────────────────────────────────────────── 1 │ ! Copyright (C) 2022 Your name. 2 │ ! See http://factorcode.org/license.txt for BSD license. 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 │ ! Use numbers as guide for the columns 16 │ [ last not-space ] filter 17 │ ! clean columns from empty spaces and strip guide 18 │ [ [ not-space ] filter but-last ] map ; 19 │ 20 │ TUPLE: inst { move read-only } { from read-only } { to read-only } ; 21 │ 22 │ C: inst 23 │ 24 │ : parse-instruction ( seqstring -- seqvec ) 25 │ [ integer-parser search first3 ] map ; 26 │ 27 │ : parse-input ( seqstr -- seqses seqinst ) 28 │ split-input 29 │ [ parse-crates ] [ parse-instruction ] bi* 30 │ ;