-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDay17.py
57 lines (40 loc) · 979 Bytes
/
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
55
56
57
from sys import argv, stdin
import itertools as it
text = stdin.read().strip()
x1, x2, y1, y2 = area = [
int("".join(v))
for k, v in it.groupby(text, key=lambda s: s.isnumeric() or s == "-")
if k
]
def in_area(x, y):
x1, x2, y1, y2 = area
return x1 <= x <= x2 and y1 <= y <= y2
def simulate(xv, yv):
maxx = x = 0
maxy = y = 0
hit = in_area(x, y)
while True:
x += xv
y += yv
xv = xv + (-1 if xv > 0 else (1 if xv < 0 else 0))
yv -= 1
hit |= in_area(x, y)
maxx = max(x, maxx)
maxy = max(y, maxy)
if y < y1 or x > x2 or (xv == 0 and x < x1):
break
return (hit, maxy)
count = 0
best = -1e9
# Pure guess
max_yv = 300
for xv in range(0, x2 + 1):
for yv in range(y1 - 1, max_yv):
hit, maxy = simulate(xv, yv)
if hit:
count += 1
best = max(maxy, best)
if argv[1] == "1":
print(best)
else:
print(count)