Solve 2016 day 21.

This commit is contained in:
felixm 2024-05-18 21:36:55 -04:00
parent a49eb9e8cb
commit 90b2134736
2 changed files with 68 additions and 1 deletions

66
2016/d21.py Normal file
View File

@ -0,0 +1,66 @@
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()

View File

@ -53,7 +53,8 @@ written in Python.
- Day 18: 9:43
- Day 19: 90:00 that wasn't easy for me
- Day 20: 67:00
- Day 21:
- Day 21: 32:33
- Day 22:
# 2017