30
1
Background
I have a ladder leaning on a wall, and a remote-controlled robot that can climb it. I can send three different commands to the robot:
UP
: the robot takes one step upwards. If it was on the highest step, it trips over, falls down and explodes.DOWN
: the robot takes one step downwards. If it was on the lowest step, nothing happens.RESET
: the robot returns to the lowest step.
I can also send a series of commands, and the robot will execute them one by one. Your task is to predict its movements.
Input
Your inputs are a positive integer N
, representing the number of steps in the ladder, and a non-empty string C
over UDR
, representing the commands I have sent to the robot.
You can assume that N < 1000
.
The robot is initialized on the lowest step of the ladder.
Output
It is guaranteed that at some point, the robot will climb over the highest step and explode. Your output is the number of commands it executes before this happens.
Example
Consider the inputs N = 4
and C = "UDDUURUUUUUUUDDDD"
The robot, denoted by @
, moves along the 4-step ladder as follows:
|-| |-| |-| |-| |-| |-| |-| |-| |-| |@| |-||
|-| |-| |-| |-| |-| |@| |-| |-| |@| |-| |-||
|-| |@| |-| |-| |@| |-| |-| |@| |-| |-| |-|v
|@| U |-| D |@| D |@| U |-| U |-| R |@| U |-| U |-| U |-| U |-|# Boom!
The remaining commands are not executed, since the robot has exploded.
The explosion took place after 10 commands, so the correct output is 10
.
Rules and scoring
You can write a full program or a function. The lowest byte count wins, and standard loopholes are disallowed.
Test cases
1 U -> 1
1 DDRUDUU -> 4
4 UDDUUUUURUUUUDDDD -> 7
4 UDDUURUUUUUUUDDDD -> 10
6 UUUUUDRUDDDDRDUUUUUUDRUUUUUUUDR -> 20
10 UUUUUURUUUUUUURUUUUUUUURUUUUUUUUUUUUUU -> 34
6 UUUDUUUUDDDDDDDDDDDDDDRRRRRRRRRRRUUUUUU -> 8
6 UUUDUUUDURUDDDUUUUUDDRUUUUDDUUUUURRUUDDUUUUUUUU -> 32
20 UUDDUDUUUDDUUDUDUUUDUDDUUUUUDUDUUDUUUUUUDUUDUDUDUUUUUDUUUDUDUUUUUUDUDUDUDUDUUUUUUUUUDUDUUDUDUUUUU -> 56
354 UUDDUUDUDUUDDUDUUUUDDDUDUUDUDUDUDDUUUUDUDUUDUDUUUDU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UUUDUDUUUUDUUUUDUDUUUUDUUUUDUDUUUUDUUUUDUDUUUUDUUUUDUDUUUUDUUUUDUDUUUUDUUUUDUDUUUUUU -> 872
1Related – Peter Taylor – 2016-03-01T20:50:47.503
8I'm disappointed the task isn't to generate that ASCII art. – user253751 – 2016-03-02T02:34:34.400
6
@immibis You know what to do...
– Martin Ender – 2016-03-02T12:31:36.473Instead of a string, can we take a list of character codes? – Cyoce – 2016-03-03T03:04:57.940
@Cyoce Only if your language has no other means to represent a string. – Zgarb – 2016-03-03T03:57:05.870
@Zgarb what about a list of commands (characters)? – Cyoce – 2016-03-03T04:37:36.977
@Cyoce Same answer, only if that's how your language represents strings. – Zgarb – 2016-03-03T04:40:41.417