15th day and 16th day WIP :(

This commit is contained in:
kplaczek
2021-12-16 21:18:02 +01:00
parent e3494e234a
commit 223863aa1f
8 changed files with 299 additions and 0 deletions

28
15/part1.php Normal file
View File

@@ -0,0 +1,28 @@
<?php
$input = file('input', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$input = array_map('str_split', $input);
$visited = array_fill(0, count($input), array_fill(0, count($input[0]), PHP_INT_MAX));
$queue = [[0, 0]];
$visited[0][0] = 0;
while (count($queue) > 0) {
[$x, $y] = array_shift($queue);
//down
if (isset($input[$x + 1][$y]) &&
$visited[$x + 1][$y] > $visited[$x][$y] + $input[$x + 1][$y]
) {
$queue[] = [$x + 1, $y];
$visited[$x + 1][$y] = $visited[$x][$y] + $input[$x + 1][$y];
}
//right
if (isset($input[$x][$y + 1]) &&
$visited[$x][$y + 1] > $visited[$x][$y] + $input[$x][$y + 1]
) {
$queue[] = [$x, $y + 1];
$visited[$x][$y + 1] = $visited[$x][$y] + $input[$x][$y + 1];
}
}
echo $visited[count($visited)-1][count($visited[0])-1]; //553