+/AoC

advent of code

git clone https://git.julia.blue/+/AoC

Joseph RyanDay 177a69a9a

master
2.5 KiB115 linesraw
1import re
2from collections import defaultdict
3
4l, r, b, t = map(int, re.findall(r"-?\d+", open("input").read()))
5inside = lambda x, y: l <= x <= r and b <= y <= t
6past = lambda x, y, v: x > r or y < b and v <= 0
7
8
9def shot(vx, vy):
10    pos = (0, 0)
11    yield pos
12    while not past(*pos, vy) and not inside(*pos):
13        pos = (pos[0] + vx, pos[1] + vy)
14        vx = max(vx - 1, 0)
15        vy -= 1
16        yield pos
17
18
19def display(vx, vy):
20    points = list(shot(vx, vy))
21    xs = [p[0] for p in points]
22    ys = [p[1] for p in points]
23    for j in range(min(b, min(ys)), max(t, max(ys)) + 1)[::-1]:
24        for i in range(max(r, max(xs)) + 1):
25            current = " "
26            if (i, j) in points:
27                current = "#"
28            elif inside(i, j):
29                current = "T"
30            print(current, end="")
31        print()
32    print("-" * 80)
33
34
35# display(7, 2)
36# display(6, 3)
37# display(9, 0)
38# display(17, -4)
39# display(6, 9)
40# display(6, 9)
41# display(7, 9)
42
43# while True:
44#     vx, vy = map(int, input("input 'vx,vy': ").split(","))
45#     display(vx, vy)
46
47
48lowest_vx = round((1 + (1 + 8 * l) ** 0.5) / 2 - 1)
49slowest_shot = list(shot(lowest_vx, -b - 1))
50print(max(y for _, y in slowest_shot))
51max_len = len(slowest_shot)
52
53
54def x_shot(vx):
55    x = 0
56    i = 0
57    result = []
58    while x < l and vx > 0:
59        i += 1
60        x += vx
61        vx = max(0, vx - 1)
62    while l <= x <= r and vx > 0:
63        result.append(i)
64        i += 1
65        x += vx
66        vx = max(0, vx - 1)
67    if vx == 0 and l <= x <= r:
68        result += list(range(i, max_len + 1))
69    return result
70
71
72def y_shot(vy):
73    y = 0
74    i = 0
75    result = []
76    while y > t or vy > 0:
77        i += 1
78        y += vy
79        vy -= 1
80    while b <= y <= t:
81        result.append(i)
82        i += 1
83        y += vy
84        vy -= 1
85    return result
86
87
88shot_len = {i: x_shot(i) for i in range(lowest_vx, r + 1)}
89inv_len = defaultdict(list)
90for k, l in shot_len.items():
91    for v in l:
92        inv_len[v].append(k)
93# for i in sorted(inv_len.items()):
94#     print(i)
95
96shot_height = {i: y_shot(i) for i in range(b, -b)}
97inv_height = defaultdict(list)
98for k, l in shot_height.items():
99    for v in l:
100        inv_height[v].append(k)
101# print()
102# for i in sorted(inv_height.items()):
103#     print(i)
104
105results = set()
106for i in range(max_len + 1):
107    for l in inv_len[i]:
108        for h in inv_height[i]:
109            results.add((l, h))
110# print(sorted(results))
111print(len(results))
112
113# ans = open("test_answers").read().split()
114# ans = sorted([tuple(map(int, s.split(","))) for s in ans])
115# print(ans)