From 28f7ae790f4092d0d9ae9b4757e2de0aaa4dc205 Mon Sep 17 00:00:00 2001 From: Clement Desmidt Date: Fri, 17 Dec 2021 10:51:20 +0100 Subject: [PATCH] :sparkles: Finit le jour 17 --- README.md | 2 +- day_17/part_1.php | 57 +++++++++++++++++++++++++++++++++++++++++++++++ day_17/part_2.php | 55 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 113 insertions(+), 1 deletion(-) create mode 100644 day_17/part_1.php create mode 100644 day_17/part_2.php diff --git a/README.md b/README.md index 48921c0..3aad916 100644 --- a/README.md +++ b/README.md @@ -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 🔲🔲 | | diff --git a/day_17/part_1.php b/day_17/part_1.php new file mode 100644 index 0000000..2afc2b9 --- /dev/null +++ b/day_17/part_1.php @@ -0,0 +1,57 @@ + $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; diff --git a/day_17/part_2.php b/day_17/part_2.php new file mode 100644 index 0000000..8c7b5f4 --- /dev/null +++ b/day_17/part_2.php @@ -0,0 +1,55 @@ + $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);