wip 2024/day13/part1
This commit is contained in:
parent
2d7ab5e287
commit
345a5bfccb
44
2024/day13/part1.py
Normal file
44
2024/day13/part1.py
Normal file
@ -0,0 +1,44 @@
|
||||
import math
|
||||
import re
|
||||
|
||||
text: str
|
||||
|
||||
with open("input.txt") as f:
|
||||
text = f.read()
|
||||
|
||||
total: int = 0
|
||||
|
||||
exp: str = r"Button A: X([+-]\d+), Y([+-]\d+)\nButton B: X([+-]\d+), Y([+-]\d+)\nPrize: X=(\d+). Y=(\d+)"
|
||||
|
||||
def resolv(Ax, Ay, Bx, By, goal_x, goal_y):
|
||||
for press_b in range(101):
|
||||
press_a = (goal_x - (press_b * Bx)) / Ax
|
||||
if press_a * Ay + press_b * By > goal_y:
|
||||
continue
|
||||
if (press_a // 1 != press_a):
|
||||
continue
|
||||
if (press_a > 100):
|
||||
continue
|
||||
return press_a, press_b
|
||||
|
||||
def resolv_min(Ax, Ay, Bx, By, goal_x, goal_y):
|
||||
tmp1 = resolv(Ax, Ay, Bx, By, goal_x, goal_y)
|
||||
tmp2 = resolv(Bx, By, Ax, Ay, goal_x, goal_y)
|
||||
if (tmp1 is None):
|
||||
return tmp2
|
||||
if (tmp2 is None):
|
||||
return tmp1
|
||||
press_a1, press_b1 = tmp1
|
||||
press_b2, press_a2 = tmp2
|
||||
if (press_a1 > press_a2):
|
||||
return press_a2, press_b2
|
||||
return press_a1, press_b1
|
||||
|
||||
for Ax, Ay, Bx, By, goal_x, goal_y in re.findall(exp, text):
|
||||
Ax, Ay, Bx, By, goal_x, goal_y = int(Ax), int(Ay), int(Bx), int(By), int(goal_x), int(goal_y)
|
||||
tmp = resolv_min(Ax, Ay, Bx, By, goal_x, goal_y)
|
||||
if (tmp is None):
|
||||
continue
|
||||
press_a, press_b = tmp
|
||||
total += press_a * 3 + press_b
|
||||
print(total)
|
Loading…
Reference in New Issue
Block a user