#!/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)])) a_cost = 3 b_cost = 1 a = () b = () r = () x,y = symbols('x y',integer=True) result = 0 for idx, inst in enumerate(instructions): 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)) if s: result += s[x]*a_cost result += s[y]*b_cost return result start_time = time.time() #rint('Part 1:',part1(), '\t\t', round((time.time() - start_time)*1000), 'ms') ######################################### # # # Part 2 # # # ######################################### def part2(): 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)])) a_cost = 3 b_cost = 1 a = () b = () r = () x,y = symbols('x y',integer=True) result = 0 for idx, inst in enumerate(instructions): 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]+10000000000000,inst[1]+10000000000000) 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)) if s: result += s[x]*a_cost result += s[y]*b_cost #input() return result start_time = time.time() start_time = time.time() print('Part 2:',part2(), '\t\t', round((time.time() - start_time)*1000), 'ms')