This challenge consists in coding an interpreter for a Mondrian painting description language (MPDL).

Language definition

The language operates on a stack of rectangles. A rectangle is defined by its upper left coordinate and lower right coordinate. Coordinates must be integers. The stack is initialized with a single rectangle with attributes (1,1,254,254)

Each command has the following format: <character><integer>

There are three commands:

v<integer> : perform a vertical split on the latest rectangle in the stack, at the position indicated by the parameter (as percentage). The source rectangle is removed from the stack and replaced with the two new rectangles that result of the split. The left rectangle is pushed on the stack, then the right rectangle. As rectangle coordinates are integers, fractions should be rounded to biggest smaller integer.

h<integer> : horizontal split. The top rectangle is pushed on the stack, then the bottom rectangle.

c<integer> : removes the latest rectangle from the stack and paints it to the color given as parameter. 1 = white, 2 = red, 3 = blue, 4 = yellow


Write a program that takes as parameter a painting description and creates a 256x256 bitmap representation of the painted rectangles. The rectangles must be separated with a 3 pixel black line. A one or two pixel rectangle should have his non-black pixels hidden by the border black pixels.

The input can be read as a parameter or as a file, up to you. The commands should be separated by a space. You can assume that the input file has correct syntax and does not have trailing or leading spaces, tabs, etc. The output can be directly displayed on the screen, or saved to a file, up to you.

The shortest code wins.


The following source:

v25 h71 v93 h50 c4 c1 c1 c2 h71 c3 h44 c1 c1

Should produce the Composition II in Red, Blue and Yellow:

enter image description here

  • 1
    \$\begingroup\$ The language isn't great. v and h arguments should be in pixels \$\endgroup\$ Commented Oct 31, 2014 at 4:58
  • \$\begingroup\$ Also, I'm not sure what's the point of rotating the stack instead of popping. \$\endgroup\$ Commented Oct 31, 2014 at 4:59
  • \$\begingroup\$ Using percentages allows you to choose whatever size for the output bitmap - the result will be same (only it will be scaled) \$\endgroup\$
    – Arnaud
    Commented Oct 31, 2014 at 5:00
  • 1
    \$\begingroup\$ Yeah something like that, but note you can still do without extra syntax elements as all operators have constant number of parameters. So the above can be still parsed when represented as v30 v50 c1 c5 h70 v50 c1 c3 c2. \$\endgroup\$
    – nutki
    Commented Oct 31, 2014 at 8:12
  • 3
    \$\begingroup\$ Really hoping someone writes a solution in Piet! \$\endgroup\$
    – Skyler
    Commented Oct 19, 2015 at 14:46

5 Answers 5


Perl 5 + ImageMagick - 297

Something to start with:

sub a{my($x,$y,$X,$Y,$d)=@_;$_=shift@ARGV;
/c/&&"-fill ".qw/white red blue yellow/[$'-1]." -draw 'rectangle $x,$y $X,$Y' "}
system"convert -size 256x256 -stroke black xc: ".a(0,0,255,255)."a.gif"

Takes input on command line and generates a.gif.


Haskell - 335

import Diagrams.Prelude
import Diagrams.Backend.SVG
d((x:n):i)|x=='v'=(b#scaleX s#c|||a#scaleX(1-s)#c,k)|x=='h'=(b#scaleY s#c===a#scaleY(1-s)#c,k)|x=='c'=(square 1#fc([white,red,blue,yellow]!!(read n-1)),i)where{s=(read n)/100;(a,j)=d i;(b,k)=d j}
main=getLine>>=renderSVG"a.svg"(Width 256).pad 1.02.c.lwG 0.012.fst.d.words

The program reads the instructions as one line from stdin, if this is unacceptable let me know.

Compiles into a program that takes in flags -w width -h height -o outputfile. Outputs an "a.svg" file, if that's not immediately clear from the code. Since the output is a vector image, it's not 'pixel perfect'.

This is my first time working with Diagrams -package, feel free to point out any mistakes I made. Especially any backend that would let me output with less code would be nice.

You can see some of the first steps I took when developing the code in http://paste.hskll.org/get/1737. It differs from the code above in imports and lacks main since the paste.hskll.org provides its own main and drawing environment.


Python - 434 405 377 364 361

My first python golf. This can probably be improved A LOT, so any feedback is appreciated.

from turtle import*
for c in input().split():
 if b>1:goto(v,-w),color('#000',['#fff','red','#00f','#ff0'][p-1]),begin_fill(),[(fd(o),rt(90))for o in[f,g]*2],end_fill()
  • 1
    \$\begingroup\$ You can save a char by merging lines 4, 5 with a semicolon. Also a+=[x] instead of a.append(x). And split doesn't need an argument if separating by whitespace. \$\endgroup\$
    – Sp3000
    Commented Nov 5, 2014 at 0:59

HTML + JavaScript ES6 (407)

Tested with Firefox 32.0.3

<canvas id=c width=256 height=256><script>r=[[1,1,253,253]]
prompt().split(" ").map(x=>{q=r.pop()

  • 1
    \$\begingroup\$ So much more golfable! x.charAt(0) -> x[0]; x.substr -> x.slice; white yellow -> #fff #ff0; document.getElementById("c") -> c ... and more \$\endgroup\$
    – edc65
    Commented Nov 2, 2014 at 22:36
  • \$\begingroup\$ @edc65 Thanks! I'll improve it further tomorrow. \$\endgroup\$
    – Mika Lammi
    Commented Nov 2, 2014 at 23:04
  • \$\begingroup\$ Thanks for the answer, but I try to test it and I have a white screen? \$\endgroup\$
    – Arnaud
    Commented Nov 3, 2014 at 2:14
  • \$\begingroup\$ @SuperChafouin What browser are you using? I don't think that arrow functions (and other ES6 stuff) are really supported except in Firefox. \$\endgroup\$
    – Mika Lammi
    Commented Nov 3, 2014 at 6:18

HTML+JavaScript (ES6) 335

Too similar to @mika answer - marking CW.

  • replace with function instead of split...map
  • spread operator
  • push 2 values at once
  • ternary operator instead of function properties

<canvas id=c><script>
o.translate(0.5,0.5), // to avoid anti-alias on straight lines


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.