Skip to content

Commit afaa077

Browse files
committed
day17
1 parent db50f27 commit afaa077

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ Day 00: Helper file | [ao
4141
[Day 14: Extended Polymerization](http://adventofcode.com/2021/day/14) | [day14.py](python/day14.py) | `Counter` to the rescue.
4242
[Day 15: Chiton](http://adventofcode.com/2021/day/15) | [day15.py](python/day15.py) | Dijkstra.
4343
[Day 16: Packet Decoder](http://adventofcode.com/2021/day/16) | [day16.py](python/day16.py) | `iter` for some elegant stream implementation.
44-
[Day 17](http://adventofcode.com/2021/day/17) | [day17.py](python/day17.py) |
44+
[Day 17: Trick Shot](http://adventofcode.com/2021/day/17) | [day17.py](python/day17.py) | Analytical + brute force.
4545
[Day 18](http://adventofcode.com/2021/day/18) | [day18.py](python/day18.py) |
4646
[Day 19](http://adventofcode.com/2021/day/19) | [day19.py](python/day19.py) |
4747
[Day 20](http://adventofcode.com/2021/day/20) | [day20.py](python/day20.py) |

inputs/17.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
target area: x=124..174, y=-123..-86

python/day17.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from aoc import read_input_line, integers
2+
3+
4+
def move(x, y, vx, vy):
5+
x += vx
6+
y += vy
7+
if vx > 0: vx -= 1
8+
vy -= 1
9+
return x, y, vx, vy
10+
11+
12+
def will_hit(vx, vy):
13+
x, y = 0, 0
14+
while x <= x2 and y >= y1:
15+
x, y, vx, vy = move(x, y, vx, vy)
16+
if x1 <= x <= x2 and y1 <= y <= y2:
17+
return True
18+
return False
19+
20+
21+
x1, x2, y1, y2 = integers(read_input_line(17))
22+
23+
print(y1 * (y1+1) // 2)
24+
print(sum(will_hit(vx, vy)
25+
for vx in range(x2+1)
26+
for vy in range(y1, -y1)))

0 commit comments

Comments
 (0)