Unvisualize Parsons code
CJam, 21 bytes
qN/:.e>(o2%:i"DRXU"f=
Fold the lines (:
) by vectorizing (.
) a character-wise maximum operation e>
. Since there is only one non-space character in each column, this one will be the result, as space has a smaller ASCII code than all printable non-space characters.
Unshift and print the first asterisk (o
, then map every other (2%
) remaining char to UDR
using modular indexing.
Old solution (29 bytes)
'*qN/z2%'*f#0+2ew);::-"RDU"f=
qN/
gets input lines. z
transposes this character matrix. 2%
drops every odd row. '*f#
finds the index of the asterisk in each row. 0+2ew);
gets all successive pairs of indices. ::-
computes their differences, and "RDU"f=
maps them to letters (via modular indexing: 0 → R
, 2 → U
, -2 ≡ 1 → D
). The leading '*
prepends the asterisk.
EDIT: I changed 2ew
to 0+2ew);
to work around CJam not handling ew
(successive slices) on lists that are too short. This makes the code work for the input string *
.
Try it here, or watch it in action:
Pyth - 28 25 27 25 bytes
2 byes saved thanks to @Jakube.
s+\*@L"RDU"-VFtBxR\*%2C.z
Try it online here.
Python 3, 129 108 98 86 bytes
There are probably several ways to golf this, but I rather like that I got it all down to one line.
Edit: Now using ''.translate()
Edit: With many thanks to wnnmaw.
Edit: I changed the input format to an array of strings instead of a newline-separated string to save bytes. Also, in the last edit, I mixed up U
and R
, so I fixed that.
lambda a:'*'+"".join(('UR'[j<'/']+'D')[j>'/']for l in zip(*a)for j in l if j in'-/\\')
Input must be an array of strings. For the example above, this looks something like:
[" *-* "," / \ "," * * "," / \ ","*-* * *-* "," \ / \ "," * * *-*"," \ / "," *-* "]
Ungolfed:
def f(a):
s = ''
for c in zip(*a): # transpose
for d in c: # for each letter in column c
if e in "-/\\": # if that letter is either -,/,\
if e < '/': # if < '/' (same as if == '-')
s += "R"
elif e > '/': # if > '/' (same as if == '\')
s += "D"
else: # if == '/'
s += "U"
return "*" + s # in the code we ''.join() it all together
# in this ungolfing, we add to an empty string