Ajoute le jour 15

Merci https://github.com/BlackScorp/astar ✌️
This commit is contained in:
Clement Desmidt 2021-12-15 12:38:01 +01:00
parent ec95bf332c
commit 81b4bc585d
31 changed files with 76098 additions and 0 deletions

5
day_15/composer.json Normal file
View File

@ -0,0 +1,5 @@
{
"require": {
"blackscorp/astar": "^1.2"
}
}

56
day_15/composer.lock generated Normal file
View File

@ -0,0 +1,56 @@
{
"_readme": [
"This file locks the dependencies of your project to a known state",
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "2ae8d8dd342f88f50fd5588b6db3f4b3",
"packages": [
{
"name": "blackscorp/astar",
"version": "v1.2.0",
"source": {
"type": "git",
"url": "https://github.com/BlackScorp/astar.git",
"reference": "9b80d6135ac31947d246cf2e33ff45d4681a6a57"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/BlackScorp/astar/zipball/9b80d6135ac31947d246cf2e33ff45d4681a6a57",
"reference": "9b80d6135ac31947d246cf2e33ff45d4681a6a57",
"shasum": ""
},
"type": "library",
"autoload": {
"psr-4": {
"BlackScorp\\Astar\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Vitalij Mik",
"email": "cccpmik+composer@gmail.com"
}
],
"description": "AStar path finding implementation in php",
"support": {
"issues": "https://github.com/BlackScorp/astar/issues",
"source": "https://github.com/BlackScorp/astar/tree/v1.2.0"
},
"time": "2017-10-06T15:21:22+00:00"
}
],
"packages-dev": [],
"aliases": [],
"minimum-stability": "stable",
"stability-flags": [],
"prefer-stable": false,
"prefer-lowest": false,
"platform": [],
"platform-dev": [],
"plugin-api-version": "2.1.0"
}

21
day_15/part_1.php Normal file
View File

@ -0,0 +1,21 @@
<?php
$map = array_map('str_split', explode("\n", file_get_contents('./input.txt')));
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();
}

56
day_15/part_2.php Normal file
View File

@ -0,0 +1,56 @@
<?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();
}

7
day_15/vendor/autoload.php vendored Normal file
View File

@ -0,0 +1,7 @@
<?php
// autoload.php @generated by Composer
require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInit9d031d4e152710d80f5f020c3fb1552c::getLoader();

View File

@ -0,0 +1,2 @@
vendor/
composer.phar

View File

@ -0,0 +1,10 @@
build:
environment:
php: '7.0.8'
tests:
override:
-
command: 'php tools/phpunit.phar test --coverage-clover=coverage.xml --whitelist=src'
coverage:
file: 'coverage.xml'
format: 'php-clover'

19
day_15/vendor/blackscorp/astar/LICENSE vendored Normal file
View File

@ -0,0 +1,19 @@
The MIT License (MIT)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

View File

@ -0,0 +1,17 @@
{
"name": "blackscorp/astar",
"description": "AStar path finding implementation in php",
"minimum-stability": "dev",
"license": "MIT",
"authors": [
{
"name": "Vitalij Mik",
"email": "cccpmik+composer@gmail.com"
}
],
"autoload":{
"psr-4":{
"BlackScorp\\Astar\\":"src/"
}
}
}

View File

@ -0,0 +1,197 @@
<?xml version="1.0" encoding="UTF-8"?>
<coverage generated="1483519571">
<project timestamp="1483519571">
<package name="BlackScorp\Astar">
<file name="C:\projects\astar\src/Astar.php">
<class name="Astar" namespace="BlackScorp\Astar">
<metrics complexity="18" methods="7" coveredmethods="7" conditionals="0" coveredconditionals="0" statements="46" coveredstatements="46" elements="53" coveredelements="53"/>
</class>
<line num="18" type="method" name="blocked" visibility="public" complexity="1" crap="1" count="2"/>
<line num="20" type="stmt" count="2"/>
<line num="21" type="stmt" count="2"/>
<line num="23" type="method" name="enableDiagonal" visibility="public" complexity="1" crap="1" count="1"/>
<line num="25" type="stmt" count="1"/>
<line num="26" type="stmt" count="1"/>
<line num="28" type="method" name="__construct" visibility="public" complexity="1" crap="1" count="6"/>
<line num="30" type="stmt" count="6"/>
<line num="31" type="stmt" count="6"/>
<line num="33" type="method" name="setHeuristic" visibility="public" complexity="1" crap="1" count="6"/>
<line num="35" type="stmt" count="6"/>
<line num="36" type="stmt" count="6"/>
<line num="43" type="method" name="search" visibility="public" complexity="3" crap="3" count="6"/>
<line num="46" type="stmt" count="6"/>
<line num="47" type="stmt" count="4"/>
<line num="48" type="stmt" count="4"/>
<line num="50" type="stmt" count="6"/>
<line num="51" type="stmt" count="6"/>
<line num="53" type="stmt" count="6"/>
<line num="54" type="stmt" count="6"/>
<line num="55" type="stmt" count="1"/>
<line num="58" type="stmt" count="5"/>
<line num="68" type="method" name="fillHeap" visibility="private" complexity="9" crap="9" count="6"/>
<line num="70" type="stmt" count="6"/>
<line num="74" type="stmt" count="6"/>
<line num="76" type="stmt" count="6"/>
<line num="77" type="stmt" count="6"/>
<line num="78" type="stmt" count="6"/>
<line num="79" type="stmt" count="6"/>
<line num="80" type="stmt" count="6"/>
<line num="82" type="stmt" count="6"/>
<line num="83" type="stmt" count="6"/>
<line num="84" type="stmt" count="6"/>
<line num="85" type="stmt" count="6"/>
<line num="86" type="stmt" count="6"/>
<line num="87" type="stmt" count="6"/>
<line num="88" type="stmt" count="6"/>
<line num="89" type="stmt" count="6"/>
<line num="90" type="stmt" count="6"/>
<line num="91" type="stmt" count="6"/>
<line num="92" type="stmt" count="6"/>
<line num="93" type="stmt" count="6"/>
<line num="95" type="stmt" count="6"/>
<line num="96" type="stmt" count="6"/>
<line num="97" type="stmt" count="6"/>
<line num="104" type="method" name="getReversedPath" visibility="private" complexity="2" crap="2" count="5"/>
<line num="106" type="stmt" count="5"/>
<line num="107" type="stmt" count="5"/>
<line num="108" type="stmt" count="5"/>
<line num="109" type="stmt" count="5"/>
<line num="110" type="stmt" count="5"/>
<line num="111" type="stmt" count="5"/>
<line num="112" type="stmt" count="5"/>
<metrics loc="113" ncloc="92" classes="1" methods="7" coveredmethods="7" conditionals="0" coveredconditionals="0" statements="46" coveredstatements="46" elements="53" coveredelements="53"/>
</file>
<file name="C:\projects\astar\src/Grid.php">
<class name="Grid" namespace="BlackScorp\Astar">
<metrics complexity="9" methods="3" coveredmethods="3" conditionals="0" coveredconditionals="0" statements="29" coveredstatements="29" elements="32" coveredelements="32"/>
</class>
<line num="9" type="method" name="__construct" visibility="public" complexity="3" crap="3" count="6"/>
<line num="11" type="stmt" count="6"/>
<line num="12" type="stmt" count="6"/>
<line num="13" type="stmt" count="6"/>
<line num="14" type="stmt" count="6"/>
<line num="15" type="stmt" count="6"/>
<line num="16" type="stmt" count="6"/>
<line num="22" type="method" name="getPoint" visibility="public" complexity="2" crap="2" count="6"/>
<line num="24" type="stmt" count="6"/>
<line num="32" type="method" name="getNeighbors" visibility="public" complexity="4" crap="4" count="6"/>
<line num="34" type="stmt" count="6"/>
<line num="35" type="stmt" count="6"/>
<line num="36" type="stmt" count="6"/>
<line num="39" type="stmt" count="6"/>
<line num="40" type="stmt" count="6"/>
<line num="41" type="stmt" count="6"/>
<line num="42" type="stmt" count="6"/>
<line num="43" type="stmt" count="6"/>
<line num="44" type="stmt" count="6"/>
<line num="45" type="stmt" count="1"/>
<line num="46" type="stmt" count="1"/>
<line num="47" type="stmt" count="1"/>
<line num="48" type="stmt" count="1"/>
<line num="49" type="stmt" count="1"/>
<line num="50" type="stmt" count="6"/>
<line num="51" type="stmt" count="6"/>
<line num="52" type="stmt" count="6"/>
<line num="53" type="stmt" count="6"/>
<line num="54" type="stmt" count="6"/>
<line num="55" type="stmt" count="6"/>
<line num="57" type="stmt" count="6"/>
<line num="58" type="stmt" count="6"/>
<metrics loc="59" ncloc="49" classes="1" methods="3" coveredmethods="3" conditionals="0" coveredconditionals="0" statements="29" coveredstatements="29" elements="32" coveredelements="32"/>
</file>
<file name="C:\projects\astar\src/Node.php">
<class name="Node" namespace="BlackScorp\Astar">
<metrics complexity="16" methods="16" coveredmethods="16" conditionals="0" coveredconditionals="0" statements="25" coveredstatements="25" elements="41" coveredelements="41"/>
</class>
<line num="17" type="method" name="__construct" visibility="public" complexity="1" crap="1" count="6"/>
<line num="19" type="stmt" count="6"/>
<line num="20" type="stmt" count="6"/>
<line num="21" type="stmt" count="6"/>
<line num="22" type="stmt" count="6"/>
<line num="27" type="method" name="getX" visibility="public" complexity="1" crap="1" count="6"/>
<line num="29" type="stmt" count="6"/>
<line num="35" type="method" name="getY" visibility="public" complexity="1" crap="1" count="6"/>
<line num="37" type="stmt" count="6"/>
<line num="43" type="method" name="getTotalScore" visibility="public" complexity="1" crap="1" count="6"/>
<line num="45" type="stmt" count="6"/>
<line num="51" type="method" name="setTotalScore" visibility="public" complexity="1" crap="1" count="6"/>
<line num="53" type="stmt" count="6"/>
<line num="54" type="stmt" count="6"/>
<line num="56" type="method" name="visit" visibility="public" complexity="1" crap="1" count="6"/>
<line num="58" type="stmt" count="6"/>
<line num="59" type="stmt" count="6"/>
<line num="61" type="method" name="close" visibility="public" complexity="1" crap="1" count="6"/>
<line num="63" type="stmt" count="6"/>
<line num="64" type="stmt" count="6"/>
<line num="69" type="method" name="setScore" visibility="public" complexity="1" crap="1" count="6"/>
<line num="71" type="stmt" count="6"/>
<line num="72" type="stmt" count="6"/>
<line num="77" type="method" name="getScore" visibility="public" complexity="1" crap="1" count="6"/>
<line num="79" type="stmt" count="6"/>
<line num="85" type="method" name="getCosts" visibility="public" complexity="1" crap="1" count="6"/>
<line num="87" type="stmt" count="6"/>
<line num="93" type="method" name="setParent" visibility="public" complexity="1" crap="1" count="6"/>
<line num="95" type="stmt" count="6"/>
<line num="96" type="stmt" count="6"/>
<line num="101" type="method" name="getParent" visibility="public" complexity="1" crap="1" count="5"/>
<line num="103" type="stmt" count="5"/>
<line num="110" type="method" name="getGuessedScore" visibility="public" complexity="1" crap="1" count="6"/>
<line num="112" type="stmt" count="6"/>
<line num="118" type="method" name="setGuessedScore" visibility="public" complexity="1" crap="1" count="6"/>
<line num="120" type="stmt" count="6"/>
<line num="121" type="stmt" count="6"/>
<line num="123" type="method" name="isClosed" visibility="public" complexity="1" crap="1" count="6"/>
<line num="125" type="stmt" count="6"/>
<line num="128" type="method" name="isVisited" visibility="public" complexity="1" crap="1" count="6"/>
<line num="130" type="stmt" count="6"/>
<metrics loc="131" ncloc="98" classes="1" methods="16" coveredmethods="16" conditionals="0" coveredconditionals="0" statements="25" coveredstatements="25" elements="41" coveredelements="41"/>
</file>
<file name="C:\projects\astar\src/ScoreHeap.php">
<class name="ScoreHeap" namespace="BlackScorp\Astar">
<metrics complexity="3" methods="1" coveredmethods="1" conditionals="0" coveredconditionals="0" statements="3" coveredstatements="3" elements="4" coveredelements="4"/>
</class>
<line num="11" type="method" name="compare" visibility="protected" complexity="3" crap="3" count="6"/>
<line num="14" type="stmt" count="6"/>
<line num="15" type="stmt" count="6"/>
<line num="17" type="stmt" count="6"/>
<metrics loc="18" ncloc="13" classes="1" methods="1" coveredmethods="1" conditionals="0" coveredconditionals="0" statements="3" coveredstatements="3" elements="4" coveredelements="4"/>
</file>
</package>
<package name="BlackScorp\Astar\Heuristic">
<file name="C:\projects\astar\src/Heuristic/Diagonal.php">
<class name="Diagonal" namespace="BlackScorp\Astar\Heuristic">
<metrics complexity="1" methods="1" coveredmethods="1" conditionals="0" coveredconditionals="0" statements="3" coveredstatements="3" elements="4" coveredelements="4"/>
</class>
<line num="9" type="method" name="compare" visibility="public" complexity="1" crap="1" count="1"/>
<line num="12" type="stmt" count="1"/>
<line num="13" type="stmt" count="1"/>
<line num="14" type="stmt" count="1"/>
<metrics loc="15" ncloc="15" classes="1" methods="1" coveredmethods="1" conditionals="0" coveredconditionals="0" statements="3" coveredstatements="3" elements="4" coveredelements="4"/>
</file>
<file name="C:\projects\astar\src/Heuristic/Euclidean.php">
<class name="Euclidean" namespace="BlackScorp\Astar\Heuristic">
<metrics complexity="1" methods="1" coveredmethods="1" conditionals="0" coveredconditionals="0" statements="3" coveredstatements="3" elements="4" coveredelements="4"/>
</class>
<line num="9" type="method" name="compare" visibility="public" complexity="1" crap="1" count="1"/>
<line num="12" type="stmt" count="1"/>
<line num="13" type="stmt" count="1"/>
<line num="14" type="stmt" count="1"/>
<metrics loc="15" ncloc="15" classes="1" methods="1" coveredmethods="1" conditionals="0" coveredconditionals="0" statements="3" coveredstatements="3" elements="4" coveredelements="4"/>
</file>
<file name="C:\projects\astar\src/Heuristic/Manhattan.php">
<class name="Manhattan" namespace="BlackScorp\Astar\Heuristic">
<metrics complexity="1" methods="1" coveredmethods="1" conditionals="0" coveredconditionals="0" statements="3" coveredstatements="3" elements="4" coveredelements="4"/>
</class>
<line num="10" type="method" name="compare" visibility="public" complexity="1" crap="1" count="4"/>
<line num="12" type="stmt" count="4"/>
<line num="13" type="stmt" count="4"/>
<line num="14" type="stmt" count="4"/>
<metrics loc="15" ncloc="15" classes="1" methods="1" coveredmethods="1" conditionals="0" coveredconditionals="0" statements="3" coveredstatements="3" elements="4" coveredelements="4"/>
</file>
</package>
<file name="C:\projects\astar\src/HeuristicInterface.php">
<metrics loc="6" ncloc="6" classes="0" methods="0" coveredmethods="0" conditionals="0" coveredconditionals="0" statements="0" coveredstatements="0" elements="0" coveredelements="0"/>
</file>
<metrics files="8" loc="372" ncloc="303" classes="7" methods="30" coveredmethods="30" conditionals="0" coveredconditionals="0" statements="112" coveredstatements="112" elements="142" coveredelements="142"/>
</project>
</coverage>

107
day_15/vendor/blackscorp/astar/readme.md vendored Normal file
View File

@ -0,0 +1,107 @@
## A-Star
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/BlackScorp/astar/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/BlackScorp/astar/?branch=master)
[![Code Coverage](https://scrutinizer-ci.com/g/BlackScorp/astar/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/BlackScorp/astar/?branch=master)
[![Build Status](https://scrutinizer-ci.com/g/BlackScorp/astar/badges/build.png?b=master)](https://scrutinizer-ci.com/g/BlackScorp/astar/build-status/master)
[A-star](https://en.wikipedia.org/wiki/A*_search_algorithm) is a path finding algorithm, written in PHP.
It can find the shortest path between two points in a two dimensional array by using different heuristics.
## Installation
~~~
composer require blackscorp/astar
~~~
## Usage
first create a two dimensional array for your map
~~~php
$map = [
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 1, 1],
[0, 0, 0, 1, 0],
];
~~~
each key represent the x and y position of the map.
each value of the array represents the costs, A-star tries to find a way with lowest costs.
you can use negative keys if your map requires it.
next convert the array to a Grid, a Grid is a collection of Nodes.
~~~php
$grid = new BlackScorp\Astar\Grid($map);
~~~
now you can fetch nodes from the Grid like so
~~~php
$startPosition = $grid->getPoint(3,1);
$endPosition = $grid->getPoint(0,0);
~~~
at the end pass the grid to the astar and search for the shortests path
~~~php
$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 $node->getY().'/'.$node->getX().'<br/>';
}
}
~~~
## Settings
by default diagonal directions are disabled, they can be enabled like so
~~~php
$astar->enableDiagonal();
~~~
as soon as the diagonal option is enabled, the algorithm use the [Manhattan](http://theory.stanford.edu/~amitp/GameProgramming/Heuristics.html) heuristic to find the shortest path.
Manhattan is not precise but the caluclation costs are low, however you can use another heuristics like Diagonal or Euclidean with following code.
~~~php
$astar->setHeuristic(new BlackScorp\Astar\Heuristic\Euclidean());
~~~
you can also create a custom heuristic.
## Block locations
there are cases where you want to block a specific path completly, independant of the costs, you can do so with following code
~~~php
astar->blocked([3,2]);
~~~
this basicly means that in the initial map
~~~php
$map = [
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 2, 2, 0, 0],
[0, 3, 0, 1, 1],
[0, 0, 0, 1, 0],
];
~~~
the values 3 and 2 cannot be passed.
## Contribute
Please feel free to make pull requests, there is still place for improvement, the Grid contains a two dimensional array which might be replaced by an SplFixedArray or something similar.
Run the tests to be sure nothing break.
## License
A-Star is free software distributed under the terms of the MIT license.

View File

@ -0,0 +1,114 @@
<?php
namespace BlackScorp\Astar;
use BlackScorp\Astar\Heuristic\Manhattan;
class Astar
{
private $diagonal = false;
private $blocked = array();
/**
* @var HeuristicInterface
*/
private $heuristic = null;
private $grid = null;
public function blocked(array $types)
{
$this->blocked = $types;
}
public function enableDiagonal()
{
$this->diagonal = true;
}
public function __construct(Grid $grid)
{
$this->grid = $grid;
}
public function setHeuristic(HeuristicInterface $heuristic)
{
$this->heuristic = $heuristic;
}
/**
* @param Node $start
* @param Node $end
* @return Node[]
*/
public function search(Node $start, Node $end)
{
if (!$this->heuristic) {
$this->setHeuristic(new Manhattan());
}
$heap = new ScoreHeap();
$heap->insert($start);
$current = $this->fillHeap($heap, $start, $end);
if ($current !== $end) {
return [];
}
return $this->getReversedPath($current);
}
/**
* @param Node $end
* @param $heap
* @param $current
* @return Node
*/
private function fillHeap(\SplHeap $heap, Node $current, Node $end)
{
while ($heap->valid() && $current !== $end) {
/**
* @var Node $current
*/
$current = $heap->extract();
$current->close();
$neighbors = $this->grid->getNeighbors($current, $this->diagonal);
foreach ($neighbors as $neighbor) {
if ($neighbor->isClosed() || in_array($neighbor->getCosts(), $this->blocked)) {
continue;
}
$score = $current->getScore() + $neighbor->getCosts();
$visited = $neighbor->isVisited();
if (!$visited || $score < $neighbor->getScore()) {
$neighbor->visit();
$neighbor->setParent($current);
$neighbor->setGuessedScore($this->heuristic->compare($neighbor, $end));
$neighbor->setScore($score);
$neighbor->setTotalScore($neighbor->getScore() + $neighbor->getGuessedScore());
if (!$visited) {
$heap->insert($neighbor);
}
}
}
}
return $current;
}
/**
* @param Node $current
* @return array
*/
private function getReversedPath(Node $current)
{
$result = [];
while ($current->getParent()) {
$result[] = $current;
$current = $current->getParent();
}
$result[]=$current;
return array_reverse($result);
}
}

View File

@ -0,0 +1,63 @@
<?php
namespace BlackScorp\Astar;
class Grid
{
/**
* @var array
*/
private $nodes = [];
public function __construct($grid)
{
foreach ($grid as $y => $cols) {
foreach ($cols as $x => $value) {
$this->nodes[$y][$x] = new Node($y, $x, $value);
}
}
}
/**
* @param $y
* @param $x
* @return Node | false
*/
public function getPoint($y, $x)
{
return isset($this->nodes[$y][$x]) ? $this->nodes[$y][$x] : false;
}
/**
* @param Node $node
* @param bool $diagonal
* @return Node[]
*/
public function getNeighbors(Node $node, $diagonal = false)
{
$result = [];
$x = $node->getX();
$y = $node->getY();
$neighbourLocations = [
[$y - 1, $x],
[$y + 1, $x],
[$y, $x - 1],
[$y, $x + 1]
];
if ($diagonal) {
$neighbourLocations[] = [$y - 1, $x - 1];
$neighbourLocations[] = [$y + 1, $x - 1];
$neighbourLocations[] = [$y - 1, $x + 1];
$neighbourLocations[] = [$y + 1, $x + 1];
}
foreach ($neighbourLocations as $location) {
list($y, $x) = $location;
$node = $this->getPoint($y, $x);
if ($node) {
$result[] = $node;
}
}
return $result;
}
}

View File

@ -0,0 +1,16 @@
<?php
namespace BlackScorp\Astar\Heuristic;
use BlackScorp\Astar\HeuristicInterface;
use BlackScorp\Astar\Node;
class Diagonal implements HeuristicInterface
{
public function compare(Node $node, Node $goal)
{
$deltaX = abs($node->getX() - $goal->getX());
$deltaY = abs($node->getY() - $goal->getY());
return max($deltaX, $deltaY);
}
}

View File

@ -0,0 +1,16 @@
<?php
namespace BlackScorp\Astar\Heuristic;
use BlackScorp\Astar\HeuristicInterface;
use BlackScorp\Astar\Node;
class Euclidean implements HeuristicInterface
{
public function compare(Node $node, Node $goal)
{
$deltaX = abs($node->getX() - $goal->getX());
$deltaY = abs($node->getY() - $goal->getY());
return sqrt($deltaX * $deltaX + $deltaY * $deltaY);
}
}

View File

@ -0,0 +1,16 @@
<?php
namespace BlackScorp\Astar\Heuristic;
use BlackScorp\Astar\HeuristicInterface;
use BlackScorp\Astar\Node;
class Manhattan implements HeuristicInterface
{
public function compare(Node $node, Node $goal)
{
$deltaX = abs($node->getX() - $goal->getX());
$deltaY = abs($node->getY() - $goal->getY());
return $deltaX + $deltaY;
}
}

View File

@ -0,0 +1,7 @@
<?php
namespace BlackScorp\Astar;
interface HeuristicInterface
{
public function compare(Node $node, Node $goal);
}

View File

@ -0,0 +1,132 @@
<?php
namespace BlackScorp\Astar;
class Node
{
private $x = 0;
private $y = 0;
private $costs = 0;
private $visited = false;
private $closed = false;
private $parent = null;
private $totalScore = 0;
private $guessedScore = 0;
private $score = 0;
public function __construct($y, $x, $costs)
{
$this->x = (int)$x;
$this->y = (int)$y;
$this->costs = (int)$costs;
}
/**
* @return int
*/
public function getX()
{
return $this->x;
}
/**
* @return int
*/
public function getY()
{
return $this->y;
}
/**
* @return int
*/
public function getTotalScore()
{
return $this->totalScore;
}
/**
* @param int $totalScore
*/
public function setTotalScore($totalScore)
{
$this->totalScore = $totalScore;
}
public function visit()
{
$this->visited = true;
}
public function close()
{
$this->closed = true;
}
/**
* @param int $score
*/
public function setScore($score)
{
$this->score = $score;
}
/**
* @return int
*/
public function getScore()
{
return $this->score;
}
/**
* @return int
*/
public function getCosts()
{
return $this->costs;
}
/**
* @param Node $parent
*/
public function setParent(Node $parent)
{
$this->parent = $parent;
}
/**
* @return null
*/
public function getParent()
{
return $this->parent;
}
/**
* @return int
*/
public function getGuessedScore()
{
return $this->guessedScore;
}
/**
* @param int $guessedScore
*/
public function setGuessedScore($guessedScore)
{
$this->guessedScore = $guessedScore;
}
public function isClosed()
{
return $this->closed === true;
}
public function isVisited()
{
return $this->visited === true;
}
}

View File

@ -0,0 +1,19 @@
<?php
namespace BlackScorp\Astar;
class ScoreHeap extends \SplHeap
{
/**
* @param Node $value1
* @param Node $value2
* @return int
*/
protected function compare($value1, $value2)
{
if ($value1->getTotalScore() === $value2->getTotalScore()) {
return 0;
}
return ($value1->getTotalScore() < $value2->getTotalScore()) ? 1 : -1;
}
}

View File

@ -0,0 +1,106 @@
<?php
use BlackScorp\Astar\Astar;
use BlackScorp\Astar\Grid;
use BlackScorp\Astar\Heuristic\Diagonal;
use BlackScorp\Astar\Heuristic\Euclidean;
require_once __DIR__ . '/../vendor/autoload.php';
class AstarTest extends PHPUnit_Framework_TestCase
{
/**
* @var Grid
*/
private $map = null;
/**
* @var Astar
*/
private $astar = null;
public function setUp()
{
$map = [
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 1, 1],
[0, 0, 0, 1, 0],
];
$this->map = new Grid($map);
$this->astar = new Astar($this->map);
}
public function testSimplePath()
{
$start = $this->map->getPoint(0, 0);
$end = $this->map->getPoint(1, 1);
$result = $this->astar->search($start, $end);
$this->assertSame(3, count($result));
}
public function testSimpleDiagonalPath()
{
$start = $this->map->getPoint(0, 0);
$end = $this->map->getPoint(1, 1);
$this->astar->enableDiagonal();
$result = $this->astar->search($start, $end);
$this->assertSame(2, count($result));
}
public function testUnreachablePath()
{
$start = $this->map->getPoint(0, 0);
$end = $this->map->getPoint(4, 4);
$this->astar->blocked(array(1));
$result = $this->astar->search($start, $end);
$this->assertEmpty($result);
}
public function testDiagonalHeuristic()
{
$start = $this->map->getPoint(0, 0);
$end = $this->map->getPoint(4, 3);
$this->astar->setHeuristic(new Diagonal());
$result = $this->astar->search($start, $end);
$this->assertSame(8, count($result));
}
public function testEuclideanHeuristic()
{
$start = $this->map->getPoint(0, 0);
$end = $this->map->getPoint(4, 3);
$this->astar->setHeuristic(new Euclidean());
$result = $this->astar->search($start, $end);
$this->assertSame(8, count($result));
}
public function testIssue2()
{
$map = [
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 2, 2, 0, 0],
[0, 3, 0, 1, 1],
[0, 0, 0, 1, 0],
];
$grid = new Grid($map);
$astar = new Astar($grid);
$astar->blocked([3, 2]);
$startPosition = $grid->getPoint(2,3);
$endPosition = $grid->getPoint(0, 0);
$result = $astar->search($startPosition, $endPosition);
$actualValues = [];
$expectedValues = [
'2-3',
'1-3',
'0-3',
'0-2',
'0-1',
'0-0',
];
foreach ($result as $node) {
$actualValues[] = sprintf('%d-%d', $node->getY(), $node->getX());
}
$this->assertSame($expectedValues,$actualValues);
}
}

File diff suppressed because one or more lines are too long

572
day_15/vendor/composer/ClassLoader.php vendored Normal file
View File

@ -0,0 +1,572 @@
<?php
/*
* This file is part of Composer.
*
* (c) Nils Adermann <naderman@naderman.de>
* Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Composer\Autoload;
/**
* ClassLoader implements a PSR-0, PSR-4 and classmap class loader.
*
* $loader = new \Composer\Autoload\ClassLoader();
*
* // register classes with namespaces
* $loader->add('Symfony\Component', __DIR__.'/component');
* $loader->add('Symfony', __DIR__.'/framework');
*
* // activate the autoloader
* $loader->register();
*
* // to enable searching the include path (eg. for PEAR packages)
* $loader->setUseIncludePath(true);
*
* In this example, if you try to use a class in the Symfony\Component
* namespace or one of its children (Symfony\Component\Console for instance),
* the autoloader will first look for the class under the component/
* directory, and it will then fallback to the framework/ directory if not
* found before giving up.
*
* This class is loosely based on the Symfony UniversalClassLoader.
*
* @author Fabien Potencier <fabien@symfony.com>
* @author Jordi Boggiano <j.boggiano@seld.be>
* @see https://www.php-fig.org/psr/psr-0/
* @see https://www.php-fig.org/psr/psr-4/
*/
class ClassLoader
{
/** @var ?string */
private $vendorDir;
// PSR-4
/**
* @var array[]
* @psalm-var array<string, array<string, int>>
*/
private $prefixLengthsPsr4 = array();
/**
* @var array[]
* @psalm-var array<string, array<int, string>>
*/
private $prefixDirsPsr4 = array();
/**
* @var array[]
* @psalm-var array<string, string>
*/
private $fallbackDirsPsr4 = array();
// PSR-0
/**
* @var array[]
* @psalm-var array<string, array<string, string[]>>
*/
private $prefixesPsr0 = array();
/**
* @var array[]
* @psalm-var array<string, string>
*/
private $fallbackDirsPsr0 = array();
/** @var bool */
private $useIncludePath = false;
/**
* @var string[]
* @psalm-var array<string, string>
*/
private $classMap = array();
/** @var bool */
private $classMapAuthoritative = false;
/**
* @var bool[]
* @psalm-var array<string, bool>
*/
private $missingClasses = array();
/** @var ?string */
private $apcuPrefix;
/**
* @var self[]
*/
private static $registeredLoaders = array();
/**
* @param ?string $vendorDir
*/
public function __construct($vendorDir = null)
{
$this->vendorDir = $vendorDir;
}
/**
* @return string[]
*/
public function getPrefixes()
{
if (!empty($this->prefixesPsr0)) {
return call_user_func_array('array_merge', array_values($this->prefixesPsr0));
}
return array();
}
/**
* @return array[]
* @psalm-return array<string, array<int, string>>
*/
public function getPrefixesPsr4()
{
return $this->prefixDirsPsr4;
}
/**
* @return array[]
* @psalm-return array<string, string>
*/
public function getFallbackDirs()
{
return $this->fallbackDirsPsr0;
}
/**
* @return array[]
* @psalm-return array<string, string>
*/
public function getFallbackDirsPsr4()
{
return $this->fallbackDirsPsr4;
}
/**
* @return string[] Array of classname => path
* @psalm-var array<string, string>
*/
public function getClassMap()
{
return $this->classMap;
}
/**
* @param string[] $classMap Class to filename map
* @psalm-param array<string, string> $classMap
*
* @return void
*/
public function addClassMap(array $classMap)
{
if ($this->classMap) {
$this->classMap = array_merge($this->classMap, $classMap);
} else {
$this->classMap = $classMap;
}
}
/**
* Registers a set of PSR-0 directories for a given prefix, either
* appending or prepending to the ones previously set for this prefix.
*
* @param string $prefix The prefix
* @param string[]|string $paths The PSR-0 root directories
* @param bool $prepend Whether to prepend the directories
*
* @return void
*/
public function add($prefix, $paths, $prepend = false)
{
if (!$prefix) {
if ($prepend) {
$this->fallbackDirsPsr0 = array_merge(
(array) $paths,
$this->fallbackDirsPsr0
);
} else {
$this->fallbackDirsPsr0 = array_merge(
$this->fallbackDirsPsr0,
(array) $paths
);
}
return;
}
$first = $prefix[0];
if (!isset($this->prefixesPsr0[$first][$prefix])) {
$this->prefixesPsr0[$first][$prefix] = (array) $paths;
return;
}
if ($prepend) {
$this->prefixesPsr0[$first][$prefix] = array_merge(
(array) $paths,
$this->prefixesPsr0[$first][$prefix]
);
} else {
$this->prefixesPsr0[$first][$prefix] = array_merge(
$this->prefixesPsr0[$first][$prefix],
(array) $paths
);
}
}
/**
* Registers a set of PSR-4 directories for a given namespace, either
* appending or prepending to the ones previously set for this namespace.
*
* @param string $prefix The prefix/namespace, with trailing '\\'
* @param string[]|string $paths The PSR-4 base directories
* @param bool $prepend Whether to prepend the directories
*
* @throws \InvalidArgumentException
*
* @return void
*/
public function addPsr4($prefix, $paths, $prepend = false)
{
if (!$prefix) {
// Register directories for the root namespace.
if ($prepend) {
$this->fallbackDirsPsr4 = array_merge(
(array) $paths,
$this->fallbackDirsPsr4
);
} else {
$this->fallbackDirsPsr4 = array_merge(
$this->fallbackDirsPsr4,
(array) $paths
);
}
} elseif (!isset($this->prefixDirsPsr4[$prefix])) {
// Register directories for a new namespace.
$length = strlen($prefix);
if ('\\' !== $prefix[$length - 1]) {
throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator.");
}
$this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length;
$this->prefixDirsPsr4[$prefix] = (array) $paths;
} elseif ($prepend) {
// Prepend directories for an already registered namespace.
$this->prefixDirsPsr4[$prefix] = array_merge(
(array) $paths,
$this->prefixDirsPsr4[$prefix]
);
} else {
// Append directories for an already registered namespace.
$this->prefixDirsPsr4[$prefix] = array_merge(
$this->prefixDirsPsr4[$prefix],
(array) $paths
);
}
}
/**
* Registers a set of PSR-0 directories for a given prefix,
* replacing any others previously set for this prefix.
*
* @param string $prefix The prefix
* @param string[]|string $paths The PSR-0 base directories
*
* @return void
*/
public function set($prefix, $paths)
{
if (!$prefix) {
$this->fallbackDirsPsr0 = (array) $paths;
} else {
$this->prefixesPsr0[$prefix[0]][$prefix] = (array) $paths;
}
}
/**
* Registers a set of PSR-4 directories for a given namespace,
* replacing any others previously set for this namespace.
*
* @param string $prefix The prefix/namespace, with trailing '\\'
* @param string[]|string $paths The PSR-4 base directories
*
* @throws \InvalidArgumentException
*
* @return void
*/
public function setPsr4($prefix, $paths)
{
if (!$prefix) {
$this->fallbackDirsPsr4 = (array) $paths;
} else {
$length = strlen($prefix);
if ('\\' !== $prefix[$length - 1]) {
throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator.");
}
$this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length;
$this->prefixDirsPsr4[$prefix] = (array) $paths;
}
}
/**
* Turns on searching the include path for class files.
*
* @param bool $useIncludePath
*
* @return void
*/
public function setUseIncludePath($useIncludePath)
{
$this->useIncludePath = $useIncludePath;
}
/**
* Can be used to check if the autoloader uses the include path to check
* for classes.
*
* @return bool
*/
public function getUseIncludePath()
{
return $this->useIncludePath;
}
/**
* Turns off searching the prefix and fallback directories for classes
* that have not been registered with the class map.
*
* @param bool $classMapAuthoritative
*
* @return void
*/
public function setClassMapAuthoritative($classMapAuthoritative)
{
$this->classMapAuthoritative = $classMapAuthoritative;
}
/**
* Should class lookup fail if not found in the current class map?
*
* @return bool
*/
public function isClassMapAuthoritative()
{
return $this->classMapAuthoritative;
}
/**
* APCu prefix to use to cache found/not-found classes, if the extension is enabled.
*
* @param string|null $apcuPrefix
*
* @return void
*/
public function setApcuPrefix($apcuPrefix)
{
$this->apcuPrefix = function_exists('apcu_fetch') && filter_var(ini_get('apc.enabled'), FILTER_VALIDATE_BOOLEAN) ? $apcuPrefix : null;
}
/**
* The APCu prefix in use, or null if APCu caching is not enabled.
*
* @return string|null
*/
public function getApcuPrefix()
{
return $this->apcuPrefix;
}
/**
* Registers this instance as an autoloader.
*
* @param bool $prepend Whether to prepend the autoloader or not
*
* @return void
*/
public function register($prepend = false)
{
spl_autoload_register(array($this, 'loadClass'), true, $prepend);
if (null === $this->vendorDir) {
return;
}
if ($prepend) {
self::$registeredLoaders = array($this->vendorDir => $this) + self::$registeredLoaders;
} else {
unset(self::$registeredLoaders[$this->vendorDir]);
self::$registeredLoaders[$this->vendorDir] = $this;
}
}
/**
* Unregisters this instance as an autoloader.
*
* @return void
*/
public function unregister()
{
spl_autoload_unregister(array($this, 'loadClass'));
if (null !== $this->vendorDir) {
unset(self::$registeredLoaders[$this->vendorDir]);
}
}
/**
* Loads the given class or interface.
*
* @param string $class The name of the class
* @return true|null True if loaded, null otherwise
*/
public function loadClass($class)
{
if ($file = $this->findFile($class)) {
includeFile($file);
return true;
}
return null;
}
/**
* Finds the path to the file where the class is defined.
*
* @param string $class The name of the class
*
* @return string|false The path if found, false otherwise
*/
public function findFile($class)
{
// class map lookup
if (isset($this->classMap[$class])) {
return $this->classMap[$class];
}
if ($this->classMapAuthoritative || isset($this->missingClasses[$class])) {
return false;
}
if (null !== $this->apcuPrefix) {
$file = apcu_fetch($this->apcuPrefix.$class, $hit);
if ($hit) {
return $file;
}
}
$file = $this->findFileWithExtension($class, '.php');
// Search for Hack files if we are running on HHVM
if (false === $file && defined('HHVM_VERSION')) {
$file = $this->findFileWithExtension($class, '.hh');
}
if (null !== $this->apcuPrefix) {
apcu_add($this->apcuPrefix.$class, $file);
}
if (false === $file) {
// Remember that this class does not exist.
$this->missingClasses[$class] = true;
}
return $file;
}
/**
* Returns the currently registered loaders indexed by their corresponding vendor directories.
*
* @return self[]
*/
public static function getRegisteredLoaders()
{
return self::$registeredLoaders;
}
/**
* @param string $class
* @param string $ext
* @return string|false
*/
private function findFileWithExtension($class, $ext)
{
// PSR-4 lookup
$logicalPathPsr4 = strtr($class, '\\', DIRECTORY_SEPARATOR) . $ext;
$first = $class[0];
if (isset($this->prefixLengthsPsr4[$first])) {
$subPath = $class;
while (false !== $lastPos = strrpos($subPath, '\\')) {
$subPath = substr($subPath, 0, $lastPos);
$search = $subPath . '\\';
if (isset($this->prefixDirsPsr4[$search])) {
$pathEnd = DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $lastPos + 1);
foreach ($this->prefixDirsPsr4[$search] as $dir) {
if (file_exists($file = $dir . $pathEnd)) {
return $file;
}
}
}
}
}
// PSR-4 fallback dirs
foreach ($this->fallbackDirsPsr4 as $dir) {
if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr4)) {
return $file;
}
}
// PSR-0 lookup
if (false !== $pos = strrpos($class, '\\')) {
// namespaced class name
$logicalPathPsr0 = substr($logicalPathPsr4, 0, $pos + 1)
. strtr(substr($logicalPathPsr4, $pos + 1), '_', DIRECTORY_SEPARATOR);
} else {
// PEAR-like class name
$logicalPathPsr0 = strtr($class, '_', DIRECTORY_SEPARATOR) . $ext;
}
if (isset($this->prefixesPsr0[$first])) {
foreach ($this->prefixesPsr0[$first] as $prefix => $dirs) {
if (0 === strpos($class, $prefix)) {
foreach ($dirs as $dir) {
if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) {
return $file;
}
}
}
}
}
// PSR-0 fallback dirs
foreach ($this->fallbackDirsPsr0 as $dir) {
if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) {
return $file;
}
}
// PSR-0 include paths.
if ($this->useIncludePath && $file = stream_resolve_include_path($logicalPathPsr0)) {
return $file;
}
return false;
}
}
/**
* Scope isolated include.
*
* Prevents access to $this/self from included files.
*
* @param string $file
* @return void
* @private
*/
function includeFile($file)
{
include $file;
}

View File

@ -0,0 +1,337 @@
<?php
/*
* This file is part of Composer.
*
* (c) Nils Adermann <naderman@naderman.de>
* Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Composer;
use Composer\Autoload\ClassLoader;
use Composer\Semver\VersionParser;
/**
* This class is copied in every Composer installed project and available to all
*
* See also https://getcomposer.org/doc/07-runtime.md#installed-versions
*
* To require its presence, you can require `composer-runtime-api ^2.0`
*/
class InstalledVersions
{
private static $installed;
private static $canGetVendors;
private static $installedByVendor = array();
/**
* Returns a list of all package names which are present, either by being installed, replaced or provided
*
* @return string[]
* @psalm-return list<string>
*/
public static function getInstalledPackages()
{
$packages = array();
foreach (self::getInstalled() as $installed) {
$packages[] = array_keys($installed['versions']);
}
if (1 === \count($packages)) {
return $packages[0];
}
return array_keys(array_flip(\call_user_func_array('array_merge', $packages)));
}
/**
* Returns a list of all package names with a specific type e.g. 'library'
*
* @param string $type
* @return string[]
* @psalm-return list<string>
*/
public static function getInstalledPackagesByType($type)
{
$packagesByType = array();
foreach (self::getInstalled() as $installed) {
foreach ($installed['versions'] as $name => $package) {
if (isset($package['type']) && $package['type'] === $type) {
$packagesByType[] = $name;
}
}
}
return $packagesByType;
}
/**
* Checks whether the given package is installed
*
* This also returns true if the package name is provided or replaced by another package
*
* @param string $packageName
* @param bool $includeDevRequirements
* @return bool
*/
public static function isInstalled($packageName, $includeDevRequirements = true)
{
foreach (self::getInstalled() as $installed) {
if (isset($installed['versions'][$packageName])) {
return $includeDevRequirements || empty($installed['versions'][$packageName]['dev_requirement']);
}
}
return false;
}
/**
* Checks whether the given package satisfies a version constraint
*
* e.g. If you want to know whether version 2.3+ of package foo/bar is installed, you would call:
*
* Composer\InstalledVersions::satisfies(new VersionParser, 'foo/bar', '^2.3')
*
* @param VersionParser $parser Install composer/semver to have access to this class and functionality
* @param string $packageName
* @param string|null $constraint A version constraint to check for, if you pass one you have to make sure composer/semver is required by your package
* @return bool
*/
public static function satisfies(VersionParser $parser, $packageName, $constraint)
{
$constraint = $parser->parseConstraints($constraint);
$provided = $parser->parseConstraints(self::getVersionRanges($packageName));
return $provided->matches($constraint);
}
/**
* Returns a version constraint representing all the range(s) which are installed for a given package
*
* It is easier to use this via isInstalled() with the $constraint argument if you need to check
* whether a given version of a package is installed, and not just whether it exists
*
* @param string $packageName
* @return string Version constraint usable with composer/semver
*/
public static function getVersionRanges($packageName)
{
foreach (self::getInstalled() as $installed) {
if (!isset($installed['versions'][$packageName])) {
continue;
}
$ranges = array();
if (isset($installed['versions'][$packageName]['pretty_version'])) {
$ranges[] = $installed['versions'][$packageName]['pretty_version'];
}
if (array_key_exists('aliases', $installed['versions'][$packageName])) {
$ranges = array_merge($ranges, $installed['versions'][$packageName]['aliases']);
}
if (array_key_exists('replaced', $installed['versions'][$packageName])) {
$ranges = array_merge($ranges, $installed['versions'][$packageName]['replaced']);
}
if (array_key_exists('provided', $installed['versions'][$packageName])) {
$ranges = array_merge($ranges, $installed['versions'][$packageName]['provided']);
}
return implode(' || ', $ranges);
}
throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
}
/**
* @param string $packageName
* @return string|null If the package is being replaced or provided but is not really installed, null will be returned as version, use satisfies or getVersionRanges if you need to know if a given version is present
*/
public static function getVersion($packageName)
{
foreach (self::getInstalled() as $installed) {
if (!isset($installed['versions'][$packageName])) {
continue;
}
if (!isset($installed['versions'][$packageName]['version'])) {
return null;
}
return $installed['versions'][$packageName]['version'];
}
throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
}
/**
* @param string $packageName
* @return string|null If the package is being replaced or provided but is not really installed, null will be returned as version, use satisfies or getVersionRanges if you need to know if a given version is present
*/
public static function getPrettyVersion($packageName)
{
foreach (self::getInstalled() as $installed) {
if (!isset($installed['versions'][$packageName])) {
continue;
}
if (!isset($installed['versions'][$packageName]['pretty_version'])) {
return null;
}
return $installed['versions'][$packageName]['pretty_version'];
}
throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
}
/**
* @param string $packageName
* @return string|null If the package is being replaced or provided but is not really installed, null will be returned as reference
*/
public static function getReference($packageName)
{
foreach (self::getInstalled() as $installed) {
if (!isset($installed['versions'][$packageName])) {
continue;
}
if (!isset($installed['versions'][$packageName]['reference'])) {
return null;
}
return $installed['versions'][$packageName]['reference'];
}
throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
}
/**
* @param string $packageName
* @return string|null If the package is being replaced or provided but is not really installed, null will be returned as install path. Packages of type metapackages also have a null install path.
*/
public static function getInstallPath($packageName)
{
foreach (self::getInstalled() as $installed) {
if (!isset($installed['versions'][$packageName])) {
continue;
}
return isset($installed['versions'][$packageName]['install_path']) ? $installed['versions'][$packageName]['install_path'] : null;
}
throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
}
/**
* @return array
* @psalm-return array{name: string, version: string, reference: string, pretty_version: string, aliases: string[], dev: bool, install_path: string, type: string}
*/
public static function getRootPackage()
{
$installed = self::getInstalled();
return $installed[0]['root'];
}
/**
* Returns the raw installed.php data for custom implementations
*
* @deprecated Use getAllRawData() instead which returns all datasets for all autoloaders present in the process. getRawData only returns the first dataset loaded, which may not be what you expect.
* @return array[]
* @psalm-return array{root: array{name: string, version: string, reference: string, pretty_version: string, aliases: string[], dev: bool, install_path: string, type: string}, versions: array<string, array{dev_requirement: bool, pretty_version?: string, version?: string, aliases?: string[], reference?: string, replaced?: string[], provided?: string[], install_path?: string, type?: string}>}
*/
public static function getRawData()
{
@trigger_error('getRawData only returns the first dataset loaded, which may not be what you expect. Use getAllRawData() instead which returns all datasets for all autoloaders present in the process.', E_USER_DEPRECATED);
if (null === self::$installed) {
// only require the installed.php file if this file is loaded from its dumped location,
// and not from its source location in the composer/composer package, see https://github.com/composer/composer/issues/9937
if (substr(__DIR__, -8, 1) !== 'C') {
self::$installed = include __DIR__ . '/installed.php';
} else {
self::$installed = array();
}
}
return self::$installed;
}
/**
* Returns the raw data of all installed.php which are currently loaded for custom implementations
*
* @return array[]
* @psalm-return list<array{root: array{name: string, version: string, reference: string, pretty_version: string, aliases: string[], dev: bool, install_path: string, type: string}, versions: array<string, array{dev_requirement: bool, pretty_version?: string, version?: string, aliases?: string[], reference?: string, replaced?: string[], provided?: string[], install_path?: string, type?: string}>}>
*/
public static function getAllRawData()
{
return self::getInstalled();
}
/**
* Lets you reload the static array from another file
*
* This is only useful for complex integrations in which a project needs to use
* this class but then also needs to execute another project's autoloader in process,
* and wants to ensure both projects have access to their version of installed.php.
*
* A typical case would be PHPUnit, where it would need to make sure it reads all
* the data it needs from this class, then call reload() with
* `require $CWD/vendor/composer/installed.php` (or similar) as input to make sure
* the project in which it runs can then also use this class safely, without
* interference between PHPUnit's dependencies and the project's dependencies.
*
* @param array[] $data A vendor/composer/installed.php data set
* @return void
*
* @psalm-param array{root: array{name: string, version: string, reference: string, pretty_version: string, aliases: string[], dev: bool, install_path: string, type: string}, versions: array<string, array{dev_requirement: bool, pretty_version?: string, version?: string, aliases?: string[], reference?: string, replaced?: string[], provided?: string[], install_path?: string, type?: string}>} $data
*/
public static function reload($data)
{
self::$installed = $data;
self::$installedByVendor = array();
}
/**
* @return array[]
* @psalm-return list<array{root: array{name: string, version: string, reference: string, pretty_version: string, aliases: string[], dev: bool, install_path: string, type: string}, versions: array<string, array{dev_requirement: bool, pretty_version?: string, version?: string, aliases?: string[], reference?: string, replaced?: string[], provided?: string[], install_path?: string, type?: string}>}>
*/
private static function getInstalled()
{
if (null === self::$canGetVendors) {
self::$canGetVendors = method_exists('Composer\Autoload\ClassLoader', 'getRegisteredLoaders');
}
$installed = array();
if (self::$canGetVendors) {
foreach (ClassLoader::getRegisteredLoaders() as $vendorDir => $loader) {
if (isset(self::$installedByVendor[$vendorDir])) {
$installed[] = self::$installedByVendor[$vendorDir];
} elseif (is_file($vendorDir.'/composer/installed.php')) {
$installed[] = self::$installedByVendor[$vendorDir] = require $vendorDir.'/composer/installed.php';
if (null === self::$installed && strtr($vendorDir.'/composer', '\\', '/') === strtr(__DIR__, '\\', '/')) {
self::$installed = $installed[count($installed) - 1];
}
}
}
}
if (null === self::$installed) {
// only require the installed.php file if this file is loaded from its dumped location,
// and not from its source location in the composer/composer package, see https://github.com/composer/composer/issues/9937
if (substr(__DIR__, -8, 1) !== 'C') {
self::$installed = require __DIR__ . '/installed.php';
} else {
self::$installed = array();
}
}
$installed[] = self::$installed;
return $installed;
}
}

21
day_15/vendor/composer/LICENSE vendored Normal file
View File

@ -0,0 +1,21 @@
Copyright (c) Nils Adermann, Jordi Boggiano
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

View File

@ -0,0 +1,10 @@
<?php
// autoload_classmap.php @generated by Composer
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
'Composer\\InstalledVersions' => $vendorDir . '/composer/InstalledVersions.php',
);

View File

@ -0,0 +1,9 @@
<?php
// autoload_namespaces.php @generated by Composer
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
);

View File

@ -0,0 +1,10 @@
<?php
// autoload_psr4.php @generated by Composer
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
'BlackScorp\\Astar\\' => array($vendorDir . '/blackscorp/astar/src'),
);

View File

@ -0,0 +1,55 @@
<?php
// autoload_real.php @generated by Composer
class ComposerAutoloaderInit9d031d4e152710d80f5f020c3fb1552c
{
private static $loader;
public static function loadClassLoader($class)
{
if ('Composer\Autoload\ClassLoader' === $class) {
require __DIR__ . '/ClassLoader.php';
}
}
/**
* @return \Composer\Autoload\ClassLoader
*/
public static function getLoader()
{
if (null !== self::$loader) {
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInit9d031d4e152710d80f5f020c3fb1552c', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(\dirname(__FILE__)));
spl_autoload_unregister(array('ComposerAutoloaderInit9d031d4e152710d80f5f020c3fb1552c', 'loadClassLoader'));
$useStaticLoader = PHP_VERSION_ID >= 50600 && !defined('HHVM_VERSION') && (!function_exists('zend_loader_file_encoded') || !zend_loader_file_encoded());
if ($useStaticLoader) {
require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInit9d031d4e152710d80f5f020c3fb1552c::getInitializer($loader));
} else {
$map = require __DIR__ . '/autoload_namespaces.php';
foreach ($map as $namespace => $path) {
$loader->set($namespace, $path);
}
$map = require __DIR__ . '/autoload_psr4.php';
foreach ($map as $namespace => $path) {
$loader->setPsr4($namespace, $path);
}
$classMap = require __DIR__ . '/autoload_classmap.php';
if ($classMap) {
$loader->addClassMap($classMap);
}
}
$loader->register(true);
return $loader;
}
}

View File

@ -0,0 +1,36 @@
<?php
// autoload_static.php @generated by Composer
namespace Composer\Autoload;
class ComposerStaticInit9d031d4e152710d80f5f020c3fb1552c
{
public static $prefixLengthsPsr4 = array (
'B' =>
array (
'BlackScorp\\Astar\\' => 17,
),
);
public static $prefixDirsPsr4 = array (
'BlackScorp\\Astar\\' =>
array (
0 => __DIR__ . '/..' . '/blackscorp/astar/src',
),
);
public static $classMap = array (
'Composer\\InstalledVersions' => __DIR__ . '/..' . '/composer/InstalledVersions.php',
);
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInit9d031d4e152710d80f5f020c3fb1552c::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit9d031d4e152710d80f5f020c3fb1552c::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit9d031d4e152710d80f5f020c3fb1552c::$classMap;
}, null, ClassLoader::class);
}
}

46
day_15/vendor/composer/installed.json vendored Normal file
View File

@ -0,0 +1,46 @@
{
"packages": [
{
"name": "blackscorp/astar",
"version": "v1.2.0",
"version_normalized": "1.2.0.0",
"source": {
"type": "git",
"url": "https://github.com/BlackScorp/astar.git",
"reference": "9b80d6135ac31947d246cf2e33ff45d4681a6a57"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/BlackScorp/astar/zipball/9b80d6135ac31947d246cf2e33ff45d4681a6a57",
"reference": "9b80d6135ac31947d246cf2e33ff45d4681a6a57",
"shasum": ""
},
"time": "2017-10-06T15:21:22+00:00",
"type": "library",
"installation-source": "dist",
"autoload": {
"psr-4": {
"BlackScorp\\Astar\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Vitalij Mik",
"email": "cccpmik+composer@gmail.com"
}
],
"description": "AStar path finding implementation in php",
"support": {
"issues": "https://github.com/BlackScorp/astar/issues",
"source": "https://github.com/BlackScorp/astar/tree/v1.2.0"
},
"install-path": "../blackscorp/astar"
}
],
"dev": true,
"dev-package-names": []
}

32
day_15/vendor/composer/installed.php vendored Normal file
View File

@ -0,0 +1,32 @@
<?php return array(
'root' => array(
'pretty_version' => 'dev-main',
'version' => 'dev-main',
'type' => 'library',
'install_path' => __DIR__ . '/../../',
'aliases' => array(),
'reference' => '637e029dc17e542854f9104c381c6ef6bdf5d137',
'name' => '__root__',
'dev' => true,
),
'versions' => array(
'__root__' => array(
'pretty_version' => 'dev-main',
'version' => 'dev-main',
'type' => 'library',
'install_path' => __DIR__ . '/../../',
'aliases' => array(),
'reference' => '637e029dc17e542854f9104c381c6ef6bdf5d137',
'dev_requirement' => false,
),
'blackscorp/astar' => array(
'pretty_version' => 'v1.2.0',
'version' => '1.2.0.0',
'type' => 'library',
'install_path' => __DIR__ . '/../blackscorp/astar',
'aliases' => array(),
'reference' => '9b80d6135ac31947d246cf2e33ff45d4681a6a57',
'dev_requirement' => false,
),
),
);