61 lines
1.3 KiB
Python
61 lines
1.3 KiB
Python
|
from lib import LETTERS_LOWER, str_to_int, str_to_ints
|
||
|
|
||
|
|
||
|
def one_cycle(xs, insts):
|
||
|
for inst in insts:
|
||
|
if inst.startswith("s"):
|
||
|
v = str_to_int(inst)
|
||
|
if v != 0:
|
||
|
xs = xs[-v:] + xs[:len(xs) - v]
|
||
|
elif inst.startswith("x"):
|
||
|
a, b = str_to_ints(inst)
|
||
|
xs[a], xs[b] = xs[b], xs[a]
|
||
|
elif inst.startswith("p"):
|
||
|
la, lb = inst[1], inst[3]
|
||
|
a, b = xs.index(la), xs.index(lb)
|
||
|
xs[a], xs[b] = xs[b], xs[a]
|
||
|
else:
|
||
|
assert False
|
||
|
return xs
|
||
|
|
||
|
|
||
|
def part_1(data):
|
||
|
xs = list(LETTERS_LOWER[:16])
|
||
|
insts = data.split(",")
|
||
|
xs = one_cycle(xs, insts)
|
||
|
r = "".join(xs)
|
||
|
print(r)
|
||
|
|
||
|
|
||
|
def part_2(data):
|
||
|
repeat = 10**9
|
||
|
xs = list(LETTERS_LOWER[:16])
|
||
|
insts = data.split(",")
|
||
|
cycle = 0
|
||
|
seen = {}
|
||
|
for i in range(repeat):
|
||
|
xs = one_cycle(xs, insts)
|
||
|
xst = tuple(xs)
|
||
|
if not xst in seen:
|
||
|
seen[xst] = i
|
||
|
else:
|
||
|
cycle = (i - seen[xst])
|
||
|
break
|
||
|
|
||
|
xs = list(LETTERS_LOWER[:16])
|
||
|
repeat %= cycle
|
||
|
for i in range(repeat):
|
||
|
xs = one_cycle(xs, insts)
|
||
|
r = "".join(xs)
|
||
|
print(r)
|
||
|
|
||
|
|
||
|
def main():
|
||
|
data = open(0).read().strip()
|
||
|
part_1(data)
|
||
|
part_2(data)
|
||
|
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
main()
|