Move arrows along a contourDrawing a cube in ASCII artReorganize By ArrowsMarching Squares LookupMaze generating one linerConvert ASCII box drawing into UnicodeDraw some ASCII-boxesOperator ASCII artIdentify the direction of lines in an ASCII-figureA Slashy Dashy SpiralExecute Triangularity Move

Three legged NOT gate? What is this symbol?

changing number of arguments to a function in secondary evaluation

I accidentally overwrote a Linux binary file

Visa National - No Exit Stamp From France on Return to the UK

Is it okay for a ticket seller to grab a tip in the USA?

Different inverter (logic gate) symbols

What is my malfunctioning AI harvesting from humans?

Bitcoin successfully deducted on sender wallet but did not reach receiver wallet

Help evaluating integral (anything simple that I am missing?)

Opposite for desideratum to mean "something not wished for"

The equation of motion for a scalar field in curved spacetime in terms of the covariant derivative

Was the 2019 Lion King film made through motion capture?

Why isn’t SHA-3 in wider use?

As a 16 year old, how can I keep my money safe from my mother?

What game uses dice with sides powers of 2?

Find y in this equation

What is the difference between 型 and 形?

AsyncDictionary - Can you break thread safety?

Dropdowns & Chevrons for Right to Left languages

Wherein the Shatapatha Brahmana it was mentioned about 8.64 lakh alphabets in Vedas?

What does Apple mean by "This may decrease battery life"?

Extremely casual way to make requests to very close friends

Am I overreacting to my team leader's unethical requests?

Generate Brainfuck for the numbers 1–255



Move arrows along a contour


Drawing a cube in ASCII artReorganize By ArrowsMarching Squares LookupMaze generating one linerConvert ASCII box drawing into UnicodeDraw some ASCII-boxesOperator ASCII artIdentify the direction of lines in an ASCII-figureA Slashy Dashy SpiralExecute Triangularity Move






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








28












$begingroup$


Sandboxed



Given a set of closed non-overlapping 2d contours (separated by at least one space even on diagonals) with arrows oriented consistently in the same clockwise or counter-clockwise direction (each contour has its own direction) and a positive number n, move the arrows n steps along the contours in the respective direction.
The arrows are represented by > v < ^ respectively for right, down, left and up directions. There the other characters are - (horizontal), | (vertical) and + (corner).
When an arrow is on a corner, it keeps its current direction and changes it only after the turn is taken.



There will always be a straight segment (or a space) between any two corners (like +-+ for the horizontal and similar for the vertical) - in other words the sharp U turns are forbidden. The segments between the corners are either vertical or horizontal and the bend at a corner is always 90 degree.



Input:



  • a positive integer - n - number of steps

  • an ASCII representation of the contours - it can be a multiline string, a list of strings, a list of characters and so on.

Output:



The same contours with all arrows shifted n steps in each contour's overall direction.



Test cases:



1.



Input:



n = 1



 +----->-> 
| |
| v---+
| |
+---<-------+


Output:



 +------>+
| v
| +>--+
| |
+--<--------+


2.



Input:



n = 2



 +-----+ +---+ 
| | | |
+-->--+ | v
| |
+--->---+ |
| |
+------<<---+


Output:



 +-----+ +---+
| | | |
+---->+ | |
| |
+----->-+ v
| |
+----<<-----+


3.



Input:



n = 3



 +---+ +---+ +-------+ 
| | | v | |
^ | | | +-<-+ |
| | ^ | | v
| +---+ +-->----+ |
| |
| +-------+ +---+ |
| | | v | |
+---+ +---+ +---+


Output:



 +>--+ ^---+ +-------+
| | | | ^ |
| | | | +---+ |
| | | | | |
| +---+ v----->-+ |
| |
| +-------+ +---+ v
| | | | | |
+---+ +-<-+ +---+


4.



Input:



n = 1



+--+ 
| |
| +---+
| |
+----+ |
| |
+-+


Output:



+--+ 
| |
| +---+
| |
+----+ |
| |
+-+


5.



Input



n = 4



^>>>>
^ v
^ v>>>>
^ v
<<<<<<<<v


Output:



^>>>>
^ v
^ v>>>>
^ v
<<<<<<<<v


6.



Input:



n = 1



^->
^ v
<<v


Output:



^>+
^ v
<<v


Write a function or a program solving the above task. The shortest code in bytes in every language wins. Don't be discouraged by the golfing languages. Explanation of the algorithm and the code is highly appreciated.










share|improve this question











$endgroup$













  • $begingroup$
    Can two contours touch their corners on a diagonal, or a contour touch itself like that?
    $endgroup$
    – xnor
    Jul 31 at 6:57







  • 4




    $begingroup$
    "Given a set of closed non-overlapping 2d contours ... with arrows oriented consistently in the same clockwise or counter-clockwise direction" sounds to me like every contour is oriented in the same direction, whereas from the test cases, it seems the arrows only have be consistent within a contour.
    $endgroup$
    – xnor
    Jul 31 at 7:00






  • 3




    $begingroup$
    @xnor Thanks for your comments! - No, contours are not allowed to touch each other/itself on a diagonal. - Each contour has its own directon. I'll update the description.
    $endgroup$
    – Galen Ivanov
    Jul 31 at 7:11






  • 2




    $begingroup$
    Is input with no space between the walls possible? Eg: Try it online!. I know you said "separated by at least one space" but I was unclear if that applied only to independent loops or if it applied to a single loop as well.
    $endgroup$
    – Jonah
    Aug 1 at 0:21







  • 1




    $begingroup$
    @Jonah No, it's not possible: There will always be a straight segment (or a space) between any two corners (like +-+ for the horizontal and similar for the vertical) - in other words the sharp U turns are forbidden.
    $endgroup$
    – Galen Ivanov
    Aug 1 at 6:19

















28












$begingroup$


Sandboxed



Given a set of closed non-overlapping 2d contours (separated by at least one space even on diagonals) with arrows oriented consistently in the same clockwise or counter-clockwise direction (each contour has its own direction) and a positive number n, move the arrows n steps along the contours in the respective direction.
The arrows are represented by > v < ^ respectively for right, down, left and up directions. There the other characters are - (horizontal), | (vertical) and + (corner).
When an arrow is on a corner, it keeps its current direction and changes it only after the turn is taken.



There will always be a straight segment (or a space) between any two corners (like +-+ for the horizontal and similar for the vertical) - in other words the sharp U turns are forbidden. The segments between the corners are either vertical or horizontal and the bend at a corner is always 90 degree.



Input:



  • a positive integer - n - number of steps

  • an ASCII representation of the contours - it can be a multiline string, a list of strings, a list of characters and so on.

Output:



The same contours with all arrows shifted n steps in each contour's overall direction.



Test cases:



1.



Input:



n = 1



 +----->-> 
| |
| v---+
| |
+---<-------+


Output:



 +------>+
| v
| +>--+
| |
+--<--------+


2.



Input:



n = 2



 +-----+ +---+ 
| | | |
+-->--+ | v
| |
+--->---+ |
| |
+------<<---+


Output:



 +-----+ +---+
| | | |
+---->+ | |
| |
+----->-+ v
| |
+----<<-----+


3.



Input:



n = 3



 +---+ +---+ +-------+ 
| | | v | |
^ | | | +-<-+ |
| | ^ | | v
| +---+ +-->----+ |
| |
| +-------+ +---+ |
| | | v | |
+---+ +---+ +---+


Output:



 +>--+ ^---+ +-------+
| | | | ^ |
| | | | +---+ |
| | | | | |
| +---+ v----->-+ |
| |
| +-------+ +---+ v
| | | | | |
+---+ +-<-+ +---+


4.



Input:



n = 1



+--+ 
| |
| +---+
| |
+----+ |
| |
+-+


Output:



+--+ 
| |
| +---+
| |
+----+ |
| |
+-+


5.



Input



n = 4



^>>>>
^ v
^ v>>>>
^ v
<<<<<<<<v


Output:



^>>>>
^ v
^ v>>>>
^ v
<<<<<<<<v


6.



Input:



n = 1



^->
^ v
<<v


Output:



^>+
^ v
<<v


Write a function or a program solving the above task. The shortest code in bytes in every language wins. Don't be discouraged by the golfing languages. Explanation of the algorithm and the code is highly appreciated.










share|improve this question











$endgroup$













  • $begingroup$
    Can two contours touch their corners on a diagonal, or a contour touch itself like that?
    $endgroup$
    – xnor
    Jul 31 at 6:57







  • 4




    $begingroup$
    "Given a set of closed non-overlapping 2d contours ... with arrows oriented consistently in the same clockwise or counter-clockwise direction" sounds to me like every contour is oriented in the same direction, whereas from the test cases, it seems the arrows only have be consistent within a contour.
    $endgroup$
    – xnor
    Jul 31 at 7:00






  • 3




    $begingroup$
    @xnor Thanks for your comments! - No, contours are not allowed to touch each other/itself on a diagonal. - Each contour has its own directon. I'll update the description.
    $endgroup$
    – Galen Ivanov
    Jul 31 at 7:11






  • 2




    $begingroup$
    Is input with no space between the walls possible? Eg: Try it online!. I know you said "separated by at least one space" but I was unclear if that applied only to independent loops or if it applied to a single loop as well.
    $endgroup$
    – Jonah
    Aug 1 at 0:21







  • 1




    $begingroup$
    @Jonah No, it's not possible: There will always be a straight segment (or a space) between any two corners (like +-+ for the horizontal and similar for the vertical) - in other words the sharp U turns are forbidden.
    $endgroup$
    – Galen Ivanov
    Aug 1 at 6:19













28












28








28


3



$begingroup$


Sandboxed



Given a set of closed non-overlapping 2d contours (separated by at least one space even on diagonals) with arrows oriented consistently in the same clockwise or counter-clockwise direction (each contour has its own direction) and a positive number n, move the arrows n steps along the contours in the respective direction.
The arrows are represented by > v < ^ respectively for right, down, left and up directions. There the other characters are - (horizontal), | (vertical) and + (corner).
When an arrow is on a corner, it keeps its current direction and changes it only after the turn is taken.



There will always be a straight segment (or a space) between any two corners (like +-+ for the horizontal and similar for the vertical) - in other words the sharp U turns are forbidden. The segments between the corners are either vertical or horizontal and the bend at a corner is always 90 degree.



Input:



  • a positive integer - n - number of steps

  • an ASCII representation of the contours - it can be a multiline string, a list of strings, a list of characters and so on.

Output:



The same contours with all arrows shifted n steps in each contour's overall direction.



Test cases:



1.



Input:



n = 1



 +----->-> 
| |
| v---+
| |
+---<-------+


Output:



 +------>+
| v
| +>--+
| |
+--<--------+


2.



Input:



n = 2



 +-----+ +---+ 
| | | |
+-->--+ | v
| |
+--->---+ |
| |
+------<<---+


Output:



 +-----+ +---+
| | | |
+---->+ | |
| |
+----->-+ v
| |
+----<<-----+


3.



Input:



n = 3



 +---+ +---+ +-------+ 
| | | v | |
^ | | | +-<-+ |
| | ^ | | v
| +---+ +-->----+ |
| |
| +-------+ +---+ |
| | | v | |
+---+ +---+ +---+


Output:



 +>--+ ^---+ +-------+
| | | | ^ |
| | | | +---+ |
| | | | | |
| +---+ v----->-+ |
| |
| +-------+ +---+ v
| | | | | |
+---+ +-<-+ +---+


4.



Input:



n = 1



+--+ 
| |
| +---+
| |
+----+ |
| |
+-+


Output:



+--+ 
| |
| +---+
| |
+----+ |
| |
+-+


5.



Input



n = 4



^>>>>
^ v
^ v>>>>
^ v
<<<<<<<<v


Output:



^>>>>
^ v
^ v>>>>
^ v
<<<<<<<<v


6.



Input:



n = 1



^->
^ v
<<v


Output:



^>+
^ v
<<v


Write a function or a program solving the above task. The shortest code in bytes in every language wins. Don't be discouraged by the golfing languages. Explanation of the algorithm and the code is highly appreciated.










share|improve this question











$endgroup$




Sandboxed



Given a set of closed non-overlapping 2d contours (separated by at least one space even on diagonals) with arrows oriented consistently in the same clockwise or counter-clockwise direction (each contour has its own direction) and a positive number n, move the arrows n steps along the contours in the respective direction.
The arrows are represented by > v < ^ respectively for right, down, left and up directions. There the other characters are - (horizontal), | (vertical) and + (corner).
When an arrow is on a corner, it keeps its current direction and changes it only after the turn is taken.



There will always be a straight segment (or a space) between any two corners (like +-+ for the horizontal and similar for the vertical) - in other words the sharp U turns are forbidden. The segments between the corners are either vertical or horizontal and the bend at a corner is always 90 degree.



Input:



  • a positive integer - n - number of steps

  • an ASCII representation of the contours - it can be a multiline string, a list of strings, a list of characters and so on.

Output:



The same contours with all arrows shifted n steps in each contour's overall direction.



Test cases:



1.



Input:



n = 1



 +----->-> 
| |
| v---+
| |
+---<-------+


Output:



 +------>+
| v
| +>--+
| |
+--<--------+


2.



Input:



n = 2



 +-----+ +---+ 
| | | |
+-->--+ | v
| |
+--->---+ |
| |
+------<<---+


Output:



 +-----+ +---+
| | | |
+---->+ | |
| |
+----->-+ v
| |
+----<<-----+


3.



Input:



n = 3



 +---+ +---+ +-------+ 
| | | v | |
^ | | | +-<-+ |
| | ^ | | v
| +---+ +-->----+ |
| |
| +-------+ +---+ |
| | | v | |
+---+ +---+ +---+


Output:



 +>--+ ^---+ +-------+
| | | | ^ |
| | | | +---+ |
| | | | | |
| +---+ v----->-+ |
| |
| +-------+ +---+ v
| | | | | |
+---+ +-<-+ +---+


4.



Input:



n = 1



+--+ 
| |
| +---+
| |
+----+ |
| |
+-+


Output:



+--+ 
| |
| +---+
| |
+----+ |
| |
+-+


5.



Input



n = 4



^>>>>
^ v
^ v>>>>
^ v
<<<<<<<<v


Output:



^>>>>
^ v
^ v>>>>
^ v
<<<<<<<<v


6.



Input:



n = 1



^->
^ v
<<v


Output:



^>+
^ v
<<v


Write a function or a program solving the above task. The shortest code in bytes in every language wins. Don't be discouraged by the golfing languages. Explanation of the algorithm and the code is highly appreciated.







code-golf ascii-art






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 1 at 14:03









Sriotchilism O'Zaic

37.2k10 gold badges168 silver badges381 bronze badges




37.2k10 gold badges168 silver badges381 bronze badges










asked Jul 31 at 6:40









Galen IvanovGalen Ivanov

9,1871 gold badge14 silver badges39 bronze badges




9,1871 gold badge14 silver badges39 bronze badges














  • $begingroup$
    Can two contours touch their corners on a diagonal, or a contour touch itself like that?
    $endgroup$
    – xnor
    Jul 31 at 6:57







  • 4




    $begingroup$
    "Given a set of closed non-overlapping 2d contours ... with arrows oriented consistently in the same clockwise or counter-clockwise direction" sounds to me like every contour is oriented in the same direction, whereas from the test cases, it seems the arrows only have be consistent within a contour.
    $endgroup$
    – xnor
    Jul 31 at 7:00






  • 3




    $begingroup$
    @xnor Thanks for your comments! - No, contours are not allowed to touch each other/itself on a diagonal. - Each contour has its own directon. I'll update the description.
    $endgroup$
    – Galen Ivanov
    Jul 31 at 7:11






  • 2




    $begingroup$
    Is input with no space between the walls possible? Eg: Try it online!. I know you said "separated by at least one space" but I was unclear if that applied only to independent loops or if it applied to a single loop as well.
    $endgroup$
    – Jonah
    Aug 1 at 0:21







  • 1




    $begingroup$
    @Jonah No, it's not possible: There will always be a straight segment (or a space) between any two corners (like +-+ for the horizontal and similar for the vertical) - in other words the sharp U turns are forbidden.
    $endgroup$
    – Galen Ivanov
    Aug 1 at 6:19
















  • $begingroup$
    Can two contours touch their corners on a diagonal, or a contour touch itself like that?
    $endgroup$
    – xnor
    Jul 31 at 6:57







  • 4




    $begingroup$
    "Given a set of closed non-overlapping 2d contours ... with arrows oriented consistently in the same clockwise or counter-clockwise direction" sounds to me like every contour is oriented in the same direction, whereas from the test cases, it seems the arrows only have be consistent within a contour.
    $endgroup$
    – xnor
    Jul 31 at 7:00






  • 3




    $begingroup$
    @xnor Thanks for your comments! - No, contours are not allowed to touch each other/itself on a diagonal. - Each contour has its own directon. I'll update the description.
    $endgroup$
    – Galen Ivanov
    Jul 31 at 7:11






  • 2




    $begingroup$
    Is input with no space between the walls possible? Eg: Try it online!. I know you said "separated by at least one space" but I was unclear if that applied only to independent loops or if it applied to a single loop as well.
    $endgroup$
    – Jonah
    Aug 1 at 0:21







  • 1




    $begingroup$
    @Jonah No, it's not possible: There will always be a straight segment (or a space) between any two corners (like +-+ for the horizontal and similar for the vertical) - in other words the sharp U turns are forbidden.
    $endgroup$
    – Galen Ivanov
    Aug 1 at 6:19















$begingroup$
Can two contours touch their corners on a diagonal, or a contour touch itself like that?
$endgroup$
– xnor
Jul 31 at 6:57





$begingroup$
Can two contours touch their corners on a diagonal, or a contour touch itself like that?
$endgroup$
– xnor
Jul 31 at 6:57





4




4




$begingroup$
"Given a set of closed non-overlapping 2d contours ... with arrows oriented consistently in the same clockwise or counter-clockwise direction" sounds to me like every contour is oriented in the same direction, whereas from the test cases, it seems the arrows only have be consistent within a contour.
$endgroup$
– xnor
Jul 31 at 7:00




$begingroup$
"Given a set of closed non-overlapping 2d contours ... with arrows oriented consistently in the same clockwise or counter-clockwise direction" sounds to me like every contour is oriented in the same direction, whereas from the test cases, it seems the arrows only have be consistent within a contour.
$endgroup$
– xnor
Jul 31 at 7:00




3




3




$begingroup$
@xnor Thanks for your comments! - No, contours are not allowed to touch each other/itself on a diagonal. - Each contour has its own directon. I'll update the description.
$endgroup$
– Galen Ivanov
Jul 31 at 7:11




$begingroup$
@xnor Thanks for your comments! - No, contours are not allowed to touch each other/itself on a diagonal. - Each contour has its own directon. I'll update the description.
$endgroup$
– Galen Ivanov
Jul 31 at 7:11




2




2




$begingroup$
Is input with no space between the walls possible? Eg: Try it online!. I know you said "separated by at least one space" but I was unclear if that applied only to independent loops or if it applied to a single loop as well.
$endgroup$
– Jonah
Aug 1 at 0:21





$begingroup$
Is input with no space between the walls possible? Eg: Try it online!. I know you said "separated by at least one space" but I was unclear if that applied only to independent loops or if it applied to a single loop as well.
$endgroup$
– Jonah
Aug 1 at 0:21





1




1




$begingroup$
@Jonah No, it's not possible: There will always be a straight segment (or a space) between any two corners (like +-+ for the horizontal and similar for the vertical) - in other words the sharp U turns are forbidden.
$endgroup$
– Galen Ivanov
Aug 1 at 6:19




$begingroup$
@Jonah No, it's not possible: There will always be a straight segment (or a space) between any two corners (like +-+ for the horizontal and similar for the vertical) - in other words the sharp U turns are forbidden.
$endgroup$
– Galen Ivanov
Aug 1 at 6:19










4 Answers
4






active

oldest

votes


















14












$begingroup$

JavaScript (ES6),  210 ... 182  180 bytes



Takes input as (m)(n), where $m$ is a list of lists of characters. Returns the result in the same format.





m=>g=n=>n?g(n-1,m=m.map((r,y)=>r.map((c,x)=>(i=0,h=$=>~$?(m[Y=y+($-2)%2]||0)[X=x+~-$%2]>h?"-|+"[n+=`;m[$Y][$X]=S[$$]`,i?2:$&1]:h($^++i):c)((S="<^>v").indexOf(c)))),eval(n)):m


Try it online!



How?



You can follow this link to see a formatted version of the source.



Wrapper



The recursive function $g$ is just used as a wrapper that invokes the main code to move all arrows by 1 step and keeps calling itself with $n-1$ until $n=0$.



Update method



We can't safely move each arrow one at a time because we would run the risk of overwriting non-updated arrows with updated ones. Instead, we first remove all arrows and compute their new positions. We apply the new positions in a second time.



This is done by re-using $n$ as a string to store the position updates as JS code.



For instance, in the first test case, $n$ is set to:



"1;m[0][7]=S[2];m[1][8]=S[3];m[2][9]=S[2];m[4][3]=S[0]"


(Note that the leading number -- which is the original value of $n$ -- is harmless.)



The new positions are applied by just doing eval(n).



Directions



Each arrow is converted into a direction $d$ (named $ in the code), using the following compass:



$$beginmatrix
&1\
0&+&2\
&3
endmatrix$$



The corresponding values of $dx$ and $dy$ are computed this way:



 d | dx = (d - 1) % 2 | dy = (d - 2) % 2
---+------------------+------------------
0 | -1 | 0
1 | 0 | -1
2 | +1 | 0
3 | 0 | +1


Corners



If the next character in the identified direction is a space or is out of bounds, it means that we're located on a corner and we need to take a 90° or 270° turn. This is why the helper function $h$ is testing up to 3 distinct directions: $d$, $doperatornamexor1$ and $doperatornamexor3$.



If we're located on a corner, we overwrite the cell with +. Otherwise, we overwrite it with either - or |, depending on the parity of $d$.



Note: The parameter of $h$ is not named $ just because it looks uber l33t but also because it allows us to compare a given character with $h$ (implicitly coerced to a string) to know if it's a space (below "$"), a contour character (above "$") or another arrow (also above "$").



Animated version






f=
m=>g=n=>n?g(n-1,m=m.map((r,y)=>r.map((c,x)=>(i=0,h=$=>~$?(m[Y=y+($-2)%2]||0)[X=x+~-$%2]>h?"-|+"[n+=`;m[$Y][$X]=S[$$]`,i?2:$&1]:h($^++i):c)((S="<^>v").indexOf(c)))),eval(n)):m

m = [
[..."+---+ +---+ +-------+"],
[..."| | | v | |"],
[..."^ | | | +-<-+ |"],
[..."| | ^ | | v"],
[..."| +---+ +-->----+ |"],
[..."| |"],
[..."| +-------+ +---+ |"],
[..."| | | v | |"],
[..."+---+ +---+ +---+"]
];

(F = _ => (o.innerHTML = m.map(r => r.join('')).join('n'), m = f(m)(1), window.setTimeout(F, 100)))()

<pre id=o></pre>








share|improve this answer











$endgroup$














  • $begingroup$
    Thank you for the explanation!
    $endgroup$
    – Galen Ivanov
    Aug 1 at 6:20


















8












$begingroup$


K (ngn/k), 183 161 157 bytes



"c*+/'3'0,c,0;$[#p:+s&~^t:A?,/x;;:r];q:q@'*'&'~^x ./:/:q:+p+/:D@4!(t^0N)+/:0 1 3;s#@[,/r;s/+q;:;A@D?q-p]/


Try it online!



/ when called with an int left arg n, this will apply the function in n times to the right arg



A:"^>v<" arrows



D,:-D:(-1 0;!2) ∆y,∆x for the 4 cardinal directions



s:(#x;#*x) shape of the input: height,width



c:~^x countours - boolean matrix showing where the non-spaces are



r:" -+|"c*+/'3'0,c,0 recreate the character matrix with a countour but without arrows, by counting self+upper+lower for each cell in c and replacing 1->-, 2->+, 3->|



t:A?,/x types of arrows: 0 1 2 3 for ^>v<, all other cells are represented as 0N (null)



p:+s&~^t coordinates of the arrows



$[#p ;;:r] if there aren't any arrows, return r



q:+p+/:D@4!(t^0N)+/:0 1 3 all 3 possible new positions for each arrow - if it keeps going forward, if it turns left, and if it turns right



q:q@'*'&'~^x ./:/:q for each arrow choose the first option that lands on the countour



@[,/r;s/+q;:;A@D?q-p] flatten r and put on it the arrows at their new positions and with their new directions



s# reshape to the original shape






share|improve this answer











$endgroup$










  • 2




    $begingroup$
    You are fast! I hope you'll explain the code after finish golfing it.
    $endgroup$
    – Galen Ivanov
    Jul 31 at 7:50










  • $begingroup$
    Thank you for the explanation!
    $endgroup$
    – Galen Ivanov
    Jul 31 at 12:24


















4












$begingroup$


Charcoal, 105 bytes



W¬ΦυΣκ⊞υS≔⊟υη≔⪫υ⸿θ≔⟦⟧υ≔>^<vζPθFθ¿№ζι«⊞υ⟦⌕ζιⅉⅈ⟧§+|-↨EKV›κ ²»ιFυ«J⊟ι⊟ι≔⊟ιιFIη«≔⊟Φ⁴∧﹪⁻⊖ι⊕λ⁴›§KV⁻⁵λ ιM✳⊗黧ζι


Try it online! Link is to verbose version of code. Includes 22 bytes used to avoid requiring a cumbersome input format. Explanation:



W¬ΦυΣκ⊞υS≔⊟υη≔⪫υ⸿θ≔⟦⟧υ


Conveniently input the contours and the number of steps.



≔>^<vζ


The direction characters are used several times so the string is cached here. The index of a direction character in this string is known as its direction.



Pθ


Print the original contours without moving the cursor.



Fθ


Loop over the characters in the contour.



¿№ζι«


If the current characters is a direction character...



⊞υ⟦⌕ζιⅉⅈ⟧


... then save the direction and position in a list...



§+|-↨EKV›κ ²


... and replace the character with the appropriate line character.



»ι


Otherwise output the character and move on to the next character.



Fυ«


Loop over the saved positions.



J⊟ι⊟ι


Jump to the saved position.



≔⊟ιι


Extract the saved direction.



FIη«


Loop over the appropriate number of steps.



≔⊟Φ⁴∧﹪⁻⊖ι⊕λ⁴›§KV⁻⁵λ ι


Find the direction of the next step, which is any direction that is neither reverse nor empty.



M✳⊗ι


Take a step in that direction. (Charcoal direction indices for the Move command are twice the value of my direction.)



»§ζι


Print the appropriate direction character.






share|improve this answer









$endgroup$














  • $begingroup$
    Thank you for the explanation!
    $endgroup$
    – Galen Ivanov
    Aug 1 at 6:20


















2












$begingroup$


APL (Dyalog Unicode), 111 bytesSBCS





A[D⍳q-p]@q⊢' ⍣⎕⊢⎕


Try it online!



similar to my k answer






share|improve this answer









$endgroup$

















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "200"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f188979%2fmove-arrows-along-a-contour%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    4 Answers
    4






    active

    oldest

    votes








    4 Answers
    4






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    14












    $begingroup$

    JavaScript (ES6),  210 ... 182  180 bytes



    Takes input as (m)(n), where $m$ is a list of lists of characters. Returns the result in the same format.





    m=>g=n=>n?g(n-1,m=m.map((r,y)=>r.map((c,x)=>(i=0,h=$=>~$?(m[Y=y+($-2)%2]||0)[X=x+~-$%2]>h?"-|+"[n+=`;m[$Y][$X]=S[$$]`,i?2:$&1]:h($^++i):c)((S="<^>v").indexOf(c)))),eval(n)):m


    Try it online!



    How?



    You can follow this link to see a formatted version of the source.



    Wrapper



    The recursive function $g$ is just used as a wrapper that invokes the main code to move all arrows by 1 step and keeps calling itself with $n-1$ until $n=0$.



    Update method



    We can't safely move each arrow one at a time because we would run the risk of overwriting non-updated arrows with updated ones. Instead, we first remove all arrows and compute their new positions. We apply the new positions in a second time.



    This is done by re-using $n$ as a string to store the position updates as JS code.



    For instance, in the first test case, $n$ is set to:



    "1;m[0][7]=S[2];m[1][8]=S[3];m[2][9]=S[2];m[4][3]=S[0]"


    (Note that the leading number -- which is the original value of $n$ -- is harmless.)



    The new positions are applied by just doing eval(n).



    Directions



    Each arrow is converted into a direction $d$ (named $ in the code), using the following compass:



    $$beginmatrix
    &1\
    0&+&2\
    &3
    endmatrix$$



    The corresponding values of $dx$ and $dy$ are computed this way:



     d | dx = (d - 1) % 2 | dy = (d - 2) % 2
    ---+------------------+------------------
    0 | -1 | 0
    1 | 0 | -1
    2 | +1 | 0
    3 | 0 | +1


    Corners



    If the next character in the identified direction is a space or is out of bounds, it means that we're located on a corner and we need to take a 90° or 270° turn. This is why the helper function $h$ is testing up to 3 distinct directions: $d$, $doperatornamexor1$ and $doperatornamexor3$.



    If we're located on a corner, we overwrite the cell with +. Otherwise, we overwrite it with either - or |, depending on the parity of $d$.



    Note: The parameter of $h$ is not named $ just because it looks uber l33t but also because it allows us to compare a given character with $h$ (implicitly coerced to a string) to know if it's a space (below "$"), a contour character (above "$") or another arrow (also above "$").



    Animated version






    f=
    m=>g=n=>n?g(n-1,m=m.map((r,y)=>r.map((c,x)=>(i=0,h=$=>~$?(m[Y=y+($-2)%2]||0)[X=x+~-$%2]>h?"-|+"[n+=`;m[$Y][$X]=S[$$]`,i?2:$&1]:h($^++i):c)((S="<^>v").indexOf(c)))),eval(n)):m

    m = [
    [..."+---+ +---+ +-------+"],
    [..."| | | v | |"],
    [..."^ | | | +-<-+ |"],
    [..."| | ^ | | v"],
    [..."| +---+ +-->----+ |"],
    [..."| |"],
    [..."| +-------+ +---+ |"],
    [..."| | | v | |"],
    [..."+---+ +---+ +---+"]
    ];

    (F = _ => (o.innerHTML = m.map(r => r.join('')).join('n'), m = f(m)(1), window.setTimeout(F, 100)))()

    <pre id=o></pre>








    share|improve this answer











    $endgroup$














    • $begingroup$
      Thank you for the explanation!
      $endgroup$
      – Galen Ivanov
      Aug 1 at 6:20















    14












    $begingroup$

    JavaScript (ES6),  210 ... 182  180 bytes



    Takes input as (m)(n), where $m$ is a list of lists of characters. Returns the result in the same format.





    m=>g=n=>n?g(n-1,m=m.map((r,y)=>r.map((c,x)=>(i=0,h=$=>~$?(m[Y=y+($-2)%2]||0)[X=x+~-$%2]>h?"-|+"[n+=`;m[$Y][$X]=S[$$]`,i?2:$&1]:h($^++i):c)((S="<^>v").indexOf(c)))),eval(n)):m


    Try it online!



    How?



    You can follow this link to see a formatted version of the source.



    Wrapper



    The recursive function $g$ is just used as a wrapper that invokes the main code to move all arrows by 1 step and keeps calling itself with $n-1$ until $n=0$.



    Update method



    We can't safely move each arrow one at a time because we would run the risk of overwriting non-updated arrows with updated ones. Instead, we first remove all arrows and compute their new positions. We apply the new positions in a second time.



    This is done by re-using $n$ as a string to store the position updates as JS code.



    For instance, in the first test case, $n$ is set to:



    "1;m[0][7]=S[2];m[1][8]=S[3];m[2][9]=S[2];m[4][3]=S[0]"


    (Note that the leading number -- which is the original value of $n$ -- is harmless.)



    The new positions are applied by just doing eval(n).



    Directions



    Each arrow is converted into a direction $d$ (named $ in the code), using the following compass:



    $$beginmatrix
    &1\
    0&+&2\
    &3
    endmatrix$$



    The corresponding values of $dx$ and $dy$ are computed this way:



     d | dx = (d - 1) % 2 | dy = (d - 2) % 2
    ---+------------------+------------------
    0 | -1 | 0
    1 | 0 | -1
    2 | +1 | 0
    3 | 0 | +1


    Corners



    If the next character in the identified direction is a space or is out of bounds, it means that we're located on a corner and we need to take a 90° or 270° turn. This is why the helper function $h$ is testing up to 3 distinct directions: $d$, $doperatornamexor1$ and $doperatornamexor3$.



    If we're located on a corner, we overwrite the cell with +. Otherwise, we overwrite it with either - or |, depending on the parity of $d$.



    Note: The parameter of $h$ is not named $ just because it looks uber l33t but also because it allows us to compare a given character with $h$ (implicitly coerced to a string) to know if it's a space (below "$"), a contour character (above "$") or another arrow (also above "$").



    Animated version






    f=
    m=>g=n=>n?g(n-1,m=m.map((r,y)=>r.map((c,x)=>(i=0,h=$=>~$?(m[Y=y+($-2)%2]||0)[X=x+~-$%2]>h?"-|+"[n+=`;m[$Y][$X]=S[$$]`,i?2:$&1]:h($^++i):c)((S="<^>v").indexOf(c)))),eval(n)):m

    m = [
    [..."+---+ +---+ +-------+"],
    [..."| | | v | |"],
    [..."^ | | | +-<-+ |"],
    [..."| | ^ | | v"],
    [..."| +---+ +-->----+ |"],
    [..."| |"],
    [..."| +-------+ +---+ |"],
    [..."| | | v | |"],
    [..."+---+ +---+ +---+"]
    ];

    (F = _ => (o.innerHTML = m.map(r => r.join('')).join('n'), m = f(m)(1), window.setTimeout(F, 100)))()

    <pre id=o></pre>








    share|improve this answer











    $endgroup$














    • $begingroup$
      Thank you for the explanation!
      $endgroup$
      – Galen Ivanov
      Aug 1 at 6:20













    14












    14








    14





    $begingroup$

    JavaScript (ES6),  210 ... 182  180 bytes



    Takes input as (m)(n), where $m$ is a list of lists of characters. Returns the result in the same format.





    m=>g=n=>n?g(n-1,m=m.map((r,y)=>r.map((c,x)=>(i=0,h=$=>~$?(m[Y=y+($-2)%2]||0)[X=x+~-$%2]>h?"-|+"[n+=`;m[$Y][$X]=S[$$]`,i?2:$&1]:h($^++i):c)((S="<^>v").indexOf(c)))),eval(n)):m


    Try it online!



    How?



    You can follow this link to see a formatted version of the source.



    Wrapper



    The recursive function $g$ is just used as a wrapper that invokes the main code to move all arrows by 1 step and keeps calling itself with $n-1$ until $n=0$.



    Update method



    We can't safely move each arrow one at a time because we would run the risk of overwriting non-updated arrows with updated ones. Instead, we first remove all arrows and compute their new positions. We apply the new positions in a second time.



    This is done by re-using $n$ as a string to store the position updates as JS code.



    For instance, in the first test case, $n$ is set to:



    "1;m[0][7]=S[2];m[1][8]=S[3];m[2][9]=S[2];m[4][3]=S[0]"


    (Note that the leading number -- which is the original value of $n$ -- is harmless.)



    The new positions are applied by just doing eval(n).



    Directions



    Each arrow is converted into a direction $d$ (named $ in the code), using the following compass:



    $$beginmatrix
    &1\
    0&+&2\
    &3
    endmatrix$$



    The corresponding values of $dx$ and $dy$ are computed this way:



     d | dx = (d - 1) % 2 | dy = (d - 2) % 2
    ---+------------------+------------------
    0 | -1 | 0
    1 | 0 | -1
    2 | +1 | 0
    3 | 0 | +1


    Corners



    If the next character in the identified direction is a space or is out of bounds, it means that we're located on a corner and we need to take a 90° or 270° turn. This is why the helper function $h$ is testing up to 3 distinct directions: $d$, $doperatornamexor1$ and $doperatornamexor3$.



    If we're located on a corner, we overwrite the cell with +. Otherwise, we overwrite it with either - or |, depending on the parity of $d$.



    Note: The parameter of $h$ is not named $ just because it looks uber l33t but also because it allows us to compare a given character with $h$ (implicitly coerced to a string) to know if it's a space (below "$"), a contour character (above "$") or another arrow (also above "$").



    Animated version






    f=
    m=>g=n=>n?g(n-1,m=m.map((r,y)=>r.map((c,x)=>(i=0,h=$=>~$?(m[Y=y+($-2)%2]||0)[X=x+~-$%2]>h?"-|+"[n+=`;m[$Y][$X]=S[$$]`,i?2:$&1]:h($^++i):c)((S="<^>v").indexOf(c)))),eval(n)):m

    m = [
    [..."+---+ +---+ +-------+"],
    [..."| | | v | |"],
    [..."^ | | | +-<-+ |"],
    [..."| | ^ | | v"],
    [..."| +---+ +-->----+ |"],
    [..."| |"],
    [..."| +-------+ +---+ |"],
    [..."| | | v | |"],
    [..."+---+ +---+ +---+"]
    ];

    (F = _ => (o.innerHTML = m.map(r => r.join('')).join('n'), m = f(m)(1), window.setTimeout(F, 100)))()

    <pre id=o></pre>








    share|improve this answer











    $endgroup$



    JavaScript (ES6),  210 ... 182  180 bytes



    Takes input as (m)(n), where $m$ is a list of lists of characters. Returns the result in the same format.





    m=>g=n=>n?g(n-1,m=m.map((r,y)=>r.map((c,x)=>(i=0,h=$=>~$?(m[Y=y+($-2)%2]||0)[X=x+~-$%2]>h?"-|+"[n+=`;m[$Y][$X]=S[$$]`,i?2:$&1]:h($^++i):c)((S="<^>v").indexOf(c)))),eval(n)):m


    Try it online!



    How?



    You can follow this link to see a formatted version of the source.



    Wrapper



    The recursive function $g$ is just used as a wrapper that invokes the main code to move all arrows by 1 step and keeps calling itself with $n-1$ until $n=0$.



    Update method



    We can't safely move each arrow one at a time because we would run the risk of overwriting non-updated arrows with updated ones. Instead, we first remove all arrows and compute their new positions. We apply the new positions in a second time.



    This is done by re-using $n$ as a string to store the position updates as JS code.



    For instance, in the first test case, $n$ is set to:



    "1;m[0][7]=S[2];m[1][8]=S[3];m[2][9]=S[2];m[4][3]=S[0]"


    (Note that the leading number -- which is the original value of $n$ -- is harmless.)



    The new positions are applied by just doing eval(n).



    Directions



    Each arrow is converted into a direction $d$ (named $ in the code), using the following compass:



    $$beginmatrix
    &1\
    0&+&2\
    &3
    endmatrix$$



    The corresponding values of $dx$ and $dy$ are computed this way:



     d | dx = (d - 1) % 2 | dy = (d - 2) % 2
    ---+------------------+------------------
    0 | -1 | 0
    1 | 0 | -1
    2 | +1 | 0
    3 | 0 | +1


    Corners



    If the next character in the identified direction is a space or is out of bounds, it means that we're located on a corner and we need to take a 90° or 270° turn. This is why the helper function $h$ is testing up to 3 distinct directions: $d$, $doperatornamexor1$ and $doperatornamexor3$.



    If we're located on a corner, we overwrite the cell with +. Otherwise, we overwrite it with either - or |, depending on the parity of $d$.



    Note: The parameter of $h$ is not named $ just because it looks uber l33t but also because it allows us to compare a given character with $h$ (implicitly coerced to a string) to know if it's a space (below "$"), a contour character (above "$") or another arrow (also above "$").



    Animated version






    f=
    m=>g=n=>n?g(n-1,m=m.map((r,y)=>r.map((c,x)=>(i=0,h=$=>~$?(m[Y=y+($-2)%2]||0)[X=x+~-$%2]>h?"-|+"[n+=`;m[$Y][$X]=S[$$]`,i?2:$&1]:h($^++i):c)((S="<^>v").indexOf(c)))),eval(n)):m

    m = [
    [..."+---+ +---+ +-------+"],
    [..."| | | v | |"],
    [..."^ | | | +-<-+ |"],
    [..."| | ^ | | v"],
    [..."| +---+ +-->----+ |"],
    [..."| |"],
    [..."| +-------+ +---+ |"],
    [..."| | | v | |"],
    [..."+---+ +---+ +---+"]
    ];

    (F = _ => (o.innerHTML = m.map(r => r.join('')).join('n'), m = f(m)(1), window.setTimeout(F, 100)))()

    <pre id=o></pre>








    f=
    m=>g=n=>n?g(n-1,m=m.map((r,y)=>r.map((c,x)=>(i=0,h=$=>~$?(m[Y=y+($-2)%2]||0)[X=x+~-$%2]>h?"-|+"[n+=`;m[$Y][$X]=S[$$]`,i?2:$&1]:h($^++i):c)((S="<^>v").indexOf(c)))),eval(n)):m

    m = [
    [..."+---+ +---+ +-------+"],
    [..."| | | v | |"],
    [..."^ | | | +-<-+ |"],
    [..."| | ^ | | v"],
    [..."| +---+ +-->----+ |"],
    [..."| |"],
    [..."| +-------+ +---+ |"],
    [..."| | | v | |"],
    [..."+---+ +---+ +---+"]
    ];

    (F = _ => (o.innerHTML = m.map(r => r.join('')).join('n'), m = f(m)(1), window.setTimeout(F, 100)))()

    <pre id=o></pre>





    f=
    m=>g=n=>n?g(n-1,m=m.map((r,y)=>r.map((c,x)=>(i=0,h=$=>~$?(m[Y=y+($-2)%2]||0)[X=x+~-$%2]>h?"-|+"[n+=`;m[$Y][$X]=S[$$]`,i?2:$&1]:h($^++i):c)((S="<^>v").indexOf(c)))),eval(n)):m

    m = [
    [..."+---+ +---+ +-------+"],
    [..."| | | v | |"],
    [..."^ | | | +-<-+ |"],
    [..."| | ^ | | v"],
    [..."| +---+ +-->----+ |"],
    [..."| |"],
    [..."| +-------+ +---+ |"],
    [..."| | | v | |"],
    [..."+---+ +---+ +---+"]
    ];

    (F = _ => (o.innerHTML = m.map(r => r.join('')).join('n'), m = f(m)(1), window.setTimeout(F, 100)))()

    <pre id=o></pre>






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Aug 1 at 12:22

























    answered Jul 31 at 9:20









    ArnauldArnauld

    89.6k7 gold badges104 silver badges366 bronze badges




    89.6k7 gold badges104 silver badges366 bronze badges














    • $begingroup$
      Thank you for the explanation!
      $endgroup$
      – Galen Ivanov
      Aug 1 at 6:20
















    • $begingroup$
      Thank you for the explanation!
      $endgroup$
      – Galen Ivanov
      Aug 1 at 6:20















    $begingroup$
    Thank you for the explanation!
    $endgroup$
    – Galen Ivanov
    Aug 1 at 6:20




    $begingroup$
    Thank you for the explanation!
    $endgroup$
    – Galen Ivanov
    Aug 1 at 6:20













    8












    $begingroup$


    K (ngn/k), 183 161 157 bytes



    "c*+/'3'0,c,0;$[#p:+s&~^t:A?,/x;;:r];q:q@'*'&'~^x ./:/:q:+p+/:D@4!(t^0N)+/:0 1 3;s#@[,/r;s/+q;:;A@D?q-p]/


    Try it online!



    / when called with an int left arg n, this will apply the function in n times to the right arg



    A:"^>v<" arrows



    D,:-D:(-1 0;!2) ∆y,∆x for the 4 cardinal directions



    s:(#x;#*x) shape of the input: height,width



    c:~^x countours - boolean matrix showing where the non-spaces are



    r:" -+|"c*+/'3'0,c,0 recreate the character matrix with a countour but without arrows, by counting self+upper+lower for each cell in c and replacing 1->-, 2->+, 3->|



    t:A?,/x types of arrows: 0 1 2 3 for ^>v<, all other cells are represented as 0N (null)



    p:+s&~^t coordinates of the arrows



    $[#p ;;:r] if there aren't any arrows, return r



    q:+p+/:D@4!(t^0N)+/:0 1 3 all 3 possible new positions for each arrow - if it keeps going forward, if it turns left, and if it turns right



    q:q@'*'&'~^x ./:/:q for each arrow choose the first option that lands on the countour



    @[,/r;s/+q;:;A@D?q-p] flatten r and put on it the arrows at their new positions and with their new directions



    s# reshape to the original shape






    share|improve this answer











    $endgroup$










    • 2




      $begingroup$
      You are fast! I hope you'll explain the code after finish golfing it.
      $endgroup$
      – Galen Ivanov
      Jul 31 at 7:50










    • $begingroup$
      Thank you for the explanation!
      $endgroup$
      – Galen Ivanov
      Jul 31 at 12:24















    8












    $begingroup$


    K (ngn/k), 183 161 157 bytes



    "c*+/'3'0,c,0;$[#p:+s&~^t:A?,/x;;:r];q:q@'*'&'~^x ./:/:q:+p+/:D@4!(t^0N)+/:0 1 3;s#@[,/r;s/+q;:;A@D?q-p]/


    Try it online!



    / when called with an int left arg n, this will apply the function in n times to the right arg



    A:"^>v<" arrows



    D,:-D:(-1 0;!2) ∆y,∆x for the 4 cardinal directions



    s:(#x;#*x) shape of the input: height,width



    c:~^x countours - boolean matrix showing where the non-spaces are



    r:" -+|"c*+/'3'0,c,0 recreate the character matrix with a countour but without arrows, by counting self+upper+lower for each cell in c and replacing 1->-, 2->+, 3->|



    t:A?,/x types of arrows: 0 1 2 3 for ^>v<, all other cells are represented as 0N (null)



    p:+s&~^t coordinates of the arrows



    $[#p ;;:r] if there aren't any arrows, return r



    q:+p+/:D@4!(t^0N)+/:0 1 3 all 3 possible new positions for each arrow - if it keeps going forward, if it turns left, and if it turns right



    q:q@'*'&'~^x ./:/:q for each arrow choose the first option that lands on the countour



    @[,/r;s/+q;:;A@D?q-p] flatten r and put on it the arrows at their new positions and with their new directions



    s# reshape to the original shape






    share|improve this answer











    $endgroup$










    • 2




      $begingroup$
      You are fast! I hope you'll explain the code after finish golfing it.
      $endgroup$
      – Galen Ivanov
      Jul 31 at 7:50










    • $begingroup$
      Thank you for the explanation!
      $endgroup$
      – Galen Ivanov
      Jul 31 at 12:24













    8












    8








    8





    $begingroup$


    K (ngn/k), 183 161 157 bytes



    "c*+/'3'0,c,0;$[#p:+s&~^t:A?,/x;;:r];q:q@'*'&'~^x ./:/:q:+p+/:D@4!(t^0N)+/:0 1 3;s#@[,/r;s/+q;:;A@D?q-p]/


    Try it online!



    / when called with an int left arg n, this will apply the function in n times to the right arg



    A:"^>v<" arrows



    D,:-D:(-1 0;!2) ∆y,∆x for the 4 cardinal directions



    s:(#x;#*x) shape of the input: height,width



    c:~^x countours - boolean matrix showing where the non-spaces are



    r:" -+|"c*+/'3'0,c,0 recreate the character matrix with a countour but without arrows, by counting self+upper+lower for each cell in c and replacing 1->-, 2->+, 3->|



    t:A?,/x types of arrows: 0 1 2 3 for ^>v<, all other cells are represented as 0N (null)



    p:+s&~^t coordinates of the arrows



    $[#p ;;:r] if there aren't any arrows, return r



    q:+p+/:D@4!(t^0N)+/:0 1 3 all 3 possible new positions for each arrow - if it keeps going forward, if it turns left, and if it turns right



    q:q@'*'&'~^x ./:/:q for each arrow choose the first option that lands on the countour



    @[,/r;s/+q;:;A@D?q-p] flatten r and put on it the arrows at their new positions and with their new directions



    s# reshape to the original shape






    share|improve this answer











    $endgroup$




    K (ngn/k), 183 161 157 bytes



    "c*+/'3'0,c,0;$[#p:+s&~^t:A?,/x;;:r];q:q@'*'&'~^x ./:/:q:+p+/:D@4!(t^0N)+/:0 1 3;s#@[,/r;s/+q;:;A@D?q-p]/


    Try it online!



    / when called with an int left arg n, this will apply the function in n times to the right arg



    A:"^>v<" arrows



    D,:-D:(-1 0;!2) ∆y,∆x for the 4 cardinal directions



    s:(#x;#*x) shape of the input: height,width



    c:~^x countours - boolean matrix showing where the non-spaces are



    r:" -+|"c*+/'3'0,c,0 recreate the character matrix with a countour but without arrows, by counting self+upper+lower for each cell in c and replacing 1->-, 2->+, 3->|



    t:A?,/x types of arrows: 0 1 2 3 for ^>v<, all other cells are represented as 0N (null)



    p:+s&~^t coordinates of the arrows



    $[#p ;;:r] if there aren't any arrows, return r



    q:+p+/:D@4!(t^0N)+/:0 1 3 all 3 possible new positions for each arrow - if it keeps going forward, if it turns left, and if it turns right



    q:q@'*'&'~^x ./:/:q for each arrow choose the first option that lands on the countour



    @[,/r;s/+q;:;A@D?q-p] flatten r and put on it the arrows at their new positions and with their new directions



    s# reshape to the original shape







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jul 31 at 8:30

























    answered Jul 31 at 7:44









    ngnngn

    8,3311 gold badge27 silver badges62 bronze badges




    8,3311 gold badge27 silver badges62 bronze badges










    • 2




      $begingroup$
      You are fast! I hope you'll explain the code after finish golfing it.
      $endgroup$
      – Galen Ivanov
      Jul 31 at 7:50










    • $begingroup$
      Thank you for the explanation!
      $endgroup$
      – Galen Ivanov
      Jul 31 at 12:24












    • 2




      $begingroup$
      You are fast! I hope you'll explain the code after finish golfing it.
      $endgroup$
      – Galen Ivanov
      Jul 31 at 7:50










    • $begingroup$
      Thank you for the explanation!
      $endgroup$
      – Galen Ivanov
      Jul 31 at 12:24







    2




    2




    $begingroup$
    You are fast! I hope you'll explain the code after finish golfing it.
    $endgroup$
    – Galen Ivanov
    Jul 31 at 7:50




    $begingroup$
    You are fast! I hope you'll explain the code after finish golfing it.
    $endgroup$
    – Galen Ivanov
    Jul 31 at 7:50












    $begingroup$
    Thank you for the explanation!
    $endgroup$
    – Galen Ivanov
    Jul 31 at 12:24




    $begingroup$
    Thank you for the explanation!
    $endgroup$
    – Galen Ivanov
    Jul 31 at 12:24











    4












    $begingroup$


    Charcoal, 105 bytes



    W¬ΦυΣκ⊞υS≔⊟υη≔⪫υ⸿θ≔⟦⟧υ≔>^<vζPθFθ¿№ζι«⊞υ⟦⌕ζιⅉⅈ⟧§+|-↨EKV›κ ²»ιFυ«J⊟ι⊟ι≔⊟ιιFIη«≔⊟Φ⁴∧﹪⁻⊖ι⊕λ⁴›§KV⁻⁵λ ιM✳⊗黧ζι


    Try it online! Link is to verbose version of code. Includes 22 bytes used to avoid requiring a cumbersome input format. Explanation:



    W¬ΦυΣκ⊞υS≔⊟υη≔⪫υ⸿θ≔⟦⟧υ


    Conveniently input the contours and the number of steps.



    ≔>^<vζ


    The direction characters are used several times so the string is cached here. The index of a direction character in this string is known as its direction.



    Pθ


    Print the original contours without moving the cursor.



    Fθ


    Loop over the characters in the contour.



    ¿№ζι«


    If the current characters is a direction character...



    ⊞υ⟦⌕ζιⅉⅈ⟧


    ... then save the direction and position in a list...



    §+|-↨EKV›κ ²


    ... and replace the character with the appropriate line character.



    »ι


    Otherwise output the character and move on to the next character.



    Fυ«


    Loop over the saved positions.



    J⊟ι⊟ι


    Jump to the saved position.



    ≔⊟ιι


    Extract the saved direction.



    FIη«


    Loop over the appropriate number of steps.



    ≔⊟Φ⁴∧﹪⁻⊖ι⊕λ⁴›§KV⁻⁵λ ι


    Find the direction of the next step, which is any direction that is neither reverse nor empty.



    M✳⊗ι


    Take a step in that direction. (Charcoal direction indices for the Move command are twice the value of my direction.)



    »§ζι


    Print the appropriate direction character.






    share|improve this answer









    $endgroup$














    • $begingroup$
      Thank you for the explanation!
      $endgroup$
      – Galen Ivanov
      Aug 1 at 6:20















    4












    $begingroup$


    Charcoal, 105 bytes



    W¬ΦυΣκ⊞υS≔⊟υη≔⪫υ⸿θ≔⟦⟧υ≔>^<vζPθFθ¿№ζι«⊞υ⟦⌕ζιⅉⅈ⟧§+|-↨EKV›κ ²»ιFυ«J⊟ι⊟ι≔⊟ιιFIη«≔⊟Φ⁴∧﹪⁻⊖ι⊕λ⁴›§KV⁻⁵λ ιM✳⊗黧ζι


    Try it online! Link is to verbose version of code. Includes 22 bytes used to avoid requiring a cumbersome input format. Explanation:



    W¬ΦυΣκ⊞υS≔⊟υη≔⪫υ⸿θ≔⟦⟧υ


    Conveniently input the contours and the number of steps.



    ≔>^<vζ


    The direction characters are used several times so the string is cached here. The index of a direction character in this string is known as its direction.



    Pθ


    Print the original contours without moving the cursor.



    Fθ


    Loop over the characters in the contour.



    ¿№ζι«


    If the current characters is a direction character...



    ⊞υ⟦⌕ζιⅉⅈ⟧


    ... then save the direction and position in a list...



    §+|-↨EKV›κ ²


    ... and replace the character with the appropriate line character.



    »ι


    Otherwise output the character and move on to the next character.



    Fυ«


    Loop over the saved positions.



    J⊟ι⊟ι


    Jump to the saved position.



    ≔⊟ιι


    Extract the saved direction.



    FIη«


    Loop over the appropriate number of steps.



    ≔⊟Φ⁴∧﹪⁻⊖ι⊕λ⁴›§KV⁻⁵λ ι


    Find the direction of the next step, which is any direction that is neither reverse nor empty.



    M✳⊗ι


    Take a step in that direction. (Charcoal direction indices for the Move command are twice the value of my direction.)



    »§ζι


    Print the appropriate direction character.






    share|improve this answer









    $endgroup$














    • $begingroup$
      Thank you for the explanation!
      $endgroup$
      – Galen Ivanov
      Aug 1 at 6:20













    4












    4








    4





    $begingroup$


    Charcoal, 105 bytes



    W¬ΦυΣκ⊞υS≔⊟υη≔⪫υ⸿θ≔⟦⟧υ≔>^<vζPθFθ¿№ζι«⊞υ⟦⌕ζιⅉⅈ⟧§+|-↨EKV›κ ²»ιFυ«J⊟ι⊟ι≔⊟ιιFIη«≔⊟Φ⁴∧﹪⁻⊖ι⊕λ⁴›§KV⁻⁵λ ιM✳⊗黧ζι


    Try it online! Link is to verbose version of code. Includes 22 bytes used to avoid requiring a cumbersome input format. Explanation:



    W¬ΦυΣκ⊞υS≔⊟υη≔⪫υ⸿θ≔⟦⟧υ


    Conveniently input the contours and the number of steps.



    ≔>^<vζ


    The direction characters are used several times so the string is cached here. The index of a direction character in this string is known as its direction.



    Pθ


    Print the original contours without moving the cursor.



    Fθ


    Loop over the characters in the contour.



    ¿№ζι«


    If the current characters is a direction character...



    ⊞υ⟦⌕ζιⅉⅈ⟧


    ... then save the direction and position in a list...



    §+|-↨EKV›κ ²


    ... and replace the character with the appropriate line character.



    »ι


    Otherwise output the character and move on to the next character.



    Fυ«


    Loop over the saved positions.



    J⊟ι⊟ι


    Jump to the saved position.



    ≔⊟ιι


    Extract the saved direction.



    FIη«


    Loop over the appropriate number of steps.



    ≔⊟Φ⁴∧﹪⁻⊖ι⊕λ⁴›§KV⁻⁵λ ι


    Find the direction of the next step, which is any direction that is neither reverse nor empty.



    M✳⊗ι


    Take a step in that direction. (Charcoal direction indices for the Move command are twice the value of my direction.)



    »§ζι


    Print the appropriate direction character.






    share|improve this answer









    $endgroup$




    Charcoal, 105 bytes



    W¬ΦυΣκ⊞υS≔⊟υη≔⪫υ⸿θ≔⟦⟧υ≔>^<vζPθFθ¿№ζι«⊞υ⟦⌕ζιⅉⅈ⟧§+|-↨EKV›κ ²»ιFυ«J⊟ι⊟ι≔⊟ιιFIη«≔⊟Φ⁴∧﹪⁻⊖ι⊕λ⁴›§KV⁻⁵λ ιM✳⊗黧ζι


    Try it online! Link is to verbose version of code. Includes 22 bytes used to avoid requiring a cumbersome input format. Explanation:



    W¬ΦυΣκ⊞υS≔⊟υη≔⪫υ⸿θ≔⟦⟧υ


    Conveniently input the contours and the number of steps.



    ≔>^<vζ


    The direction characters are used several times so the string is cached here. The index of a direction character in this string is known as its direction.



    Pθ


    Print the original contours without moving the cursor.



    Fθ


    Loop over the characters in the contour.



    ¿№ζι«


    If the current characters is a direction character...



    ⊞υ⟦⌕ζιⅉⅈ⟧


    ... then save the direction and position in a list...



    §+|-↨EKV›κ ²


    ... and replace the character with the appropriate line character.



    »ι


    Otherwise output the character and move on to the next character.



    Fυ«


    Loop over the saved positions.



    J⊟ι⊟ι


    Jump to the saved position.



    ≔⊟ιι


    Extract the saved direction.



    FIη«


    Loop over the appropriate number of steps.



    ≔⊟Φ⁴∧﹪⁻⊖ι⊕λ⁴›§KV⁻⁵λ ι


    Find the direction of the next step, which is any direction that is neither reverse nor empty.



    M✳⊗ι


    Take a step in that direction. (Charcoal direction indices for the Move command are twice the value of my direction.)



    »§ζι


    Print the appropriate direction character.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Jul 31 at 19:46









    NeilNeil

    87k8 gold badges46 silver badges183 bronze badges




    87k8 gold badges46 silver badges183 bronze badges














    • $begingroup$
      Thank you for the explanation!
      $endgroup$
      – Galen Ivanov
      Aug 1 at 6:20
















    • $begingroup$
      Thank you for the explanation!
      $endgroup$
      – Galen Ivanov
      Aug 1 at 6:20















    $begingroup$
    Thank you for the explanation!
    $endgroup$
    – Galen Ivanov
    Aug 1 at 6:20




    $begingroup$
    Thank you for the explanation!
    $endgroup$
    – Galen Ivanov
    Aug 1 at 6:20











    2












    $begingroup$


    APL (Dyalog Unicode), 111 bytesSBCS





    A[D⍳q-p]@q⊢' ⍣⎕⊢⎕


    Try it online!



    similar to my k answer






    share|improve this answer









    $endgroup$



















      2












      $begingroup$


      APL (Dyalog Unicode), 111 bytesSBCS





      A[D⍳q-p]@q⊢' ⍣⎕⊢⎕


      Try it online!



      similar to my k answer






      share|improve this answer









      $endgroup$

















        2












        2








        2





        $begingroup$


        APL (Dyalog Unicode), 111 bytesSBCS





        A[D⍳q-p]@q⊢' ⍣⎕⊢⎕


        Try it online!



        similar to my k answer






        share|improve this answer









        $endgroup$




        APL (Dyalog Unicode), 111 bytesSBCS





        A[D⍳q-p]@q⊢' ⍣⎕⊢⎕


        Try it online!



        similar to my k answer







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jul 31 at 19:40









        ngnngn

        8,3311 gold badge27 silver badges62 bronze badges




        8,3311 gold badge27 silver badges62 bronze badges






























            draft saved

            draft discarded
















































            If this is an answer to a challenge…



            • …Be sure to follow the challenge specification. However, please refrain from exploiting obvious loopholes. Answers abusing any of the standard loopholes are considered invalid. If you think a specification is unclear or underspecified, comment on the question instead.


            • …Try to optimize your score. For instance, answers to code-golf challenges should attempt to be as short as possible. You can always include a readable version of the code in addition to the competitive one.
              Explanations of your answer make it more interesting to read and are very much encouraged.


            • …Include a short header which indicates the language(s) of your code and its score, as defined by the challenge.


            More generally…



            • …Please make sure to answer the question and provide sufficient detail.


            • …Avoid asking for help, clarification or responding to other answers (use comments instead).




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f188979%2fmove-arrows-along-a-contour%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Get product attribute by attribute group code in magento 2get product attribute by product attribute group in magento 2Magento 2 Log Bundle Product Data in List Page?How to get all product attribute of a attribute group of Default attribute set?Magento 2.1 Create a filter in the product grid by new attributeMagento 2 : Get Product Attribute values By GroupMagento 2 How to get all existing values for one attributeMagento 2 get custom attribute of a single product inside a pluginMagento 2.3 How to get all the Multi Source Inventory (MSI) locations collection in custom module?Magento2: how to develop rest API to get new productsGet product attribute by attribute group code ( [attribute_group_code] ) in magento 2

            Category:9 (number) SubcategoriesMedia in category "9 (number)"Navigation menuUpload mediaGND ID: 4485639-8Library of Congress authority ID: sh85091979ReasonatorScholiaStatistics

            Magento 2.3: How do i solve this, Not registered handle, on custom form?How can i rewrite TierPrice Block in Magento2magento 2 captcha not rendering if I override layout xmlmain.CRITICAL: Plugin class doesn't existMagento 2 : Problem while adding custom button order view page?Magento 2.2.5: Overriding Admin Controller sales/orderMagento 2.2.5: Add, Update and Delete existing products Custom OptionsMagento 2.3 : File Upload issue in UI Component FormMagento2 Not registered handleHow to configured Form Builder Js in my custom magento 2.3.0 module?Magento 2.3. How to create image upload field in an admin form