Advertisement
robotape

holiday-bullshit-sandbox.py

Dec 13th, 2013
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.48 KB | None | 0 0
  1. import itertools
  2.  
  3. # followed lights along the series, clockwise, starting at left side, top
  4. # rationelle being that way you have an uninterupted string
  5. days = [
  6.     'GBBGGYR YBYBYBR YBBRYYB RRBYGBY GYBBGYR GGBYBBY RYBYGYY',
  7.     'GGBGGBG BBGGYRR YBBYYRB RBBGGYY BYBBYRR GBYYBYY YGGRBRG',
  8.     'GRRGGYY GGGBYBY YBYBGYB YYRRBGR GGYBGBB BYGBBYB BBYGGBB',
  9.     'GBBGGBG GRBBBGG BBRRBRB YYBYYBG BBGGYGG BRRBBYY RBRBBBG GBYBBBY',
  10.     'GGBGBYG YBGGYRB BRBYGGB GGBRGGB GGBGBRR GRBBYBB RBRBRYY',
  11.     'GBRYGYB GYRRBBB YRRRBBB GGRRBBR BRRBGGR RGBBYBG YBRBBGG YYBBGYB BRRYYRB RRRRBRR BRYBBRR RRRYRYR Y'
  12.     ]
  13.  
  14. base64 = [chr(i) for i in range(65,65+26)]+[chr(i) for i in range(97,97+26)]+[chr(i) for i in range(48,48+10)]+['+','/']
  15. b64map = dict(zip(range(64),base64))
  16.  
  17. # Our 4 symbols and codes
  18. symbols = ('R','G','B','Y')
  19. codes = ['00','01','10','11']
  20. ints = [1, 2, 3, 4]
  21. #codes = ['0','1','0','1']
  22.  
  23. # split string into groups of n characters
  24. # http://stackoverflow.com/questions/2231663/slicing-a-list-into-a-list-of-sub-lists
  25. def grouper(n, iterable, fillvalue=None):
  26.     "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
  27.     args = [iter(iterable)] * n
  28.     return itertools.izip_longest(fillvalue=fillvalue, *args)
  29.  
  30. # decode binary string into into 7bit characters
  31. def seven_bit_decode(bin_string):
  32.     res = []
  33.     for g in grouper(7, bin_string):
  34.         try:
  35.             res.append(chr(int('0'+''.join(g),2)))
  36.         except:
  37.             pass
  38.     return ''.join(res)
  39.  
  40. def eight_bit_decode(bin_string):
  41.     res = []
  42.     for g in grouper(8, bin_string):
  43.         try:
  44.             res.append(chr(int(''.join(g),2)))
  45.         except:
  46.             pass
  47.     return ''.join(res)
  48.  
  49. def five_bit_decode(bin_string):
  50.     res = []
  51.     for g in grouper(5, bin_string):
  52.         try:
  53.             res.append(chr(97+int(''.join(g),2)))
  54.         except:
  55.             pass
  56.     return ''.join(res)
  57.  
  58. # interprets the binary string as
  59. def six_bit_decode(bin_string):
  60.     res = []
  61.     for g in grouper(6, bin_string):
  62.         try:
  63.             res.append(b64map[int(''.join(g),2)])
  64.         except:
  65.             pass
  66.     return ''.join(res)
  67.  
  68. def bincount(s):
  69.     res = {'R':0,'G':0,'B':0,'Y':0}
  70.     for c in s:
  71.         res[c] += 1
  72.     return res
  73.  
  74.  
  75. day = ''.join(days)
  76. day = day.replace(' ','') #[::-1]
  77.  
  78. d = dict(zip(symbols,ints))
  79. numeric = [d[x] for x in day]
  80.  
  81. counts = bincount(day) #np.bincount(np.array(numeric))
  82.  
  83. print "Light color distribution"
  84. print counts
  85.  
  86. # get all permutations for the symbols
  87. for p in itertools.permutations(symbols):
  88.     # create the mapping
  89.     d = dict(zip(p,codes))
  90.     print 'Permutation:',d
  91.     # map string to binary based on our mapping
  92.     binary = reduce(lambda x, y: x.replace(y, d[y]), d, day)
  93.  
  94.     print len(binary)
  95.     # decode assuming 7 bit ascii
  96.     print '7 bit ascii:',seven_bit_decode(binary)
  97.     print '8 bit ascii:',eight_bit_decode(binary)
  98.     print '6 bit "base64" ascii',six_bit_decode(binary)
  99.     #five_bit_decode(binary)
  100.  
  101. print '\n'
  102.  
  103. try:
  104.     import matplotlib.pyplot as plt
  105.     from matplotlib import colors
  106.     import numpy as np
  107.  
  108.     def output_binary_figure(bin_string):
  109.         N = np.ceil(np.sqrt(len(bin_string)))
  110.         X = [int(x) for x in bin_string]
  111.         X += [0]*(N*N-len(X))
  112.         A = np.array(X,dtype=np.bool).reshape((N,N))
  113.         plt.imshow(A, cmap='Greys',  interpolation='nearest')
  114.         plt.show()
  115.  
  116.     def output_figure(s):
  117.         N = np.ceil(np.sqrt(len(s)))
  118.         d = dict(zip(symbols,ints))
  119.         X = [d[x] for x in s]
  120.         X += [0]*(N*N-len(X))
  121.         A = np.array(X).reshape((N,N))
  122.         cmap = colors.ListedColormap(['white', 'red','green','blue','yellow'])
  123.         plt.imshow(A, cmap=cmap, interpolation='nearest')
  124.         plt.show()
  125.  
  126.     output_binary_figure(binary)
  127.     output_figure(day)
  128. except:
  129.     pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement