#!/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 in Sets: value = Sets[x] if isinstance(value, str) and not x.lstrip('-').isdigit(): return sets_return(value) return value elif x.isdigit() or x.lstrip('-').isdigit(): return int(x) else: return None if part == 1: instructions = [] Sets = {} count = 0 with open(input_f) as file: for line in file: instructions.append(list(parse_input(line.rstrip()))) x = 0 print(len(instructions)) while True: i = instructions[x] print(i) if isinstance(i[1], str): if i[1] not in Sets: Sets[i[1]] = None if i[0] == 'set': Sets[i[1]] = sets_return(i[2],Sets) x += 1 elif i[0] == 'sub': #print(sets_return(i[2],Sets)) Sets[i[1]] -= sets_return(i[2],Sets) x += 1 elif i[0] == 'mul': Sets[i[1]] *= sets_return(i[2],Sets) x += 1 count += 1 elif i[0] == 'jnz': #tmp = sets_return(i[1],Sets) #print(type(tmp),tmp) if sets_return(i[1],Sets) != 0: x += sets_return(i[2],Sets) if x > len(instructions) or x < 0: exit() print(x,i) print(Sets) input() #else: # x += 1 # print(x) # input() # elif i[0] == 'mod': # Sets[i[1]] %= sets_return(i[2],Sets) # x += 1 # elif i[0] == 'snd': # last_sound = sets_return(i[1],Sets) # x += 1 # elif i[0] == 'rcv': # if sets_return(i[1]) != 0: # Sets[i[1]] = sets_return(i[1],Sets) # #print(last_sound) # exit() # x += 1 print(count) ######################################### # # # Part 2 # # # ######################################### if part == 2: exit()