! parsing full input ! 2022 nomennescio USING: accessors ascii io.encodings.utf8 io.files kernel math math.parser prettyprint regexp sequences splitting ; IN: aoc2022 TUPLE: move repeat from to ; : parse-crates ( strings -- stacks ) flip [ [ Letter? ] filter reverse ] map harvest ; : parse-moves ( strings -- moves ) [ R/ \d+/ all-matching-slices [ dec> ] map first3 move boa ] map ; : parse-file ( path encoding -- stacks moves ) file-lines [ empty? ] split-when first2 [ parse-crates ] [ parse-moves ] bi* ; : move-from ( stacks move -- stacks' crates ) dup from>> 1 - pick [ swap repeat>> cut* swap ] change-nth ; : move-to ( stacks crates move -- stacks' ) to>> 1 - pick [ prepend ] change-nth ; : move-crane ( stacks move -- stacks' ) [ move-from reverse ] [ move-to ] bi ; : move-crane-9001 ( stacks move -- stacks' ) [ move-from ] [ move-to ] bi ; : top ( stacks -- top ) [ last ] "" map-as ; : part1 ( -- ) "input-5.txt" utf8 parse-file [ move-crane ] each top . ; : part2 ( -- ) "input-5.txt" utf8 parse-file [ move-crane-9001 ] each top . ; : day5 ( -- ) part1 part2 ; MAIN: day5