AdventOfCode/2017/23/solution.py

85 lines
1.8 KiB
Python

#!/bin/python3
import sys,re
from pprint import pprint
sys.path.insert(0, '../../')
from fred import list2int
input_f = 'input'
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
def sets_return(x,Sets):
if x.isalpha():
return Sets[x]
return int(x)
if part == 1:
instructions = []
Sets = {
'a': 1,
'b': 0,
'c': 0,
'd': 0,
'e': 0,
'f': 0,
'g': 0,
'h': 0
}
count = 0
with open(input_f) as file:
for line in file:
instructions.append(list(parse_input(line.rstrip())))
intr = 0
while 0 <= intr < len(instructions):
i = instructions[intr]
x = i[1]
y = i[2]
if i[0] == 'set':
Sets[x] = sets_return(y,Sets)
elif i[0] == 'sub':
Sets[x] -= sets_return(y,Sets)
elif i[0] == 'mul':
Sets[x] *= sets_return(y,Sets)
count += 1
elif i[0] == 'jnz':
if sets_return(x,Sets) != 0:
intr += sets_return(y,Sets)-1
intr += 1
print(count)
#########################################
# #
# Part 2 #
# #
#########################################
if part == 2:
exit()