-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path13.py
56 lines (35 loc) · 1.05 KB
/
13.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from aoc.utilities.fetch import get_input
from aoc.utilities.decorators import solution
import re
def parse(data):
return list(map(parse_game, data.split("\n\n")))
def parse_game(s):
pattern = r"[XY][+=](\d+),?\s?[XY][+=](\d+)"
matches = re.findall(pattern, s)
return [(int(x), int(y)) for x, y in matches]
@solution
def solve_all(games):
ans1, ans2 = 0, 0
for a, b, p in games:
first, second = (
solve(*a, *b, *p),
solve(*a, *b, p[0] + 10000000000000, p[1] + 10000000000000),
)
ans1 += first if first != float("inf") else 0
ans2 += second if second != float("inf") else 0
print(f"{ans1}\n{ans2}")
def solve(ax, ay, bx, by, px, py):
a_num = py * bx - px * by
a_den = ay * bx - ax * by
if a_num % a_den != 0:
return float("inf")
A = a_num / a_den
b_num = px - ax * A
b_den = bx
if b_num % b_den != 0:
return float("inf")
B = b_num / b_den
return int(3 * A + B)
data = get_input(13)
games = parse(data)
solve_all(games)