-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaoc17.py
More file actions
executable file
·98 lines (73 loc) · 2.15 KB
/
aoc17.py
File metadata and controls
executable file
·98 lines (73 loc) · 2.15 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env python3
import utils
def sign(n):
if n == 0:
return 0
elif n < 0:
return -1
else:
return 1
def x_target(initial_vx, x_min, x_max):
x = 0
vx = initial_vx
n = 0
while True:
x += vx
vx -= sign(vx)
n += 1
if x_min <= x <= x_max:
yield (initial_vx, n, vx == 0)
if vx == 0:
return
elif vx == 0:
return
elif vx < 0 and x < x_min:
return
elif vx > 0 and x > x_max:
return
def x_velocities(x_min, x_max):
for vx in range(0, x_max + 1):
yield from x_target(vx, x_min, x_max)
def y_targets(initial_vy, y_min, y_max):
y = 0
vy = initial_vy
n = 0
while True:
if y_min <= y <= y_max:
yield (initial_vy, n)
y += vy
vy -= 1
n += 1
if vy < 0 and y < y_min:
return
def y_velocities(y_min, y_max):
for vy in range(-1000, 1000):
yield from y_targets(vy, y_min, y_max)
def y_pos(vy, n):
return (n / 2) * ((2 * vy) + (n - 1) * (-1))
def valid_y_match(x_data, vy, iters):
for _, x_iters, stopped in x_data:
if stopped and iters >= x_iters:
return True
elif iters == x_iters:
return True
return False
def valid_velocities(x_data, y_data):
for vx, x_iters, stopped in x_data:
for vy, y_iters in y_data:
if (stopped and y_iters >= x_iters) or (x_iters == y_iters):
yield (vx, vy)
def main():
x_min, x_max = 96, 125
y_min, y_max = -144, -98
x_data = [(vx, iters, stopped) for vx, iters, stopped in x_velocities(x_min, x_max)]
print(f"x data ({len(x_data)}): {x_data}")
y_data = [(vy, iters) for vy, iters in y_velocities(y_min, y_max)]
print(f"y data ({len(y_data)}): {y_data}")
max_vy = max(vy for vy, iters in y_data if valid_y_match(x_data, vy, iters))
print(f"max vy: {max_vy}")
highest_pos = 0 if max_vy <= 0 else y_pos(max_vy, max_vy)
print(f"highest pos: {highest_pos}")
print(len(set(valid_velocities(x_data, y_data))))
if __name__ == "__main__":
main()