-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday-3.php
38 lines (30 loc) · 999 Bytes
/
day-3.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
<?php
$input = trim(file_get_contents('input/' . substr(basename(__FILE__), 0, -4)));
$input = explode("\n", $input);
$map = [];
// Part 1
foreach ($input as $line) {
preg_match('/(\d+).*?(\d+).*?(\d+).*?(\d+).*?(\d+)/', $line, $parts);
for ($x = $parts[2]; $x < $parts[2] + $parts[4]; $x++) {
for ($y = $parts[3]; $y < $parts[3] + $parts[5]; $y++) {
$map[$x . '.' . $y] = isset($map[$x . '.' . $y]) ? 2 : 1;
}
}
}
echo 'Answer 1: ' . array_count_values($map)[2] . PHP_EOL;
// Part 2
foreach ($input as $line) {
preg_match('/(\d+).*?(\d+).*?(\d+).*?(\d+).*?(\d+)/', $line, $parts);
$isOverlap = false;
for ($x = $parts[2]; $x < $parts[2] + $parts[4]; $x++) {
for ($y = $parts[3]; $y < $parts[3] + $parts[5]; $y++) {
if ($map[$x . '.' . $y] == 2 && $isOverlap = true) {
break 2;
}
}
}
if (!$isOverlap) {
echo 'Answer 2: ' . $parts[1] . PHP_EOL;
break;
}
}