Idź słowami!


33

Moim skromnym zdaniem standardowy tekst jest nudny. Dlatego proponuję nowy standard pisania, chodzące słowa!

Chodzące słowa

Chodzące słowa to słowa, które będą reagować na określone znaki. Dla celów tego wyzwania znaki spustowe są [u, d, r, l]odup down right left .

Ilekroć napotkasz taki znak podczas drukowania tekstu, zmienisz kierunek tekstu.
Na przykład tekst abcdefspowoduje:

abcd
   e
   f

Zasady

  • Obie wielkie litery UDRL i małe litery udrlpowinny zmieniać kierunek, ale wielkość powinna być zachowana na wydruku
  • Dane wejściowe będą zawierać tylko znaki do wydrukowania (0-9, A-Z, a-z, !@#%^&*() etc...) , bez znaków nowej linii!
  • Ilekroć tekst się zderzy, nadpisze stary znak w tej pozycji
  • Wyjście powinno być prezentowane użytkownikowi w każdej modnej sprawie, ale powinno być pojedynczym wyjściem (bez tablicy wierszy)
  • Dozwolone są końcowe i wiodące znaki nowej linii
  • Końcowe spacje są dozwolone
  • Obowiązują standardowe luki

Przypadki testowe

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
                                       !

To jest , wygrywa najkrótszy kod w bajtach!


4
Ten format ma poważny problem z utratą danych ...: p
Mukul Kumar

Czy dozwolone są spacje wiodące / końcowe?
Arnauld

@Arnauld Tak długo, jak długo nie zakłócają pozycji postaci, którymi są. Więc najprawdopodobniej tylko końcowe spacje
Bassdrop Cumberwubwubwub

1
How would the output of the word golf look by itself?
gabe3886

2
@gabe3886 gfl
Bassdrop Cumberwubwubwub

Odpowiedzi:


4

Charcoal, 29 27 20 19 bytes

FS«F№rdlu↧ι≔ιω✳∨ωrι

Try it online! Link is to verbose version of code. Explanation:

FS«

Loop over the input characters.

F№rdlu↧ι

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
@Veskah The was supposed to be a . Sorry about that.
Neil

7

Dyalog APL, 63 bytes

s@(n+11 9∘○¨+\0j1*⊃¨,⍨\(8∘≠⍴¨⊢)0,'rdluRDLU'⍳¯1↓s)⍴∘'',⍨2×n←≢s←⍞

uses ⎕IO←0 and features from v16 (@)

n←≢s←⍞ raw input s and its length n

⍴∘'',⍨2×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


3

Pyth, 68 65 bytes

KUJ,00ImXH~+VJ=@as_BM_MBU2Kx"rdlu"rd0dzjcsm.x@Hd;*Fm=Z}hdedSMCHlZ

Test suite

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.

3

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  

you can remove the second switch completely.In first switch where you write d=0;, replace this statement by the statement in second switch case 0: statement and do similar to other cases and you may not need a second switch.And lastly remove this statement a[y,x]=s[i] and write it on top of first switch.
Mukul Kumar

@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! :)
Pete Arden

did you write a[y,x]=s[i] before first switch?
Mukul Kumar

2
You can use switch(s[i].toLowerCase()) (or what's the c# equivalent...) and then remove all uppercase cases. Should save some bytes.
steenbergh

1
@steenbergh Thanks, major savings there! No, you can't directly ToUpper() because it's a char not a string. The choices are either s[i].ToString().ToUpper() or char.ToUpper(s[i]) - I think the char one is slightly shorter. Cheers :)
Pete Arden

3

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>


3

05AB1E, 27 26 25 23 22 bytes

Saved 3 bytes thanks to Grimy

ā¤‹>Šε’uχ’slkDÈiV}Y}Λ

Try it online!

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
"bridal" is a dictionary word. -1. EDIT: make that -2.
Grimmy

@Grimy: I wondered if the dictionary could be used here, but that's really smart!
Emigna

22
Grimmy

2

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

Welcome to the site! I'm not an expert in JavaScript but this looks pretty good.
Wheat Wizard

Welcome! You can save 11 bytes by replacing ['r','u','l','d'] with "ruld"
0 '

Also you do not need the z= at the start of your program
0 '

Thanks for the tip! JS never ceases to amaze me with it's convenience.
Jhal

You can use template literals at several places to save some bytes, e.g. a.split`` .
user2428118

2

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.


You can remove 5 bytes by replacing 'r':(1,0) with 'r':d and by removing the space at w[a] for. Also this is insane!!! How long did this take you?

@JackBates A day, in between work. I get a little obsessive.
Lord Ratte

Don't we all? That's the whole point of coding!

I believe you can replace that whole X,Y=map(...) line with X,Y=zip(*m). Works here. (*m unpacks it to a list of its keys, and zip regroups them to two tuples.)
Nick Matteo

You can also put the for loop on one line to save four bytes.
Nick Matteo

2

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
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 $argn save 3 Bytes
Jörg Hülsermann

@JörgHülsermann Are you stalking me? :D You´re right.
Titus

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
Jörg Hülsermann

@JörgHülsermann Don´t worry; I found another 21 bytes in addition to your 12. Thanks for making me revisit this.
Titus

Over 10 percent it is nice
Jörg Hülsermann

2

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
d<69?1:d>84?-1:0 can be 5-d/14
Grimmy

1
And in the same vein, d==82?1:d==76?-1:0 can be 3-(d^16)/23
Grimmy

@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! :)
Kevin Cruijssen

1

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.


1

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.


starting position 70 could be not enough. Moreover, leading spaces are not allowed
edc65

1

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)


What happens when X or Y is less than 0?
edc65

Now it will scroll all the text when the cursor goes offscreen.
12Me21

0

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 print sux
  • No simple way of creating an array of known length destroyed the score.
Korzystając z naszej strony potwierdzasz, że przeczytałeś(-aś) i rozumiesz nasze zasady używania plików cookie i zasady ochrony prywatności.
Licensed under cc by-sa 3.0 with attribution required.