Hey there, it's time to get moving.
Today’s challenge is modified from user @JKphobic on CodeWars:
You live in a city where all roads are laid o...
For further actions, you may consider blocking this person and/or reporting abuse
My first attempt at one of these coding challenge things!
I'm not great with maths, but I'm guessing that all walks must have an even length, otherwise you can't end up back at your starting location, you'd always be one away...
If that assumption is true, then I think this works:
Basically, for each "pair" of blocks you want to walk, either add
["n", "s"]
or["e", "w"]
to an array, then flatten and shuffle! Because you're always adding both a movement and it's inverse, you'll always end up where you started!Ruby's proc / block thing still confuses me a little so I'm not sure if this is the most elegant one-liner (excluding checking for even walk length), but ayy it works!
Great solution! I believe your assumption holds up and makes this solution nice and elegant!
Great job on your first challenge too!!
nice 🤩
Here my contribution with javascript language:
Here's a trivial solution in Python
If you add the requirement that you can't walk the same street twice...
JS
Not a great solution, but a solution.
And a one-liner but not really randomized, just going north-then-south:
And the link to a demo on CodePen
Just make sure the number of s's and n's is the same, and the same for w's and e's.
Perl solution:
Barely in before/at midnight!
But here is my Rust version!
I took a bit of a different route! @spaciecat killed the simple algorithm answer so I wanted to do something different!
I decided to make a program that lets the user build the path they want to take! Cause why not let the User choose where to explore! I might clean this up and turn it into a full post sometime soon!
Here is a really quick demo GIF
Spacie's algo, in Clojure:
PHP
My solution in Swift language :
During my reflexion about this challenge, I figured out that if input is an odd number, you will never be able to go back at your start position.
Here my JS implementation:
PHP
Here is my JavaScript solution.
My approach was to depart in some random direction, duplicate that initial set of directions, reverse each individual direction within that copy (north goes to south, etc.), then reverse that entire copy to return to the starting point.
The part I had trouble with was swapping each individual direction. I stored my directions in an array that follows a compass because I figured that the reverse direction is always two away: north's array position + 2 = south, east + 2 = west. I ran into some trouble shifting the directions because I could not add two in all cases because it would go outside of the array bounds, so I put in the if-statement. I'm not sure if there is a way to "wrap" an array in JavaScript without going out of the array bounds. I believe in Python this can be done with the slice notation (double colon syntax). If anyone has a tip for JavaScript, feel free to share.
For wrapping some sum
x + y
inside of a rangen
, you can use(x + y) % n
(and of course bounds-checking withif
works too).N.B. different languages treat negative mod differently. For JS, in the negative case you'd want something like
((x % n) + n) % n
instead of plain%
.BASH
Powershell
I went the voluntary choice route with a voluntary initial route, and reversing it to get back.