blob: 4958c9ac036087e8bcb1e0b3385c5961e989a3b8 (
plain)
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
|
import sys
from copy import deepcopy
infile = sys.argv[1] if len(sys.argv) > 1 else "5_2022.txt"
data = open(infile).read()
lines = [x for x in data.split("\n")]
i = []
cmds = []
for line in lines:
if line == "":
break
sz = (len(line) + 1) // 4
while len(i) < sz:
i.append([])
for j in range(len(i)):
ch = line[1 + 4 * j]
if ch != " " and "A" <= ch <= "Z":
i[j].append(ch)
p1 = deepcopy(i)
p2 = deepcopy(i)
found = False
for cmd in lines:
if cmd == "":
found = True
continue
if not found:
continue
words = cmd.split()
qty = int(words[1])
frm = int(words[3]) - 1
too = int(words[5]) - 1
for ST, do_rev in [(p1, True), (p2, False)]:
MOVE = ST[frm][:qty]
ST[frm] = ST[frm][qty:]
ST[too] = (list(reversed(MOVE)) if do_rev else MOVE) + ST[too]
print("".join([s[0] for s in p1 if len(s) > 0]))
print("".join([s[0] for s in p2 if len(s) > 0]))
|