-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexercise_day6.php
More file actions
46 lines (37 loc) · 1017 Bytes
/
Copy pathexercise_day6.php
File metadata and controls
46 lines (37 loc) · 1017 Bytes
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
<?php
/*
*adventofcode assessment
*Question 6
*/
$input = trim(file_get_contents('inputs/' . substr(basename(__FILE__), 0, -4)));
$input = explode("\n", $input);
$count = 0;
for ($i = 0; $i < 400; $i++) {
for ($j = 0; $j < 400; $j++) {
$min = PHP_INT_MAX;
$total = $p = 0;
foreach ($input as $index => $line) {
$point = explode(', ', $line);
$dist = abs($point[0] - $i) + abs($point[1] - $j);
$total += $dist;
if ($dist < $min) {
$min = $dist;
$p = $index;
} elseif ($dist == $min) {
$p = '.';
}
}
if ($total < 10000) {
$count++;
}
$area[$p] = ($area[$p] ?? 0) + 1;
if ($i == 0 || $i == 399 || $j == 0 || $j == 399) {
$infinite[$p] = true;
}
}
}
$area = array_diff_key($area, $infinite);
rsort($area);
echo 'Answer 1: ' . $area[0] . PHP_EOL;
echo 'Answer 2: ' . $count . PHP_EOL;
?>