6 kyu

The simplest path

22 of 120sebaas

Description:

Story

John found a path to a treasure, and while searching for its precise location he wrote a list of directions using symbols "^", "v", "<", ">" which mean north, south, west, and east respectively. On his way John had to try many different paths, sometimes walking in circles, and even missing the treasure completely before finally noticing it.


Task

Simplify the list of directions written by John by eliminating any loops.

Note: a loop is any sublist of directions which leads John to the coordinate he had already visited.


Examples

   Path        |  Result   
---------------------------
  "<>>"        |  ">"      
  "<^^>v<^^^"  |  "<^^^^"  
  ""           |  ""       
  "^<<v"       |  "^<<v"   

Simplification explanation

  1. Let's say John starts at point A and goes to point B.
  2. On his way he passes point C.
  3. Later he returns to point B (remember that John may be going in circles sometimes).
  4. The sublist of directions between the first and second visits of point B SHOULD BE REMOVED, as John doesn't progress by following those.
  5. He starts walking around again, and passes through point C.
  6. These directions SHOULD NOT BE REMOVED because after simplifying his path he arrives at point C for the first time! (Remember that John doesn't follow the directions he simplifies, so visiting it for the "first time" is a hypothetical path, and visiting it for the "second time" is his real path; when hypothetical and real paths intersect, we ignore this fact.)

Example

    > > v
    ^   v
> > C > D > >
^   ^   v
^ < B < <
    ^
    A

John visits points A -> B -> C -> D -> B -> C -> D, realizes that -> C -> D -> B steps are meaningless and removes them, getting this path: A -> B -> (*removed*) -> C -> D.

    ∙ ∙ ∙
    ∙   ∙
> > C > D > >
^   ∙   ∙
^ < B ∙ ∙
    ^
    A

Following the final, simplified route John visits points C and D, but for the first time, not the second (because we ignore the steps made on a hypothetical path), and he doesn't need to alter the directions list anymore.

Algorithms

More By Author:

Check out these other kata created by sebaas

Stats:

CreatedFeb 11, 2016
PublishedFeb 11, 2016
Warriors Trained499
Total Skips20
Total Code Submissions1244
Total Times Completed120
Ruby Completions22
Python Completions105
Total Stars23
% of votes with a positive feedback rating97% of 45
Total "Very Satisfied" Votes42
Total "Somewhat Satisfied" Votes3
Total "Not Satisfied" Votes0
Total Rank Assessments4
Average Assessed Rank
6 kyu
Highest Assessed Rank
4 kyu
Lowest Assessed Rank
8 kyu
Ad
Contributors
  • sebaas Avatar
  • FArekkusu Avatar
  • Just4FunCoder Avatar
  • saudiGuy Avatar
Ad