-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday17.py
54 lines (43 loc) · 1.49 KB
/
day17.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
import sys
from re import findall
from dataclasses import dataclass
@dataclass
class Point:
x: int = 0
y: int = 0
def shoot(target, vx, vy):
probe = Point()
highest = -(sys.maxsize - 1)
hit = False
while probe.x <= target.br.x and probe.y >= target.br.y:
probe.x += vx
probe.y += vy
vx = max(0, vx-1)
vy -= 1
highest = max(highest, probe.y)
if hit := (probe in target):
break
return hit, highest
def highest_position_of_good_shots(target):
# Just brute force the combinations, don't have time to do the math...
for vx in range(1, target.br.x + 1):
for vy in range(target.br.y, -target.br.y + 1):
good, highest = shoot(target, vx, vy)
if good:
yield highest
class Rect(object):
def __init__(self, ranges) -> None:
self.tl = Point()
self.br = Point()
self.tl.x, self.br.x, self.br.y, self.tl.y = ( int(i) for i in findall(r"-?\d+", ranges) )
def __contains__(self, point):
return self.tl.x <= point.x <= self.br.x and self.br.y <= point.y <= self.tl.y
def main(args = ()):
fileName = "day17.txt" if len(args) < 1 else args[0]
with open(fileName) as lines:
target = Rect(lines.readline().strip())
heights = list(highest_position_of_good_shots(target))
print(f"Best shot has y =", max(heights))
print(len(heights), "combinations hit the target")
if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))