14
\$\begingroup\$

This is the first in a series, the second is Two roads diverged in a yellow wood (part 2)

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;

Then took the other, as just as fair,
And having perhaps the better claim,
Because it was grassy and wanted wear;
Though as for that the passing there
Had worn them really about the same,

And both that morning equally lay
In leaves no step had trodden black.
Oh, I kept the first for another day!
Yet knowing how way leads on to way,
I doubted if I should ever come back.

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,.

Your actual challenge

You will take input in the form like:

#     ##
 #   ##
  # ##
   #
   #
   #

and you have to find the thinner road.

The road starts at the bottom with a #. The other 2 roads, which always terminate on the top row, are the roads you have to examine. The road that is the thickest is most traveled by, and therefore it is not what you want. The other one is the least traveled by, and it is the one you want.

Output

Your program/function must output one of 2 distinct values (eg. 0 or 1, true or false), one for each possible position of the road not taken. For example, you could output 0 if the road not taken is on the left of the road taken, and 1 otherwise, or you could output the string "left" or "right", true, false, etc.

Test cases:

 ##    #
  ##  #
   ###
    #
    #
    #

Might output "right".

 ##  #   
  ## #  
   ###
    ##
     #
     #
     #

Might output "right".

 ##  #   
  ## #  
   ###
    ##
   # 
  #  
 #   

Might output "right".

 ##   #  
  ## #  
   ###
    #
   # 
  #  
  #  

Might output "right".

 #    ## 
  #  ## 
   ###
    #
   # 
  #  
  #  

Might output "left"

 #    ## 
  #  ## 
   ###
    #
     #
     #
     #

Might output "left"

Notes

  • This is , so the shortest answer in bytes wins
  • Standard loopholes forbidden
  • You must specify your outputs for "left" and "right" and they must be distinct
  • Input will be one big string, and may have any amount of lines
  • You don't need to worry about valid input.
  • The road is always a Y shape, so you only have to look at the top.
  • Have any questions? Comment below:

Lowest byte count wins!

\$\endgroup\$
  • 1
    \$\begingroup\$ Yes, always # and ##, and horizontal crosssection. \$\endgroup\$ – programmer5000 Mar 28 '17 at 19:49
  • 1
    \$\begingroup\$ Wasn't me. I quite like it. Well defined, good back story and clear examples. It's fairly easily doable and I think the idea of inviting obscure languages only adds extra flavour and could encourage some interesting answers. \$\endgroup\$ – ElPedro Mar 28 '17 at 20:52
  • 1
    \$\begingroup\$ IBM/Lotus Notes Formula Language and Acc!! responses are proof of how inviting obscure languages to easy questions leads to interesting results. \$\endgroup\$ – programmer5000 Mar 28 '17 at 20:55
  • 3
    \$\begingroup\$ I was just saying to myself: "You know a challenge is simple when you think, 'Hey, this will be easy in Acc!!'" ;) \$\endgroup\$ – DLosc Mar 28 '17 at 21:00
  • 22
    \$\begingroup\$ @programmer5000: I'm one of the downvoters. I downvoted the challenge because it's something of a chameleon challenge (in that it looks like it's about parsing or otherwise measuring the roads, but actually it's just "split on spaces, take the first section"), with a lot of background that's only tangentially related and to obscure the challenge still more; and because it's extremely easy (something that normally makes me downvote challenges). It's also incompletely specified (e.g. can inputs have widths other than 1 and 2?) \$\endgroup\$ – user62131 Mar 28 '17 at 22:09

23 Answers 23

16
\$\begingroup\$

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!

\$\endgroup\$
  • 4
    \$\begingroup\$ This looks like the source code for Pokemon Go. \$\endgroup\$ – Taylor Lopez Mar 29 '17 at 18:40
18
\$\begingroup\$

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>

\$\endgroup\$
  • 3
    \$\begingroup\$ Welcome to the site! Nice explanation and snippet, it's a very thorough first answer. :) \$\endgroup\$ – DJMcMayhem Mar 28 '17 at 21:39
  • 2
    \$\begingroup\$ Thanks. I've been surfing code golf for a while and finally decided to make one. Suppose its StackOverflow rubbing off on me. \$\endgroup\$ – David Archibald Mar 28 '17 at 21:41
  • 1
    \$\begingroup\$ Wow, totally outgolfed! \$\endgroup\$ – programmer5000 Mar 28 '17 at 21:44
  • 1
    \$\begingroup\$ That's a really cool approach. Hadn't thought of that one. \$\endgroup\$ – ElPedro Mar 28 '17 at 21:54
  • 1
    \$\begingroup\$ I'll edit it as soon as @programmer5000 responds. It's a great idea, thanks. \$\endgroup\$ – David Archibald Mar 28 '17 at 22:33
10
\$\begingroup\$

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.
\$\endgroup\$
10
\$\begingroup\$

Acc!!, 30 bytes

Because of the way Acc!! takes input, it will give the output after just one line of input is entered. But if you pipe the input in or redirect it from a file, you shouldn't notice the difference.

Count i while 35-N {
}
Write N

Takes input from stdin. Outputs if the left-hand road is less traveled, or # if the right-hand road is less traveled. Try it online!

Explanation

N reads the ASCII value of a character from stdin every time it is referenced. We loop while 35-N is truthy; that is, while 35-N != 0 or N != 35. Therefore, when the loop exits, we have just read the first # character on the line. The next character is then read with N and written back to stdout with Write.

\$\endgroup\$
  • \$\begingroup\$ Wow! Yet another obscure language... \$\endgroup\$ – programmer5000 Mar 28 '17 at 20:38
  • 7
    \$\begingroup\$ @programmer5000 If you want obscure programming languages, you've come to the right site. ;) \$\endgroup\$ – DLosc Mar 28 '17 at 20:40
  • \$\begingroup\$ Where can I run Acc!!? Is there a GitHub link, Tio, or other? \$\endgroup\$ – programmer5000 Mar 28 '17 at 20:42
  • \$\begingroup\$ @programmer5000 The Python 3 source code is in the post I linked in the header, but I'll see if I can cobble together a TIO link for you. \$\endgroup\$ – DLosc Mar 28 '17 at 20:48
  • \$\begingroup\$ @programmer5000 Added TIO link to the answer. \$\endgroup\$ – DLosc Mar 28 '17 at 20:58
8
\$\begingroup\$

Retina, 5 bytes

Outputs 1 if right, 0 if left.

^ *##

Try it online


If the values for a positive result didn't have to be distinct (5 bytes):

Outputs a positive integer if right, zero if left.

## +#

Try it online

\$\endgroup\$
  • 1
    \$\begingroup\$ Wow! How does it work? Care editing your answer to explain? \$\endgroup\$ – programmer5000 Mar 28 '17 at 19:58
  • 1
    \$\begingroup\$ @programmer5000 It just tests the input against the regular expression for a match. \$\endgroup\$ – mbomb007 Mar 28 '17 at 19:59
6
\$\begingroup\$

IBM/Lotus Notes Formula Language, 37 35 26 bytes

Edit I always forget that @Like with wildcards is 2 bytes cheaper than @Contains.

Edit 2 Actually doesn't need the @if as it just prints 1 or 0 depending on whether the formula results to @True or @False.

@Like(@Left(a;"##");"%#%")

Computed field formula. Simply take everything to the left of the first ## it finds in field a and if there is a # in it outputs 1 for left otherwise outputs 0 for right.

enter image description here

enter image description here

With thanks to @DavidArchibald, here is a solution for 22 bytes. Out of respect for Davids solution I won't post it as my main answer.

@Left(@Trim(a);2)="##"

This one outputs 1 for right and 0 for left.

\$\endgroup\$
  • \$\begingroup\$ Wow! Quite the obscure language! May be accepted if a better answer does not come soon... \$\endgroup\$ – programmer5000 Mar 28 '17 at 20:18
  • 1
    \$\begingroup\$ Used to be not so obscure in the good old days when I was a young(er) programmer ;-) \$\endgroup\$ – ElPedro Mar 28 '17 at 20:22
4
\$\begingroup\$

Pip, 8 6 bytes

a~`#+`

Takes input as a command-line argument (which will need quoting and escaping of newlines when run from an actual command line). Outputs # if the left-hand road is less traveled, and ## if the right-hand road is less traveled. Try it online!

Explanation

This uses Pip's recently added regex first-match operator.

a         First cmdline arg
 ~        Regex match the first instance of...
  `#+`    ...one or more #s (i.e. a cross-section of the left-hand road)
          Print (implicit)

The straightforward regex solution (a port of mbomb007's Retina answer) is 9 bytes:

`^ +##`Na
\$\endgroup\$
  • \$\begingroup\$ Did you count the command-line argument in the byte count? \$\endgroup\$ – programmer5000 Mar 28 '17 at 20:14
  • \$\begingroup\$ @programmer5000 Taking input via command-line argument is an input method that is permitted by default (and is the usual way Pip gets input). The byte penalty applies to non-standard command-line flags, which I'm not using in this submission. That said, in this particular case, one could change the a to a q and get the input from stdin instead. \$\endgroup\$ – DLosc Mar 28 '17 at 20:18
  • \$\begingroup\$ Oh. I didn't understand. \$\endgroup\$ – programmer5000 Mar 28 '17 at 20:19
4
\$\begingroup\$

Chip, 7 bytes

AZ~S
at

Try it online!

Outputs 0x0 for left, and 0x1 for right. (The TIO includes flag -v so you can see the binary values in stderr. To see the output in ASCII, e*f can be appended to the end of the first line.)

Chip operates on individual bits within a byte stream, which actually makes it quite good at this specific problem.

A is the least significant bit of the input byte, and '#' is the only character of input for which this bit is set. When this bit is first encountered, we have reached the first '#' of the first line.

Z delays that signal for one cycle, so that we are now looking at the next character.

t is now activated, which means to terminate execution after this cycle is completed. We don't need to look further than the width of the first road.

~S suppresses output for all cycles except the final one. If this wasn't here, we'd get an output on every cycle.

a puts the current value of its neighbors (only A in this case) to the least significant bit of the output byte.

All of this means that we get a 0x1 if the first '#' is immediately followed by another '#', and 0x0 otherwise.

\$\endgroup\$
4
\$\begingroup\$

C, 35 bytes

f(char*s){while(35^*s++);return*s;}

Same idea as PragmaticProgrammer's answer: find the first #, and output what comes after it -- # for "right", and <space> for "left".

C (loophole), 16 bytes

According to the test cases, it looks like the left road is always exactly one space from the left margin. So...

#define f(s)2[s]
\$\endgroup\$
  • \$\begingroup\$ The loophole was not intended, those test cases were incorrect. But your C answer is impressive! \$\endgroup\$ – programmer5000 Mar 29 '17 at 12:06
3
\$\begingroup\$

Batch, 46 bytes

@set/ps=
@for %%a in (%s%)do @echo %%a&exit/b

Reads one line from STDIN, splits it on spaces, and prints out the first word, so outputs # for left and ## for right. If an array of quoted command-line parameters is acceptable, then for 36 bytes:

@for %%a in (%~1)do @echo %%a&exit/b

Unquotes the first argument so that it will be split on spaces and print its first word.

\$\endgroup\$
3
\$\begingroup\$

Python 2, 21 bytes

Try it online

lambda S:S.split()[0]

Output # for left and ## for right

\$\endgroup\$
3
\$\begingroup\$

Retina, 5 bytes

!1`#+

Try it online!

An alternative 5-byte solution. Prints # for left and ## for right. The idea is to match all the runs of #s (#+) and print (!) only the first one of them (1).

\$\endgroup\$
3
\$\begingroup\$

Haskell, 21 bytes

f n=snd$span(==' ')n!!1

or in point-free style:

(!!1).snd.span(' '==)

"#" means right, and " " means left

The function just takes a string, drops the beginning spaces, and then, it takes the second character (space if the left is skinny and # if the left is thick)

EDIT: Saved three bytes thanks to Laikoni and nimi!

\$\endgroup\$
  • \$\begingroup\$ Anonymous functions are acceptable too, so (!!2).dropWhile(' '==) suffices as answer. \$\endgroup\$ – Laikoni Mar 29 '17 at 6:14
  • \$\begingroup\$ It's !!1 for the 2nd element. You can shorten the test to <'!'. In the pointfree version, you can replace dropWhile by snd.span. \$\endgroup\$ – nimi Mar 29 '17 at 14:38
2
\$\begingroup\$

Brainfuck, 32 bytes

+[>,>+++++[<------>-]<--[,.>>]<]

Ungolfed:

+[                       while 1
>,>+++++[<------>-]<--     3 if hash; 0 if space
[,.>>]                     print next char and break iff current char is hash
<]

Prints # for right and for left.

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Perl 5, 8 + 1 = 9 bytes

die$F[0]

Try it online!

Run with -a (1 byte penalty).

Output is # at filename line 1, <> line 1 (where filename is the filename of the script) if the left road is less travelled, or ## at filename line 1, <> line 1 if the right road is less travelled.

Explanation

The -a option automatically reads input and splits it into columns around whitespace, ignoring leading whitespace. As such, the first datum of input is what we need; that's $F[0]. It also puts the program in an implicit loop, which we don't want. However, the use of die allows us to output a string, and exit the implicit loop, at the same time (and with no more characters than say, the more usual way to print a string).

\$\endgroup\$
  • \$\begingroup\$ Nice creativity and low byte count! Unfortunately, there is already a 2-byte answer. Are you interested in trying the third challenge in this series? \$\endgroup\$ – programmer5000 Mar 29 '17 at 17:48
  • 2
    \$\begingroup\$ @programmer5000: I don't think there's a 2-byte answer in Perl (and, in fact, this is currently the shortest answer in a non-golfing language). Typically, we aim to find the best solution in each language / via each approach, otherwise there'd be no point in using anything other than a golfing language. \$\endgroup\$ – user62131 Mar 29 '17 at 19:13
2
\$\begingroup\$

C 54 bytes

char*strchr();char c(char*s){return*(strchr(s,35)+1);}

C++ 58 bytes

#include<cstring>
char c(char*s){return*(strchr(s,35)+1);}

Since OP specified it can be a "program/function" I elected to write a function to save characters. However, I still included the "#include" statement and accompanying line break in the character count as they are required to compile the function.

Output

Returns a space " " character to indicate left, or a hash "#" character to indicate right.

Explanation

The strchr() function walks a given string and returns a pointer to the first occurrence of a specified character. It has an overload which accepts an integer as the second argument as opposed to a char which saves me 1 character. E.g. '#' can be replaced with 35. I then add one to the pointer returned from the function to get the character immediately following, and dereference it, then return the resulting char.

Note

I would also like to take this opportunity to formally express my annoyance at Visual Studio auto-formatting my code when I'm trying to golf (╯°□°)╯︵ ┻━┻.

Edit: Thanks to Ray for pointing out some differences in C and C++ and where I could save characters <3.

\$\endgroup\$
  • \$\begingroup\$ C/C++ isn't a language: there are expressions that mean different things in C and C++. For instance, in C++, if a function has an empty parameter list, it means that it takes no arguments. Whereas in C, it means that the parameters are unspecified. So if you decide this is a C program, you can replace #include <string.h>\n with char*strchr(); and save 6 bytes while still being perfectly legal. (And if you prefer C++ for whatever reason, you can replace #include <string.h> with #include <cstring> and save 1 byte.) \$\endgroup\$ – Ray Mar 30 '17 at 21:46
  • \$\begingroup\$ Interesting, I wasn't aware of that. Updated my answer, thanks. \$\endgroup\$ – PragmaticProgrammer Mar 31 '17 at 9:43
1
\$\begingroup\$

JavaScript (ES6), 37 bytes

p=s=>/^ *#( |$)/.test(s.split("\n")[0])

Explanation:

p is a function that returns true if the road less traveled by is on the left and false otherwise. This is my first answer on this site, so it probably could be golfed more (maybe the regex.)

It works by taking the top line of the input and seeing if it matches the regex /^ *#( |$)/ (start of string, any amount of spaces, a #, and a space or end of string.)

This is just to give people clarification about the format and generate ideas. I am sure it can be beat and golfed further. Happy golfing!

p=s=>/^ *#[^#]/.test(s.split("\n")[0])
<textarea rows = "8" cols = "8" oninput = "console.log(p(this.value))"></textarea>

\$\endgroup\$
  • \$\begingroup\$ I think you can have a space instead of [^#] \$\endgroup\$ – Kritixi Lithos Mar 28 '17 at 19:46
  • \$\begingroup\$ Fixed regex issue. \$\endgroup\$ – programmer5000 Mar 28 '17 at 19:48
  • \$\begingroup\$ I made a shorter Javascript answer. \$\endgroup\$ – David Archibald Mar 28 '17 at 21:39
1
\$\begingroup\$

Japt, 3 bytes

x

(2 bytes for -g1 flag) Outputs # for right and a space for left. Based on the JavaScript answer by David Archibald.

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Excel, 17 bytes

=left(trim(A1),2)

Assumes input in cell A1.

Returns ## for right and # (# and space) for left.

\$\endgroup\$
1
\$\begingroup\$

Dyvil, 12 bytes

s=>s.trim[1]

Explanation:

s=>          // lambda expression
   s.trim    // removes leading (and trailing) whitespace
         [1] // gets the second character

Usage:

let f: String -> char = s=>s.trim[1]
print f('...')

Returns (whitespace) for left and # for right.

\$\endgroup\$
1
\$\begingroup\$

Java 7, 166 66 63 52 43 bytes

int c(String s){return s.trim().charAt(1);}

Outputs 35 for right and 32 for left.
Based on @Clashsoft's Dyvil answer.

Explanation:

int c(String s){   // Method with String parameter and integer return-type
  return s.trim()  //  Remove leading and trailing whitspaces
   .charAt(1);     //  and return the second character (as int value)
}                  // End of method

Test code:

class M{
  static int c(String s){return s.trim().charAt(1);}

  public static void main(String[] a){
    System.out.println(c(" ##    #\n  ##  #\n   ###\n    #\n    #\n    #"));
    System.out.println(c(" ##  #   \n  ## #  \n   ###\n    ##\n     #\n     #\n     #"));
    System.out.println(c(" ##  #   \n  ## #  \n   ###\n    ##\n   # \n  #  \n #   "));
    System.out.println(c(" ##   #  \n  ## #  \n   ###\n    #\n   # \n  #  \n  #  "));
    System.out.println(c(" #    ## \n  #  ## \n   ###\n    #\n   # \n  #  \n  #  "));
    System.out.println(c(" #    ## \n  #  ## \n   ###\n    #\n     #\n     #\n     #"));
  }
}

Try it here.

Output:

35
35
35
35
32
32
\$\endgroup\$
0
\$\begingroup\$

Befunge 98, 11 bytes

-!jv~'
@.~<

Try it Online!

Prints 32 for left, and 35 for right, both with a single trailing space.

Explanation

-!jv      This is a no-op the first time through, but used later

    ~'    Pushes the ASCII value of the next character, and pushes 32
-!        Subtracts the 2, and nots. If the character was a space, the top will be 1
  jv      Goes to the second line if the character was not a space

  ~<      Pushes the next characer's ASCII value
 .        Prints it (32 for " " and 35 for "#")
@         Ends the program

One trick I used was putting the -!jv first, even though it didn't do anything. This let me both get rid of the space after the ' and saved some padding. With this last, the code would be

~' -!jv
   @.~<

for 15 bytes.

\$\endgroup\$
0
\$\begingroup\$

Ruby, 20 bytes

->r{r.strip[1]==' '}

Returns true for left, false for right.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.