1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
#advent of code 2024
#day 13
#similar problem was last year
#rewritten to include both parts, could get rid of second for loop but w/e
import re
def formula(machinevalues,p2):
xa,ya,xb,yb,XP,YP = machinevalues;
XP += p2;
YP += p2;
pusha = (yb*XP - xb*YP)/(yb*xa - xb*ya); #number of pushes - button A
pushb = (XP - xa*pusha)/xb; #number of pushes - button B
Valid = (pusha%1 == 0)*(pushb%1==0); #check if solution is an integer
cost = 3*int(pusha) + int(pushb);
return Valid*cost;
machines = [];
f = open("13.in","r");
for l in f.read().split("\n\n"):
values = re.findall(r'\d+',l);
machines.append([int(v) for v in values]);
f.close();
part1, part2 = 0, 0;
for machine in machines:
part1 += formula(machine,0);
part2 += formula(machine,10000000000000);
print("part 1 =",part1);
print("part 2 =",part2);
|