AdventOfCode/2017/18/solution.py

110 lines
2.5 KiB
Python

#!/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):
pattern = r"^([a-zA-Z]{3})\s{1}(\w)?\s?(-?\w+)$"
match = re.match(pattern, input_str)
if match:
if match.group(2):
return match.group(1),match.group(2),match.group(3)
else:
return match.group(1),match.group(3)
return None
instructions = []
sets = {}
last_sound = 0
if part == 1:
with open(input_f) as 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':
if isinstance(i[2], str):
sets[i[1]] = int(i[2])
else:
sets[i[1]] = int(sets[i[2]])
x += 1
elif i[0] == 'add':
sets[i[1]] = int(sets[i[1]]) + int(i[2])
x += 1
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])
x += 1
elif i[0] == 'mod':
sets[i[1]] = int(sets[i[1]]) % int(i[2])
x += 1
elif i[0] == 'snd':
last_sound = sets[i[1]]
x += 1
elif i[0] == 'rcv':
if sets[i[1]] != 0:
sets[i[1]] = int(sets[i[1]])
x += 1
else:
print('skipped')
x += 1
elif i[0] == 'jgz': #this is the one that fails
if isinstance(i[2], str):
if sets[i[1]] > 0:
x += int(sets[i[2]])
else:
x += int(i[2])
else:
x += 1
print(x,sets,last_sound)
input()
#print(programs)
#print(instructions)
#print(len(instructions))
#for idx,i in enumerate(instructions):
#########################################
# #
# Part 2 #
# #
#########################################
if part == 2:
exit()