diff options
author | Brian Chu <brianmchu42@gmail.com> | 2021-12-19 22:57:37 -0800 |
---|---|---|
committer | Brian Chu <brianmchu42@gmail.com> | 2021-12-19 22:57:37 -0800 |
commit | d4f482804b9777768fa4f9fedde0cd549578502b (patch) | |
tree | 211098ceaad6046234e0f55fe86dc539f791d4a4 /day17.py | |
parent | 04d85bf194d50f8da584a8c657d7490649befb7c (diff) | |
download | AdventOfCode2021-d4f482804b9777768fa4f9fedde0cd549578502b.tar.gz |
catch up on solutions up to day 20
Diffstat (limited to 'day17.py')
-rw-r--r-- | day17.py | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/day17.py b/day17.py new file mode 100644 index 0000000..53881ea --- /dev/null +++ b/day17.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python + +# target area: 236 <= x <= 262, -78 <= y <= -58 +# i was originally going to do a brute force solution +# since the problem implies that the displacement on the x axis is the Vx'th triangular number +# which gives a lower bound on the number of steps to calculate the displacement on the y axis +# but there's actually a really brilliant solution located at +# https://github.com/prendradjaja/advent-of-code-2021/blob/main/17--trick-shot/a.py +# which I am paraphrasing here + +yvel = 77 +print(sum(n for n in range(1, yvel+1))) + + +# part 2 +xMin, xMax, yMin, yMax = 236, 262, -78, -58 + +def is_hit(vel): + for pos in trajectory(vel): + if xMin <= pos[0] <= xMax and yMin <= pos[1] <= yMax: + return True + return False + +def trajectory(vel): + pos = (0,0) + while pos[0] <= xMax and pos[1] >= yMin: + yield pos + pos = (pos[0] + vel[0], pos[1] + vel[1]) + vel = ( + max(0, vel[0] - 1), + vel[1] - 1 + ) + +result = 0 +for xvel in range(0, xMax+1): + for yvel in range(yMin, -yMin): + if is_hit((xvel, yvel)): + result += 1 +print(result) |