summaryrefslogtreecommitdiff
path: root/5_2022.py
diff options
context:
space:
mode:
Diffstat (limited to '5_2022.py')
-rw-r--r--5_2022.py39
1 files changed, 39 insertions, 0 deletions
diff --git a/5_2022.py b/5_2022.py
new file mode 100644
index 0000000..4958c9a
--- /dev/null
+++ b/5_2022.py
@@ -0,0 +1,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]))