132 lines
3.3 KiB
Python
132 lines
3.3 KiB
Python
|
#!/bin/python3
|
||
|
import sys,re,collections
|
||
|
|
||
|
from pprint import pprint
|
||
|
sys.path.insert(0, '../../')
|
||
|
from fred import list2int
|
||
|
|
||
|
input_f = 'input'
|
||
|
|
||
|
part = 2
|
||
|
#########################################
|
||
|
# #
|
||
|
# Part 1 #
|
||
|
# #
|
||
|
#########################################
|
||
|
|
||
|
inst = []
|
||
|
|
||
|
programs = 'abcdefghijklmnop'
|
||
|
#programs = 'abcde'
|
||
|
|
||
|
def parse_input(input_str):
|
||
|
pattern = r"^(s(\d+)|x(\d+)/(\d+)|p([a-zA-Z])/([a-zA-Z]))$"
|
||
|
|
||
|
match = re.match(pattern, input_str)
|
||
|
if match:
|
||
|
if match.group(2):
|
||
|
return ('s', int(match.group(2)))
|
||
|
elif match.group(3) and match.group(4):
|
||
|
return ('x', int(match.group(3)), int(match.group(4)))
|
||
|
elif match.group(5) and match.group(6):
|
||
|
return ('p', match.group(5), match.group(6))
|
||
|
return None
|
||
|
|
||
|
def swap_pos(d, index1, index2):
|
||
|
if not (0 <= index1 < len(d)) or not (0 <= index2 < len(d)):
|
||
|
raise IndexError("Index out of range")
|
||
|
d[index1], d[index2] = d[index2], d[index1]
|
||
|
|
||
|
def swap_items(d, item1, item2):
|
||
|
try:
|
||
|
index1 = d.index(item1)
|
||
|
index2 = d.index(item2)
|
||
|
|
||
|
d[index1], d[index2] = d[index2], d[index1]
|
||
|
except ValueError as e:
|
||
|
raise ValueError(f"One or both items not found in deque: {e}")
|
||
|
|
||
|
if part == 1:
|
||
|
with open(input_f) as file:
|
||
|
for line in file:
|
||
|
instructions = line.rstrip().split(',')
|
||
|
|
||
|
programs = collections.deque(list(programs))
|
||
|
|
||
|
#print(programs)
|
||
|
#print(instructions)
|
||
|
print(len(instructions))
|
||
|
|
||
|
for idx,i in enumerate(instructions):
|
||
|
inst = parse_input(i)
|
||
|
print(idx,end=' ')
|
||
|
#print(i)
|
||
|
#rint(inst)
|
||
|
if inst[0] == 's':
|
||
|
#print('Spin', i)
|
||
|
programs.rotate(inst[1])
|
||
|
|
||
|
elif inst[0] == 'x':
|
||
|
#print('Exchange',i)
|
||
|
swap_pos(programs,inst[1],inst[2])
|
||
|
|
||
|
elif inst[0] == 'p':
|
||
|
#print('Partner',i)
|
||
|
swap_items(programs,inst[1],inst[2])
|
||
|
else:
|
||
|
print(inst)
|
||
|
input()
|
||
|
|
||
|
|
||
|
|
||
|
for i in programs:
|
||
|
print(i,end='')
|
||
|
print()
|
||
|
|
||
|
|
||
|
#########################################
|
||
|
# #
|
||
|
# Part 2 #
|
||
|
# #
|
||
|
#########################################
|
||
|
if part == 2:
|
||
|
|
||
|
|
||
|
with open(input_f) as file:
|
||
|
for line in file:
|
||
|
instructions = line.rstrip().split(',')
|
||
|
|
||
|
programs = collections.deque(list(programs))
|
||
|
|
||
|
#print(programs)
|
||
|
#print(instructions)
|
||
|
print(len(instructions))
|
||
|
|
||
|
for r in range(0,1000000000):
|
||
|
for idx,i in enumerate(instructions):
|
||
|
inst = parse_input(i)
|
||
|
#print(idx,end=' ')
|
||
|
#print(i)
|
||
|
#rint(inst)
|
||
|
if inst[0] == 's':
|
||
|
#print('Spin', i)
|
||
|
programs.rotate(inst[1])
|
||
|
|
||
|
elif inst[0] == 'x':
|
||
|
#print('Exchange',i)
|
||
|
swap_pos(programs,inst[1],inst[2])
|
||
|
|
||
|
elif inst[0] == 'p':
|
||
|
#print('Partner',i)
|
||
|
swap_items(programs,inst[1],inst[2])
|
||
|
else:
|
||
|
print(inst)
|
||
|
input()
|
||
|
if r % 10000 == 0:
|
||
|
print(r)
|
||
|
|
||
|
|
||
|
|
||
|
for i in programs:
|
||
|
print(i,end='')
|
||
|
print()
|