Paste: AoC 2022 Day 13

Author: nomennescio
Mode: factor
Date: Mon, 26 Dec 2022 18:23:46
Plain Text |
! 2022 nomennescio
USING: arrays io.encodings.utf8 io.files json.reader kernel match math math.order multiline prettyprint sets sequences sequences.extras splitting ;
QUALIFIED-WITH: multi-methods mm
IN: aoc2022

: parse-file ( path encoding -- pairs ) file-lines { "" } split [ [ json> ] map ] map ;
: parse-lines ( path encoding -- lines ) file-lines harvest { "[[2]]" "[[6]]" } append [ json> ] map ;

DEFER: <#

mm:GENERIC: <@ ( obj1 obj2 -- <=> )
mm:METHOD:  <@ { integer integer } <=> ;
mm:METHOD:  <@ { sequence sequence } <# ;
mm:METHOD:  <@ { integer sequence } [ 1array ] dip <# ;
mm:METHOD:  <@ { sequence integer } 1array <# ;

: <# ( seq1 seq2 -- <=> ) 2dup [ length 0 > ] bi@ swap [ [ [ unclip ] bi@ swapd <@ dup +eq+ = [ drop <# ] [ 2nip ] if ] [ +gt+ 2nip ] if ] [ +lt+ +eq+ ? 2nip ] if ;

: part1 ( pairs -- n ) flip first2 [ 1 + -rot <# +gt+ = [ drop 0 ] when ] 2map-index sum ;
: part2 ( lines -- n ) [ <# ] sort { { { 2 } } { { 6 } } } [ [ <# +eq+ = ] curry dupd find drop 1 + ] map product nip ;

CONSTANT: file "input-13.txt"

: day13 ( -- ) file [ utf8 parse-file part1 . ] [ utf8 parse-lines part2 . ] bi ;

MAIN: day13

Annotation: Fix using clause

Author: nomennescio
Mode: factor
Date: Mon, 26 Dec 2022 18:25:56
Plain Text |
USING: arrays io.encodings.utf8 io.files json.reader kernel match math math.order multiline prettyprint sequences sequences.extras sets sorting splitting ;

New Annotation

Summary:
Author:
Mode:
Body: