-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathd20.py
54 lines (51 loc) · 1.6 KB
/
d20.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
inp = open('input_d20.txt').read().strip()
init = 'abcdefgh'
#init = 'dgfaehcb'
def rotate(st,n=1):
for _ in range(n):
st = st[-1] + st[:-1]
return st
def rotateleft(st,n=1):
for _ in range(n):
st = st[1:] + st[0]
return st
def swap(x,y):
global init
c = list(init)
c[x],c[y]=c[y],c[x]
init = ''.join(c)
for l in inp.split('\n'):
print(l)
print(init)
if l.startswith('swap position'):
x = int(l.split('swap position ')[1].split(' with')[0])
y = int(l.split('with position ')[1])
swap(x,y)
elif l.startswith('swap letter'):
x = l.split('swap letter ')[1].split(' with')[0]
y = l.split('with letter ')[1]
xi = init.index(x)
yi = init.index(y)
swap(xi,yi)
elif l.startswith('rotate based'):
x = l.split('letter ')[1]
xi = init.index(x)
init = rotate(init, 1+xi+(0 if xi < 4 else 1))
elif l.startswith('rotate'):
if 'left' in l:
x = int(l.split('left ')[1].split(' step')[0])
init = rotateleft(init,x)
else:
x = int(l.split('right ')[1].split(' step')[0])
init = rotate(init,x)
elif l.startswith('reverse '):
x = int(l.split('positions ')[1].split(' through')[0])
y = int(l.split('through ')[1])
init = init[:x] + init[x:y+1][::-1] + init[y+1:]
elif l.startswith('move '):
x = int(l.split('position ')[1].split(' to')[0])
y = int(l.split('to position ')[1])
tm = init[x]
init = init[:x] + init[x+1:]
init = init[:y] + tm + init[y:]
print(init)