#!/usr/bin/python

import os, sys, string

def to_short(bytes):
  return bytes[0] << 8 | bytes[1]

def to_long(bytes):
  return bytes[0] << 24 | bytes[1] << 16 | bytes[2] << 8  | bytes[3]

def decode_mario(comp):
  length = to_long(comp[4:8])
  r_off = to_long(comp[8:12])
  v_off = to_long(comp[12:16])
  #print "length = %d, r_off = %d, v_off = %d" % (length, r_off, v_off)
  flags = comp[16:r_off]
  runs = comp[r_off:v_off]
  verbatim = comp[v_off:]
  flags.reverse()
  runs.reverse()
  verbatim.reverse()
  s = ''
  num_flags=0

  while length:
    if num_flags == 0:
      f = to_long([flags.pop(), flags.pop(), flags.pop(), flags.pop()])
      num_flags = 32
      #print "flags = %#x" % f
    
    if f & 0x80000000:
      #print "verbatim: before %d" % len(s)
      s = s + chr(verbatim.pop())
      #print "verbatim: after %d" % len(s)
      length = length - 1
    else:
      code = to_short([runs.pop(), runs.pop()])
      runlen = ((code >> 12) & 0xf) + 3
      offset = len(s) - (code & 0xfff) - 1
      #print "run: before %d (%d, %d)" % (len(s), runlen, offset)
      while runlen:
        s = s + s[offset]
        offset = offset + 1
        runlen = runlen - 1
        length = length - 1
      #print "run: after %d" % len(s)
    f = f << 1
    num_flags = num_flags - 1
    #print "length = %d (%d)" % (length, len(s))

  #print "final length = %d" % len(s)
  return s

out = []
lines = open(sys.argv[1]).readlines()
for l in lines:
  if string.find(l, ',') != -1:
    s = string.split(l, ',')
    for h in s:
      if h != '\n':
        out.append(string.atoi(h, 16))

t = decode_mario(out)
    
colors = [0x0000,0x318d,0x4211,0x1085,0xd6b5,0xffff,0xf7bd,0x2109, 0x6319,0xc631,0x8421,0xa529,0x739d,0xe739,0x94a5,0xb5ad, 0x5295,0x0843,0x294b,0x39cf,0x4a53,0x18c7,0xbdef,0x7bdf, 0xce73,0x8c63,0x6b5b,0x5ad7,0xad6b,0x9ce7,0xef7b,0xdef7]

sys.stdout.write("P6\n%d 32\n255\n" % (len(t)/32))
for i in xrange(0, len(t)):
  s = colors[ord(t[i])]
  r = (s >> 11) & 0x1f
  g = (s >>  6) & 0x1f
  b = (s >>  1) & 0x1f
  sys.stdout.write(chr(r<<3))
  sys.stdout.write(chr(g<<3))
  sys.stdout.write(chr(b<<3))



