Skip to content

Commit 6b38944

Browse files
committed
Day 13 Part 2
1 parent 72269ef commit 6b38944

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

Day_13/README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,34 @@ Your puzzle answer was `[REDACTED]`.
5454

5555
The first half of this puzzle is complete! It provides one gold star: ⭐
5656

57+
## \--- Part Two ---
58+
59+
As you go to win the first prize, you discover that the claw is nowhere near where you expected it would be. Due to a unit conversion error in your measurements, the position of every prize is actually `10000000000000` higher on both the `X` and `Y` axis!
60+
61+
Add `10000000000000` to the `X` and `Y` position of every prize. After making this change, the example above would now look like this:
62+
63+
```
64+
Button A: X+94, Y+34
65+
Button B: X+22, Y+67
66+
Prize: X=10000000008400, Y=10000000005400
67+
68+
Button A: X+26, Y+66
69+
Button B: X+67, Y+21
70+
Prize: X=10000000012748, Y=10000000012176
71+
72+
Button A: X+17, Y+86
73+
Button B: X+84, Y+37
74+
Prize: X=10000000007870, Y=10000000006450
75+
76+
Button A: X+69, Y+23
77+
Button B: X+27, Y+71
78+
Prize: X=10000000018641, Y=10000000010279
79+
```
80+
81+
Now, it is only possible to win a prize on the second and fourth claw machines. Unfortunately, it will take _many more than `100` presses_ to do so.
82+
83+
Using the corrected prize coordinates, figure out how to win as many prizes as possible. _What is the fewest tokens you would have to spend to win all possible prizes?_
84+
85+
Your puzzle answer was `[REDACTED]`.
86+
87+
Both parts of this puzzle are complete! They provide two gold stars: ⭐⭐

Day_13/task_13b.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import re
2+
from sympy import symbols, Eq, solve
3+
4+
A_PRICE = 3
5+
B_PRICE = 1
6+
7+
8+
def load_data(filename):
9+
with open(filename, "r") as f:
10+
data = f.read().split("\n\n")
11+
12+
machines = []
13+
for machine in data:
14+
machine = re.findall(r"\d+", machine)
15+
machine = list(map(int, machine))
16+
machines.append(machine)
17+
18+
return machines
19+
20+
21+
def find_way_to_win(machine):
22+
a1, a2, b1, b2, r1, r2 = machine
23+
r1 = r1 + 10000000000000 # fix for unit conversion
24+
r2 = r2 + 10000000000000 # fix for unit conversion
25+
26+
x, y = symbols("x,y")
27+
eq1 = Eq((a1 * x + b1 * y), r1)
28+
eq2 = Eq((a2 * x + b2 * y), r2)
29+
30+
sol_dict = solve((eq1, eq2), (x, y))
31+
x = sol_dict[x]
32+
y = sol_dict[y]
33+
34+
if x == int(x) and y == int(y):
35+
return x, y
36+
else:
37+
return 0, 0
38+
39+
40+
def calculate_tokens(machines):
41+
token = 0
42+
for machine in machines:
43+
a, b = find_way_to_win(machine)
44+
token += a * A_PRICE + b * B_PRICE
45+
return token
46+
47+
48+
if "__main__" == __name__:
49+
machines = load_data("Day_13/puzzle_input.txt")
50+
print(calculate_tokens(machines))

Day_13/test_task_13b.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from task_13b import load_data, calculate_tokens
2+
import pytest
3+
4+
5+
@pytest.mark.parametrize(
6+
"file_path, expected",
7+
[
8+
("Day_13/example_13a.txt", 875318608908),
9+
],
10+
)
11+
def test_calculate_tokens(file_path, expected):
12+
machines = load_data(file_path)
13+
assert calculate_tokens(machines) == expected

0 commit comments

Comments
 (0)