Paste: AoC 2022 day 3
Author: | xr |
Mode: | factor |
Date: | Sat, 3 Dec 2022 10:26:58 |
Plain Text |
USING: sets grouping combinators kernel math io.files io.encodings.utf8 sequences ;
IN: aoc2022-3
: split-string ( seq -- first second ) dup length 2 / cut ;
: load-file ( path -- seq ) utf8 file-lines ;
: duplicate-item ( seq seq -- item ) intersect first ;
: reduce1 ( seq quot -- n ) [ unclip ] dip reduce ; inline
: score ( n -- n ) {
{ [ dup CHAR: a >= ] [ CHAR: a - 1 + ] }
{ [ dup CHAR: A >= ] [ CHAR: A - 27 + ] }
} cond ;
CONSTANT: input "resource:/work/aoc2022-3/input.txt"
: part1 ( -- n )
input load-file
[ split-string duplicate-item score ] map-sum
;
: part2 ( -- n )
input load-file
3 group
[ [ intersect ] reduce1 first score ] map-sum
;
New Annotation