Question #6 -- Jumping Jack in 2 Space
Jumping Jack is a precision "jumper" who lives in the Cartesian Plane. Jack carries around a magic stick at all times, which he uses to measure his "jumps". Jack knows how to execute a number of commands:
- J:
jump forward a distance equal to the length of the stick.
- R:
turn 90 degrees to the right
- L:
turn 90 degrees to the left
- H:
press the "halve" button on the magic stick, which exactly halves the length of the stick.
- D:
press the "double" button on the magic stick, which exactly doubles the length of the stick.
Every day when Jack wakes up he mysteriously finds himself at the origin of his world (0, 0) facing north. Equally myserious is the fact that no matter what length his stick was at the end of the previous day, it always returns to its "natural length" of100.0
units by morning. Jacks job each day is to execute exactly one sequence of commands. For example, he might have to execute: JJRHJRHJLDDDJJLDJ. That is he might have to jump forward a distance equal to the length of the stick twice, turn right 90 degrees, press the "halve" button on the stick, jump forward the stick's length, and so on!
The Program
Write a program which reads a sequence of commands (represented as a sequence of letters) from the standard input file, simulates Jack's execution of the commands, and then displays the distance (as the crow flies) that Jack finds himself from the origin.
IO Examples
os> run JumpingJack
Command sequence: JJ
Jack's distance from home is: 200.0
os> run JumpingJack
Command sequence: JRJ
Jack's distance from home is: 141.4213562373095
os> run JumpingJack
Command sequence: JHJ
Jack's distance from home is: 150.0
os> run JumpingJack
Command sequence: JLJHJ
Jack's distance from home is: 180.27756377319946
os> run JumpingJack
Command sequence: JDDJLHJRHJJJLHJLHJLHJ
Jack's distance from home is: 810.5746418436738
|
|