Skip to content

Commit 72269ef

Browse files
committed
Day 13 Part 1
1 parent 4db7c24 commit 72269ef

File tree

5 files changed

+133
-0
lines changed

5 files changed

+133
-0
lines changed

Day_13/README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
## \--- Day 13: Claw Contraption ---
2+
3+
Next up: the [lobby](https://adventofcode.com/2020/day/24) of a resort on a tropical island. The Historians take a moment to admire the hexagonal floor tiles before spreading out.
4+
5+
Fortunately, it looks like the resort has a new [arcade](https://en.wikipedia.org/wiki/Amusement_arcade)! Maybe you can win some prizes from the [claw machines](https://en.wikipedia.org/wiki/Claw_machine)?
6+
7+
The claw machines here are a little unusual. Instead of a joystick or directional buttons to control the claw, these machines have two buttons labeled `A` and `B`. Worse, you can't just put in a token and play; it costs _3 tokens_ to push the `A` button and _1 token_ to push the `B` button.
8+
9+
With a little experimentation, you figure out that each machine's buttons are configured to move the claw a specific amount to the _right_ (along the `X` axis) and a specific amount _forward_ (along the `Y` axis) each time that button is pressed.
10+
11+
Each machine contains one _prize_; to win the prize, the claw must be positioned _exactly_ above the prize on both the `X` and `Y` axes.
12+
13+
You wonder: what is the smallest number of tokens you would have to spend to win as many prizes as possible? You assemble a list of every machine's button behavior and prize location (your puzzle input). For example:
14+
15+
```
16+
Button A: X+94, Y+34
17+
Button B: X+22, Y+67
18+
Prize: X=8400, Y=5400
19+
20+
Button A: X+26, Y+66
21+
Button B: X+67, Y+21
22+
Prize: X=12748, Y=12176
23+
24+
Button A: X+17, Y+86
25+
Button B: X+84, Y+37
26+
Prize: X=7870, Y=6450
27+
28+
Button A: X+69, Y+23
29+
Button B: X+27, Y+71
30+
Prize: X=18641, Y=10279
31+
```
32+
33+
This list describes the button configuration and prize location of four different claw machines.
34+
35+
For now, consider just the first claw machine in the list:
36+
37+
- Pushing the machine's `A` button would move the claw `94` units along the `X` axis and `34` units along the `Y` axis.
38+
- Pushing the `B` button would move the claw `22` units along the `X` axis and `67` units along the `Y` axis.
39+
- The prize is located at `X=8400`, `Y=5400`; this means that from the claw's initial position, it would need to move exactly `8400` units along the `X` axis and exactly `5400` units along the `Y` axis to be perfectly aligned with the prize in this machine.
40+
41+
The cheapest way to win the prize is by pushing the `A` button `80` times and the `B` button `40` times. This would line up the claw along the `X` axis (because `80*94 + 40*22 = 8400`) and along the `Y` axis (because `80*34 + 40*67 = 5400`). Doing this would cost `80*3` tokens for the `A` presses and `40*1` for the `B` presses, a total of `280` tokens.
42+
43+
For the second and fourth claw machines, there is no combination of A and B presses that will ever win a prize.
44+
45+
For the third claw machine, the cheapest way to win the prize is by pushing the `A` button `38` times and the `B` button `86` times. Doing this would cost a total of `200` tokens.
46+
47+
So, the most prizes you could possibly win is two; the minimum tokens you would have to spend to win all (two) prizes is `480`.
48+
49+
You estimate that each button would need to be pressed _no more than `100` times_ to win a prize. How else would someone be expected to play?
50+
51+
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?_
52+
53+
Your puzzle answer was `[REDACTED]`.
54+
55+
The first half of this puzzle is complete! It provides one gold star: ⭐
56+

Day_13/example_13a.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Button A: X+94, Y+34
2+
Button B: X+22, Y+67
3+
Prize: X=8400, Y=5400
4+
5+
Button A: X+26, Y+66
6+
Button B: X+67, Y+21
7+
Prize: X=12748, Y=12176
8+
9+
Button A: X+17, Y+86
10+
Button B: X+84, Y+37
11+
Prize: X=7870, Y=6450
12+
13+
Button A: X+69, Y+23
14+
Button B: X+27, Y+71
15+
Prize: X=18641, Y=10279

Day_13/task_13a.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
24+
x, y = symbols("x,y")
25+
eq1 = Eq((a1 * x + b1 * y), r1)
26+
eq2 = Eq((a2 * x + b2 * y), r2)
27+
28+
sol_dict = solve((eq1, eq2), (x, y))
29+
x = sol_dict[x]
30+
y = sol_dict[y]
31+
32+
if x == int(x) and y == int(y):
33+
return x, y
34+
else:
35+
return 0, 0
36+
37+
38+
def calculate_tokens(machines):
39+
token = 0
40+
for machine in machines:
41+
a, b = find_way_to_win(machine)
42+
token += a * A_PRICE + b * B_PRICE
43+
return token
44+
45+
46+
if "__main__" == __name__:
47+
machines = load_data("Day_13/puzzle_input.txt")
48+
print(calculate_tokens(machines))

Day_13/test_task_13a.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from task_13a 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", 480),
9+
],
10+
)
11+
def test_calculate_tokens(file_path, expected):
12+
machines = load_data(file_path)
13+
assert calculate_tokens(machines) == expected

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sympy==1.13.3

0 commit comments

Comments
 (0)