17th day both parts
This commit is contained in:
1
17/input.sample
Normal file
1
17/input.sample
Normal file
@@ -0,0 +1 @@
|
|||||||
|
target area: x=20..30, y=-10..-5
|
||||||
35
17/part1.php
Normal file
35
17/part1.php
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
<?php
|
||||||
|
$input = file('input', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
|
||||||
|
|
||||||
|
preg_match('#target area: x=(?P<x1>[\d-]*)..(?P<x2>[\d-]*), y=(?P<y2>[\d-]*)..(?P<y1>[\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
|
||||||
38
17/part2.php
Normal file
38
17/part2.php
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
<?php
|
||||||
|
$input = file('input', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
|
||||||
|
|
||||||
|
preg_match('#target area: x=(?P<x1>[\d-]*)..(?P<x2>[\d-]*), y=(?P<y2>[\d-]*)..(?P<y1>[\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
|
||||||
Reference in New Issue
Block a user