Paste: AOC day 15

Author: chunes
Mode: factor
Date: Tue, 15 Dec 2020 07:05:44
Plain Text |
USING: arrays assocs kernel locals make math namespaces
prettyprint sequences ;
IN: aoc.2020.15

SYMBOL: seen

: initialize-seen ( seq -- )
    <enumerated> [ first2 swap dup 2array ] H{ } map>assoc
    seen set ;

: last-number ( -- n ) building get last ;
: current-index ( -- i ) building get length ;
: next-number ( -- n ) last-number seen get at first2 - ;

:: remember ( -- n )
    current-index 0 0 1 next-number :> n n dup seen get key?
    [ seen get at [ exchange ] keep set-nth ]
    [ current-index dup 2array swap seen get set-at 4drop ] if
    n ;

: play-game ( n seq -- newseq )
    dup [ length - ] dip
    dup initialize-seen [ % [ remember , ] times ] { } make ;

: part1 ( -- ) 2020 { 0 3 1 6 7 5 } play-game last . ;
: part2 ( -- ) 30,000,000 { 0 3 1 6 7 5 } play-game last . ;  ! Part 2 takes about 32 seconds. Hmmm..

New Annotation

Summary:
Author:
Mode:
Body: