Paste: aoc days 1&2
Author: | yotes |
Mode: | factor |
Date: | Fri, 9 Dec 2022 22:32:13 |
Plain Text |
USING: arrays circular io io.encodings.utf8 io.files
io.pathnames kernel math math.order math.parser prettyprint
sequences sorting splitting vocabs.metadata.resources words multiline ;
IN: mutt.aoc2022
: day ( -- ) ;
: lookup-word* ( name -- word )
"mutt.aoc2022" lookup-word ; inline
: get-input-path ( num -- path )
"mutt.aoc2022"
[ vocab-resource-files nth ]
[ vocab-dir-in-root ] bi prepend-path ;
: get-input ( num -- lines )
get-input-path utf8 file-lines ;
:: run ( num -- )
num [ number>string "d" prepend [ "-p1" append ] [ "-p2" append ] bi 2array ]
[ get-input ] bi :> input :> words
words [ lookup-word* [ input ] dip
[ [ num number>string "d" prepend lookup-word* execute ] dip execute ] curry
call( input -- result ) ] map first2 :> part2 :> part1
"Part 1 result: " write part1 .
"Part 2 result: " write part2 . ; inline
: d1 ( i -- i' )
"" 1array split
[ [ string>number ] map-sum ] map
[ >=< ] sort ;
: d1-p1 ( i -- r ) first ;
: d1-p2 ( i -- r ) 3 head sum ;
: rotate ( n seq -- seq' )
dup -rot <circular> swap
neg over change-circular-start swap
like ;
: parse-rps-inputs ( opponent player -- opponent' player' )
[ first ] bi@
[ CHAR: A - ] [ CHAR: X - ] bi* ;
: rps ( opponent player -- score )
dup -rot
{ 3 0 6 } rotate
nth ?1+ + ;
: get-move ( opponent outcome -- opponent player )
neg over swap { 2 0 1 } rotate nth ;
: d2 ( i -- i' )
[ " " split ] map ;
: d2-p1 ( i -- r )
[ first2 parse-rps-inputs rps ] map-sum ;
: d2-p2 ( i -- r )
[ first2 parse-rps-inputs get-move rps ] map-sum ;
New Annotation