ShareMyScreen/AdventOfCode/2022/20/GrovePositioningSystem
To move a value I must rotate a substring of the vector. Should I represent the vector as a linked-list? I don't see how that's an improvement. I will just do the change by brute force. I read in the vector:
] list =. 0&". onaoclines 1 2 _3 3 _2 0 4
I plan to search for each number in the vector. For that to work, they have to be unique, as in the example. I check my puzzle input to make sure:
$ list =. 0&".;._2 LF ,~ wd 'clippaste' 5000 $~.list 3638
There are repeats! That's OK, I guess; I'll just do the operations on an index vector. I'm going to code this as simply as possible.
Hey! The spec is ambiguous. If I move the first value in a 7-atom vector forward 7 places, is that equivalent to a move of 0 or of 1? Either interpretation is reasonable. I guess 1 because that will make my code a bit easier.
After an embarrassing number of ill-thought-out tries, I arrive at a simple solution.
mix =: {{ NB. y is vector to mix iv =. i. # y NB. index vector to be rotated for_i. (<:#y) | y do. iv =. (iv i. i_index) |. iv NB. rotate selected element to front iv =. (1 |. (>:i) {. iv) , (>:i) }. iv NB. rotate front section left 1 end. +/ y {~ iv {~ (#y) | ((iv { y) i. 0) + 1000 2000 3000 NB. Find 0 in the mixed input, and the desired indexes, and the values }} mix list 3
Part 2 surprises me by not introducing difficulties.
mix =: {{ NB. y is vector to mix iv =. i. # y NB. index vector to be rotated for_i. ($~ 10 * $) (<:#y) | y do. iv =. (iv i. (#y) | i_index) |. iv NB. rotate selected element to front iv =. (1 |. (>:i) {. iv) , (>:i) }. iv NB. rotate front section left 1 end. +/ y {~ iv {~ (#y) | ((iv { y) i. 0) + 1000 2000 3000 NB. Find 0 in the mixed input, and the desired indexes, and the values }} mix list * 811589153 1623178306
($~ 10 * $) is a hook: the right side creates the new shape, 10 times the old; then the left side ($~) creates an array of that shape, cyclically repeating the atoms of the argument.