Paste: AoC 2022 Day 20

Author: Kacarott
Mode: factor
Date: Tue, 20 Dec 2022 10:25:35
Plain Text |
! Copyright (C) 2022 Keldan Chapman.
! See http://factorcode.org/license.txt for BSD license.
USING: kernel AOC prettyprint splitting sequences math.parser
       sequences.extras accessors math ;
IN: AOC.2022.20

TUPLE: unique val ;
M: unique equal? 2drop f ;

: parse ( input mul -- seq ) [ split-lines ] dip '[ dec> _ * unique boa ] V{ } map-as ;

: mix ( input times mul -- seq )
    '[ _ parse ] dip over clone '[ _ [ dup pick index
        [ nip swap remove-nth! ]
        [ over val>> + pick length rem rot [ insert-nth! ] keep ] 2bi
    ] each ] times ;

: result ( seq -- result )
    [ val>> ] map 0 over index { 1000 2000 3000 } spin
    '[ _ + _ [ length rem ] keep nth ] map-sum ;

: part-1 ( input -- result ) 1 1 mix result ;
: part-2 ( input -- result ) 10 811589153 mix result ;

MAIN: [ 20 read-day-input [ part-1 . ] [ part-2 . ] bi ]

New Annotation

Summary:
Author:
Mode:
Body: