#!/bin/python3 import sys,time,re from sympy import * from pprint import pprint sys.path.insert(0, '../../') from fred import list2int,get_re,nprint,lprint,loadFile start_time = time.time() input_f = 'input' ######################################### # # # Part 1 # # # ######################################### def part1(): instructions = [] with open(input_f) as file: for line in file: match = get_re(r".*X[\+|=](\d+), Y[\+|=](\d+)",line.rstrip()) if match: instructions.append(list2int([match.group(1),match.group(2)])) #print(instructions) a_cost = 3 b_cost = 1 a = () b = () r = () x,y = symbols('x y',integer=True) result = 0 for idx, inst in enumerate(instructions): #print(inst) if idx%3 == 0: a = (inst[0],inst[1]) elif idx%3 == 1: b = (inst[0],inst[1]) elif idx%3 == 2: r = (inst[0],inst[1]) eq1 = Eq(a[0]*x + b[0]*y,r[0]) eq2 = Eq(a[1]*x + b[1]*y,r[1]) s = solve((eq1,eq2),(x,y)) #print(s) if s: result += s[x]*a_cost result += s[y]*b_cost #input() return result start_time = time.time() print('Part 1:',part1(), '\t\t', round((time.time() - start_time)*1000), 'ms') ######################################### # # # Part 2 # # # ######################################### def part1(): return start_time = time.time() print('Part 2:',part1(), '\t\t', round((time.time() - start_time)*1000), 'ms')