Part 1
data = open("Day 13 input.txt", "r").read().strip().replace("Button A: X+", "").replace("Button B: X+", "").replace("Prize: X=", "").replace(", Y+", " ").replace(", Y=", " ").split("\n\n")
total = 0
for i in data:
temp = i.split("\n")
a = map(int, temp[0].split())
b = map(int, temp[1].split())
target = map(int, temp[2].split())
cheapest = None
for apress in range(100):
for bpress in range(100):
if a[0]*apress+b[0]*bpress == target[0] and a[1]*apress+b[1]*bpress == target[1]:
if cheapest == None or apress*3 + bpress < cheapest:
cheapest = apress*3 + bpress
if cheapest != None: total += cheapest
print total
Part 2
data = open("Day 13 input.txt", "r").read().strip().replace("Button A: X+", "").replace("Button B: X+", "").replace("Prize: X=", "").replace(", Y+", " ").replace(", Y=", " ").split("\n\n")
total = 0
for i in data:
temp = i.split("\n")
a = map(int, temp[0].split())
b = map(int, temp[1].split())
target = map(int, temp[2].split())
target[0] += 10000000000000
target[1] += 10000000000000
x = a[0]*(b[0]*target[1] - b[1]*target[0])/(a[1]*b[0] - b[1]*a[0])
apresses = (x/a[0])
bpresses = (target[0]-x)/b[0]
cost = apresses*3 + bpresses
if a[0]*apresses + b[0]*bpresses == target[0] and a[1]*apresses + b[1]*bpresses == target[1]:
total += cost
print total
Slightly washed, removed redundancies in the math. This solution brought to you by .replace()
The most boring type of puzzle, just implement a formula and call it a day. The only "aha" moment is realizing that there's only one solution each.