I hate code bloat!

So I have decided to replace my Windows 7 system by a golfed version. However, I know that it is not easy, so let's build a prototype first. It will take a list of windows from the user and show them using a pseudo-graphic display on the standard output.

For example: user input:


The Code Golf® Window Manager™ outputs:

│::::::::::::::│ └──┘


  • Taken from standard input (or, if your system has no stdin, any method that can provide several lines of input)
  • Each line contains 4 numbers, delimited by commas - coordinates of a window
  • First two numbers: upper-left corner; last two numbers: lower-right corner
  • Using x,y notation


  • Write it to standard output (or, if your system has no stdout, anything that displays monospaced text)
  • Use single box-drawing characters from Code Page 437 for drawing window boundaries
  • Windows specified later in input obscure those specified earlier
  • Fill the windows with the colon character: :
  • Leading and trailing spaces are OK, as long as they don't break alignment of the windows


  • Maximum resolution I want to support: 76 (horizontal) by 57 (vertical)
  • No need to support bad input
  • Maximum number of windows: 255 (if you need a limit)
  • My Windows 7 cmd shell displays codepage 437 characters by default; if anyone has a way to do that on linux (using xterm or whatever), please describe it here

For reference, the character codes are:

┌da   ─c4   ┬c2   ┐bf

│b3   :3a   │b3   │b3

├c3   ─c4   ┼c5   ┤b4

└c0   ─c4   ┴c1   ┘d9

Total: 12 different characters.

  • \$\begingroup\$ Note that you can display pretty windows on this page if you paste the following code in browser address bar : javascript:$('pre').css('line-height','1em') \$\endgroup\$
    – Michael M.
    Commented Jul 22, 2014 at 11:19
  • \$\begingroup\$ I got the correct display in gnome-terminal by following the instructions on stackoverflow.com/questions/5509829/… \$\endgroup\$
    – brm
    Commented Jul 23, 2014 at 17:45

3 Answers 3


JavaScript ES6 (FF ≥ 31.0), 404 chars

w=s=>{a=[];for(i=0;i<57;)a[i++]=Array(76).fill(0);s.split('\n').map(e=>{r=e.split(',');a[x=r[1]][w=r[0]]|=5;a[x][y=r[2]]|=6;a[z=r[3]][w]|=9;a[z][y]|=10;for(i=x;++i<z;)a[i][w]|=12,a[i][w]&=14,a[i][y]|=12,a[i][y]&=13;for(i=w;++i<y;)a[x][i]|=3,a[x][i]&=11,a[z][i]|=3,a[z][i]&=7;for(i=x;++i<z;)for(j=w;++j<y;)a[i][j]=16});console.log(a.map(e=>e.map(t=>t==16?':':' xx─x┌┐┬x└┘┴│├┤┼'[t&15]).join('')).join('\n'))}

Without ES6 :

function w(s){a=[];for(i=0;i<57;i++){a[i]=[];for(j=0;j<76;j++)a[i][j]=0}s.split('\n').forEach(function(e){r=e.split(',');a[r[1]][r[0]]|=5;a[r[1]][r[2]]|=6;a[r[3]][r[0]]|=9;a[r[3]][r[2]]|=10;for(i=r[1];++i<r[3];)a[i][r[0]]|=12,a[i][r[0]]&=14,a[i][r[2]]|=12,a[i][r[2]]&=13;for(i=r[0];++i<r[2];)a[r[1]][i]|=3,a[r[1]][i]&=11,a[r[3]][i]|=3,a[r[3]][i]&=7;for(i=r[1];++i<r[3];)for(j=r[0];++j<r[2];)a[i][j]=16});console.log(a.map(function(e){return e.map(function(t){return t==16?':':' xx─x┌┐┬x└┘┴│├┤┼'[t&15]}).join('')}).join('\n'))}

w('0,0,15,10\n15,10,20,15\n10,13,15,15\n9,1,16,3\n17,5,20,7\n11,2,17,4\n15,4,19,6\n13,3,18,5'); outputs correctly the OP's example.

The edges of windows are built using bitwise operators (Up=8, Bottom=4, Left=2, Right=1).

  • \$\begingroup\$ No tested exhaustively, but seems to work fine with string literal instead of array, in 548 characters. (Tested only in Firefox.) \$\endgroup\$
    – manatwork
    Commented Jul 22, 2014 at 10:01
  • \$\begingroup\$ You can save a bunch of characters by using ECMAScript 6: function w(s){...} becomes w=(s)=>{...} (and like wise for all your other function literals). And the character lookup table can probably be replaced by a string with the same characters. \$\endgroup\$ Commented Jul 22, 2014 at 10:12
  • \$\begingroup\$ Later today, after Firefox 31.0 will be released, you will be able to use Array.fill() to initialize to “desktop”. \$\endgroup\$
    – manatwork
    Commented Jul 22, 2014 at 10:53
  • \$\begingroup\$ @manatwork, I tried with FF Aurora [].fill([].fill(0,0,76),0,57), too bad it doesn't work. Can I write shorter than new Array(57).fill(new Array(76).fill(0)) ? \$\endgroup\$
    – Michael M.
    Commented Jul 22, 2014 at 10:58
  • \$\begingroup\$ Try to skip the new operators: Array(57).fill(Array(76).fill(0)). \$\endgroup\$
    – manatwork
    Commented Jul 22, 2014 at 11:04

Python, 397 characters

import os
M=[[0]*76 for _ in J(57)]
for A,C,B,D in[map(int,q.split(','))for q in os.read(0,9999).split('\n')]:
 for x in J(A+1,B):
    for y in J(C+1,D):M[C][A]|=5;M[C][B]|=6;M[D][A]|=9;M[D][B]|=10;M[C][x]|=3;M[D][x]|=3;M[y][A]|=12;M[y][B]|=12;M[y][x]=16;M[y][x-1]&=~1;M[y][x+1]&=~2;M[y-1][x]&=~4;M[y+1][x]&=~8
C=" rl─d┌┐┬u└┘┴│├┤┼:"
for l in M:print''.join((C+C[1:])[m]for m in l)

Change C="... to C=u"... and it'll print in unicode instead! It might be tricky to get the file to save properly because the code page 437 characters are not escaped (the first 'coding' comment line is required).

The approach is to incrementally build a map using bit-wise operators. Less golfed version with comments:

import os
# set up the field
# Each element is a bitfield. Flags are:
# 16 - inside a window?
# 8  - up
# 4  - down
# 2  - left
# 1  - right
M=[[0]*76 for _ in J(57)]
# for each window...
for A,C,B,D in[map(int,q.split(','))for q in os.read(0,9999).split('\n')]:
    # add the directions for the corners
    # add the top and bottom edges
    for y in J(C+1,D):M[y][A]|=12;M[y][B]|=12
    # add the left and right edges
    for x in J(A+1,B):M[C][x]|=3;M[D][x]|=3 
    # deal with the middle
    for x in J(A+1,B):
       for y in J(C+1,D):
           # Clear the current spot by setting to inside a window
           # Remove the right direction from the left spot, top from the bottom, etc
 # print it out
 C=u" rl─d┌┐┬u└┘┴│├┤┼:"
 for l in M:print''.join((C+C[1:])[m]for m in l)

Python, 672 chars

The less readable version:

import sys
Z=[map(int,l.split(",")) for l in sys.stdin.readlines()]
S=[[[0]*5 for x in r(77) ] for y in r(58)]
for i in r(len(Z)):
 for a,b,c in [(C,A,2),(C,A,3),(D,A,1),(D,A,2),(C,B,3),(C,B,4),(D,B,1),(D,B,4)]:S[a][b][c]=1
 for x in r(A+1,B):
  for a,b in [(C,2),(C,3),(C,4),(D,1),(D,2),(D,4)]:S[a][x][b]=(b+1)&1
 for y in r(C+1,D):
  for a,b in [(A,1),(A,2),(A,3),(B,1),(B,3),(B,4)]:S[y][a][b]=b&1
 for x in r(A+1,B):
  for y in r(C+1,D):S[y][x]=[i+1]+[0]*4
for l in S:
 for k in l:
  c=' ';x=M[k[1]*8|k[2]*4|k[3]*2|k[4]]
  if k[0]:c=':'
  if x:c=chr(x) 

Started from the version below:

import sys

coords = [ tuple(map(int,l.strip().split(","))) for l in sys.stdin.readlines() ]

screen = [ [ [-1, [False,False,False,False]] for x in range(0, 77) ] for y in range(0, 58) ]

def mergeBorders(screen, w):
    x0,y0,x1,y1 = w
    screen[y0][x0][1][1] = True
    screen[y0][x0][1][2] = True
    screen[y1][x0][1][0] = True
    screen[y1][x0][1][1] = True
    screen[y0][x1][1][2] = True
    screen[y0][x1][1][3] = True
    screen[y1][x1][1][0] = True
    screen[y1][x1][1][3] = True

    for x in range(x0+1,x1):
        screen[y0][x][1][1] = True
        screen[y0][x][1][2] = False
        screen[y0][x][1][3] = True
        screen[y1][x][1][0] = False
        screen[y1][x][1][1] = True
        screen[y1][x][1][3] = True

    for y in range(y0+1,y1):
        screen[y][x0][1][0] = True
        screen[y][x0][1][1] = False
        screen[y][x0][1][2] = True
        screen[y][x1][1][0] = True
        screen[y][x1][1][2] = True
        screen[y][x1][1][3] = False

def paintInside(screen, w, wId):
    x0,y0,x1,y1 = w
    for x in range(x0+1,x1):
        for y in range(y0+1,y1):
            screen[y][x][0] = wId 
            screen[y][x][1] = [False, False, False, False]

for wId in range(len(coords)):
    w = coords[wId]
    mergeBorders(screen, w)
    paintInside(screen, w, wId)

borderMap = { (False, True, True, False): 0xda,
              (False, True, False, True): 0xc4,
              (False, True, True, True):  0xc2,
              (False, False, True, True): 0xbf,
              (True, False, True, False): 0xb3,
              (True, True, True, False):  0xc3,
              (True, True, True, True):   0xc5,
              (True, False, True, True):  0xb4,
              (True, True, False, False): 0xc0,
              (True, True, False, True):  0xc1,
              (True, False, False, True): 0xd9 }

def borderChar(c):
    return chr(borderMap[(c[0],c[1],c[2],c[3])])

for screenLine in screen:
    for contents in screenLine:
        c = ' '
        if True in contents[1]:
            c = borderChar(contents[1])
        elif contents[0] >= 0:
            c = ':'

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.