-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday17part2.py
More file actions
40 lines (33 loc) · 782 Bytes
/
Copy pathday17part2.py
File metadata and controls
40 lines (33 loc) · 782 Bytes
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
import re
data = "target area: x=179..201, y=-109..-63"
minx, maxx, miny, maxy = map(int, re.findall(r"-?\d+", data))
def steps_for_dy(dy):
y = 0
steps = 0
valid = []
while y >= miny:
if miny <= y <= maxy:
valid.append(steps)
y += dy
dy -= 1
steps += 1
return valid
def can_land_dx(step):
total = set()
for dx in range(0, maxx + 1):
x = 0
odx = dx
for _ in range(step):
x += dx
if dx > 0:
dx -= 1
if minx <= x <= maxx:
total.add(odx)
return total
total = 0
for dy in range(miny - 1, -miny + 1):
iter = set()
for step in steps_for_dy(dy):
iter |= can_land_dx(step)
total += len(iter)
print(total)