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 abcdef
spowoduje:
abcd
e
f
Zasady
- Obie wielkie litery
UDRL
i małe literyudrl
powinny 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 code-golf , wygrywa najkrótszy kod w bajtach!
code-golf
grid
printable-ascii
Bassdrop Cumberwubwubwub
źródło
źródło
golf
look by itself?gfl
Odpowiedzi:
Charcoal,
29272019 bytesTry it online! Link is to verbose version of code. Explanation:
Loop over the input characters.
If the current letter is a direction...
then update the current walking direction.
Print the character in the current walking direction, defaulting to right if no direction has been set yet.
źródło
⌕
was supposed to be a№
. Sorry about that.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 inputs
and its lengthn
⍴∘'',⍨2×n
create a 2n by 2n matrix of spacess@(...)
amend the matrix with the characters ofs
at the specified (pairs of) indiceshow the indices are computed:
¯1↓s
drop the last char ofs
'rdluRDLU'⍳'
encode'r'
as 0,'d'
as 1, etc; other chars as 80,
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 each0j1*
imaginary constant i to the power of+\
cumulative sums11 9∘○¨
real and imaginary part from each; get coords in the range-n
...n
n+
centre them on the big matrixźródło
Pyth,
6865 bytesTest 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:źródło
C#,
525474 BytesEdit: Saved 51 Bytes thanks to @steenbergh
It's not pretty, but it does work...
Golfed:
Ungolfed:
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:Test:
źródło
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.a[y,x]=s[i]
before first switch?switch(s[i].toLowerCase())
(or what's the c# equivalent...) and then remove all uppercase cases. Should save some bytes.ToUpper()
because it's achar
not astring
. The choices are eithers[i].ToString().ToUpper()
orchar.ToUpper(s[i])
- I think thechar
one is slightly shorter. Cheers :)JavaScript (ES6), 218
220 232Edit I was using
u
andt
to keep track of the top and leftmost position, but I realized thatt
is not needed at allLess golfed
Test
źródło
05AB1E,
27 26 25 2322 bytesSaved 3 bytes thanks to Grimy
Try it online!
Explanation
źródło
Javascript, 4̶6̶6̶,
455, 433 BytesEdits: 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 :)
Ungolfed:
}
I more or less took the approach of:
źródło
['r','u','l','d']
with"ruld"
z=
at the start of your programa.split``
.Python 3,
314309290268 BytesI 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.
źródło
'r':(1,0)
with'r':d
and by removing the space atw[a] for
. Also this is insane!!! How long did this take you?X,Y=map(...)
line withX,Y=zip(*m)
. Works here. (*m
unpacks it to a list of its keys, and zip regroups them to two tuples.)PHP,
238 223 205204 bytes12 bytes saved by Jörg (
stripos
instead ofpreg_match
), 1 byte +braces by leading instead of trailing newline, 16+braces golfed from the direction change, 1 more with ternary instead ofif
.Run as pipe with
php -nR '<code>'
or try it online.breakdown
źródło
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 BytesJava 10,
288286280263 bytes-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 thetest
-method to see the actual result.)źródło
d<69?1:d>84?-1:0
can be5-d/14
d==82?1:d==76?-1:0
can be3-(d^16)/23
Perl, 204 + 3 = 207 bytes
+3 for
-F
Whitespace is not part of the code and is provided for legibility.
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.źródło
Excel VBA, 205 bytes
I'm kinda surprised at Excel's ability to compete with existing answers. It works because
w
andz
keep track of the direction.źródło
SmileBASIC,
148146 bytesCall the function with
W "text",vx,vy
, where vx and vy is the direction at the start (default is 1,0)źródło
Swift 3, 283 bytes
Ungolfed
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
print
suxźródło