Skip to content

Commit 5244418

Browse files
committedDec 13, 2024
feat: 2024 day 13
1 parent 3a54699 commit 5244418

File tree

4 files changed

+93
-2
lines changed

4 files changed

+93
-2
lines changed
 

‎2024/13a.py

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
from aoc import *
2+
import re
3+
from itertools import product
4+
5+
6+
def main(infi: str):
7+
inp = filerstrip(infi)
8+
machines = []
9+
for machine in inp.split('\n\n'):
10+
lines = machine.split('\n')
11+
a = re.fullmatch(r'Button A: X\+(.*?), Y\+(.*?)', lines[0])
12+
b = re.fullmatch(r'Button B: X\+(.*?), Y\+(.*?)', lines[1])
13+
prize = re.fullmatch(r'Prize: X=(.*?), Y=(.*?)', lines[2])
14+
machines.append(
15+
(
16+
(int(a[1]), int(a[2])),
17+
(int(b[1]), int(b[2])),
18+
(int(prize[1]), int(prize[2])),
19+
)
20+
)
21+
tokens_needed = 0
22+
INITIAL = 999999999999999999
23+
for machine in machines:
24+
tokens = INITIAL
25+
for a, b in product(range(1, 101), range(1, 101)):
26+
x = a * machine[0][0] + b * machine[1][0]
27+
y = a * machine[0][1] + b * machine[1][1]
28+
if (x, y) == machine[2]:
29+
tokens = min(tokens, a * 3 + b * 1)
30+
if tokens != INITIAL:
31+
tokens_needed += tokens
32+
return tokens_needed
33+
34+
35+
36+
DAY = 13
37+
FILE_TEST = f"{DAY}_testa.txt"
38+
# FILE_TEST = f"{DAY}_testb.txt"
39+
FILE_EXP = f"{DAY}_exp.txt"
40+
FILE = f"{DAY}.txt"
41+
# test_and_submit(main, FILE_TEST, FILE_EXP, FILE, DAY)
42+
# print(main(FILE_TEST))
43+
print(main(FILE))

‎2024/13b.py

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from aoc import *
2+
import re
3+
from pprint import pprint as pp
4+
from sympy.solvers.solveset import linsolve
5+
from sympy import symbols, core
6+
7+
8+
def main(infi: str):
9+
inp = filerstrip(infi)
10+
tokens = 0
11+
for machine in inp.split('\n\n'):
12+
lines = machine.split('\n')
13+
ax, ay = map(
14+
int,
15+
re.fullmatch(r'Button A: X\+(.*?), Y\+(.*?)', lines[0]).groups(),
16+
)
17+
bx, by = map(
18+
int,
19+
re.fullmatch(r'Button B: X\+(.*?), Y\+(.*?)', lines[1]).groups(),
20+
)
21+
px, py = map(
22+
int, re.fullmatch(r'Prize: X=(.*?), Y=(.*?)', lines[2]).groups()
23+
)
24+
a, b = symbols('a, b', integer=True, positive=True)
25+
system = (
26+
ax * a + bx * b - px - 10000000000000,
27+
ay * a + by * b - py - 10000000000000,
28+
)
29+
res = linsolve(system, a, b)
30+
assert len(res) == 1
31+
solution = next(iter(res))
32+
if all(
33+
type(sol) is core.numbers.Integer and sol > 0 for sol in solution
34+
):
35+
tokens += 3 * solution[0] + solution[1]
36+
return tokens
37+
38+
39+
DAY = 13
40+
FILE_TEST = f"{DAY}_testa.txt"
41+
# FILE_TEST = f"{DAY}_testb.txt"
42+
FILE_EXP = f"{DAY}_exp.txt"
43+
FILE = f"{DAY}.txt"
44+
# test_and_submit(main, FILE_TEST, FILE_EXP, FILE, DAY)
45+
# print(main(FILE_TEST))
46+
print(main(FILE))

‎README.org

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* Personal Stats 2024
55
| Day | Time | Rank | Score | Time | Rank | Score |
66
|-----+----------+------+-------+----------+-------+-------|
7+
| 13 | 00:39:23 | 4308 | 0 | 12:34:52 | 21706 | 0 |
78
| 12 | 00:43:21 | 4911 | 0 | >24h | 27971 | 0 |
89
| 11 | 00:16:43 | 4574 | 0 | 04:24:24 | 13605 | 0 |
910
| 10 | 00:54:16 | 7416 | 0 | 00:56:37 | 6784 | 0 |

‎requirements.txt

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
advent-of-code-data==2.0.4
2-
more-itertools==10.5.0
1+
advent-of-code-data==2.0.4
2+
more-itertools==10.5.0
3+
sympy==1.13.3

0 commit comments

Comments
 (0)
Please sign in to comment.