-
Notifications
You must be signed in to change notification settings - Fork 3
/
solution.py
74 lines (55 loc) · 1.69 KB
/
solution.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# prompt: https://adventofcode.com/2021/day/17
from dataclasses import dataclass
from re import findall
from typing import Tuple, cast
from ...base import TextSolution, answer
Range = Tuple[int, int]
@dataclass
class Probe:
vel_x: int
vel_y: int
targ_x: Range
targ_y: Range
pos_x = 0
pos_y = 0
def step(self) -> None:
self.pos_x += self.vel_x
self.pos_y += self.vel_y
if self.vel_x < 0:
self.vel_x += 1
if self.vel_x > 0:
self.vel_x -= 1
self.vel_y -= 1
def is_in_target(self) -> bool:
return (self.targ_x[0] <= self.pos_x <= self.targ_x[1]) and (
self.targ_y[0] <= self.pos_y <= self.targ_y[1]
)
def fly(self) -> bool:
"""
returns true if probe hit the target, false otherwise
"""
while self.pos_x <= self.targ_x[1] and self.pos_y >= self.targ_y[0]:
self.step()
if self.is_in_target():
return True
return False
class Solution(TextSolution):
_year = 2021
_day = 17
@answer((7381, 3019))
def solve(self) -> Tuple[int, int]:
ranges = [
cast(Range, tuple(map(int, x)))
for x in findall(r"(-?\d+)\.\.(-?\d+)", self.input)
]
assert len(ranges) == 2, "Found too many ranges from input"
y1 = ranges[1][0]
# math magic based on physics
max_y = y1 * (y1 + 1) // 2
total = 0
for x in range(int((ranges[0][0] * 2) ** 0.5), ranges[0][1] + 1):
for y in range(y1, -y1):
p = Probe(x, y, *ranges)
if p.fly():
total += 1
return max_y, total