+/AoC
advent of code
git clone https://git.julia.blue/+/AoC
7a69a9a
master
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 ): 10pos = (0 ,0 ) 11yield pos 12while not past (* pos ,vy )and not inside (* pos ): 13pos = (pos [0 ]+ vx ,pos [1 ]+ vy ) 14vx = max (vx - 1 ,0 ) 15vy -= 1 16yield pos 17 18 19def display (vx ,vy ): 20points = list (shot (vx ,vy )) 21xs = [p [0 ]for p in points ] 22ys = [p [1 ]for p in points ] 23for j in range (min (b ,min (ys )),max (t ,max (ys ))+ 1 )[::- 1 ]: 24for i in range (max (r ,max (xs ))+ 1 ): 25current = " " 26if (i ,j )in points : 27current = "#" 28elif inside (i ,j ): 29current = "T" 30current ,end = "" ) 31"-" * 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 )) 50max (y for _ ,y in slowest_shot )) 51max_len = len (slowest_shot ) 52 53 54def x_shot (vx ): 55x = 0 56i = 0 57result = [] 58while x < l and vx > 0 : 59i += 1 60x += vx 61vx = max (0 ,vx - 1 ) 62while l <= x <= r and vx > 0 : 63result .append (i ) 64i += 1 65x += vx 66vx = max (0 ,vx - 1 ) 67if vx == 0 and l <= x <= r : 68result += list (range (i ,max_len + 1 )) 69return result 70 71 72def y_shot (vy ): 73y = 0 74i = 0 75result = [] 76while y > t or vy > 0 : 77i += 1 78y += vy 79vy -= 1 80while b <= y <= t : 81result .append (i ) 82i += 1 83y += vy 84vy -= 1 85return 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 (): 91for v in l : 92inv_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 (): 99for v in l : 100inv_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 ): 107for l in inv_len [i ]: 108for h in inv_height [i ]: 109results .add ((l ,h )) 110# print(sorted(results)) 111len (results )) 112 113# ans = open("test_answers").read().split() 114# ans = sorted([tuple(map(int, s.split(","))) for s in ans]) 115# print(ans)