Background
You are planning your trip away from the Diamond City, and must travel through Diamond Road. However, Diamond Road branches away from Diamond City to different locations.
Out of curiosity, you'd like to measure the total distance of unique roads used for all paths.
We define a path as a string that only contains /
or \
, which represent roads. You will be given a list of paths that can be traversed.
Starting from a central left most point, a path can be plotted on a map such that every /
denotes a path upwards and every \
denotes a path downwards. On every path, each road must strictly go from left to right.
Subsequent roads must be joined at the same level as the previous road if they are different types. If they are the same type, the level will change.
For example:
Given //\\/\\\
The map generated would be:
/\
Start here _ / \/\
\
\
Since you can have multiple paths, these paths may cross and share the same road.
For example:
Given /\ /\/
The map generated would be:
Start here _ /\/
Here, the first two roads in both paths are shared, but the total length of roads used would be 3, as shown on the map.
You must calculate the amount of unique roads used in all paths.
Your Task
Sample Input: A list of paths used, or a string of paths separated by spaces.
Output: Return the total length of roads used.
Explained Examples
Input => Output
/ \ / \ => 2
Map:
Start here _ /
\
Of the roads used, the total distance is 2.
Input => Output
/\/ \/\ /// => 8
Map:
/
/
Start here _ /\/
\/\
Of the roads used, the total distance is 8.
Input => Output
//// \/\/ /\/\ //\\ => 12
Map:
/
/
/\
Start here _ /\/\
\/\/
Of the roads used, the total distance is 12.
Input => Output
/\//\//\/ \/\/\\//\ \//\/\/\/ \//\//\// /\/\/\/ \/ \\\ \//\/ \ => 28
Map:
/\/
/\/\/
Start here _ /\/\/\/\/
\/\/\ /\
\ \/
\
Of the roads used, the total distance is 28.
Test Cases
Input => Output
/ \ / \ => 2
/\/ \/\ /// => 8
//// \/\/ /\/\ //\\ => 12
/\//\//\/ \/\/\\//\ \//\/\/\/ \//\//\// /\/\/\/ \/ \\\ \//\/ \ => 28
\ => 1
/ => 1
\ \/\ => 3
/\ \/\/\/ => 8
/\ / \/\ \ => 5
//////////////////// => 20
////////// ////////// => 10
////////// \\\\\\\\\\ => 20
\ /\ \/ /\ \\\\\\\\\\ => 13
\/\ /\/ //\ \\/ \/ /\ / \ => 10
/\ /\/ /\/\ /\/\/ /\/\/\ /\/\/\/\ => 8
/\ \/ /\ /\ \/ /\ /\ \/ \/ \/ /\ /\ => 4
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ / => 2
This is code-golf, so shortest answer wins.
ps. I need help with tagging this question