Paste: AOC day 13

Author: chunes
Mode: factor
Date: Sun, 13 Dec 2020 21:40:16
Plain Text |
USING: fry io.encodings.ascii io.files kernel locals math
math.functions math.parser prettyprint sequences splitting ;
IN: aoc.2020.13

: wait ( depart id -- n ) tuck mod - ;

: part1 ( -- )
    "input.txt" ascii file-lines first2
    [ dec> ] dip ",x" split harvest [ dec> ] map
    dupd [ wait ] with infimum-by [ wait ] keep * . ;

: next-fixed ( search fixed n index -- search fixed' )
    '[ dup _ wait _ = ] [ over + ] until ;

:: find-time ( seq -- n )
    seq unclip dup rot
    [
        1 + over integer?
        [ over mod over :> n next-fixed [ n lcm ] dip ] [ 2drop ] if
    ] each-index nip ;

: part2 ( -- )
    "input.txt" ascii file-lines second "," split [ dec> ] map
    find-time . ; ! 11 ms


! The key insight for part 2 is that not only do single buses
! have fixed cycles, but pairs of buses also have (much longer)
! fixed cycles for appearing near each other. So the more input
! there is, the larger the search increment becomes.

New Annotation

Summary:
Author:
Mode:
Body: