advent_of_code_2019/day_10/part_1.php
2019-12-10 15:07:47 +01:00

54 lines
1.5 KiB
PHP

<?php
include 'functions.php';
$rows = explode("\n", file_get_contents('input.txt'));
// Make the list of asteroids coordinates
$asteroids = array_flatten(
array_filter(
array_map(static function($row, $i) {
return array_filter(array_map(static function($col, $j) use ($i) {
if ($col === '#') {
return sprintf('%s,%s', $i, $j);
}
return null;
}, str_split($row), range(0, strlen($row))));
}, $rows, range(0, count($rows)))
)
);
$asteroid_count = [];
foreach ($asteroids as $asteroid_base) {
$count = 0;
foreach ($asteroids as $asteroid_target) {
if ($asteroid_base === $asteroid_target) {
continue;
}
$points_base = explode(',', $asteroid_base);
$points_target = explode(',', $asteroid_target);
$points = getPointsBetween($points_base[0], $points_base[1], $points_target[0], $points_target[1]);
$add_point = true;
foreach ($points as $point) {
if ($point === $asteroid_base || $point === $asteroid_target) {
continue;
}
if (in_array($point, $asteroids)) {
$add_point = false;
break;
}
}
if ($add_point) {
$count++;
}
}
$asteroid_count[$asteroid_base] = $count;
}
arsort($asteroid_count);
var_dump($asteroid_count);
echo current($asteroid_count);