Animating the Quantum Drunkard’s Walk

A recent PPCG challenge titled The Quantum Drunkard’s Walk was about a tiny drunken person for which quantum mechanics apply and who — being drunk — will randomly walk in one of the four cardinal directions each step they take.
As they experience the strange world of quanta, they can simultaneously take every path at once and their paths influence each other. The challenge then asked to output all possible positions the quantum drunkard could occupy, all paths they could take in ASCII representation.

The question also states this problem’s equivalence to a cellular automaton, when one removes the story boilerplate.
Cells in this cellular automaton are square and can occupy one of three states: empty, awake or sleeping. Each iteration, all cells change according to three rules.

  • An empty cell wakes up iff there is exactly one awake cell amongst its cardinal neighbours, else it stays empty.
  • An awake cell goes to sleep.
  • A sleeping cell continues to sleep.

Being code golf, the aim was to come up with a minimally sized source code; my attempt required 214 bytes and prints a nested array containing one-length strings (characters), as this output method is cheaper than concatenating all characters to one string.

python quantum.py -rmi 200
python quantum.py -rmi 200

However, one user took the challenge idea a bit further and created an animated gif showing the walk’s, or cellular automaton’s, progression over time with an increasing number of iterations. My Python program shown in this post does exactly that, generating an animated gif showing the automaton’s progression. I even implemented rainbow support, possibly improving the automaton’s visual appearance.
Python source code can be downloaded and seen below.

I use the Python Imaging Library to produce all frames and use a shell call to let ImageMagick convert all frames to an animated gif. Animation parameters are taken via shell arguments, a full list of features follows (also available via the -h flag).

  • --iterations N Number of iterations (initial frame not counted)
  • --colorempty C Empty cell color (#rrggbb)
  • --colorawake C Awake cell color (#rrggbb)
  • --colorsleeping C Sleeping cell color (#rrggbb)
  • --rainbow Use rainbow colors (overrides color settings)
  • --scale N Cell square pixel size
  • --convert Execute ImageMagick’s convert command
  • --delay N Delay between frames in output image.
  • --loop N Gif loops (0 means for indefinite looping)
  • --keepfiles Do not delete files when converting
python quantum.py -s 25 -md 50
python quantum.py -s 25 -md 50

# Python 2.7 code; Jonathan Frech; 1st of December 2017

Continue reading

Advertisements

Generic C / Python Polyglot

A polyglot — coming from the Greek words πολύς (many) and γλώττα (tongue) — is a piece of source code which can run in multiple languages, often performing language-dependent tasks.
Even though such a source code’s feature may not seem particularily useful or possible with certain language combinations, trying to bend not one but multiple language’s syntactic rules and overall behavior is still interesting.

An example of a rather simple polyglot would be a Python 2 / Python 3 polyglot — if one counts those as two separate languages. Because of their enormous similarities, one can pick out differences and use those to determine which language runs the source code.

if 1/2>0:print("Python 3")
else:print("Python 2")

Utilizing Python’s version difference regarding integer division and real division is one of the most common ways to tell them apart, as it can also be used to only control a specific part of a program instead of having to write two nearly identical programs (increases the program’s style and cuts on bytes — an important consideration if one golfs a polyglot).

However, polyglots combining languages that are not as similar as Python 2 and Python 3 require more effort. The following is a general Python 2 / C polyglot, meaning that nearly all C and Python 2 programs can be mixed using this template (there are a few rules both languages need to abide which will come apparent later).

#define _\
"""
main(){printf("C");}
#define _"""
#define/*
print"Python 2"
#*/_f

In the above code, main(){printf("C");} can be nearly any C code and print"Python 2" can be nearly any Python 2 code.
Language determination is exclusively done via syntax. A C compiler sees a #define statement and a line continuation \, another two #define statements with a block comment in between and actual compilable C source code (view first emphasis).
Python, on the other hand, treats all octothorps, #, as comments, ignoring the line continuation, and triple-quoted strings, """...""", as strings rather than statements and thus only sees the Python code (view second emphasis).

My first ever polyglot used this exact syntactical language differentiation and solved a task titled Life and Death of Trees (link to my answer).

TImg

Texas Instrument’s TI-84 Plus is a graphing calculator with a variety of features. It has built-in support for both fractions and complex numbers, can differentiate and integrate given functions and supports programming capabilities. The latter allows to directly manipulate the calculator’s monochrome display’s 5985 pixels (the screen has dimensions 95x63). TImg is a Python program (source code is listed below and can also be downloaded) which takes in an image and outputs TI-BASIC source code which, when run on the graphing calculator, will produce the given image — in potentially lower quality.

TImg
TI-84 Plus’ screen dimensions (bitmap).

PIL — the Python Imaging Library — is used to read in the image and further for processing. The supplied image may be rotated and resized to better fit the TI-84’s screen and any color or even grayscale information is reduced to an actual bitmap — every pixel only has two distinct values.
Direct pixel manipulation on the TI-84 is done via the Graph screen. To get remove any pixels the system draws on its own, the first three commands are ClrDraw, GridOff and AxesOff which should result in a completely blank screen — assuming that no functions are currently being drawn. All subsequent commands are in charge of drawing the previously computed bitmap. To turn certain pixels on, Pxl-On(Y,X is used where Y and X are the pixel’s coordinates.

fractal
A fractal (bitmap).

Since the TI-84 Plus only has 24 kilobytes of available RAM, the source code for a program which would turn on every single pixel individually does not fit. Luckily, though, a program which only individually turns on half of the screen’s pixels fits. To ensure that TImg’s output fits on the hardware it is designed to be used with, an image’s bitmap is inverted when the required code would otherwise exceed 3500 lines — a value slightly above the required code to draw half of the pixels.

jblog
A J-Blog screenshot (bitmap).

By default, the resulting code draws pixels starting at the screen’s top-left corner and ending at its bottom-right. A command line flag --shuffle can be set which changes this behavior to let pixels pseudo-randomly appear on the screen (pseudo-randomness is calculated in the Python script; the TI-BASIC source code is completely deterministic).
And — of course — one can feed the program an image of the calculator the BASIC code runs on; self-referential TIception.

tiception
TIception (input image).

# Python 2.7 code; Jonathan Frech; 5th, 6th of October 2017

Continue reading