solved day13 part 2
This commit is contained in:
parent
adb6512003
commit
26d6292256
@ -18,6 +18,8 @@ for i in range(0, len(data), 4):
|
|||||||
|
|
||||||
# Very simple niave solution for part 1
|
# Very simple niave solution for part 1
|
||||||
|
|
||||||
|
print('<', '-' * 10, ' Part 1 ', '-' * 10, '>')
|
||||||
|
|
||||||
total_cost = 0
|
total_cost = 0
|
||||||
for machine in machines:
|
for machine in machines:
|
||||||
ax, ay = machine[0]
|
ax, ay = machine[0]
|
||||||
@ -40,12 +42,24 @@ for machine in machines:
|
|||||||
cost = (smallest_a * BUTTON_A_COST) + (smallest_b * BUTTON_B_COST)
|
cost = (smallest_a * BUTTON_A_COST) + (smallest_b * BUTTON_B_COST)
|
||||||
total_cost += cost
|
total_cost += cost
|
||||||
|
|
||||||
print('Presses: ', smallest_a, smallest_b)
|
|
||||||
print('Minimum tokens: ', cost)
|
|
||||||
|
|
||||||
print('Total cost: ', total_cost)
|
print('Total cost: ', total_cost)
|
||||||
|
|
||||||
|
print('<', '-' * 10, ' Part 2 ', '-' * 10, '>')
|
||||||
|
|
||||||
|
# Part 2 (unfortunately lifted this formula from A.P. (https://github.com/CodingAP) )
|
||||||
|
|
||||||
|
total_cost = 0
|
||||||
|
for machine in machines:
|
||||||
|
ax, ay = machine[0]
|
||||||
|
bx, by = machine[1]
|
||||||
|
x, y = [n + 10000000000000 for n in machine[2]]
|
||||||
|
|
||||||
|
b: float = ((ax * y) - (ay * x)) / ((ax * by) - (ay * bx))
|
||||||
|
a: float = (x - (bx * b)) / ax
|
||||||
|
|
||||||
|
if b.is_integer() and a.is_integer():
|
||||||
|
cost = (a * BUTTON_A_COST) + (b * BUTTON_B_COST)
|
||||||
|
total_cost += cost
|
||||||
|
|
||||||
|
print('Total cost: ', int(total_cost))
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user