AdventOfCode/2017/18/solution.py

100 lines
2.2 KiB
Python
Raw Normal View History

2024-11-26 19:42:34 +01:00
#!/bin/python3
import sys,re
from pprint import pprint
sys.path.insert(0, '../../')
from fred import list2int
input_f = 'test'
part = 1
#########################################
# #
# Part 1 #
# #
#########################################
def parse_input(input_str):
2024-11-27 06:33:28 +01:00
pattern = r"^([a-zA-Z]{3})\s{1}(\w)?\s?(-?\w+)$"
2024-11-26 19:42:34 +01:00
match = re.match(pattern, input_str)
if match:
if match.group(2):
2024-11-27 06:33:28 +01:00
return match.group(1),match.group(2),match.group(3)
else:
return match.group(1),match.group(3)
2024-11-26 19:42:34 +01:00
return None
2024-11-27 06:33:28 +01:00
instructions = []
sets = {}
last_sound = 0
2024-11-26 19:42:34 +01:00
if part == 1:
with open(input_f) as file:
for line in file:
2024-11-27 06:33:28 +01:00
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
2024-11-26 19:42:34 +01:00
2024-11-27 06:33:28 +01:00
print(x,sets,last_sound)
input()
#print(programs)
#print(instructions)
#print(len(instructions))
#for idx,i in enumerate(instructions):
2024-11-26 19:42:34 +01:00
#########################################
# #
# Part 2 #
# #
#########################################
if part == 2:
exit()