SHARE
TWEET

pqh

boothby May 19th, 2012 876 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #
  2. # piet quinelike haiku
  3. # this is in public domain
  4. # make war not poems
  5. #
  6.  
  7. def P(s):
  8.  h=v=0;l=len(s)+1;R=[[192,0,0]]
  9.  C=[1,3,2,6,4,5];V=[0,192,192,255,0,255]
  10.  for x in map("=|^+-*/%~>.,:@$?#!".find,s):
  11.   C=C[x//3:]+C[:x//3];V=V[x%3*2:]+V[:x%3*2]
  12.   R+=[[V[(C[0]//i)%2]for i in[1,2,4]]]
  13.  return R
  14.  
  15. def addup(n):
  16.     if n == 0:
  17.         return '|~'
  18.  
  19.     b = bin(n)[3:]
  20.     m = {'0':':+', '1':'::+'}
  21.     s = '|'+''.join(m[i] for i in b[::-1])
  22.     s+= '+'*b.count('1')
  23.    
  24.     return s
  25.  
  26. def printstring(s):
  27.     p = ''
  28.     o = 0
  29.     for c in s:
  30.         co = ord(c)
  31.         if co > o:
  32.             p+= addup(co-o) + '+'*(o!=0)+':!'
  33.         elif co == o:
  34.             p+= ':!'
  35.         else:
  36.             p+= addup(o-co) + '-'*(o!=0)+':!'
  37.         o = co
  38.     return p
  39.  
  40. alph = """\
  41.      ooooo oooo  ooooo oooo  ooooo ooooo ooooo o   o ooooo    oo o   o o     ooooo o   o  ooo  ooooo  ooo  ooooo ooooo ooooo o   o o   o o o o o   o o   o ooooo
  42.      o   o o   o o     o   o o     o     o     o   o   o       o o  o  o     o o o oo  o o   o o   o o   o o   o o       o   o   o o   o o o o  o o   o o     o  
  43.      ooooo oooo  o     o   o ooooo ooooo o ooo ooooo   o       o ooo   o     o o o o o o o   o ooooo o   o ooooo ooooo   o   o   o o   o o o o   o     o     o  
  44.      o   o o   o o     o   o o     o     o   o o   o   o   o   o o  o  o     o o o o  oo o   o o     o  oo o  o      o   o   o   o  o o  o o o  o o    o    o    
  45.      o   o oooo  ooooo oooo  ooooo o     ooooo o   o ooooo ooooo o   o ooooo o o o o   o  ooo  o      oooo o   o ooooo   o   ooooo   o   ooooo o   o   o   ooooo  
  46.                                                                                                                                                                   """.split('\n')
  47.  
  48. pixwid = 6
  49. pixhgt = 6
  50.  
  51. lett = ' ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  52. Alph = dict((a,[alph[i][pixwid*j:pixwid*(j+1)] for i in range(pixhgt)]) for j,a in enumerate(lett))
  53.  
  54.  
  55. print "filename then haiku"
  56. print "punctuation ruins poem"
  57. print "you might as well yell"
  58.  
  59. filename = raw_input()
  60. hku = [raw_input() for _ in range(3)]
  61.  
  62. hlen = max(map(len,hku))
  63.  
  64. msg = [''.join(Alph[c][i] for c in hku[0]+' '*(hlen-len(hku[0]))) for i in range(pixhgt)]
  65. msg+= [''.join(Alph[c][i] for c in hku[1]+' '*(hlen-len(hku[1]))) for i in range(pixhgt)]
  66. msg+= [''.join(Alph[c][i] for c in hku[2]+' '*(hlen-len(hku[2]))) for i in range(pixhgt)]
  67.  
  68. last = 0
  69.  
  70. outfile = open(filename,'w')
  71. outfile.write( "P3 %d %d 255 "%(pixwid*hlen*2 + 5, pixhgt*3*2) )
  72.  
  73. left = [" 255 0 0  255 0   0    255 255 255 ",
  74.         " 0   0 0  0   0   0    255 255 255 ",
  75.         " 255 0 0  255 255 255  255 255 255 ",
  76.         " 255 0 0  255 255 255  255 255 255 "]
  77.  
  78. right = [" 255 255 255  255 0 0 ",
  79.          " 255 255 255  255 0 0 ",
  80.          " 255 0   0    255 0 0 ",
  81.          " 0   0   0      0 0 0 "]
  82.  
  83. lastleft = [" 0 0 0 0 0 0 255 255 255 ",
  84.             " 255 0 0 255 0 0 255 255 255 "]
  85. lastright = [" 255 0 0 255 0 0 ",
  86.              " 0 0 0 0 0 0 "]
  87.  
  88. ins = printstring('\n'.join(hku))
  89.  
  90. for i in range(pixhgt*3):
  91.     line = msg[i]
  92.     outline = []
  93.     progline = ''
  94.     if i%2:
  95.         line = line[::-1]    
  96.     dotlen = 0
  97.     for c in line:
  98.         if dotlen and c == ' ':
  99.             next=last+2*dotlen-1
  100.             p = ins[last:next]
  101.             p+= '='*(next-last-len(p))
  102.             outline += P(p)
  103.             progline += 's' + p
  104.             last = next
  105.             dotlen = 0
  106.         if c == ' ':
  107.             outline += [[255]*3]*2
  108.             progline += '  '
  109.         else:
  110.             dotlen += 1
  111.     if dotlen:
  112.         next=last+2*dotlen-1
  113.         p = ins[last:next]
  114.         p+= '='*(next-last-len(p))
  115.         outline += P(p)
  116.         progline += 's' + p
  117.         last = next
  118.  
  119.     if i%2:
  120.         progline = progline[::-1]
  121.         outline = outline[::-1]
  122.         line = line[::-1]
  123.     outfile.write(left[(2*i)%4])
  124.     outfile.write(' '.join(map(str,sum(outline,[]))))
  125.     outfile.write(right[(2*i)%4])
  126.     if i == pixhgt*3-1:
  127.         outfile.write(lastleft[i%2])
  128.         outfile.write(''.join([' 0 0 0 ',' 255 255 255 '][c==' ']*2 for c in line))
  129.         outfile.write(lastright[i%2])
  130.     else:
  131.         outfile.write(left[(2*i+1)%4])
  132.         outfile.write(''.join([' 0 0 0 ',' 255 255 255 '][c==' ']*2 for c in line))
  133.         outfile.write(right[(2*i+1)%4])
  134.     print progline
  135.  
  136. if last == len(ins):
  137.     print "congratulations,",
  138. print "your haiku was %.2f%% zen"%(last*100./len(ins))
RAW Paste Data
Top