! Copyright (C) 2022 Your name. ! See http://factorcode.org/license.txt for BSD license. USING: math.vectors math.functions aoc vectors arrays multiline math math.parser kernel sequences sequences.extras ; IN: aoc.2022.20 : remove-nth!* ( from seq -- n ) [ nth ] [ remove-nth! drop ] 2bi ; : move-to! ( from to seq -- ) [ nip remove-nth!* ] [ nipd insert-nth! ] 3bi ; : calc-index ( a n -- i ) -1 + rem ; : shuffle-rule! ( i seq -- ) [ nth first ] [ [ + ] dip length calc-index ] [ swapd move-to! ] 2tri ; : (action) ( acc seq quot: ( x y -- z ) -- ) dup [ -rot ] dip 2keep pick [ [ nip shuffle-rule! ] [ [ nip 1 + ] dip rot (action) ] 3bi ] [ 4drop ] if ; inline recursive : seq-find ( i seq -- j ) [ = ] with [ second ] prepose find drop ; : action ( i seq -- ) [ seq-find ] (action) ; CONSTANT: input P" work/aoc/2022/20/input.txt" CONSTANT: test-input P" work/aoc/2022/20/test.txt" : 2vector ( x y -- vec ) 2array >vector ; : parse ( file -- seq ) load-file >vector [ [ dec> ] dip 2vector ] map-index ; : answer ( seq -- n ) [ first ] map dup [ length ] [ [ 0 = ] find drop ] bi { 1000 2000 3000 } n+v [ swap mod ] with map swap nths sum ; : parse2 ( file -- seq ) load-file >vector [ [ dec> 811589153 * ] dip 2vector ] map-index ; : part ( seq n -- n ) [ 0 over action ] times answer ; : day20 ( -- ) input parse 1 part . input parse2 10 part . ;