-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path202413.py
51 lines (39 loc) · 1.2 KB
/
202413.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
import re
from itertools import groupby
from typing import List, Tuple
from utils import input_as_strings_iter
def solve(px: int, py: int, ax: int, ay: int, bx: int, by: int) -> int:
det = ax * by - ay * bx
if det == 0:
return 0
a = px * by - py * bx
b = py * ax - px * ay
# is there an integer solution?
if a % det != 0 or b % det != 0:
return 0
a = a // det
b = b // det
# only non-negative solutions make sense
if a >= 0 and b >= 0:
return 3 * a + b
return 0
input: List[List[Tuple[int, int]]] = []
for key, group in groupby(input_as_strings_iter("202413.txt"), lambda x: x != ""):
if not key:
continue
res: List[Tuple[int, int]] = []
for s in group:
a = re.match(r".+: X[+=](\d+), Y[+=](\d+)", s)
assert a is not None
ax = int(a.group(1))
ay = int(a.group(2))
res.append((ax, ay))
assert len(res) == 3
input.append(res)
part_one = 0
part_two = 0
for [(ax, ay), (bx, by), (px, py)] in input:
part_one += solve(px, py, ax, ay, bx, by)
part_two += solve(px + 10000000000000, py + 10000000000000, ax, ay, bx, by)
print(f"Part one: {part_one}")
print(f"Part two: {part_two}")