diff --git a/09/part1.php b/09/part1.php new file mode 100644 index 0000000..5761bec --- /dev/null +++ b/09/part1.php @@ -0,0 +1,31 @@ + array_unshift($line, 9), $input); +array_unshift($input, array_fill(0, count($input[0]), 9)); +$input[] = array_fill(0, count($input[0]), 9); + +foreach ($lowpoints as $lowpoint) { + $searched = []; + $input[$lowpoint[0]][$lowpoint[1]] = '*'; + getBasinSize($input, $lowpoint[0], $lowpoint[1], $searched); + $basinSizes[] = count($searched); +} + +function getBasinSize(&$input, $i, $j, &$searched) +{ + if (!isset($searched[$i . ',' . $j])) { + $searched[$i . ',' . $j] = 1; + if ($input[$i][$j] != '*') { + $input[$i][$j] = ' '; + } + if ($input[$i - 1][$j] != 9) { + getBasinSize($input, $i - 1, $j, $searched); + } + if ($input[$i][$j - 1] != 9) { + getBasinSize($input, $i, $j - 1, $searched); + } + if ($input[$i][$j + 1] != 9) { + getBasinSize($input, $i, $j + 1, $searched); + } + if ($input[$i + 1][$j] != 9) { + getBasinSize($input, $i + 1, $j, $searched); + } + } +} + +rsort($basinSizes); +echo array_product(array_slice($basinSizes, 0, 3)); //970200 diff --git a/README.md b/README.md index 0b6e4c1..fb67113 100644 --- a/README.md +++ b/README.md @@ -37,5 +37,10 @@ ### DAY 8 - Decoding 7-segments displays +- [part1](08/part1.php) +- [part2](08/part2.php) + +### DAY 9 - Smoke Basin + - [part1](08/part1.php) - [part2](08/part2.php) \ No newline at end of file