You will be given two pieces of input: a string in run-length encoded format defining the running track, and a capital letter representing the lane to start from. For example, the string "3a4A6b5B" expands to "aaaAAAAbbbbbbBBBBB". You then use the expanded string to create a track, as such:
A) aaaAAAA
B) bbbbbbBBBBB
This is a track with two lanes. Lowercase letters represent air. You can not run on air! Uppercase letters represent road you can run on. Your goal for this challenge is, given a capital letter, output how far a racer starting on that lane could run. Racers are allowed to switch lanes if there is a piece of road directly above or below them. They are also allowed to run backwards! On this particular track the output is 0 for any letter input, because neither of the tracks has runnable road at position 1.
Examples:
Input: "4A5B4c3C", "A"
This code expands to a track that looks like this:
A) AAAA
B) BBBBB
C) ccccCCC
The output for this example is 7, because a runner starting on lane A could move down to lane B, and then lane C, and end up at the 7th position.
Input: "4A2B3D", "D"
Track:
A) AAAA
B) BB
C)
D) DDD
The output is 3, because a runner starting on lane D has no way to get to lane B or A
Input: "4A4a4A3b6B5C", "A"
Track:
A) AAAAaaaaAAAA
B) bbbBBBBBB
C) CCCCC
The output is 12, because the runner on A can switch over to B, and then come back to A at the end. The max distance for "C" is also 12. For "B" it is 0.
Input: "12M4n10N11O", "M"
Track:
M) MMMMMMMMMMMM
N) nnnnNNNNNNNNNN
O) OOOOOOOOOOO
Simple example with multi-digit run-lengths. Output is 14.
Input: "4A5B1b2B4c3C", "A"
Track:
A) AAAA
B) BBBBBbBB
C) ccccCCC
The output is 8, because the runner at A can go down to B, then down to C, then come back to B. (Thank you to FryAmTheEggman for this example.)
Input: "1a2A2a2B1c1C1d3D", "B"
Track:
A)aAAaa
B)BB
C)cC
D)dDDD
Output is 4. Runner has to check both paths two see which goes further. (Thanks to user81655 for this example.)
Input: "2A1b1B2C1D3E","A"
Track:
A) AA
B) bB
C) CC
D) D
E) EEE
Output is 3. You have to run backwards to reach the furthest destination. (Once again, thanks to user81655 for this example.)
Notes:
- If a track does not have a letter at a certain position, that counts as air too. As such, if the input is "Q" and no road has been placed on lane "Q" the output should be 0.
- There are two pieces of input. The first is a run-length encoded string. The second is a capital letter (you can use string or char datatype for this.) For readability, there should be some reasonable separator between these inputs (space, new line, tab, comma, semi-colon).
- The run-length encoded string will always list elements in alphabetical order
- The very longest the entire length of a lane can be is 1000. Therefore, the greatest possible output is 1000.
Track Generator:
In honor of our first answer, here is a track generator. Try to come up with something to stump the current answers! (Note: Just because the generator doesn't show an error message doesn't mean your track code is necessarily valid. See above examples for proper form.)
function reset() {
var t = document.getElementById("track");
t.innerHTML = "";
for(var i = 0;i<26;i++) {
var c = String.fromCharCode(i+65);
t.innerHTML += "<div><span>"+c+") </span><span id='"+c+"'></span></div>";
}
}
function rand() {
var track = "";
for(var i = 0;i<26;i++) {
var blocks = Math.floor(Math.random()*4);
var start = Math.floor(Math.random()*2);
for(var j = 0;j<blocks;j++) {
var letter = String.fromCharCode(65+i+32*((start+j)%2));
var length = Math.floor(Math.random()*4)+1;
track += length+letter;
}
}
document.getElementById("code").value = track;
}
function gen() {
var s = document.getElementById("code").value;
var check = s.match(/(\d+[A-Za-z])+/);
if(check == null || check[0]!=s) {
alert("Invalid Track");
return false;
}
reset();
var n = s.match(/\d+/g);
var o = s.match(/[A-Za-z]/g);
for(var i = 0;i<n.length;i++) {
var c = o[i].toUpperCase();
document.getElementById(c).textContent += o[i].repeat(n[i]);
}
return true;
}
<body onload="reset()">
Track: <input type="text" id="code" size="75%" /><input type="submit" onclick="gen()" /><input type="button" value="Random Track" onclick="rand()" /><code id="track"/>
</body>
4A2B3D
could be removed? For instance, adding0c
? If not, is it expected when say1A1Z
were given, lanes B-Y are assumed to exist (but are empty)? \$\endgroup\$12M4n10N11O
example, output 14, is then false: the longest path starts at M0 and ends at C0, for a length of 25. \$\endgroup\$