solved day13 p1

This commit is contained in:
0x01FE 2025-03-10 14:45:13 -05:00
parent 3d6e6397e1
commit 078c7dc82c
3 changed files with 1345 additions and 0 deletions

1279
2024/day13/input.txt Normal file

File diff suppressed because it is too large Load Diff

51
2024/day13/main.py Normal file
View File

@ -0,0 +1,51 @@
import re
BUTTON_A_COST = 3
BUTTON_B_COST = 1
MAX_PRESSES = 100
with open('input.txt', 'r') as file:
data: list[str] = file.readlines()
machines = []
for i in range(0, len(data), 4):
button_a = [int(n) for n in re.findall('[0-9]+', data[i])]
button_b = [int(n) for n in re.findall('[0-9]+', data[i+1])]
prize = [int(n) for n in re.findall('[0-9]+', data[i+2])]
machines.append([button_a, button_b, prize])
# Very simple niave solution for part 1
total_cost = 0
for machine in machines:
ax, ay = machine[0]
bx, by = machine[1]
x, y = machine[2]
smallest_a, smallest_b = float('inf'), float('inf')
for a in range(MAX_PRESSES):
for b in range(MAX_PRESSES):
if (a * ax) + (b * bx) == x and (a * ay) + (b * by) == y:
if a < smallest_a:
smallest_a = a
if b < smallest_b:
smallest_b = b
if smallest_a == float('inf') or smallest_b == float('inf'):
continue
cost = (smallest_a * BUTTON_A_COST) + (smallest_b * BUTTON_B_COST)
total_cost += cost
print('Presses: ', smallest_a, smallest_b)
print('Minimum tokens: ', cost)
print('Total cost: ', total_cost)

15
2024/day13/test.txt Normal file
View File

@ -0,0 +1,15 @@
Button A: X+94, Y+34
Button B: X+22, Y+67
Prize: X=8400, Y=5400
Button A: X+26, Y+66
Button B: X+67, Y+21
Prize: X=12748, Y=12176
Button A: X+17, Y+86
Button B: X+84, Y+37
Prize: X=7870, Y=6450
Button A: X+69, Y+23
Button B: X+27, Y+71
Prize: X=18641, Y=10279