advent_of_code_2021/day_15/part_2.php

57 lines
1.4 KiB
PHP
Raw Normal View History

<?php
$map = array_map(
static function ($line) {
return array_map('intval', str_split($line));
},
explode("\n", file_get_contents('./input.txt'))
);
$height = count($map);
$width = count($map[0]);
for ($i = 1; $i < 5; $i++) {
for ($row = 0; $row < $height; $row++) {
for ($cell = 0; $cell < $width; $cell++) {
$new_cell = $map[$row][$cell] + $i;
if ($new_cell > 9) {
$new_cell = $new_cell - 9;
}
$map[$row][] = $new_cell;
}
}
}
$width = count($map[0]);
for ($i = 1; $i < 5; $i++) {
for ($row = 0; $row < $height; $row++) {
$new_row = [];
for ($cell = 0; $cell < $width; $cell++) {
$new_cell = $map[$row][$cell] + $i;
if ($new_cell > 9) {
$new_cell = $new_cell - 9;
}
$new_row[] = $new_cell;
}
$map[] = $new_row;
}
}
include_once './vendor/autoload.php';
$grid = new BlackScorp\Astar\Grid($map);
$startPosition = $grid->getPoint(0, 0);
$endPosition = $grid->getPoint(count($map) - 1, count($map[0]) - 1);
$astar = new BlackScorp\Astar\Astar($grid);
$nodes = $astar->search($startPosition,$endPosition);
if(count($nodes) === 0){
echo "Path not found";
}else{
foreach($nodes as $node){
echo sprintf('%s / %s : %s %s', $node->getX(), $node->getY(), $node->getScore(), "\n");
}
echo $node->getTotalScore();
}