Revision f5e85ec3-a040-4665-89c6-c4f8b60d2bf1 - Code Golf Stack Exchange

# C#, <strike>525</strike> 474 Bytes

<I>Edit: Saved 51 Bytes thanks to @steenbergh</I>

<I>It's not pretty, but it does work...</I>

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 

AltStyle によって変換されたページ (->オリジナル) /