Paste: AoC 2022 Day 5

Author: nomennescio
Mode: factor
Date: Mon, 5 Dec 2022 13:41:18
Plain Text |
! 2022 nomennescio
USING: io.encodings.utf8 io.files kernel math.intervals math.parser prettyprint sequences ;
IN: aoc2022

TUPLE: move repeat from to ;

: parse-crates ( strings -- stacks ) [ " " split ] map [ first ] sort-with [ second ] map ;
: 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 "" like ;
: part1 ( -- ) "input-5.edit.txt" utf8 parse-file [ move-crane ] each top . ;
: part2 ( -- ) "input-5.edit.txt" utf8 parse-file [ move-crane-9001 ] each top . ;
: day5 ( -- ) part1 part2 ;

MAIN: day5

Annotation: Refactor

Author: nomennescio
Mode: factor
Date: Mon, 5 Dec 2022 13:45:45
Plain Text |
! crates are in order anyway
: parse-crates ( strings -- stacks ) [ " " split second ] map ;

Annotation: refactor

Author: nomennescio
Mode: factor
Date: Mon, 5 Dec 2022 13:50:03
Plain Text |
: top ( stacks -- top ) [ last ] "" map-as ;

Annotation: refactor

Author: nomennescio
Mode: factor
Date: Mon, 5 Dec 2022 13:56:26
Plain Text |
USING: accessors io.encodings.utf8 io.files kernel math math.parser prettyprint regexp sequences sorting splitting ;

Annotation: refactor

Author: nomennescio
Mode: factor
Date: Mon, 5 Dec 2022 14:26:52
Plain Text |
! 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

New Annotation

Summary:
Author:
Mode:
Body: