Paste: AOC day 1 fast
Author: | chunes |
Mode: | factor |
Date: | Tue, 1 Dec 2020 11:54:50 |
Plain Text |
USING: combinators deques dlists io.encodings.ascii io.files
kernel literals locals math math.parser prettyprint sequences
sorting ;
IN: aoc.2020.01fast
CONSTANT: input $[
"resource:work/aoc/2020/01/input.txt" ascii file-lines
[ string>number ] map natural-sort
]
DEFER: find-sum
:: (find-sum) ( deque target -- pair )
deque peek-front :> low
deque peek-back :> high
low high + :> sum
{
{ [ sum target = ] [ { low high } ] }
{ [ sum target < ] [ deque pop-front drop deque target find-sum ] }
[ deque pop-back drop deque target find-sum ]
} cond ;
: find-sum ( deque target -- pair/f )
over deque-empty? [ 2drop f ] [ (find-sum) ] if ;
: >deq ( seq -- deque ) DL{ } clone [ push-all-back ] keep ;
: part1 ( -- ) input >deq 2020 find-sum product . ;
:: part2 ( -- )
{ } :> pair!
input [ 2020 swap - input >deq swap find-sum dup pair! ]
find nip pair first2 * * . ;
New Annotation