#!/usr/bin/env python3 import sys, re from sympy import solve from sympy.abc import x, y def prizes(a, b, prize): sols = solve([a[0] * x + b[0] * y - prize[0], a[1] * x + b[1] * y - prize[1]], x, y, dict=True) if len(sols) > 1: print('help!', sols) if int(sols[0][x]) != sols[0][x] or int(sols[0][y]) != sols[0][y]: return 0 else: return 3*sols[0][x]+sols[0][y] if __name__ == '__main__': cms = open(sys.argv[1]).read().split('\n\n') res1, res2 = 0, 0 for cm in cms: g = re.search(r'Button A: X\+(?P\d+), Y\+(?P\d+)\nButton B: X\+(?P\d+), Y\+(?P\d+)\nPrize: X=(?P\d+), Y=(?P\d+)', cm) button_a = (int(g['xa']), int(g['ya'])) button_b = (int(g['xb']), int(g['yb'])) prize = (int(g['px']), int(g['py'])) res1 += prizes(button_a, button_b, prize) prize = (prize[0]+10000000000000, prize[1]+10000000000000) res2 += prizes(button_a, button_b, prize) # challenge 1 print(f"challenge 1:\n{res1}\n") # challenge 2 print(f"challenge 2:\n{res2}")