5th day 1st part
This commit is contained in:
25
05/part1.php
Normal file
25
05/part1.php
Normal 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
|
||||
|
||||
Reference in New Issue
Block a user