/** * @test */ public function calculatesLongestRouteDistance() { $cityMap = new CityMap(); $cityMap->setCityDistance($this->london, $this->dublin, 464); $cityMap->setCityDistance($this->london, $this->belfast, 518); $cityMap->setCityDistance($this->dublin, $this->belfast, 141); $this->assertSame(982, $this->calculator->longestRouteDistance($cityMap)); }
/** * Calculates and returns the longest route's distance for a given {@link CityMap} * @param CityMap $cityMap * @return int distance of longest route */ public function longestRouteDistance(CityMap $cityMap) { $adjacencyMatrix = $cityMap->getAdjacencyMatrix(); $cities = array_keys($adjacencyMatrix); $distances = []; foreach ($cities as $city) { $distances[] = $this->recursiveCost($adjacencyMatrix, $city, $cities, static::MAX); } return max($distances); }
/** * Creates a {@link CityMap} from given input text * @param string $input one pair of cities per line * @return CityMap */ public function parse($input) { $cityMap = new CityMap(); foreach (explode("\n", $input) as $line) { $isMatched = preg_match("/(.+) to (.+) = (\\d+)/", $line, $matches); if ($isMatched !== 1) { throw new InvalidInputFormatException("Invalid input line: {$line}"); } $cityMap->setCityDistance($matches[1], $matches[2], intval($matches[3])); } return $cityMap; }
/** * @test */ public function parsesCityMap() { $input = <<<INPUT London to Dublin = 464 London to Belfast = 518 Dublin to Belfast = 141 INPUT; $expected = new CityMap(); $expected->setCityDistance(static::LONDON, static::DUBLIN, 464); $expected->setCityDistance(static::LONDON, static::BELFAST, 518); $expected->setCityDistance(static::DUBLIN, static::BELFAST, 141); $this->assertEquals($expected, static::$parser->parse($input)); }
/** * {@inheritdoc} */ public static function GetKeyMaps() { if (self::$KM == null) { self::$KM = array(); self::$KM["fk_adress_city"] = new KeyMap("fk_adress_city", "Idcity", "Address", "City", KM_TYPE_ONETOMANY, KM_LOAD_LAZY); // use KM_LOAD_EAGER with caution here (one-to-one relationships only) self::$KM["fk_placelocation_city"] = new KeyMap("fk_placelocation_city", "Idcity", "PlaceLocation", "City", KM_TYPE_ONETOMANY, KM_LOAD_LAZY); // use KM_LOAD_EAGER with caution here (one-to-one relationships only) self::$KM["fk_city_institution1"] = new KeyMap("fk_city_institution1", "Institution", "Institution", "Idinstitution", KM_TYPE_MANYTOONE, KM_LOAD_LAZY); // you change to KM_LOAD_EAGER here or (preferrably) make the change in _config.php self::$KM["fk_city_state1"] = new KeyMap("fk_city_state1", "State", "State", "Idstate", KM_TYPE_MANYTOONE, KM_LOAD_LAZY); // you change to KM_LOAD_EAGER here or (preferrably) make the change in _config.php } return self::$KM; }