Paste: AoC 2022 Day 13
Author: | Kacarott |
Mode: | factor |
Date: | Tue, 13 Dec 2022 06:20:24 |
Plain Text |
USING: kernel AOC prettyprint splitting sequences eval arrays
math math.order sorting ;
IN: AOC.2022.13
: parse ( input -- pair ) "\n\n" split-subseq [
"[" "{ " replace "]" " }" replace "," " " replace
eval( -- x x ) 2array
] map ;
: seq<seq ( seq1 seq2 -- <=> ) 2dup = [ 2drop +eq+ ] [ swap [ drop +lt+ ] [
swap [ drop +gt+ ] [
[ unclip ] bi@ swapd 2dup [ number? ] bi@ and [
2dup = [ 2drop seq<seq ] [ <=> 2nip ] if
] [
[ dup number? [ 1array ] when ] bi@
seq<seq dup +eq+ = [ drop seq<seq ] [ 2nip ] if
] if
] if-empty
] if-empty ] if ;
: part-1 ( input -- result ) parse [ [ first2 seq<seq +lt+ = ] dip 1 + 0 ? ] map-index sum ;
: part-2 ( input -- result ) parse concat { { { 2 } } { { 6 } } } append [ seq<seq ] sort
[ { { 2 } } ] [ { { 6 } } ] bi [ swap index 1 + ] 2bi@ * ;
MAIN: [ 13 read-day-input [ part-1 . ] [ part-2 . ] bi ]
New Annotation