Snakes look like this:

@     ^  v
^  >>>^  v
^        v

The snake can cross over itself as in this case:


For a crossover to be valid, the characters on either side must be moving the same direction. The case of


can be considered unclear and invalid.

The output is a string of WASD representing going from the head to the tail (@).

Given a snake that doesn't backtrack and isn't ambiguous, can you write a program that will output the string of moves that the snake takes?

This is code-golf, so shortest answer wins!

Test cases:

(Note: The @ can be replaced with any character not in v^<>)


  v<<  @
  v    ^

Output: ddddssaassdddddww


^  v
^  v

Output: dddsssaaawww


   v       @
   v       ^
   >>>>v   ^

Output: dddsssddddsddddwww


@<< v
  ^ v
v ^

Output: ssaaaassddwwwwaa



Output: ssawwasssawww

  • 1
    \$\begingroup\$ Does the input have to be a single String or can we take a String[]? Is each line of the input padded to be the same length or do we have to deal with irregular line length? \$\endgroup\$ – CAD97 Mar 18 '16 at 18:04
  • 2
    \$\begingroup\$ This is giving me horrible flash backs to the path of an Ant on a rubiks cube question. \$\endgroup\$ – Matt Mar 18 '16 at 19:08
  • \$\begingroup\$ Will the beginning segment always be on line 0, char 0, or will we have to find it? \$\endgroup\$ – MayorMonty Mar 18 '16 at 20:30
  • 1
    \$\begingroup\$ @SpeedyNinja test cases 2 and 4 both have starts not at (0,0), and test case 0 (snakes look like) doesn't start OR end at (0,0). \$\endgroup\$ – CAD97 Mar 18 '16 at 20:31
  • 1
    \$\begingroup\$ @CAD97 Oh, that's devlish ;) \$\endgroup\$ – MayorMonty Mar 18 '16 at 20:32

Java, 626 539 536 529 bytes

-87 bytes by saving a few in a lot of places. Thanks goes to Mr Public for pointing some out.

-3 bytes because I can't manage to remove all the spaces first try (thanks mbomb007)

+8 bytes to fix for this case:


-15 bytes by front-loading variable declaration

s->{String o="",t;String[]p=s.split("\n");int h=p.length,w=p[0].length(),y=0,x,b=0,a,n,m;char[][]d=new char[h][w];for(;y<h;y++)for(x=0;x<w;x++){d[y][x]=p[y].charAt(x);if(d[y][x]=='@')d[y][x]=' ';}for(;b<h;b++)for(a=0;a<w;a++){t="";x=a;y=b;n=0;m=0;while(!(y<0|y>h|x<0|x>w||d[y][x]==' ')){if(y+m>=0&y+m<h&x+n>=0&x+n<w&&d[y+m][x+n]==d[y-m][x-n])d[y][x]=d[y-m][x-n];n=m=0;switch(d[y][x]){case'^':t+="W";m--;break;case'<':t+="A";n--;break;case'v':t+="S";m++;break;case'>':t+="D";n++;}x+=n;y+=m;}o=t.length()>o.length()?t:o;}return o;}

Readable version:

static Function<String,String> parser = snake -> {
    // declare all variables in one place to minimize declaration overhead
    String output = "", path;
    String[] split = snake.split("\n");
    int h=split.length, w=split[0].length(), y=0, x, startY=0, startX, dx, dy;
    char[][] board = new char[h][w];
    // setup char[][] board
    for (; y<h; y++)
        for (x=0; x<w; x++) {
            if(board[y][x]=='@')board[y][x]=' ';
    // find the longest possible path
    for (; startY<h; startY++)
        for (startX=0; startX<w; startX++) {
            path = "";
            x=startX; y=startY; dx=0; dy=0;
            while (!(y<0 | y>h | x<0 | x>w || board[y][x] == ' ')) {
                if (y + dy >= 0 & y + dy < h & x + dx >= 0 & x + dx < w
                        && board[y + dy][x + dx] == board[y - dy][x - dx]) {
                    board[y][x] = board[y - dy][x - dx];
                } dx = dy = 0;
                switch(board[y][x]) {
                    case '^':path+="W";dy--;break;
                    case '<':path+="A";dx--;break;
                    case 'v':path+="S";dy++;break;
                    case '>':path+="D";dx++;break;
                x+=dx; y+=dy;
            output = path.length()>output.length()?path:output;
    return output;

Takes a String like v @\n>>>^. Creates a path starting at each coordinate, then returns the longest one. The lookahead required for the overlapping paths was the hardest part.

|improve this answer|||||
  • 2
    \$\begingroup\$ I am very impressed. I didn't expect anyone to even attempt this. And you're the first one. +1. (2016 bytes is okay, and even better for 2016 :D) \$\endgroup\$ – user51533 Mar 18 '16 at 20:29
  • \$\begingroup\$ Strip all the spaces, names, etc then I'll +1. I'm not voting up until it's golfed properly. \$\endgroup\$ – mbomb007 Mar 18 '16 at 20:52
  • 2
    \$\begingroup\$ Or, have two code snippets, one of the fully golfed version, one of the working readable example. \$\endgroup\$ – Mr Public Mar 18 '16 at 20:53
  • \$\begingroup\$ @mbomb007 I finished the logic golfing so here's the properly golfed version! \$\endgroup\$ – CAD97 Mar 18 '16 at 21:09
  • 2
    \$\begingroup\$ @CAD97 For this challenge, I would say this is an excellent golf in Java. \$\endgroup\$ – Mr Public Mar 18 '16 at 21:52

Ruby, 217

z=a.index ?@
e,n=[a[z,c+=1][?\n]?p: c,d=c*a[/.*
/].size,a[z-c,c][?\n]?p: -c,-d].zip(b.chars).reject{|i,k|!i||a[v=i+z]!=k||0>v}.max_by{|q|q&[a[z]]}until n

This starts at the @ and walks backwards, looking for neighbors that point to the current position (z). In order to choose the right way at 4-way intersections, it favors neighbors pointing in the same direction (max_by{...}). If no immediate neighbors are found, it assumes that there must have been a cross-over and reaches out one level at a time until it finds one (until n and c+=1). This process repeats for the number of body segments (not including the head) (.scan(/\w/){...}).

The test case I added to the puzzle kept tripping me up, so I went form 182 char to 218. Those extra characters were all making sure my horizontal moves didn't go into the next/prev lines. I wonder if I can deal with that in a better way.


  position=a.index ?@ # start at the @
  a.tr!('<^>v',b='awds') # translate arrows to letters
  a.scan(/\w/){           # for each letter found...
    until distance
      neighbors = [
        a[position,search_distance][?\n]?p: search_distance,  # look right by search_distance unless there's a newline
        width=search_distance*a[/.*\n/].size,   # look down (+width)
        a[position-search_distance,search_distance][?\n]?p: -search_distance, # look left unless there's a newline
        -width                  # look up (-width)
      distance,letter = neighbors.zip(b.chars).reject{ |distance, letter_to_find|
        !distance || # eliminate nulls
         a[new_position=distance+position]!=letter_to_find || # only look for the letter that "points" at me
         0>new_position # and make sure we're not going into negative indices
       }.max_by{ |q| 
        # if there are two valid neighbors, we're at a 4-way intersection
        # this will make sure we prefer the neighbor who points in the same 
        # direction we're pointing in.  E.g., when position is in the middle of 
        # the below, the non-rejected array includes both the top and left.
        #   v
        #  >>>
        #   v
        # We want to prefer left.
        q & [a[position]] 
        # ['>',x] & ['>'] == ['>']
        # ['v',x] & ['>'] == []
        # ['>'] > [], so we select '>'.
    result=(letter*search_distance)+result # prepend result
  result # if anyone has a better way of returning result, I'm all ears
|improve this answer|||||
  • \$\begingroup\$ You should be able to simplify your logic somewhat as your added case has been deemed out of the intended scope. \$\endgroup\$ – CAD97 Mar 20 '16 at 19:58

Your Answer

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