From 06ca3641aa34dbb30b71dcc2ae70e37620e5a85b Mon Sep 17 00:00:00 2001 From: kplaczek Date: Fri, 17 Dec 2021 23:30:25 +0100 Subject: [PATCH] 17th day both parts --- 17/input | 1 + 17/input.sample | 1 + 17/part1.php | 35 +++++++++++++++++++++++++++++++++++ 17/part2.php | 38 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 75 insertions(+) create mode 100644 17/input create mode 100644 17/input.sample create mode 100644 17/part1.php create mode 100644 17/part2.php diff --git a/17/input b/17/input new file mode 100644 index 0000000..5891c18 --- /dev/null +++ b/17/input @@ -0,0 +1 @@ +target area: x=230..283, y=-107..-57 diff --git a/17/input.sample b/17/input.sample new file mode 100644 index 0000000..f40609b --- /dev/null +++ b/17/input.sample @@ -0,0 +1 @@ +target area: x=20..30, y=-10..-5 \ No newline at end of file diff --git a/17/part1.php b/17/part1.php new file mode 100644 index 0000000..934a297 --- /dev/null +++ b/17/part1.php @@ -0,0 +1,35 @@ +[\d-]*)..(?P[\d-]*), y=(?P[\d-]*)..(?P[\d-]*)#', $input[0], $found); +$totalMaxY = 0; + +for ($x = 0; $x < 500; $x++) { + for ($y = -100; $y < 500; $y++) { + $velocityX = $x; + $velocityY = $y; + $position = [0, 0]; + $maxY = 0; + while ($position[0] < $found['x2'] && $position[1] > $found['y2']) { + $position[0] += $velocityX; + $position[1] += $velocityY; + if ($velocityX != 0) { + $velocityX += $velocityX > 0 ? -1 : 1; + } + if ($position[1] > $maxY) { + $maxY = $position[1]; + } + $velocityY--; + if ($position[0] >= $found['x1'] && + $position[0] <= $found['x2'] && + $position[1] <= $found['y1'] && + $position[1] >= $found['y2']) { + if ($maxY > $totalMaxY) { + $totalMaxY = $maxY; + } + break; + } + } + } +} +echo $totalMaxY; //5671 \ No newline at end of file diff --git a/17/part2.php b/17/part2.php new file mode 100644 index 0000000..0d0593e --- /dev/null +++ b/17/part2.php @@ -0,0 +1,38 @@ +[\d-]*)..(?P[\d-]*), y=(?P[\d-]*)..(?P[\d-]*)#', $input[0], $found); +$totalMaxY = 0; +$velocities = 0; + +for ($x = 0; $x < 1500; $x++) { + for ($y = -500; $y < 500; $y++) { + $velocityX = $x; + $velocityY = $y; + $position = [0, 0]; + $maxY = 0; + while ($position[0] < $found['x2'] && $position[1] > $found['y2']) { + $position[0] += $velocityX; + $position[1] += $velocityY; + if ($velocityX != 0) { + $velocityX += $velocityX > 0 ? -1 : 1; + } + if ($position[1] > $maxY) { + $maxY = $position[1]; + } + $velocityY--; + if ($position[0] >= $found['x1'] && + $position[0] <= $found['x2'] && + $position[1] <= $found['y1'] && + $position[1] >= $found['y2']) { + $velocities++; + if ($maxY > $totalMaxY) { + $totalMaxY = $maxY; + } + break; + } + } + } +} + +echo $velocities; //4556