-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday21.py
executable file
·65 lines (52 loc) · 1.89 KB
/
day21.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from itertools import permutations
with open('day21_input.txt') as f:
steps = f.read().split('\n')
def password(pword, steps):
pword = list(pword)
for step in steps:
if step.startswith('rotate'):
pword = rota(pword, step)
elif step.startswith('swap'):
pword = swap(pword, step)
elif step.startswith('reverse'):
pword = reverse(pword, step)
elif step.startswith('move'):
pword = move(pword, step)
return ''.join(pword)
def rota(pword, step):
if step[7] == 'b':
n = pword.index(step[-1])
if n >= 4:
n += 2
else:
n += 1
n = -(n % len(pword))
elif step[7] == 'r':
n = -(int(step[13]))
elif step[7] == 'l':
n = int(step[12])
else:
raise IndexError
return pword[n:] + pword[:n]
def swap(pword, step):
if step[5] == 'p':
A, B = int(step[14]), int(step[-1])
pword[A], pword[B] = pword[B], pword[A]
else: #swap letter
A, B = pword.index(step[12]), pword.index(step[-1])
pword[A], pword[B] = step[-1], step[12]
return pword
def reverse(pword, step):
X, Y = int(step[18]), int(step[-1])+1
sub = pword[X: Y]
sub = sub[::-1]
pword[X:Y] = sub
return pword
def move(pword, step):
X, Y = int(step[14]), int(step[-1])
pword.insert(Y, pword.pop(X))
return pword
test = ['swap position 4 with position 0', 'swap letter d with letter b', 'reverse positions 0 through 4', 'rotate left 1 step', 'move position 1 to position 4', 'move position 3 to position 0', 'rotate based on position of letter b', 'rotate based on position of letter d']
#print(password('abcde', test))
#print(password('abcdefgh', steps))
print({''.join(p) for p in permutations('abcdefgh') if password(p, steps) == 'fbgdceah'})