Paste: My attempt at AOC day 1
Author: | Leo Mehraban |
Mode: | factor |
Date: | Wed, 4 Dec 2024 00:55:08 |
Plain Text |
USING: kernel sequences math splitting math.parser ;
IN: aoc
FROM: sequences.extras => filter-index ;
: seperate-minimum ( seq -- seq min ) [ unclip-last swap V{ } clone [ overd [ > ] 1check [ spin suffix ] [ suffix ] if ] reduce ] keep like swap ;
: find-one-diff ( seq1 seq2 -- rest-seq1 rest-seq2 diff )
[ seperate-minimum ] bi@ swapd - abs ;
: unzip-seq ( seq -- seq1 seq2 ) [ [ nip even? ] filter-index ] [ [ nip odd? ] filter-index ] bi ;
: solve-day-one ( seq1 seq2 -- diff-sum ) 0 [ 2over 2length [ 0 > ] bi@ and ] [ [ find-one-diff ] dip + ] while 2nip ;
: parse-day-one-input ( string -- seq1 seq2 ) split-lines harvest [ split-words harvest ] map concat [ dec> ] map unzip-seq ;
: times-appears ( elt seq -- n ) [ = ] with filter length ;
: solve-day-one-part-two ( seq1 seq2 -- similarity ) 0 [ [ swap times-appears ] keep * + ] with reduce ;
New Annotation