67 lines
2.1 KiB
Python
67 lines
2.1 KiB
Python
from itertools import permutations
|
|
|
|
|
|
def part_1(data, text):
|
|
text = list(text)
|
|
for line in data.splitlines():
|
|
words = line.split()
|
|
# print(text)
|
|
# print(words)
|
|
match words:
|
|
case ["move", "position", a, "to", "position", b]:
|
|
l = text.pop(int(a))
|
|
text.insert(int(b), l)
|
|
case ["rotate", "right", a, "steps"] | ["rotate", "right", a, "step"]:
|
|
a = int(a)
|
|
ntext = list(text)
|
|
for i in range(len(text)):
|
|
ntext[(i + a) % len(text)] = text[i]
|
|
text = ntext
|
|
case ["rotate", "left", a, "steps"] | ["rotate", "left", a, "step"]:
|
|
a = int(a)
|
|
ntext = list(text)
|
|
for i in range(len(text)):
|
|
ntext[(i - a) % len(text)] = text[i % len(text)]
|
|
text = ntext
|
|
case ["swap", "letter", a, "with", "letter", b]:
|
|
ia = text.index(a)
|
|
ib = text.index(b)
|
|
text[ia], text[ib] = text[ib], text[ia]
|
|
case ["swap", "position", a, "with", "position", b]:
|
|
ia, ib = int(a), int(b)
|
|
text[ia], text[ib] = text[ib], text[ia]
|
|
case ["reverse", "positions", a, "through", b]:
|
|
a, b = int(a), int(b)
|
|
assert b > a
|
|
text[a:b + 1] = reversed(text[a:b + 1])
|
|
case ["rotate", "based", "on", "position", "of", "letter", c]:
|
|
i = text.index(c)
|
|
a = 1 + i + (1 if i >= 4 else 0)
|
|
ntext = list(text)
|
|
for i in range(len(text)):
|
|
ntext[(i + a) % len(text)] = text[i % len(text)]
|
|
text = ntext
|
|
case _:
|
|
assert False
|
|
return "".join(text)
|
|
|
|
|
|
def part_2(data):
|
|
pw = "fbgdceah"
|
|
for p in permutations(pw):
|
|
if part_1(data, p) == pw:
|
|
print("".join(p))
|
|
return
|
|
|
|
|
|
def main():
|
|
data = open(0).read().strip()
|
|
print(part_1(data, "abcdefgh"))
|
|
part_2(data)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|
|
|
|
|