USING: arrays circular io.encodings.utf8 io.files kernel math math.parser math.vectors prettyprint sequences sequences.rotated ; IN: aoc.20 : I ( -- seq ) "~/factor/aoc/20/test.in" utf8 file-lines [ dec> ] map ; : remove-elt ( seq idx -- seq elt ) over length rem cut unclip [ append ] dip ; : add-elt ( seq elt idx -- seq ) 1 - pick length rem 1 + swapd cut rot prefix append ; : move-elt ( seq idx -- seq ) [ remove-elt dup first ] keep + add-elt ; : (mix) ( seq -- seq ) dup length [ over [ second ] map index move-elt ] each-integer ; : mix ( seq times -- seq ) [ [ 2array ] map-index ] dip [ (mix) ] times [ first ] map ; : decrypt ( seq times -- n ) mix 0 over index { 1000 2000 3000 } swap nths sum ; : part1 ( -- n ) I 1 decrypt ; : part2 ( -- n ) I 811589153 v*n 10 decrypt ; : solve ( -- ) part1 . part2 . ;