21
\$\begingroup\$

A friend of yours has given you directions to the best restaurant in town. It's a series of left and right turns. Unfortunately, they forgot to mention for how long you need to go straight ahead between those turns. Luckily you have a street map with all the restaurants on it. Maybe you can figure out which restaurant they meant?

Input

The map is given as a rectangular grid of ASCII characters. . is a road, # is a building, A to Z are the various restaurants. You start in the top left corner, going east. Example:

.....A
.#.###
B....C
##.#.#
D....E
##F###

Your friend's instructions will be given as a (potentially empty) string or list of characters containing Ls and Rs.

Output

You can walk any path that corresponds to the left and right turns in the input string, provided that you take at least one step forward before each of them, as well as at the end. In particular this means if the string starts with R you cannot go south immediately in the left-most column. It also means you can't turn around 180° on the spot.

You cannot walk through buildings or restaurants except the one you reach at the end. You may assume that the top left corner is a ..

You should output all the restaurants that can be reached with your friend's instructions, as a string or list.

You may assume that the instructions will lead to at least one restaurant. E.g. a single L would be invalid for the above map.

Some examples for the above map:

<empty> A
R       F
RR      B,D
RL      C,E
RLRL    E
RLLR    C
RLLL    B
RLRR    D
RLRRRR  A,C
RLLLRLL B

Note in particular that R doesn't reach B.

You may write a program or function, taking input via STDIN (or closest alternative), command-line argument or function argument and outputting the result via STDOUT (or closest alternative), function return value or function (out) parameter.

Standard rules apply.

Additional Test Cases

Here is a larger map, courtesy of Conor O'Brien (which I modified a bit):

.......Y..........................######
.####.....#.##....##..######....#.###.##
B.........#.##.#..##....##...##.#.#P...#
.#.#####..#.##..#.##....##.#....#.####.#
.#.#...C..#.##...G##..#.##.#....#.#....#
.#.#.#.#..#.####.###.#..##.#....#.#.NO.#
.#.#A#.#..#.##...F###...##.#.##.#......#
.#.###....#.##....##....##.#....###....#
.#.....##...##....##...D##........###R.#
.#.##..##...##E...##..######....####...#
.....X....#.#.....................##S.T#
###########.###########M############...#
#................................###.#.#
#.#########.########.######.#.######.#.#
#......V#.....######.IJ...........##.#.#
#########.###......ZH############L##.#.#
#########.##########.###############.#.#
####K##...##########.#....#..........#.#
####....########U......##...#######Q.#.#
#####################################W.#

And here are a few selected lists of directions and their expected results:

<empty>                                 Y
RR                                      B
RLL                                     Y
RLRR                                    B,C,X
RLLLRRR                                 G
RLRLRLRL                                I,Z
RLLRRRLRRLRR                            C,D,F,G,Y
RLRRLLRLLLRL                            B,C,Y
RLLRRLRRRLLLL                           F,M,N,O,Y
RLRRLLLRRRRLLLL                         F,M,Y
RLRRLRRRRRRRRRR                         E,F,Y
RLRRRLLLRLLRRLL                         M,N,O
RLLRRLRRLRLRLRRLLR                      E,U
RLRLLRLRRLRRRRRLRL                      F,G,I,Z
RLLRRLLRLLRRRLRRLLRR                    W
RLLLRRRLRRLLLLLRLLLLLL                  D,G,X
RLRLLRLRRLRLRRRLRLLLRR                  B,C,E,J,X
RLRLRLLLLRLRRRRRRLRLRRLR                Y
RLRLRRRLRLLLLRLRRLLLLRLLRRL             E,M,X
RLRLLLRRRLLLRLLRLLRLRRLRLRR             B,E,F,K
RLRRRLLLLLLLLLLLLLLLRRRRLLL             A,B

Bonus question: is there an input that results in only I or only U? If so, what's the shortest such path?

\$\endgroup\$
17
\$\begingroup\$

Perl, 150 149 146 145 141 140 138 136 135 133 130 126 125 124

Added +7 for -F -Xn0i

An initial attempt.

Run with the map on STDIN and the directions after the -i option, e.g.

perl -F -Xn0iRL incomplete.pl
.....A
.#.###
B....C
##.#.#
D....E
##F###

Close STDIN with ^D or ^Z or whatever works on your operating system.

incomplete.pl:

%P=0;$^I=~s``{%;=!/
/;%P=map{$_|=$F[$^H=$_+=(1,@+,-1,"-@+")[$d&3]]=~/(\w)|#|^$/*~!\$;{$1}}(%P)x@F}$d-=B&$'^u`eg;print%

Replace the ^H by the literal control character to get the given score

Bonus question:

  • There is no input that results in only I
  • The shortest input that results in only U is RLLRRLLRLRLRRLRRLRLRLRRLLR
  • The longest input needed to result in a unique set is RLLRRRLRLRLLLRRLRLLLLLRRRLLRRRLLLLLLLRRLRRRR which gives B O R
\$\endgroup\$
  • 4
    \$\begingroup\$ The Ton Hospel? :) \$\endgroup\$ – Lynn Feb 24 '16 at 21:03
  • 14
    \$\begingroup\$ There is only one alien with that name \$\endgroup\$ – Ton Hospel Feb 24 '16 at 21:15
  • 2
    \$\begingroup\$ @TonHospel It's a honor to have you here. \$\endgroup\$ – msh210 Feb 24 '16 at 21:30
8
\$\begingroup\$

Python 2, 180 177 168 163 161 158 bytes

def a(v,o,c=0,A=0,d='.',O={0}):
 while'.'==d:w=v.find('\n');c+=[1,~w,-1,w+1][A%4];d=v[c];o>v<a(v+' '*w,o[1:],c,ord(o[0])-~A,d);d>v>o<O.add(d)
 return`O`[9::5]

Parameter v is the map as a string; o is the LR string.

Mitch Schwartz saved 2 3 10 lots of bytes. Thanks!

I saved two bytes by setting O={0} and returning `O`[9::5], which might not be very portable: it assumes that hash(0) == 0, I think, because that causes the order of elements in repr(O) to be

set([0, 'A', 'B', 'C'])

and creatively slicing that string gets me the answer.

\$\endgroup\$
  • \$\begingroup\$ I think this suffers from an exponential explosion if you run it on a big almost empty grid with longish turn strings \$\endgroup\$ – Ton Hospel Feb 25 '16 at 0:57
  • \$\begingroup\$ Oh, yes, it's an absolute performance disaster. It works for the example grids, though! \$\endgroup\$ – Lynn Feb 25 '16 at 1:04
1
\$\begingroup\$

C++ 465

C++ is so verbose...

#include <vector>
#include <iostream>
using namespace std;
#define M m[y][x]
#define A if(M!=46)break
vector<string>m;char n[99];int r(int x,int y,int z,const char *d){for(;;){if(z%2)y=y-2+z;else x=x+1-z;if(y<0||y>=m.size()||x<0||x>=m[y].size())break;if(*d){A;r(x,y,(*d==82?z+3:*d==76?z+1:z)%4,d+1);}else{if(M>64&&M<91)n[M]++;A;}}}int main(int c,char**v){string l;while(getline(cin,l))m.push_back(l);r(0,0,0,c>1?v[1]:"");for(char j=0;j<99;j++)if(n[j])cout<<j<<" ";}

I will try to shorten it further. Suggestions are welcome.

\$\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.