2024-11-25 20:17:15 +01:00
|
|
|
#!/bin/python3
|
|
|
|
import sys,re,collections
|
|
|
|
|
|
|
|
from pprint import pprint
|
2024-11-26 19:42:34 +01:00
|
|
|
|
2024-11-25 20:17:15 +01:00
|
|
|
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:
|
|
|
|
|
2024-11-26 19:42:34 +01:00
|
|
|
start_value = list('abcdefghijklmnop')
|
2024-11-25 20:17:15 +01:00
|
|
|
|
|
|
|
with open(input_f) as file:
|
|
|
|
for line in file:
|
|
|
|
instructions = line.rstrip().split(',')
|
|
|
|
|
|
|
|
programs = collections.deque(list(programs))
|
|
|
|
|
|
|
|
#print(programs)
|
|
|
|
#print(instructions)
|
2024-11-26 19:42:34 +01:00
|
|
|
#print(len(instructions))
|
|
|
|
|
|
|
|
duplicates = []
|
|
|
|
done = False
|
|
|
|
indx = 0
|
|
|
|
while not done:
|
|
|
|
#print(r,list(programs))
|
|
|
|
#input()
|
2024-11-25 20:17:15 +01:00
|
|
|
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()
|
2024-11-26 19:42:34 +01:00
|
|
|
|
|
|
|
|
|
|
|
if start_value == list(programs):
|
|
|
|
#print(start_value)
|
|
|
|
#print(list(programs))
|
|
|
|
done = True
|
|
|
|
duplicates.append(list(programs))
|
2024-11-25 20:17:15 +01:00
|
|
|
|
2024-11-26 19:42:34 +01:00
|
|
|
indx += 1
|
2024-11-25 20:17:15 +01:00
|
|
|
|
|
|
|
|
2024-11-26 19:42:34 +01:00
|
|
|
print(''.join(duplicates[(1000000000%indx)-1]))
|