5th day 1st part

This commit is contained in:
kplaczek
2021-12-05 09:56:58 +01:00
parent fd05fbc8e5
commit 613487e420
3 changed files with 531 additions and 1 deletions

25
05/part1.php Normal file
View File

@@ -0,0 +1,25 @@
<?php
echo '<pre>';
$input = file('input', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$input = array_map(fn($line) => explode(',', str_replace(' -> ', ',', $line)), $input);
$map = array_fill(0, 1000, array_fill(0, 1000, 0));
foreach ($input as $coordinates) {
if ($coordinates[0] == $coordinates[2]) {
$start = min([$coordinates[1], $coordinates[3]]);
$end = max([$coordinates[1], $coordinates[3]]);
for ($i = $start; $i <= $end; $i++) {
$map[$coordinates[0]][$i]++;
}
}
if ($coordinates[1] == $coordinates[3]) {
$start = min([$coordinates[0], $coordinates[2]]);
$end = max([$coordinates[0], $coordinates[2]]);
for ($i = $start; $i <= $end; $i++) {
$map[$i][$coordinates[1]]++;
}
}
}
echo count(array_filter(call_user_func_array('array_merge', $map), fn($el) => $el > 1)); //5632