Two roads diverged in a yellow wood (part 1)
JavaScript(ES6), 19 12 bytes
Edit:
A more golfed version is
a=>a.trim[1]
Returns #
for right and a space for left.
Original:
a=>a.trim()[1]=='#'
Explanation
Ungolfed:
function(input) {
return input.trim().charAt(1) === '#';
};
The first thing this function does is remove white space the beginning and end of the input. This means that the first character is always #
. Then from there I check the second character(JavaScript starts at 0) and see if it is a #
character.
This returns a boolean. If the path is right
it will be true
, if it is left it will return false
.
How I golfed it
In ES6 there is an anonymous function shorthand called an arrow function. This means that I can take my wrapper function and turn it into:
input => ...;
Due to the rules of arrow functions it will return the rest of the code. From there I converted charAt(1)
to [1]
as it is a shorter way, though not recommended. Then I took ===
and turned it into ==
. While they are different in this case it doesn't matter. Finally, I renamed input
to a
and removed all whitespace.
Output right and left
While the puzzle doesn't actually need the program to output right and left, here's an example of other outputs:
a=>a.trim()[1]=='#'?'right':'left'
The only added part is ?'right':'left'
. This creates a ternary operator, a condensed if statement, this means that the(ungolfed) code is equal to*:
function(input) {
let output = input.trim().charAt(1) === '#';
if(output) {
return 'right';
} else {
return 'left'
}
};
Example
// Function assignment not counted in byte count
let f =
a=>a.trim()[1]=='#'
<textarea placeholder="Put path in here" id="path" rows="10" style="width:100%"></textarea>
<button onclick="document.getElementById('result').textContent = f(document.getElementById('path').value)">Submit</button>
<p id="result"></p>
CJam, 1 byte
r
r
puts the first string of adjacent non-whitespace characters from STDIN on the stack, so this prints ##
for left and #
for right.
Try it online!
Pyth, 2 bytes
hc
Outputs #
for left and ##
for right.
Try it online
Explanation
hc
cQ Split the (implicit) input on whitespace.
h Get the first part.