! Copyright (C) 2022 Your name. ! See http://factorcode.org/license.txt for BSD license. USING: math.vectors math.functions aoc vectors 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!* first ] [ nipd insert-nth! ] 3bi ; ! Quotient and remainder based on floor. : knuthq ( a n -- q ) / floor ; : knuthr ( a n -- q ) 2dup knuthq * - ; : calc-index ( a n -- i ) [ [ knuthq ] [ knuthr ] 2bi + ] keep knuthr ; : shuffle-rule! ( i seq -- ) [ nth first ] [ [ + ] dip length make-index ] [ swapd move-to! ] 2tri ; : action ( i seq -- ) [ [ sequence? ] find-from drop ] keep over [ [ shuffle-rule! ] [ action ] 2bi ] [ 2drop ] if ; CONSTANT: input P" work/aoc/2022/20/input.txt" CONSTANT: test-input P" work/aoc/2022/20/test.txt" : parse ( file -- seq ) load-file >vector [ dec> 1vector ] map ; : answer ( seq -- n ) [ length ] [ [ 0 = ] find drop ] bi { 1000 2000 3000 } n+v [ swap mod ] with map swap nths sum ; : part1 ( file -- seq ) parse 0 over [ action ] keep answer