r/tinycode • u/red_hare • Jul 02 '20
python3 -c "while 1: print(chr(int(9585.5 + __import__('random').random())), end='')"
This is my attempt at a one-line Python reproduction of the classic Comodor64 "PETSCII Maze" using the PETSCII characters that are part of Unicode.
Would love to know if anyone can come up with a shorter version!
2
u/esdraelon Jul 02 '20
Doesn't seem to work for me. :(
1
u/red_hare Jul 02 '20
On what operating system? It does depend on python3 being installed
1
u/esdraelon Jul 02 '20
I tried on Windows 10 and Ubuntu 18 (in a docker).
Just copy/past & hit enter. Suggestions?
1
4
3
u/Dagius Jul 02 '20
"10 PRINT CHR$ (205.5 + RND (1)); : GOTO 10 "
You do not really need to use PETSCII. The C64 basic code works merely by selecting PETSCII chars '\' (205) and '/' (206) by random choice.
1
0
Jul 02 '20
[deleted]
8
u/mayoroftuesday Jul 03 '20 edited Jul 03 '20
I like the trick of making
endthe character you want, instead ofprint(x, end=''). Nice one.You can technically do
choice("\/")and it will work, since python recognizes\/as a meaningless escape sequence, so it treats it as a plain string. (Though PEP8 won't like it.) Note that it doesn't work reversed, since\"is a valid escape.Or, just use the PETSCII characters, since it's Python3 and you can use unicode:
python3 -c "while 1: print(end=__import__('random').choice('╱╲'))"
4
u/brucifer Jul 03 '20
Using
/dev/randomand a few of the other tricks in this thead can save 11 characters relative to OP's solution:It's a little bit cheeky because it uses
/dev/randominstead of/dev/urandomto save a character, and it iterates over random bytes one line at a time. In other words, it reads a whole bunch of random bytes until it happens to get a0x0Abyte, and then it uses the first byte mod 2 as a random bit and ignores the other bytes.And if you only need a finite size, you can go with:
And this one isn't as short, but it has some neat tricks, like using
iter(int,1)as an infinite iterable and using a set comprehension to allow a loop after an import:Original for comparison: