Started on 2017/18

This commit is contained in:
FrederikBaerentsen 2024-11-27 06:33:28 +01:00
parent 72a2175d09
commit 68c005577b

View File

@ -14,22 +14,79 @@ part = 1
######################################### #########################################
def parse_input(input_str): def parse_input(input_str):
pattern = r"^(s(\d+)|x(\d+)/(\d+)|p([a-zA-Z])/([a-zA-Z]))$" pattern = r"^([a-zA-Z]{3})\s{1}(\w)?\s?(-?\w+)$"
match = re.match(pattern, input_str) match = re.match(pattern, input_str)
if match: if match:
if match.group(2): if match.group(2):
return ('s', int(match.group(2))) return match.group(1),match.group(2),match.group(3)
elif match.group(3) and match.group(4): else:
return ('x', int(match.group(3)), int(match.group(4))) return match.group(1),match.group(3)
elif match.group(5) and match.group(6):
return ('p', match.group(5), match.group(6))
return None return None
instructions = []
sets = {}
last_sound = 0
if part == 1: if part == 1:
with open(input_f) as file: with open(input_f) as file:
for line in file: for line in file:
instructions.append(list(parse_input(line.rstrip())))
x = 0
while True:
i = instructions[x]
print(i)
if i[0] == 'set':
sets[i[1]] = i[2]
elif i[0] == 'add':
sets[i[1]] = int(sets[i[1]]) + int(i[2])
elif i[0] == 'mul':
if i[2] in sets:
sets[i[1]] = int(sets[i[1]]) * int(sets[i[2]])
else:
sets[i[1]] = int(sets[i[1]]) * int(i[2])
elif i[0] == 'mod':
sets[i[1]] = int(sets[i[1]]) % int(i[2])
elif i[0] == 'snd':
last_sound = sets[i[1]]
elif i[0] == 'rcv':
if sets[i[1]] != 0:
sets[i[1]] = int(sets[i[1]])
else:
print('skipped')
if i[0] == 'jgz':
if isinstance(i[2], str):
if sets[i[1]] > 0:
x += int(sets[i[2]])
else:
x += int(i[2])
else:
x += 1
else:
x += 1
print(x,sets,last_sound)
input()
#print(programs)
#print(instructions)
#print(len(instructions))
#for idx,i in enumerate(instructions):