The simplest path
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
- Let's say John starts at point
A
and goes to pointB
. - On his way he passes point
C
. - Later he returns to point
B
(remember that John may be going in circles sometimes). - The sublist of directions between the first and second visits of point
B
SHOULD BE REMOVED, as John doesn't progress by following those. - He starts walking around again, and passes through point
C
. - 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.
Similar Kata:
Stats:
Created | Feb 11, 2016 |
Published | Feb 11, 2016 |
Warriors Trained | 499 |
Total Skips | 20 |
Total Code Submissions | 1244 |
Total Times Completed | 120 |
Ruby Completions | 22 |
Python Completions | 105 |
Total Stars | 23 |
% of votes with a positive feedback rating | 97% of 45 |
Total "Very Satisfied" Votes | 42 |
Total "Somewhat Satisfied" Votes | 3 |
Total "Not Satisfied" Votes | 0 |
Total Rank Assessments | 4 |
Average Assessed Rank | 6 kyu |
Highest Assessed Rank | 4 kyu |
Lowest Assessed Rank | 8 kyu |