advent_of_code_2019/day_10/functions.php

44 lines
830 B
PHP
Raw Permalink Normal View History

2019-12-10 15:07:47 +01:00
<?php
/**
* @param array $array
* @return array|bool
*/
function array_flatten(array $array) {
return iterator_to_array(array_flatten_iterator($array), false);
}
/**
* @param array $array
* @return Generator
*/
function array_flatten_iterator(array $array) {
foreach ($array as $value) {
if (is_array($value)) {
yield from array_flatten_iterator($value);
} else {
yield $value;
}
}
}
/**
* @param int $x1
* @param int $y1
* @param int $x2
* @param int $y2
*
* @return array
*/
function getPointsBetween(int $x1, int $y1, int $x2, int $y2)
{
$dx = $x2-$x1;
$dy = $y2-$y1;
$max = max(abs($dx), abs($dy));
for($t = 0; $t<=$max; $t++) {
$points[] = sprintf('%s,%s', $x1+($t*$dx)/$max, $y1+($t*$dy)/$max);
}
return $points;
}