-
Notifications
You must be signed in to change notification settings - Fork 0
/
day21.py
98 lines (82 loc) · 2.81 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import os
import get_input
def shift_left(seq, n):
n %= len(seq)
return seq[n:] + seq[:n]
def shift_right(seq, n):
n %= len(seq)
return seq[-n:] + seq[:-n]
day = int(os.path.basename(__file__).replace('day', '').replace('.py', ''))
data = get_input.get_input(day)
password = list('abcdefgh')
lines = data.split('\n')
for line in lines:
parts = line.split(' ')
if parts[0] == 'swap':
if parts[1] == 'position':
i1 = int(parts[2])
i2 = int(parts[5])
password[i1], password[i2] = password[i2], password[i1]
elif parts[1] == 'letter':
l1 = parts[2]
l2 = parts[5]
i1 = password.index(l1)
i2 = password.index(l2)
password[i1], password[i2] = password[i2], password[i1]
elif parts[0] == 'rotate':
if parts[1] == 'left':
password = shift_left(password, int(parts[2]))
elif parts[1] == 'right':
password = shift_right(password, int(parts[2]))
elif parts[1] == 'based':
i = password.index(parts[6])
if i < 4:
r = 1 + i
else:
r = 2 + i
password = shift_right(password, r)
elif parts[0] == 'reverse':
i1 = int(parts[2])
i2 = int(parts[4])+1
password[i1:i2] = reversed(password[i1:i2])
elif parts[0] == 'move':
i1 = int(parts[2])
i2 = int(parts[5])
letter = password.pop(i1)
password.insert(i2, letter)
print('Scrambled password: {}'.format(''.join(password)))
password = list('fbgdceah')
# Magic on paper
reverse_based = [1, 1, 6, 2, 7, 3, 0, 4]
for line in lines[::-1]:
parts = line.split(' ')
if parts[0] == 'swap':
if parts[1] == 'position':
i1 = int(parts[2])
i2 = int(parts[5])
password[i1], password[i2] = password[i2], password[i1]
elif parts[1] == 'letter':
l1 = parts[2]
l2 = parts[5]
i1 = password.index(l1)
i2 = password.index(l2)
password[i1], password[i2] = password[i2], password[i1]
elif parts[0] == 'rotate':
if parts[1] == 'left':
password = shift_right(password, int(parts[2]))
elif parts[1] == 'right':
password = shift_left(password, int(parts[2]))
elif parts[1] == 'based':
i = password.index(parts[6])
r = reverse_based[i]
password = shift_left(password, r)
elif parts[0] == 'reverse':
i1 = int(parts[2])
i2 = int(parts[4])+1
password[i1:i2] = reversed(password[i1:i2])
elif parts[0] == 'move':
i1 = int(parts[2])
i2 = int(parts[5])
letter = password.pop(i2)
password.insert(i1, letter)
print('Decrambled password: {}'.format(''.join(password)))