Note: This is based on Two roads diverged in a yellow wood (part 2), a previous challenge of mine. Because of the popularity of that question and Two roads diverged in a yellow wood (part 1), I wanted to make a third. But the first 2 were too easy (a 2-byte answer on the first, a 15-byte answer on the second.) So I made something more complex...
The inspiration
This challenge is inspired by Robert Frost's famous poem, The Road Not Taken:
Two roads diverged in a yellow wood,
And sorry I could not travel both
And be one traveler, long I stood
And looked down one as far as I could
To where it bent in the undergrowth;...2 paragraphs trimmed...
I shall be telling this with a sigh
Somewhere ages and ages hence:
Two roads diverged in a wood, and I —
I took the one less traveled by,
And that has made all the difference.
Notice the second to last line, I took the one less traveled by,
.
The backstory
You were assigned to help a blind adventurer who is walking on a road and was inspired by The Road Not Taken. The adventurer is approaching a fork in the road and would like to take the path less traveled by. You must find where the adventurer actually is and tell the adventurer where to turn.
The challenge
Your goal is to find the road least traveled by on your map where the road forks. Your map is a string containing newlines (or \n
, if you prefer) and has an unknown width and height. In the map, roads are made up of digits 0 to 9, the intersection are made of #
s. You must find the road you are currently on, and out of the other roads the road most traveled by, and the road less traveled by for your blind adventurer. Woods in you map is represented by a space. Here is a simple map:
2 2
1 0
#
2
2
This map is 5 wide and 5 tall. Notice how the road forks in a Y shape. The Y may be oriented any way, so you must be able to understand a "rotated" map.
What the #
means
Where the map forks there will be a #
. This does not impact the score of any path.
What the numbers actually mean
Each path (a line of numbers, may have a bend in it) has a score. A path's score is determined by adding up its digits, so for the first example, the first path (from upper-left, clockwise) has a score of 2+1=3, the second has 2+0=2, and the third has 2+2=4. Roads may contain numbers connected diagonally.
Finding where you are
You are on the path with the highest score. The other 2 paths are the road more traveled by, and the road less traveled by. You need to find the road with the lowest score.
Telling your traveler where to go
You must tell your traveler to go "left" or "right". Keep in mind that directions are from your traveler's point of view (he is facing the fork.)
Example maps
14
9#
04
Output: "right" (traveler is at the 9
road, 0+4 < 1+4
9
9
9
9
9
#
8 8
8 8
88 88
8 7
Output: "left" (traveler is at the 99999
road, 8+8+8+8+8 > 8+8+8+8+7
02468
#98765
13579
Output: "right" (traveler is at the 98765
road, 0+2+4+6+8 < 1+3+5+7+9)
4 2
4 2
#
4
4
2
2
Output: "right" (traveler is at the 4422
road, 4+4 > 2+2)
9
9
9
#
8 7
8 7
8 7
Output "left" (traveler is at the 999
road, 8+8+8 > 7+7+7
Stuff to know:
- Maps will be padded with spaces to make each line the same length.
- You must output to STDOUT / console / file the string
left
orright
, optionally followed by a trailing newline. - You must take input as either a string containing newlines,
\n
s, or an array/list of lines (each line is a string). Where that input is put must be a function, command-line argument, file, or STDIN one line at a time or similar. A variable is not a acceptable input device (unless it is a function parameter.) Likewise, function expressions in JS and other languages must be assigned to a variable. - This is code-golf, so the shortest answer in bytes wins!
- Standard loopholes forbidden
Things you can assume
- Your input will be valid. Nothing like this will be tested for:
0 0 0 0 0 # 0 0
- Paths' scores will never be tied.
- The input can be of any length in width or height, less than the string limit of your language.
- There will always be at least 1 space between the 2 paths.
- Paths may have bends, turns, etc. These are roads, not highways.
#
always be in the center horizontally? \$\endgroup\$