-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday-10.php
50 lines (40 loc) · 1.29 KB
/
day-10.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php
$input = trim(file_get_contents('input/' . substr(basename(__FILE__), 0, -4)));
$input = explode("\n", $input);
$points = [];
foreach ($input as $line) {
preg_match('/<.*?(\-?\d+).*?(\-?\d+)>.*?<(.*?\-?\d+).*?(\-?\d+)>/', $line, $matches);
$points[] = ['pos' => [(int) $matches[1], (int) $matches[2]], 'vel' => [(int) $matches[3], (int) $matches[4]]];
}
$found = false;
$seconds = 0;
while (true) {
$seconds++;
$grid = [];
$minX = $minY = PHP_INT_MAX;
$maxX = $maxY = PHP_INT_MIN;
foreach ($points as $key => &$point) {
$point['pos'][0] += $point['vel'][0];
$point['pos'][1] += $point['vel'][1];
$grid[$point['pos'][0]][$point['pos'][1]] = true;
$minX = min($point['pos'][0], $minX);
$maxX = max($point['pos'][0], $maxX);
$minY = min($point['pos'][1], $minY);
$maxY = max($point['pos'][1], $maxY);
}
unset($point);
if ($maxX - $minX < 100 && $maxY - $minY < 15) {
$found = true;
}
if ($found) {
for ($i = $minY; $i <= $maxY; $i++) {
for ($j = $minX; $j <= $maxX; $j++) {
echo isset($grid[$j][$i]) ? '#' : '.';
}
echo PHP_EOL;
}
echo PHP_EOL;
echo 'Answer 2: ' . $seconds . PHP_EOL;
break;
}
}