My dog is barking at me, but I'm too lazy to take him for a walk. I have an idea! I'll get the people at Code Golf to do it for me!
Your challenge is to simulate a dog's walk. Here are the rules for walking the dog:
- The human (
H
) will start at0,0
on a (Cartesian) coordinate plane, and will randomly go one space either up, left, right, or down every second. - The dog (
D
) will start at the same location, and will go either zero, one, two, or three spaces either up, left, right, or down every second (randomly, of course). The dog is less predictable and will sometimes run faster or stop completely. - The dog will never get more than a certain amount of units away from the human (in Euclidean distance), which is the length of the leash (
L
). For example, ifL
is6
, a valid position would beH(0,0) D(4,4)
(since the distance is about 5.65 units), but notH(0,0) D(5,4)
(about 6.4 units).- If, when the dog moves, it would violate the leash distance requirement, it must go the farthest it can in its determined direction that does not violate the distance requirement. For example, if the position is
H(0,0) D(3,4)
and the dog is randomly decided to move 3 spaces to the right, it will go toD(4,4)
, since that's the farthest it can go without getting more than 6 units away. (Note that this might result in a movement of 0 spaces, i.e. no movement at all!)
- If, when the dog moves, it would violate the leash distance requirement, it must go the farthest it can in its determined direction that does not violate the distance requirement. For example, if the position is
- Miscellaneous rules: the human moves first. The human must not exceed the leash length on its move. If it does, it must choose another direction. "Random" means "with an even distribution and with no patterns."
The input will be given in this format (can be STDIN, function parameter, file, etc.):
"<amount of seconds the walk will last> <leash length>"
For example:
300 6 // a 5 minute walk with a 6 yard leash
You must give output in this format (can be STDOUT, function return value, file, etc.):
H(0,0) D(0,0)
H(0,1) D(2,0)
H(1,1) D(2,1)
H(1,0) D(2,-1)
...
(The amount of lines output will be seconds + 1
, since the H(0,0) D(0,0)
line doesn't count.)
This is code-golf, so the shortest code in bytes will win!