Advertisement
Nickpips

Spiral

Mar 22nd, 2016 (edited)
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.02 KB | None | 0 0
  1. #!/usr/bin/python
  2. import math
  3. import re
  4.  
  5. def getspiral(length):
  6.     spiral = [[-1 for j in range(0,length)] for k in range(0,length)]
  7.    
  8.     k = 0
  9.     c1 = 0
  10.     c2 = length-1
  11.     r1 = 0
  12.     r2 = length-1
  13.    
  14.     while k < length * length:
  15.         i = c1
  16.         while i < c2+1:
  17.             spiral[r1][i] = k
  18.             k += 1
  19.             i += 1
  20.         j = r1+1
  21.         while j < r2+1:
  22.             spiral[j][c2] = k
  23.             k += 1
  24.             j += 1
  25.         i = c2-1
  26.         while i > c1-1:
  27.             spiral[r2][i] = k
  28.             k += 1
  29.             i -= 1
  30.         j = r2-1
  31.         while j > r1:
  32.             spiral[j][c1] = k
  33.             k += 1
  34.             j -= 1
  35.            
  36.         c1 += 1
  37.         c2 -= 1
  38.         r1 += 1
  39.         r2 -= 1
  40.        
  41.     return spiral
  42.    
  43. def getzigzag(length):
  44.     if length == 1:
  45.         return {0:(0,0)}
  46.     zigzag = [[0 for j in range(0,length)] for k in range(0,length)]
  47.    
  48.     coord = (length-1,0)
  49.    
  50.     ind = 0
  51.    
  52.     while coord != (1,length):
  53.         try:
  54.             if coord[0] >= 0 and coord[1] >= 0:
  55.                 zigzag[coord[0]][coord[1]] = ind
  56.                 ind += 1
  57.         except IndexError:
  58.             nothing = 0
  59.         if coord[0] == length-1:
  60.             coord = (length-2-coord[1],0)
  61.         else:
  62.             coord = (coord[0]+1,coord[1]+1)
  63.    
  64.     return zigzag
  65.  
  66. def getperm(key,length):
  67.     msg = range(0,length)
  68.    
  69.     levels = []
  70.    
  71.     spiral = getspiral(key)
  72.     zigzag = getzigzag(key)
  73.    
  74.     dictzigzag = {}
  75.    
  76.     for i in range(0,key):
  77.         for j in range(0,key):
  78.             dictzigzag[zigzag[i][j]] = (i,j)
  79.    
  80.     for l in range(0,int(math.ceil(float(len(msg))/(key*key)))):
  81.         level = [['' for j in range(0,key)] for k in range(0,key)]
  82.         for x in range(0,key):
  83.             for y in range(0,key):
  84.                 try:
  85.                     if l % 2 == 0:
  86.                         level[x][y] = msg[l*key*key+spiral[x][y]]
  87.                     else:
  88.                         level[x][y] = msg[l*key*key+(key*key-1-spiral[x][y])]
  89.                 except IndexError:
  90.                     level[x][y] = -1
  91.         levels.append(level)
  92.    
  93.     enc = []
  94.    
  95.     for i in range(0,key*key):
  96.         for x in range(0,len(levels))[::-1]:
  97.             toappend = levels[x][dictzigzag[i][0]][dictzigzag[i][1]]
  98.             if toappend != -1:
  99.                 enc.append(toappend)
  100.    
  101.     return enc
  102.  
  103. f = open("input","r").read().strip()
  104.  
  105. mappings = []
  106. length = len(f)
  107.  
  108. for i in range(1,len(f)):
  109.     perm = getperm(i,length)
  110.     mapping = {}
  111.     for j in range(0,length):
  112.         mapping[perm[j]] = j
  113.     mappings.append(mapping)
  114.     if i > 10:
  115.         map1 = mappings[i-11]
  116.         map2 = mappings[i-6]
  117.         map3 = mappings[i-1]
  118.         enc = ''
  119.         for c in range(0,length):
  120.             enc += f[map3[map2[map1[c]]]]
  121.         if "l"+"a"+"s"+"a"+"c"+"t"+"f" in enc:
  122.             print re.search("l"+"a"+"s"+"a"+"c"+"t"+"f"+"{.*?}",enc).group(0)
  123.             break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement