2025-03-10 14:45:13 -05:00

52 lines
1.2 KiB
Python

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)