Advertisement
Guest User

Untitled

a guest
Dec 21st, 2016
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.58 KB | None | 0 0
  1.  
  2.  
  3.     import re
  4.     import itertools
  5.  
  6.     password = 'abcdefgh'
  7.  
  8.     def swapXY(s, x, y):
  9.         tmp_a = s[x]
  10.         tmp_b = s[y]
  11.  
  12.         if x<y:
  13.             return s[:x] + tmp_b + s[x+1:y] + tmp_a + s[y+1:]
  14.         else:
  15.             return s[:y] + tmp_a + s[y+1:x] + tmp_b + s[x+1:]
  16.  
  17.     def swapAB(s, a, b):
  18.         tmp_a = s.index(a)
  19.         tmp_b = s.index(b)
  20.         if tmp_a<tmp_b:
  21.             return s[:tmp_a] + s[tmp_b] + s[tmp_a+1:tmp_b] + s[tmp_a] + s[tmp_b+1:]
  22.         else:
  23.             return s[:tmp_b] + s[tmp_a] + s[tmp_b+1:tmp_a] + s[tmp_b] + s[tmp_a+1:]
  24.            
  25.     def rotateL(s, c):
  26.         return s[c:] + s[:c]
  27.  
  28.     def rotateR(s, c):
  29.         return s[-c:] + s[:-c]
  30.  
  31.     def rotateA(s, a):
  32.         tmp = s.index(a)
  33.         if tmp >= 4:
  34.             tmp += 2
  35.         else:
  36.             tmp += 1
  37.         tmp %= len(s)    
  38.         return s[-tmp:] + s[:-tmp]
  39.  
  40.     def reverseXY(s, a, b):
  41.         return s[:a] + ''.join(reversed(s[a:b+1])) + s[b+1:]
  42.  
  43.     def moveXY(s, a, b):
  44.         if b<a:
  45.             return s[:b] + s[a] + s[b:a] + s[a+1:]
  46.         else:
  47.             return s[:a] + s[a+1:b+1] + s[a] +s[b+1:]
  48.  
  49.  
  50.     data = open('advent_password.txt').readlines()
  51.  
  52.     A = 0
  53.     B = 0
  54.     a = ''
  55.     b = ''
  56.  
  57.     for x in itertools.permutations(password):
  58.         password = ''.join(x)
  59.         for line in data:
  60.             if line.startswith('rotate left'):
  61.                 A = re.findall(r'(\d+)', line)[0]
  62.                 password = rotateL(password,int(A))
  63.             if line.startswith('rotate right'):
  64.                 A = re.findall(r'(\d+)', line)[0]
  65.                 password = rotateR(password,int(A))
  66.             if line.startswith('rotate based on position of '):
  67.                 a = re.findall(r'letter (\w+)', line)[0]
  68.                 password = rotateA(password,a)
  69.             if line.startswith('swap letter '):
  70.                 a, b = re.findall(r'letter (\w+)', line)
  71.                 password = swapAB(password,a, b)
  72.             if line.startswith('swap position '):
  73.                 A,B = map(int, re.findall(r'position (\d+)', line))
  74.                 password = swapXY(password,A, B)
  75.             if line.startswith('reverse positions '):
  76.                 A, B = map(int, re.findall(r'(\d+) through (\d+)', line)[0])
  77.                 password = reverseXY(password,A, B)
  78.             if line.startswith('move position '):
  79.                 A,B = map(int, re.findall(r'position (\d+)', line))
  80.                 password = moveXY(password,A, B)
  81.         if password == 'fbgdceah':
  82.             print(''.join(x))
  83.             break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement