Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- import math
- import re
- def getspiral(length):
- spiral = [[-1 for j in range(0,length)] for k in range(0,length)]
- k = 0
- c1 = 0
- c2 = length-1
- r1 = 0
- r2 = length-1
- while k < length * length:
- i = c1
- while i < c2+1:
- spiral[r1][i] = k
- k += 1
- i += 1
- j = r1+1
- while j < r2+1:
- spiral[j][c2] = k
- k += 1
- j += 1
- i = c2-1
- while i > c1-1:
- spiral[r2][i] = k
- k += 1
- i -= 1
- j = r2-1
- while j > r1:
- spiral[j][c1] = k
- k += 1
- j -= 1
- c1 += 1
- c2 -= 1
- r1 += 1
- r2 -= 1
- return spiral
- def getzigzag(length):
- if length == 1:
- return {0:(0,0)}
- zigzag = [[0 for j in range(0,length)] for k in range(0,length)]
- coord = (length-1,0)
- ind = 0
- while coord != (1,length):
- try:
- if coord[0] >= 0 and coord[1] >= 0:
- zigzag[coord[0]][coord[1]] = ind
- ind += 1
- except IndexError:
- nothing = 0
- if coord[0] == length-1:
- coord = (length-2-coord[1],0)
- else:
- coord = (coord[0]+1,coord[1]+1)
- return zigzag
- def getperm(key,length):
- msg = range(0,length)
- levels = []
- spiral = getspiral(key)
- zigzag = getzigzag(key)
- dictzigzag = {}
- for i in range(0,key):
- for j in range(0,key):
- dictzigzag[zigzag[i][j]] = (i,j)
- for l in range(0,int(math.ceil(float(len(msg))/(key*key)))):
- level = [['' for j in range(0,key)] for k in range(0,key)]
- for x in range(0,key):
- for y in range(0,key):
- try:
- if l % 2 == 0:
- level[x][y] = msg[l*key*key+spiral[x][y]]
- else:
- level[x][y] = msg[l*key*key+(key*key-1-spiral[x][y])]
- except IndexError:
- level[x][y] = -1
- levels.append(level)
- enc = []
- for i in range(0,key*key):
- for x in range(0,len(levels))[::-1]:
- toappend = levels[x][dictzigzag[i][0]][dictzigzag[i][1]]
- if toappend != -1:
- enc.append(toappend)
- return enc
- f = open("input","r").read().strip()
- mappings = []
- length = len(f)
- for i in range(1,len(f)):
- perm = getperm(i,length)
- mapping = {}
- for j in range(0,length):
- mapping[perm[j]] = j
- mappings.append(mapping)
- if i > 10:
- map1 = mappings[i-11]
- map2 = mappings[i-6]
- map3 = mappings[i-1]
- enc = ''
- for c in range(0,length):
- enc += f[map3[map2[map1[c]]]]
- if "l"+"a"+"s"+"a"+"c"+"t"+"f" in enc:
- print re.search("l"+"a"+"s"+"a"+"c"+"t"+"f"+"{.*?}",enc).group(0)
- break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement