15th day and 16th day WIP :(
This commit is contained in:
28
15/part1.php
Normal file
28
15/part1.php
Normal 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
|
||||
Reference in New Issue
Block a user