logo CodeStepByStep logo

rainRisk2

Write a method named rainRisk2 (similar to the previous exercise, rainRisk). The situation is similar to the one described in rainRisk except that the actions in the file now describe the movement of a waypoint near the ferry.

The waypoint is initially located 10 units east and 1 unit north from the ferry. The waypoint is relative to the ship; that is, if the ship moves, the waypoint moves with it.

Each line of the input data contains an action followed by an integer value. The actions are the following:

  • Action N means to move the waypoint north by the given value.
  • Action S means to move the waypoint south by the given value.
  • Action E means to move the waypoint east by the given value.
  • Action W means to move the waypoint west by the given value.
  • Action L means to rotate the waypoint around the ship left (counter-clockwise) the given number of degrees, assumed to be a multiple of 90.
  • Action R means to rotate the waypoint around the ship right (clockwise) the given number of degrees, assumed to be a multiple of 90.
  • Action F means to move forward to the waypoint a number of times equal to the given value.

Note that the actions now largely describe waypoint movement, not ferry movement, except for F.

For example, suppose the file named instructions.txt contains the following text:

F10
N3
F7
R90
F11

These instructions would be interpreted as follows:

  • F10 moves the ship to the waypoint 10 times (a total of 100 units east and 10 units north), leaving the ship at (100, 10). The waypoint stays 10 units east and 1 unit north of the ship.
  • N3 moves the waypoint 3 units north to 10 units east and 4 units north of the ship. The ship remains at (100, 10).
  • F7 moves the ship to the waypoint 7 times (a total of 70 units east and 28 units north), leaving the ship at (170, 38). The waypoint stays 10 units east and 4 units north of the ship.
  • R90 rotates the waypoint around the ship clockwise 90 degrees, moving it to 4 units east and 10 units south of the ship. The ship remains at (170, 38).
  • F11 moves the ship to the waypoint 11 times (a total of 44 units east and 110 units south), leaving the ship at (214, 72). The waypoint stays 4 units east and 10 units south of the ship.

As before, your code should simulate all the movement instructions and then return the straight-line "Manhattan distance" from its original position, defined as the sum of the absolute x-distance and y-distance. For the instructions shown previously, the ship's Manhattan distance is 214 + 72 = 286. So the call of rainRisk2("instructions.txt") would return 286.

You may assume that the file exists and is readable, that it follows the format described above, and that there will be at least one line of input in the file.

(This exercise is based on the Advent of Code 2020, day 12.)

Method: Write a Java method as described, not a complete program or class.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.