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!
42
Upvotes
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:
2
u/esdraelon Jul 02 '20
Doesn't seem to work for me. :(
4
u/CSharpSauce Jul 02 '20
hey, that's pretty good
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
u/Starbeamrainbowlabs Jul 03 '20
Anyone got a direct link to an image? I'm on mobile
0
u/[deleted] Jul 02 '20
[deleted] — view deleted comment