It is in my humble opinion that standard text is boring. Therefore I propose a new writing standard, walking words!
Walking words
Walking words are words which will respond to certain characters. For the purpose of this challenge the trigger characters are [u, d, r, l] from up down right left.
Whenever you encounter such a character when printing text, you will move the direction of the text.
For example, the text abcdef will result in:
abcd
e
f
Rules
- Both uppercase
UDRLand lowercaseudrlshould change the direction, but case should be preserved in the output - Input will only contain printable characters
(0-9, A-Z, a-z, !@#%^&*() etc...), no newlines! - Whenever the text will collide, it will overwrite the old character at that position
- Output should be presented to the user in any fashionable matter, but it should be a single output (no array of lines)
- Trailing and leading newlines are allowed
- Trailing spaces are allowed
- Standard loopholes apply
Test cases
empty input => empty output or a newline
u =>
u
abc =>
abc
abcd =>
abcd
abcde =>
abcd
e
abcdde =>
abcd
d
e
codegolf and programming puzzles =>
cod
e
g
o
dna fl sel
z
p z
rogramming pu
ABCDELFUGHI =>
I
AHCD
G E
UFL
It is in my humble opinion that standard text is boring. Therefore I propose a new writing standard, walking words! =>
dnats taht noinipo el
a b
rd m
It is in my hu
t
e
x
t
i
s
b
o
ring. Therefore I propose a new writing stand
a
rd
,
w
a
rdw gnikl
s
!
This is code-golf, shortest code in bytes wins!
14 Answers 14
Dyalog APL, 63 bytes
s@(n+11 9∘○しろまる ̈+0円j1*⊃ ̈,⍨\(8∘≠⍴ ̈⊢)0,'rdluRDLU'⍳ ̄1↓s)×ばつn←≢s←⍞
uses ⎕IO←0 and features from v16 (@)
n←≢s←⍞ raw input s and its length n
×ばつn create a 2n by 2n matrix of spaces
s@(...) amend the matrix with the characters of s at the specified (pairs of) indices
how the indices are computed:
̄1↓s drop the last char of s
'rdluRDLU'⍳' encode 'r' as 0, 'd' as 1, etc; other chars as 8
0, prepend a 0
(8∘≠⍴ ̈⊢) turn every 8 into an empty list, all others into a 1-element list
,⍨\ cumulative swapped concatenations (abcd -> a ba cba dcba)
⊃ ̈ first from each
0j1* imaginary constant i to the power of
+\ cumulative sums
11 9∘○しろまる ̈ real and imaginary part from each; get coords in the range -n...n
n+ centre them on the big matrix
Charcoal, (削除) 29 (削除ここまで) (削除) 27 (削除ここまで) (削除) 20 (削除ここまで) 19 bytes
FS«FNordlu↧ι≔ιω✳∨ωrι
Try it online! Link is to verbose version of code. Explanation:
FS«
Loop over the input characters.
FNordlu↧ι
If the current letter is a direction...
≔ιω
then update the current walking direction.
✳∨ωrι
Print the character in the current walking direction, defaulting to right if no direction has been set yet.
-
1\$\begingroup\$ @Veskah The
⌕was supposed to be aNo. Sorry about that. \$\endgroup\$Neil– Neil2019年05月24日 17:14:42 +00:00Commented May 24, 2019 at 17:14
Pyth, (削除) 68 (削除ここまで) 65 bytes
KUJ,00ImXH~+VJ=@as_BM_MBU2Kx"rdlu"rd0dzjcsm.x@Hd;*Fm=Z}hdedSMCHlZ
This uses a dictionary, indexed by a pair of coordinates, that is updated as the input read, then printed at the end. It also uses a ton of clever golfing tricks.
Here's how I wrote it, using the interpreter's -m flag to strip the whitespace and comments before running:
KUJ,00 ; Initialize J to [0, 0] and K to [0, 1].
; J is the current location, K is the current direction.
I ; If the following is truthy, which will be when the input
; is nonempty,
m ; Map d over z, the input.
XH ; Assign to H (a hash table, initially empty)
~+VJ ; At location J, then update J by adding elementwise
=@ ; K (Next variable is implicit), which is set to
as_BM_MBU2K ; [0, 1], bifurcated on mapped negation, then mapped on
; reversal bifuraction with the old value of K appended.
; e.g. [[0, 1], [1, 0], [0, -1], [-1, 0], K]
x"rdlu"rd0 ; indexed at location equal to the index of the lowercase
; of the current character into "rdlu", -1 if missing.
d ; Insert the current character with that key.
z ; map over z.
jc ; Join on newlines the result of chopping into a rectangle
sm ; the concatenation of the map
.x@Hd; ; Lookup the character at the given location,
; if none then ' '
*Fm ; Locations are the cartesian product of the map
=Z}hded ; Inclusive range from the head of each list to
; the end of each list
; Saved in Z for later
SMCH ; Transpose the list of keys, and sort the x and y values
; separately.
lZ ; Width of the rectangle should equal the number of
; x values, which is the length of the last entry.
C#, (削除) 525 (削除ここまで) 474 Bytes
Edit: Saved 51 Bytes thanks to @steenbergh
It's not pretty, but it does work...
Golfed:
string W(string s){var l=s.Length;var a=new char[2*l+1,2*l+1];int x=2*l/2;int y=2*l/2;int d=0;for(int i=0;i<l;i++){switch(char.ToUpper(s[i])){case'U':d=3;break;case'D':d=1;break;case'L':d=2;break;case'R':d=0;break;}a[y,x]=s[i];switch(d){case 0:x+=1;break;case 1:y+=1;break;case 2:x-=1;break;case 3:y-=1;break;}}string o="";for(int i=0;i<2*l+1;i++){string t="";for(int j=0;j<2*l+1;j++)t+=a[i,j]+"";o+=t==string.Join("",Enumerable.Repeat('0円',2*l+1))?"":(t+"\r\n");}return o;}
Ungolfed:
public string W(string s)
{
var l = s.Length;
var a = new char[2 * l + 1, 2 * l + 1];
int x = 2 * l / 2;
int y = 2 * l / 2;
int d = 0;
for (int i = 0; i < l; i++)
{
switch (char.ToUpper(s[i]))
{
case 'U':
d = 3;
break;
case 'D':
d = 1;
break;
case 'L':
d = 2;
break;
case 'R':
d = 0;
break;
}
a[y, x] = s[i];
switch (d)
{
case 0:
x += 1;
break;
case 1:
y += 1;
break;
case 2:
x -= 1;
break;
case 3:
y -= 1;
break;
}
}
string o = "";
for (int i = 0; i < 2 * l + 1; i++)
{
string t = "";
for (int j = 0; j < 2 * l + 1; j++)
t += a[i, j] + "";
o += t == string.Join("", Enumerable.Repeat('0円', 2 * l + 1)) ? "" : (t + "\r\n");
}
return o;
}
Explanation:
Uses a two-dimensional array and the d value to increment the position of the array in the correction direction, where d values are:
0 => RIGHT
1 => DOWN
2 => LEFT
3 => UP
Test:
var walkTheWords = new WalkTheWords();
Console.WriteLine(walkTheWords.W("codegolf and programming puzzles"));
cod
e
g
o
dna fl sel
z
p z
rogramming pu
-
\$\begingroup\$ you can remove the second switch completely.In first switch where you write
d=0;, replace this statement by the statement in second switchcase 0:statement and do similar to other cases and you may not need a second switch.And lastly remove this statementa[y,x]=s[i]and write it on top of first switch. \$\endgroup\$Mukul Kumar– Mukul Kumar2016年11月14日 16:08:03 +00:00Commented Nov 14, 2016 at 16:08 -
\$\begingroup\$ @MukulKumar Nice idea, I can't get it to work. I tried to do it in one switch initially. It's got to stay in it's current dual-switch configuration! :) \$\endgroup\$Pete Arden– Pete Arden2016年11月14日 17:33:23 +00:00Commented Nov 14, 2016 at 17:33
-
\$\begingroup\$ did you write
a[y,x]=s[i]before first switch? \$\endgroup\$Mukul Kumar– Mukul Kumar2016年11月14日 17:56:03 +00:00Commented Nov 14, 2016 at 17:56 -
2\$\begingroup\$ You can use
switch(s[i].toLowerCase())(or what's the c# equivalent...) and then remove all uppercase cases. Should save some bytes. \$\endgroup\$steenbergh– steenbergh2016年11月25日 14:28:24 +00:00Commented Nov 25, 2016 at 14:28 -
1\$\begingroup\$ @steenbergh Thanks, major savings there! No, you can't directly
ToUpper()because it's acharnot astring. The choices are eithers[i].ToString().ToUpper()orchar.ToUpper(s[i])- I think thecharone is slightly shorter. Cheers :) \$\endgroup\$Pete Arden– Pete Arden2016年11月28日 16:20:36 +00:00Commented Nov 28, 2016 at 16:20
JavaScript (ES6), 218 (削除) 220 232 (削除ここまで)
Edit I was using u and t to keep track of the top and leftmost position, but I realized that t is not needed at all
w=>[...w].map(c=>u=(((g[y]=g[y]||[])[x]=c,n=parseInt(c,36)|0)-21&&n-27?a=n-13?n-30?a:!(b=-1):!(b=1):(b=0,a=n/3-8),y+=b,x+=a)<u?x:u,a=1,b=0,x=y=u=w.length,g=[])+g.map(r=>[...r.slice(u)].map(c=>z+=c||' ',z+=`
`),z='')&&z
Less golfed
w=>{
a = 1, b = 0;
x = y = u = w.length;
g = [];
[...w].map(c => (
r = g[y]||[],
r[x] = c,
g[y] = r,
n = parseInt(c,36)|0,
n-21 && n-27 ? n-13 && n-30?0 : (a=0, b=n-13?-1:1) : (b=0, a=n/3-8),
x += a, u = x<u? x : u,
y += b
))
z=''
g.map(r=>[...r.slice(u)].map(c=>z += c||' ', z += '\n'))
return z
}
Test
F=
w=>[...w].map(c=>u=(((g[y]=g[y]||[])[x]=c,n=parseInt(c,36)|0)-21&&n-27?a=n-13?n-30?a:!(b=-1):!(b=1):(b=0,a=n/3-8),y+=b,x+=a)<u?x:u,a=1,b=0,x=y=u=w.length,g=[])+g.map(r=>[...r.slice(u)].map(c=>z+=c||' ',z+=`
`),z='')&&z
function update() {
w=I.value
O.textContent=F(w)
}
update()
#I {width:90%}
<input id=I value='It is in my humble opinion that standard text is boring. Therefore I propose a new writing standard, walking words!' oninput='update()'>
<pre id=O></pre>
05AB1E, (削除) 27 26 25 23 (削除ここまで) 22 bytes
Saved 3 bytes thanks to Grimy
⤋>Šε’uχ’slkDÈiV}Y}Λ
Explanation
ā # push [1 ... len(input)]
¤‹ # check each number if its less than the max
> # increment
# results in a list as long as the input where each number is 2
# apart from the last one, this is the lengths to draw
Š # move 2 copies of the input to the top of the stack
# the first one is the string to draw
ε } # for each char in the second copy
’uχ’slk # get the chars index in "ubridal"
D # duplicate
Èi } # if the index is even
V # store it in Y
Y # push Y (initially 2)
# this gives us the list of directions
Λ # draw everything on the canvas
-
1
-
\$\begingroup\$ @Grimy: I wondered if the dictionary could be used here, but that's really smart! \$\endgroup\$Emigna– Emigna2019年05月24日 11:59:29 +00:00Commented May 24, 2019 at 11:59
-
Javascript, 4̶6̶6̶, (削除) 455 (削除ここまで), 433 Bytes
Edits: 11 Bytes saved, thanks to user 1000000000 10 or so saved, thanks to user2428118 Also removed some unnecessary semi-colons.
I'm pretty sure this can be golfed further, but i couldn't quite manage it. I'm still new whole thing, so any advice is much appreciated :)
z=a=>{b=c=0;j=[[]];d='';a.split``.forEach(g=>{h=g.toLowerCase();if('ruld'.includes(h)){d=h}f=x=>new Array(x[0].length).fill` `;switch(d){case 'l':if(!b){j.forEach(e => e.unshift` `);++b}j[c][b--]=g;break;case 'u':if(!c){j.unshift(f(j));++c}j[c--][b]=g;break;case 'd':if(c == j.length-1){j.push(f(j))}j[c++][b]=g;break;default:if(b==(j[0].length-1)){j.forEach(row=>row.push` `)}j[c][b++] = g;break}});j.forEach(x=>console.log(x.join``))}
<input id="a"> </input>
<input type="submit" onclick="z(document.getElementById('a').value);"/>
Ungolfed:
z=a=>{
b=c=0;
j=[[]];
d='';
a.split``.forEach(g=>{
h=g.toLowerCase();
if('ruld'.includes(h)){d=h;}
f=x=>new Array(x[0].length).fill` `;
switch(d){
case 'l':
if(!b){
j.forEach(e => e.unshift` `);
++b;
}
j[c][b--] = g;
break;
case 'u':
if(!c){
j.unshift(f(j));
++c;
}
j[c--][b] = g;
break;
case 'd':
if(c == j.length-1){
j.push(f(j));
}
j[c++][b] = g;
break;
default:
if(b == (j[0].length-1)){
j.forEach(row=>row.push` `);
}
j[c][b++] = g;
break;
}
});
j.forEach(x => console.log(x.join``));
}
I more or less took the approach of:
- Have an array to store output
- Calculate the x and y position of next character in the array
- If the co-ordinates were about to be outside of the array, extend the array in that direction. Either by pushing and extra space onto the end of that row or adding another row entirely.
- Make array[y][x] = current character
- print the resulting array
-
\$\begingroup\$ Welcome to the site! I'm not an expert in JavaScript but this looks pretty good. \$\endgroup\$2017年01月28日 00:39:20 +00:00Commented Jan 28, 2017 at 0:39
-
\$\begingroup\$ Welcome! You can save 11 bytes by replacing
['r','u','l','d']with"ruld"\$\endgroup\$0 '– 0 '2017年01月28日 01:03:31 +00:00Commented Jan 28, 2017 at 1:03 -
\$\begingroup\$ Also you do not need the
z=at the start of your program \$\endgroup\$0 '– 0 '2017年01月28日 01:09:35 +00:00Commented Jan 28, 2017 at 1:09 -
\$\begingroup\$ Thanks for the tip! JS never ceases to amaze me with it's convenience. \$\endgroup\$Jhal– Jhal2017年01月28日 12:27:54 +00:00Commented Jan 28, 2017 at 12:27
-
\$\begingroup\$ You can use template literals at several places to save some bytes, e.g.
a.split``. \$\endgroup\$user2428118– user24281182017年01月29日 22:36:58 +00:00Commented Jan 29, 2017 at 22:36
Python 3, (削除) 314 (削除ここまで) (削除) 309 (削除ここまで) (削除) 290 (削除ここまで) 268 Bytes
x=y=0
d,m=(1,0),{}
q={'u':(0,-1),'d':(0,1),'l':(-1,0),'r':d}
for c in input():m[x,y]=c;d=q.get(c.lower(),d);x,y=x+d[0],y+d[1]
X,Y=zip(*m)
O,P=min(X),min(Y)
r,p=0,print
exec("t=~~O;exec(\"p(m.get((t,r+P),' '),end='');t+=1;\"*-~abs(max(X)-O));r+=1;p();"*-~abs(max(Y)-P))
I tried running my program as input to my program with some interesting results. Hah, try interpreting that, Python!
Shaved 5 bytes - compliments to Jack Bates.
23 bytes whisked away by kundor
Note: I think there was some error of measurement with my bytes because of using different editors. However, I'm fairly certain the latest one is correct.
-
\$\begingroup\$ You can remove 5 bytes by replacing
'r':(1,0)with'r':dand by removing the space atw[a] for. Also this is insane!!! How long did this take you? \$\endgroup\$user63571– user635712017年01月24日 22:14:48 +00:00Commented Jan 24, 2017 at 22:14 -
\$\begingroup\$ @JackBates A day, in between work. I get a little obsessive. \$\endgroup\$NeRoboto– NeRoboto2017年01月25日 08:51:41 +00:00Commented Jan 25, 2017 at 8:51
-
\$\begingroup\$ Don't we all? That's the whole point of coding! \$\endgroup\$user63571– user635712017年01月25日 15:05:50 +00:00Commented Jan 25, 2017 at 15:05
-
\$\begingroup\$ I believe you can replace that whole
X,Y=map(...)line withX,Y=zip(*m). Works here. (*munpacks it to a list of its keys, and zip regroups them to two tuples.) \$\endgroup\$Nick Matteo– Nick Matteo2017年01月31日 03:10:10 +00:00Commented Jan 31, 2017 at 3:10 -
\$\begingroup\$ You can also put the for loop on one line to save four bytes. \$\endgroup\$Nick Matteo– Nick Matteo2017年01月31日 03:13:11 +00:00Commented Jan 31, 2017 at 3:13
PHP, (削除) 238 223 205 (削除ここまで) 204 bytes
12 bytes saved by Jörg (stripos instead of preg_match), 1 byte +braces by leading instead of trailing newline, 16+braces golfed from the direction change, 1 more with ternary instead of if.
for($m=$d=1;$o=ord($c=$argn[$i++]);$m=min($m,$x),$n=max($n,$x))stripos(_ulrd,$r[$y+=$e][$x+=$d]=$c)?$d=($e=[1,-1][$o&11])?0:$o%4-1:0;ksort($r);foreach($r as$s)for($i=$m-print"\n";$i++<$n;)echo$s[$i]??" ";
Run as pipe with php -nR '<code>' or try it online.
breakdown
for($m=$d=1; # init max index and x-offset to 1
$o=ord($c=$argn[$i++]); # loop through characters
$m=min($m,$x),$n=max($n,$x)) # 3. adjust min and max x offset
stripos(_ulrd,
$r[$y+=$e][$x+=$d]=$c # 1. move cursor; add character to grid
)? # 2. if direction change
$d=(
$e=[1,-1][$o&11] # set y direction
)
?0:$o%4-1 # set x direction
:0;
ksort($r); # sort rows by index
foreach($r as$s) # loop through rows
for($i=$m-print"\n"; # print newline, reset $i
$i++<$n;) # loop $i from min index to max index
echo$s[$i]??" "; # print character, space if empty
-
1\$\begingroup\$ If i see this right
strspn($r[$y+=$e][$x+=$d]=$c,udlruDLR)should save some bytes instead of use the regex, ´stripos(_ulrd,$r[$y+=$e][$x+=$d]=$c)` should e better as strspn$argnsave 3 Bytes \$\endgroup\$Jörg Hülsermann– Jörg Hülsermann2017年05月09日 10:53:33 +00:00Commented May 9, 2017 at 10:53 -
\$\begingroup\$ @JörgHülsermann Are you stalking me? :D You´re right. \$\endgroup\$Titus– Titus2017年05月09日 14:04:14 +00:00Commented May 9, 2017 at 14:04
-
\$\begingroup\$ No someone has edited his post today and I have seen your answer and I saw that you can make it shorter. Sorry about that the improvement is not so great that you can beat the JS answer. It makes happy and proud if I can find some bytes in your answers but I not searching this cases \$\endgroup\$Jörg Hülsermann– Jörg Hülsermann2017年05月09日 14:17:17 +00:00Commented May 9, 2017 at 14:17
-
\$\begingroup\$ @JörgHülsermann Don´t worry; I found another 21 bytes in addition to your 12. Thanks for making me revisit this. \$\endgroup\$Titus– Titus2017年05月09日 14:25:13 +00:00Commented May 9, 2017 at 14:25
-
\$\begingroup\$ Over 10 percent it is nice \$\endgroup\$Jörg Hülsermann– Jörg Hülsermann2017年05月09日 14:37:56 +00:00Commented May 9, 2017 at 14:37
Java 10, (削除) 288 (削除ここまで) (削除) 286 (削除ここまで) (削除) 280 (削除ここまで) 263 bytes
s->{int l=s.length(),x=l,y=l,d=82,A=x,B=y;var r=new char[l+=l][l];for(var c:s.toCharArray()){A=x<A?x:A;B=y<B?y:B;r[x][y]=c;c&=~32;d="DLRU".contains(""+c)?c:d;x+=5-d/14;y+=3-(d^16)/23;}s="";for(x=A;x<l;x++,s+="\n")for(y=B;y<l;y++)s+=r[x][y]<1?32:r[x][y];return s;}
-17 bytes thanks to @Grimy.
Explanation:
Try it here. (NOTE: I remove all trailing spaces/newlines to make the output a bit more compact. Feel free to remove the .replaceAll("(m?)\\s+$","") in the test-method to see the actual result.)
s->{ // Method with String as both parameter and return-type
int l=s.length(), // Length of input String
x=l,y=l, // x,y coordinates, starting at `l`,`l`
d=82, // Direction, starting towards the right
A=x,B=y; // Min x & y values to remove leading spaces at the end
var r=new char[l+=l][l]; // character-matrix, with size `l`+`l` by `l`+`l`
for(var c:s.toCharArray()){ // Loop over the characters of the input String:
A=x<A?x:A; // Adjust minimum x `A` if necessary
B=y<B?y:B; // Adjust minimum y `B` if necessary
r[x][y]=c; // Fill x,y with the current character
c&=~32; // Make character uppercase if it isn't yet
d="DLRU".contains(""+c)?c:d; // Change the direction if necessary
x+=5-d/14; // Set the next x coordinate based on the direction
y+=3-(d^16)/23;} // Set the next y coordinate based on the direction
s=""; // After the loop: create an empty result-String
for(x=A;x<l;x++, // Loop `x` in the range [`A`, `l`):
s+="\n") // And append a new-line after every iteration
for(y=B;y<l;y++) // Inner loop `y` in the range [`B`, `l`):
s+=r[x][y]<1? // If the cell at x,y is empty:
32 // Append a space to the result-String
:r[x][y]; // Else: append the character to the result-String
return s;} // After the nested loop: teturn result-String
-
1\$\begingroup\$
d<69?1:d>84?-1:0can be5-d/14\$\endgroup\$Grimmy– Grimmy2019年05月24日 16:15:42 +00:00Commented May 24, 2019 at 16:15 -
1\$\begingroup\$ And in the same vein,
d==82?1:d==76?-1:0can be3-(d^16)/23\$\endgroup\$Grimmy– Grimmy2019年05月24日 16:22:45 +00:00Commented May 24, 2019 at 16:22 -
\$\begingroup\$ @Grimy Thanks. I knew those two parts could somehow be golfed, but I'm pretty bad at those bitwise/arithmetic transformations, so I didn't bother trying. Thanks for the -17 bytes! :) \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2019年05月24日 16:39:36 +00:00Commented May 24, 2019 at 16:39
Perl, 204 + 3 = 207 bytes
+3 for -F
Whitespace is not part of the code and is provided for legibility.
%p=(d,1,l,2,u,3,r,$x=$y=0);
for(@F){
$m{"$x,$y"}=$_;
$g=$p{lc$_}if/[dlur]/i;
$g%2?($y+=2-$g):($x+=1-$g);
($a>$x?$a:$b<$x?$b:$x)=$x;
($c>$y?$c:$d<$y?$d:$y)=$y
}
for$k($c..$d){
print($m{"$_,$k"}||$")for$a..$b;
say""
}
Similar to my solution to the Fizz Buzz challenge, I create a hash with x,y coordinates for every step along the way, keeping the maximums and minimums of the x- and y- coordinates stored, then loop through and print everything out.
If I'm desperate I might be able to turn the last three lines of the first for loop into a single disgusting statement that may save a byte or two, but I'm not looking forward to the completely unreadable result.
Excel VBA, 205 bytes
Sub t(a)
Z=1:x=70:y=x:For i=1 To Len(a)
s=Mid(a,i,1):Cells(y,x).Value=s:Select Case LCase(s)
Case "l":Z=-1:w=0
Case "d":Z=0:w=1
Case "r":Z=1:w=0
Case "u":Z=0:w=-1
End Select:x=x+Z:y=y+w:Next:End Sub
I'm kinda surprised at Excel's ability to compete with existing answers. It works because w and z keep track of the direction.
-
\$\begingroup\$ starting position 70 could be not enough. Moreover, leading spaces are not allowed \$\endgroup\$edc65– edc652017年01月28日 14:00:44 +00:00Commented Jan 28, 2017 at 14:00
SmileBASIC, (削除) 148 (削除ここまで) 146 bytes
DEF W M,S,T
WHILE""<M
A=INSTR(@DLURdlur,M[0])*PI()/2IF A>0THEN S=COS(A)T=SIN(A)
X=CSRX+S
Y=CSRY+T?SHIFT(M);
SCROLL-!X,-!Y
LOCATE!X+X,Y+!Y
WEND
END
Call the function with W "text",vx,vy, where vx and vy is the direction at the start (default is 1,0)
-
\$\begingroup\$ What happens when X or Y is less than 0? \$\endgroup\$edc65– edc652017年01月28日 13:59:55 +00:00Commented Jan 28, 2017 at 13:59
-
\$\begingroup\$ Now it will scroll all the text when the cursor goes offscreen. \$\endgroup\$12Me21– 12Me212017年01月28日 16:14:36 +00:00Commented Jan 28, 2017 at 16:14
Swift 3, 283 bytes
func w(a:String){var t=a.characters,d=t.count,c=Array(repeating:Array(repeating:" ",count:d*2),count:d*2),i=d,j=d,l=["d":(1,0),"u":(-1,0),"l":(0,-1),"r":(0,1)],e=(0,1)
t.map{c[i][j]="\(0ドル)"
e=l["\(0ドル)".lowercased()] ?? e
i+=e.0
j+=e.1}
c.map{0ドル.map{print(0,ドルterminator:"")};print()}}
Ungolfed
func w(a:String){
var t=a.characters,d=t.count,c=Array(repeating:Array(repeating:" ",count:d*2),count:d*2),i=d,j=d,l=["d":(1,0),"u":(-1,0),"l":(0,-1),"r":(0,1)],e=(0,1)
t.map{
c[i][j]="\(0ドル)"
e=l["\(0ドル)".lowercased()] ?? e
i+=e.0
j+=e.1
}
c.map{
0ドル.map{
print(0,ドルterminator:"")
};
print()
}
}
Warning
Longer input will requires bigger screen. Output has no treatment for "empty" row/columns as I understood that is acceptable by the rules of the challenge.
Rant
- Newline being the default terminator for
printsux - No simple way of creating an array of known length destroyed the score.
golflook by itself? \$\endgroup\$gfl\$\endgroup\$