Two roads diverged in a yellow wood (part 1)

14

3

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!

programmer5000

Posted 2017-03-28T19:40:11.323

Reputation: 7 828

What defines the thickness of a road (I assume it is horizontal crosssection)? Will the roads always be one and two wide? – Post Rock Garf Hunter – 2017-03-28T19:48:27.857

1Yes, always # and ##, and horizontal crosssection. – programmer5000 – 2017-03-28T19:49:34.540

Is the road always a Y shape and never upside down? In that case, isn't it sufficient to only look at the top row? That kinda trivializes the problem. – AdmBorkBork – 2017-03-28T20:00:49.220

Is it valid to replace actual newlines with the characters `\n" in the input? – Okx – 2017-03-28T20:08:01.713

Yes, it is valid. – programmer5000 – 2017-03-28T20:08:56.620

@programmer5000 Don't be strict on the input. Some languages have to take input that way. The challenge should not be about the input format, but rather about the challenge you've described. – mbomb007 – 2017-03-28T20:10:43.953

Why does this question have 2 upvotes and 2 downvotes? – programmer5000 – 2017-03-28T20:43:40.893

1Wasn'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. – ElPedro – 2017-03-28T20:52:00.853

1IBM/Lotus Notes Formula Language and Acc!! responses are proof of how inviting obscure languages to easy questions leads to interesting results. – programmer5000 – 2017-03-28T20:55:24.783

3I was just saying to myself: "You know a challenge is simple when you think, 'Hey, this will be easy in Acc!!'" ;) – DLosc – 2017-03-28T21:00:48.947

22

@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?)

– None – 2017-03-28T22:09:07.433

@programmer5000 is this comment correct? Can I return ' ' or #?

– David Archibald – 2017-03-28T22:23:00.213

Sorry for another question, but I'm new to code golf. is removing the a=> making it no longer a function, and expecting an already defined variable of a to be defined valid? – David Archibald – 2017-03-28T22:51:26.220

1

@DavidArchibald The default policy is that submissions must be a function or a full program. Expecting the input in a predefined variable makes the code a snippet, which isn't allowed unless the OP specifically permits it.

– DLosc – 2017-03-29T03:53:02.820

Thanks, @DLosc. I thought I remembered seeing a challenge with an answer like that. Must've been stated by the OP. – David Archibald – 2017-03-29T04:36:48.697

Will the input string be gruaranteed to begin in a #? It is the case in all of the test cases... – MildlyMilquetoast – 2017-03-29T17:32:05.543

@Mistah No, my test cases aren't great. – programmer5000 – 2017-03-29T17:32:42.163

Answers

16

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!

Dennis

Posted 2017-03-28T19:40:11.323

Reputation: 196 637

4This looks like the source code for Pokemon Go. – Taylor Lopez – 2017-03-29T18:40:13.717

18

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>

David Archibald

Posted 2017-03-28T19:40:11.323

Reputation: 343

3Welcome to the site! Nice explanation and snippet, it's a very thorough first answer. :) – James – 2017-03-28T21:39:26.507

2Thanks. I've been surfing code golf for a while and finally decided to make one. Suppose its StackOverflow rubbing off on me. – David Archibald – 2017-03-28T21:41:12.020

1Wow, totally outgolfed! – programmer5000 – 2017-03-28T21:44:20.790

Your regex was a nice idea, but they're pretty space consuming. I saw that only the first 2 non-whitespace characters mattered and worked off of that. – David Archibald – 2017-03-28T21:45:56.683

1That's a really cool approach. Hadn't thought of that one. – ElPedro – 2017-03-28T21:54:57.097

Could take three bytes off my answer by copying your idea but not going to post it. Enjoy the upvotes :) – ElPedro – 2017-03-28T22:04:00.897

@ElPedro Go ahead. The more the merrier. You could credit me and put your original as a second answer if you want to. – David Archibald – 2017-03-28T22:07:54.310

I think you can leave off =='#' and have the function return or # instead of true or false, right? (Welcome to PPCG, BTW!) – DLosc – 2017-03-28T22:20:02.287

1I'll edit it as soon as @programmer5000 responds. It's a great idea, thanks. – David Archibald – 2017-03-28T22:33:31.827

Yes, that is allowed. – programmer5000 – 2017-03-28T22:34:31.233

10

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.

user48543

Posted 2017-03-28T19:40:11.323

Reputation:

10

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.

DLosc

Posted 2017-03-28T19:40:11.323

Reputation: 21 213

Wow! Yet another obscure language... – programmer5000 – 2017-03-28T20:38:54.930

7@programmer5000 If you want obscure programming languages, you've come to the right site. ;) – DLosc – 2017-03-28T20:40:41.110

Where can I run Acc!!? Is there a GitHub link, Tio, or other? – programmer5000 – 2017-03-28T20:42:41.583

@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. – DLosc – 2017-03-28T20:48:03.767

@programmer5000 Added TIO link to the answer. – DLosc – 2017-03-28T20:58:56.283

8

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

mbomb007

Posted 2017-03-28T19:40:11.323

Reputation: 21 944

1Wow! How does it work? Care editing your answer to explain? – programmer5000 – 2017-03-28T19:58:15.993

1@programmer5000 It just tests the input against the regular expression for a match. – mbomb007 – 2017-03-28T19:59:43.700

6

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.

ElPedro

Posted 2017-03-28T19:40:11.323

Reputation: 5 301

Wow! Quite the obscure language! May be accepted if a better answer does not come soon... – programmer5000 – 2017-03-28T20:18:03.817

1Used to be not so obscure in the good old days when I was a young(er) programmer ;-) – ElPedro – 2017-03-28T20:22:42.310

4

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

DLosc

Posted 2017-03-28T19:40:11.323

Reputation: 21 213

Did you count the command-line argument in the byte count? – programmer5000 – 2017-03-28T20:14:31.810

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

– DLosc – 2017-03-28T20:18:23.163

Oh. I didn't understand. – programmer5000 – 2017-03-28T20:19:02.897

4

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.

Phlarx

Posted 2017-03-28T19:40:11.323

Reputation: 1 366

4

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]

Quentin

Posted 2017-03-28T19:40:11.323

Reputation: 1 187

The loophole was not intended, those test cases were incorrect. But your C answer is impressive! – programmer5000 – 2017-03-29T12:06:32.447

3

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.

Neil

Posted 2017-03-28T19:40:11.323

Reputation: 95 035

3

Python 2, 21 bytes

Try it online

lambda S:S.split()[0]

Output # for left and ## for right

Dead Possum

Posted 2017-03-28T19:40:11.323

Reputation: 3 256

3

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!

Generic Display Name

Posted 2017-03-28T19:40:11.323

Reputation: 365

Anonymous functions are acceptable too, so (!!2).dropWhile(' '==) suffices as answer. – Laikoni – 2017-03-29T06:14:16.430

It's !!1 for the 2nd element. You can shorten the test to <'!'. In the pointfree version, you can replace dropWhile by snd.span. – nimi – 2017-03-29T14:38:41.093

3

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

Martin Ender

Posted 2017-03-28T19:40:11.323

Reputation: 184 808

2

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!

Ray

Posted 2017-03-28T19:40:11.323

Reputation: 1 488

2

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.

PragmaticProgrammer

Posted 2017-03-28T19:40:11.323

Reputation: 91

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.) – Ray – 2017-03-30T21:46:10.317

Interesting, I wasn't aware of that. Updated my answer, thanks. – PragmaticProgrammer – 2017-03-31T09:43:58.310

2

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

user62131

Posted 2017-03-28T19:40:11.323

Reputation:

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?

– programmer5000 – 2017-03-29T17:48:21.520

2@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. – None – 2017-03-29T19:13:26.437

1

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>

programmer5000

Posted 2017-03-28T19:40:11.323

Reputation: 7 828

I think you can have a space instead of [^#] – user41805 – 2017-03-28T19:46:14.977

Fixed regex issue. – programmer5000 – 2017-03-28T19:48:22.617

I made a shorter Javascript answer.

– David Archibald – 2017-03-28T21:39:37.493

1

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!

Tom

Posted 2017-03-28T19:40:11.323

Reputation: 3 078

1

Excel, 17 bytes

=left(trim(A1),2)

Assumes input in cell A1.

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

pajonk

Posted 2017-03-28T19:40:11.323

Reputation: 2 480

1

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

Kevin Cruijssen

Posted 2017-03-28T19:40:11.323

Reputation: 67 575

1

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.

Clashsoft

Posted 2017-03-28T19:40:11.323

Reputation: 835

0

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.

MildlyMilquetoast

Posted 2017-03-28T19:40:11.323

Reputation: 2 907

0

Ruby, 20 bytes

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

Returns true for left, false for right.

dkudriavtsev

Posted 2017-03-28T19:40:11.323

Reputation: 5 781