diff options
Diffstat (limited to '2024/aoc2024-d13.py')
-rw-r--r-- | 2024/aoc2024-d13.py | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/2024/aoc2024-d13.py b/2024/aoc2024-d13.py new file mode 100644 index 0000000..8eaff25 --- /dev/null +++ b/2024/aoc2024-d13.py @@ -0,0 +1,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); + |