Skip to content

Commit 2a7d2c6

Browse files
author
Adrian Reimann
committed
solution for day 17 part 2
1 parent a3ebec0 commit 2a7d2c6

File tree

2 files changed

+192
-0
lines changed

2 files changed

+192
-0
lines changed

day_17/17-2.php

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
//read input (it is only one line in this puzzle)
4+
$target_area = [];
5+
$input = fopen("input.txt", "r");
6+
while (($input_line = fgets($input)) !== false) {
7+
$line = trim($input_line);
8+
$line_parts = explode('=', $line); //three pieces, discard the first, the second is x, the third is y
9+
10+
//y part is fine, but x part as a comma, space and 'y' in its split string behind
11+
$x_part = explode(',', $line_parts[1])[0];
12+
13+
//assign the 2 coords each as the x and y coordinates
14+
$target_area['x'] = explode('..', $x_part);
15+
$target_area['y'] = explode('..', $line_parts[2]);
16+
17+
break; //break as only one line
18+
}
19+
fclose($input);
20+
21+
//min y to see when we can stop trying the trajectory, max x and max y to give us an idea of what range to bruteforce
22+
$min_y = min($target_area['y'][0], $target_area['y'][1]);
23+
$max_y = max(abs($target_area['y'][0]), abs($target_area['y'][1]));
24+
$max_x = max(abs($target_area['x'][0]), abs($target_area['x'][1]));
25+
26+
//brute force possibilities from a high y going down, for each y try every x, if it hits the target area increase count by 1
27+
$hit_target_count = 0;
28+
for($y = $max_y*2; $y > -($max_y*2); $y--) {
29+
for($x = $max_x*2; $x > -($max_x*2); $x--) {
30+
//now run the trajectory, keeping track of the highest y coordinate
31+
$highest = 0;
32+
$x2 = $x;
33+
$y2 = $y;
34+
$curr_x = $curr_y = 0;
35+
while ($y2 >= $min_y) { //follow the trajectory until it's "below" the target area
36+
37+
//only modify x towards 0, until it hits 0, then no change
38+
if ($x2 > 0) {
39+
$curr_x += $x2--;
40+
}
41+
elseif ($x2 < 0) {
42+
$curr_x += $x2++;
43+
}
44+
45+
//y just always decreases
46+
$curr_y += $y2--;
47+
48+
if (($target_area['x'][0] <= $curr_x && $curr_x <= $target_area['x'][1]) && ($target_area['y'][0] <= $curr_y && $curr_y <= $target_area['y'][1])) {
49+
$hit_target_count++; //we are in the target area, stop checking this trajectory and increase the count
50+
break;
51+
}
52+
}
53+
}
54+
}
55+
56+
//result
57+
echo "There are $hit_target_count inital values hitting the target in their trajectory." . PHP_EOL;

day_17/puzzle.txt

+135
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
--- Day 17: Trick Shot ---
2+
3+
You finally decode the Elves' message. HI, the message says. You continue searching for the sleigh keys.
4+
5+
Ahead of you is what appears to be a large ocean trench. Could the keys have fallen into it? You'd better send a probe to investigate.
6+
7+
The probe launcher on your submarine can fire the probe with any integer velocity in the x (forward) and y (upward, or downward if negative) directions. For example, an initial x,y velocity like 0,10 would fire the probe straight up, while an initial velocity like 10,-1 would fire the probe forward at a slight downward angle.
8+
9+
The probe's x,y position starts at 0,0. Then, it will follow some trajectory by moving in steps. On each step, these changes occur in the following order:
10+
11+
The probe's x position increases by its x velocity.
12+
The probe's y position increases by its y velocity.
13+
Due to drag, the probe's x velocity changes by 1 toward the value 0; that is, it decreases by 1 if it is greater than 0, increases by 1 if it is less than 0, or does not change if it is already 0.
14+
Due to gravity, the probe's y velocity decreases by 1.
15+
16+
For the probe to successfully make it into the trench, the probe must be on some trajectory that causes it to be within a target area after any step. The submarine computer has already calculated this target area (your puzzle input). For example:
17+
18+
target area: x=20..30, y=-10..-5
19+
20+
This target area means that you need to find initial x,y velocity values such that after any step, the probe's x position is at least 20 and at most 30, and the probe's y position is at least -10 and at most -5.
21+
22+
Given this target area, one initial velocity that causes the probe to be within the target area after any step is 7,2:
23+
24+
.............#....#............
25+
.......#..............#........
26+
...............................
27+
S........................#.....
28+
...............................
29+
...............................
30+
...........................#...
31+
...............................
32+
....................TTTTTTTTTTT
33+
....................TTTTTTTTTTT
34+
....................TTTTTTTT#TT
35+
....................TTTTTTTTTTT
36+
....................TTTTTTTTTTT
37+
....................TTTTTTTTTTT
38+
39+
In this diagram, S is the probe's initial position, 0,0. The x coordinate increases to the right, and the y coordinate increases upward. In the bottom right, positions that are within the target area are shown as T. After each step (until the target area is reached), the position of the probe is marked with #. (The bottom-right # is both a position the probe reaches and a position in the target area.)
40+
41+
Another initial velocity that causes the probe to be within the target area after any step is 6,3:
42+
43+
...............#..#............
44+
...........#........#..........
45+
...............................
46+
......#..............#.........
47+
...............................
48+
...............................
49+
S....................#.........
50+
...............................
51+
...............................
52+
...............................
53+
.....................#.........
54+
....................TTTTTTTTTTT
55+
....................TTTTTTTTTTT
56+
....................TTTTTTTTTTT
57+
....................TTTTTTTTTTT
58+
....................T#TTTTTTTTT
59+
....................TTTTTTTTTTT
60+
61+
Another one is 9,0:
62+
63+
S........#.....................
64+
.................#.............
65+
...............................
66+
........................#......
67+
...............................
68+
....................TTTTTTTTTTT
69+
....................TTTTTTTTTT#
70+
....................TTTTTTTTTTT
71+
....................TTTTTTTTTTT
72+
....................TTTTTTTTTTT
73+
....................TTTTTTTTTTT
74+
75+
One initial velocity that doesn't cause the probe to be within the target area after any step is 17,-4:
76+
77+
S..............................................................
78+
...............................................................
79+
...............................................................
80+
...............................................................
81+
.................#.............................................
82+
....................TTTTTTTTTTT................................
83+
....................TTTTTTTTTTT................................
84+
....................TTTTTTTTTTT................................
85+
....................TTTTTTTTTTT................................
86+
....................TTTTTTTTTTT..#.............................
87+
....................TTTTTTTTTTT................................
88+
...............................................................
89+
...............................................................
90+
...............................................................
91+
...............................................................
92+
................................................#..............
93+
...............................................................
94+
...............................................................
95+
...............................................................
96+
...............................................................
97+
...............................................................
98+
...............................................................
99+
..............................................................#
100+
101+
The probe appears to pass through the target area, but is never within it after any step. Instead, it continues down and to the right - only the first few steps are shown.
102+
103+
If you're going to fire a highly scientific probe out of a super cool probe launcher, you might as well do it with style. How high can you make the probe go while still reaching the target area?
104+
105+
In the above example, using an initial velocity of 6,9 is the best you can do, causing the probe to reach a maximum y position of 45. (Any higher initial y velocity causes the probe to overshoot the target area entirely.)
106+
107+
Find the initial velocity that causes the probe to reach the highest y position and still eventually be within the target area after any step. What is the highest y position it reaches on this trajectory?
108+
109+
Your puzzle answer was 3003.
110+
111+
--- Part Two ---
112+
113+
Maybe a fancy trick shot isn't the best idea; after all, you only have one probe, so you had better not miss.
114+
115+
To get the best idea of what your options are for launching the probe, you need to find every initial velocity that causes the probe to eventually be within the target area after any step.
116+
117+
In the above example, there are 112 different initial velocity values that meet these criteria:
118+
119+
23,-10 25,-9 27,-5 29,-6 22,-6 21,-7 9,0 27,-7 24,-5
120+
25,-7 26,-6 25,-5 6,8 11,-2 20,-5 29,-10 6,3 28,-7
121+
8,0 30,-6 29,-8 20,-10 6,7 6,4 6,1 14,-4 21,-6
122+
26,-10 7,-1 7,7 8,-1 21,-9 6,2 20,-7 30,-10 14,-3
123+
20,-8 13,-2 7,3 28,-8 29,-9 15,-3 22,-5 26,-8 25,-8
124+
25,-6 15,-4 9,-2 15,-2 12,-2 28,-9 12,-3 24,-6 23,-7
125+
25,-10 7,8 11,-3 26,-7 7,1 23,-9 6,0 22,-10 27,-6
126+
8,1 22,-8 13,-4 7,6 28,-6 11,-4 12,-4 26,-9 7,4
127+
24,-10 23,-8 30,-8 7,0 9,-1 10,-1 26,-5 22,-9 6,5
128+
7,5 23,-6 28,-10 10,-2 11,-1 20,-9 14,-2 29,-7 13,-3
129+
23,-5 24,-8 27,-9 30,-7 28,-5 21,-10 7,9 6,6 21,-5
130+
27,-10 7,2 30,-9 21,-8 22,-7 24,-9 20,-6 6,9 29,-5
131+
8,-2 27,-8 30,-5 24,-7
132+
133+
How many distinct initial velocity values cause the probe to be within the target area after any step?
134+
135+
Your puzzle answer was 940.

0 commit comments

Comments
 (0)