Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import itertools
- # followed lights along the series, clockwise, starting at left side, top
- # rationelle being that way you have an uninterupted string
- days = [
- 'GBBGGYR YBYBYBR YBBRYYB RRBYGBY GYBBGYR GGBYBBY RYBYGYY',
- 'GGBGGBG BBGGYRR YBBYYRB RBBGGYY BYBBYRR GBYYBYY YGGRBRG',
- 'GRRGGYY GGGBYBY YBYBGYB YYRRBGR GGYBGBB BYGBBYB BBYGGBB',
- 'GBBGGBG GRBBBGG BBRRBRB YYBYYBG BBGGYGG BRRBBYY RBRBBBG GBYBBBY',
- 'GGBGBYG YBGGYRB BRBYGGB GGBRGGB GGBGBRR GRBBYBB RBRBRYY',
- 'GBRYGYB GYRRBBB YRRRBBB GGRRBBR BRRBGGR RGBBYBG YBRBBGG YYBBGYB BRRYYRB RRRRBRR BRYBBRR RRRYRYR Y'
- ]
- 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)]+['+','/']
- b64map = dict(zip(range(64),base64))
- # Our 4 symbols and codes
- symbols = ('R','G','B','Y')
- codes = ['00','01','10','11']
- ints = [1, 2, 3, 4]
- #codes = ['0','1','0','1']
- # split string into groups of n characters
- # http://stackoverflow.com/questions/2231663/slicing-a-list-into-a-list-of-sub-lists
- def grouper(n, iterable, fillvalue=None):
- "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
- args = [iter(iterable)] * n
- return itertools.izip_longest(fillvalue=fillvalue, *args)
- # decode binary string into into 7bit characters
- def seven_bit_decode(bin_string):
- res = []
- for g in grouper(7, bin_string):
- try:
- res.append(chr(int('0'+''.join(g),2)))
- except:
- pass
- return ''.join(res)
- def eight_bit_decode(bin_string):
- res = []
- for g in grouper(8, bin_string):
- try:
- res.append(chr(int(''.join(g),2)))
- except:
- pass
- return ''.join(res)
- def five_bit_decode(bin_string):
- res = []
- for g in grouper(5, bin_string):
- try:
- res.append(chr(97+int(''.join(g),2)))
- except:
- pass
- return ''.join(res)
- # interprets the binary string as
- def six_bit_decode(bin_string):
- res = []
- for g in grouper(6, bin_string):
- try:
- res.append(b64map[int(''.join(g),2)])
- except:
- pass
- return ''.join(res)
- def bincount(s):
- res = {'R':0,'G':0,'B':0,'Y':0}
- for c in s:
- res[c] += 1
- return res
- day = ''.join(days)
- day = day.replace(' ','') #[::-1]
- d = dict(zip(symbols,ints))
- numeric = [d[x] for x in day]
- counts = bincount(day) #np.bincount(np.array(numeric))
- print "Light color distribution"
- print counts
- # get all permutations for the symbols
- for p in itertools.permutations(symbols):
- # create the mapping
- d = dict(zip(p,codes))
- print 'Permutation:',d
- # map string to binary based on our mapping
- binary = reduce(lambda x, y: x.replace(y, d[y]), d, day)
- print len(binary)
- # decode assuming 7 bit ascii
- print '7 bit ascii:',seven_bit_decode(binary)
- print '8 bit ascii:',eight_bit_decode(binary)
- print '6 bit "base64" ascii',six_bit_decode(binary)
- #five_bit_decode(binary)
- print '\n'
- try:
- import matplotlib.pyplot as plt
- from matplotlib import colors
- import numpy as np
- def output_binary_figure(bin_string):
- N = np.ceil(np.sqrt(len(bin_string)))
- X = [int(x) for x in bin_string]
- X += [0]*(N*N-len(X))
- A = np.array(X,dtype=np.bool).reshape((N,N))
- plt.imshow(A, cmap='Greys', interpolation='nearest')
- plt.show()
- def output_figure(s):
- N = np.ceil(np.sqrt(len(s)))
- d = dict(zip(symbols,ints))
- X = [d[x] for x in s]
- X += [0]*(N*N-len(X))
- A = np.array(X).reshape((N,N))
- cmap = colors.ListedColormap(['white', 'red','green','blue','yellow'])
- plt.imshow(A, cmap=cmap, interpolation='nearest')
- plt.show()
- output_binary_figure(binary)
- output_figure(day)
- except:
- pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement