Finit le jour 17

This commit is contained in:
Clement Desmidt 2021-12-17 10:51:20 +01:00
parent 757e193dde
commit 28f7ae790f
3 changed files with 113 additions and 1 deletions

View File

@ -6,5 +6,5 @@
|-----------|-----------|------------|----------|----------|---------|----------|
| | | 1 ✅✅ | 2 ✅✅ | 3 ✅✅ | 4 ✅✅ | 5 ✅✅ |
| 6 ✅✅ | 7 ✅✅ | 8 ✅✅ | 9 ✅✅ | 10 ✅✅ | 11 ✅✅ | 12 ✅✅ |
| 13 🔲🔲 | 14 ✅✅ | 15 ✅✅ | 16 🔲🔲 | 17 🔲🔲 | 18 🔲🔲 | 19 🔲🔲 |
| 13 ❌❌ | 14 ✅✅ | 15 ✅✅ | 16 ❌❌ | 17 ✅✅ | 18 🔲🔲 | 19 🔲🔲 |
| 20 🔲🔲 | 21 🔲🔲 | 22 🔲🔲 | 23 🔲🔲 | 24 🔲🔲 | 25 🔲🔲 | |

57
day_17/part_1.php Normal file
View File

@ -0,0 +1,57 @@
<?php
$input = file_get_contents('./input.txt');
preg_match('/^target area: x=(\d+)\.\.(\d+), y=(-?\d+)\.\.(-?\d+)$/', $input, $matches);
[, $x_min, $x_max, $y_min, $y_max] = $matches;
$target = ['x_min' => $x_min, 'x_max' => $x_max, 'y_min' => $y_min, 'y_max' => $y_max];
function increment(&$x, &$y, &$x_velocity, &$y_velocity)
{
$current_x = $x;
$x += $x_velocity;
$y += $y_velocity;
if ($x_velocity > 0) $x_velocity -= 1;
elseif ($x_velocity < 0) $x_velocity += 1;
else $x_velocity = 0;
$y_velocity -= 1;
}
function check($x, $y, $target)
{
return $x >= $target['x_min'] &&
$x <= $target['x_max'] &&
$y >= $target['y_min'] &&
$y <= $target['y_max'];
}
$x = 0;
$y = 0;
$x_velocity = 6;
$y_velocity = 9;
$max = 0;
for ($i = 1; $i < abs($target['x_min']); $i++) {
$tmp_max = 0;
for ($j = 1; $j < abs($target['y_min']); $j++) {
$x = 0;
$y = 0;
$x_velocity = $i;
$y_velocity = $j;
$tmp_max = max($tmp_max, $y);
while(!check($x, $y, $target)) {
echo sprintf('x: %s, y: %s%s', $x, $y, "\n");
increment($x, $y, $x_velocity, $y_velocity);
$tmp_max = max($tmp_max, $y);
if ($y <= $target['y_min']) {
break;
}
}
if (check($x, $y, $target)) {
$max = max($max, $tmp_max);
}
}
}
echo $max;

55
day_17/part_2.php Normal file
View File

@ -0,0 +1,55 @@
<?php
$input = file_get_contents('./input.txt');
preg_match('/^target area: x=(\d+)\.\.(\d+), y=(-?\d+)\.\.(-?\d+)$/', $input, $matches);
[, $x_min, $x_max, $y_min, $y_max] = $matches;
$target = ['x_min' => $x_min, 'x_max' => $x_max, 'y_min' => $y_min, 'y_max' => $y_max];
function increment(&$x, &$y, &$x_velocity, &$y_velocity)
{
$current_x = $x;
$x += $x_velocity;
$y += $y_velocity;
if ($x_velocity > 0) $x_velocity -= 1;
elseif ($x_velocity < 0) $x_velocity += 1;
else $x_velocity = 0;
$y_velocity -= 1;
}
function check($x, $y, $target)
{
return $x >= $target['x_min'] &&
$x <= $target['x_max'] &&
$y >= $target['y_min'] &&
$y <= $target['y_max'];
}
$x = 0;
$y = 0;
$x_velocity = 6;
$y_velocity = 9;
$possibilities = [];
for ($i = 0; $i <= abs($target['x_max']); $i++) {
for ($j = -abs($target['y_min']); $j <= abs($target['y_min']); $j++) {
if ($i === 0 && $j === 0) continue;
$x = 0;
$y = 0;
$x_velocity = $i;
$y_velocity = $j;
while(!check($x, $y, $target)) {
echo sprintf('x: %s, y: %s%s', $x, $y, "\n");
increment($x, $y, $x_velocity, $y_velocity);
if ($y <= $target['y_min']) {
break;
}
}
if (check($x, $y, $target)) {
$possibilities[] = sprintf('%s,%s', $i, $j);
}
}
}
echo count($possibilities);