Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re
- import itertools
- password = 'abcdefgh'
- def swapXY(s, x, y):
- tmp_a = s[x]
- tmp_b = s[y]
- if x<y:
- return s[:x] + tmp_b + s[x+1:y] + tmp_a + s[y+1:]
- else:
- return s[:y] + tmp_a + s[y+1:x] + tmp_b + s[x+1:]
- def swapAB(s, a, b):
- tmp_a = s.index(a)
- tmp_b = s.index(b)
- if tmp_a<tmp_b:
- return s[:tmp_a] + s[tmp_b] + s[tmp_a+1:tmp_b] + s[tmp_a] + s[tmp_b+1:]
- else:
- return s[:tmp_b] + s[tmp_a] + s[tmp_b+1:tmp_a] + s[tmp_b] + s[tmp_a+1:]
- def rotateL(s, c):
- return s[c:] + s[:c]
- def rotateR(s, c):
- return s[-c:] + s[:-c]
- def rotateA(s, a):
- tmp = s.index(a)
- if tmp >= 4:
- tmp += 2
- else:
- tmp += 1
- tmp %= len(s)
- return s[-tmp:] + s[:-tmp]
- def reverseXY(s, a, b):
- return s[:a] + ''.join(reversed(s[a:b+1])) + s[b+1:]
- def moveXY(s, a, b):
- if b<a:
- return s[:b] + s[a] + s[b:a] + s[a+1:]
- else:
- return s[:a] + s[a+1:b+1] + s[a] +s[b+1:]
- data = open('advent_password.txt').readlines()
- A = 0
- B = 0
- a = ''
- b = ''
- for x in itertools.permutations(password):
- password = ''.join(x)
- for line in data:
- if line.startswith('rotate left'):
- A = re.findall(r'(\d+)', line)[0]
- password = rotateL(password,int(A))
- if line.startswith('rotate right'):
- A = re.findall(r'(\d+)', line)[0]
- password = rotateR(password,int(A))
- if line.startswith('rotate based on position of '):
- a = re.findall(r'letter (\w+)', line)[0]
- password = rotateA(password,a)
- if line.startswith('swap letter '):
- a, b = re.findall(r'letter (\w+)', line)
- password = swapAB(password,a, b)
- if line.startswith('swap position '):
- A,B = map(int, re.findall(r'position (\d+)', line))
- password = swapXY(password,A, B)
- if line.startswith('reverse positions '):
- A, B = map(int, re.findall(r'(\d+) through (\d+)', line)[0])
- password = reverseXY(password,A, B)
- if line.startswith('move position '):
- A,B = map(int, re.findall(r'position (\d+)', line))
- password = moveXY(password,A, B)
- if password == 'fbgdceah':
- print(''.join(x))
- break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement