Skip to content

Instantly share code, notes, and snippets.

@f2k1de
Created January 18, 2020 17:38
Show Gist options
  • Save f2k1de/f1f2652019e5ac4e7df8f61e99e30d98 to your computer and use it in GitHub Desktop.
Save f2k1de/f1f2652019e5ac4e7df8f61e99e30d98 to your computer and use it in GitHub Desktop.
<?php
define('DEPARTURE', 2);
define('STATIONNAME', 0);
define('STATIONNAMENEXT', 1);
define('DURATION', 3);
$array = array();
for($i = 0; $i < 1254; $i++) {
$file = file_get_contents("ice_zugläufe/". $i . ".txt");
if($file != "") {
$file = json_decode($file, true);
$array[$i]['train'] = $file['train'];
$array[$i]['stops'] = $file['stops'];
for($j = 0; $j < count($array[$i]['stops']); $j++) {
@$array[$i]['stops'][$j]['departure']['scheduledTime'] = date("H:i", ($array[$i]['stops'][$j]['departure']['scheduledTime'])/1000);
@$array[$i]['stops'][$j]['arrival']['scheduledTime'] = date("H:i", ($array[$i]['stops'][$j]['arrival']['scheduledTime'])/1000);
unset($array[$i]['stops'][$j]['arrival']['time']);
unset($array[$i]['stops'][$j]['arrival']['reihung']);
unset($array[$i]['stops'][$j]['departure']['time']);
unset($array[$i]['stops'][$j]['departure']['reihung']);
}
}
}
//print_r($array);
$array2 = array();
for($i = 0; $i < count($array); $i++) {
if(isset($array[$i]['train']['name'])) {
$train = $array[$i]['train']['name'];
for($j = 0; $j < count($array[$i]['stops']) - 1; $j++) {
$stationname = $array[$i]['stops'][$j]['station']['id'];
$stationnamenext = $array[$i]['stops'][$j+1]['station']['id'];
$departure = parseTimeToSec($array[$i]['stops'][$j]['departure']['scheduledTime']);
$duration = parseTimeToSec($array[$i]['stops'][$j+1]['arrival']['scheduledTime']) - $departure;
if($duration < 0) {
$duration = $duration + 1400;
}
$array2[] = $stationname . " " . $stationnamenext . " " . $departure . " " . $duration . " " . $train . "\n";
}
//sleep(1);
}
}
for($i = 0; $i < count($array2); $i++) {
}
usort($array2, "cmp2");
/*
//print_r($array2);
$output = "";
for($i = 0; $i < count($array2); $i++) {
$output = $output . $array2[$i];
}
//file_put_contents("testdatareal.txt", $output);
*/
function parseTimeToSec($uhrzeit) {
//echo $uhrzeit;
$stunden = substr($uhrzeit, 0, 2);
$minuten = substr($uhrzeit, 3, 2);
$insminuten = $minuten + ($stunden * 60);
return $insminuten;
}
for($i = 0; $i < count($array2); $i++) {
$array2[$i] = explode(" ", $array2[$i]);
}
//print_r($array2);
function gothroughlistandreturnnextstopsintwohours($station, $zeitpunkt, $fahrplan, $checkedStations, $stationsToCheck, $routes) {
$temparr = array();
for($i = 0; $i < count($fahrplan); $i++) {
//echo $i;
// Alle Abfahrten vor der Losfahrtzeit ignorieren
if($fahrplan[$i][DEPARTURE] > $zeitpunkt) {
// Nur die Fahrten aus dem Fahrplan unserer Abfahrtstation nehmen.
if($fahrplan[$i][STATIONNAME] == $station) {
//print_r($fahrplan[$i]);
// Nur wenn Station nicht schon mal gecheckt, checken, da sonst Kreisfahrten
$gefunden = false;
for($j = 0; $j < count($checkedStations); $j++) {
if($fahrplan[$i][STATIONNAMENEXT] == $checkedStations[$j]['station']) {
if(($fahrplan[$i][DEPARTURE]) > $checkedStations[$j]['time']) {
$gefunden = true;
}
}
}
//if(! in_array($fahrplan[$i][STATIONNAMENEXT], $checkedStations)) {
if($gefunden == false) {
$temparr[] = $fahrplan[$i];
$found = false;
$prevstop = "";
$prevtrain = "";
// Überprüfen ob schon in der Queue für weitere Stationen zu überprüfen
for($j = 0; $j < count($stationsToCheck); $j++) {
if($fahrplan[$i][STATIONNAMENEXT] == $stationsToCheck[$j]['station']) {
$found = true;
}
if($fahrplan[$i][STATIONNAME] == $stationsToCheck[$j]['station']) {
@$prevstop = $stationsToCheck[$j]['previousstop'];
@$prevtrain = $stationsToCheck[$j]['prevtrain'];
}
}
if($found == false) {
$stationsToCheck[] = ['station' => $fahrplan[$i][STATIONNAMENEXT],
'time' => $fahrplan[$i][DEPARTURE] + $fahrplan[$i][DURATION],
'previousstop' => $prevstop . "," . $fahrplan[$i][STATIONNAME],
'prevtrain' => $prevtrain . "," . str_replace("\n", "", $fahrplan[$i][5])];
}
/* $routes[] = ['from' => $fahrplan[$i][STATIONNAME],
'to' => $fahrplan[$i][STATIONNAMENEXT],
'time' => $fahrplan[$i][DEPARTURE],
'duration' => $fahrplan[$i][DURATION]];*/
}
// Nach 120 Minuten abbrechen
if($fahrplan[$i][DEPARTURE] > $zeitpunkt+240) {
break(1);
}
}
}
}
$helparray = ['fahrplan' => $temparr, 'stationstocheck' => $stationsToCheck, 'routes' => $routes];
return $helparray;
}
function getdepraturefortraininstation($fahrplan, $station, $zug) {
for($i = 0; $i < count($fahrplan); $i++) {
if($fahrplan[$i][STATIONNAME] == $station) {
if($fahrplan[$i][5] == $zug) {
return $fahrplan[$i][DEPARTURE];
}
}
}
}
$departuretime = "1";
$departureStation = '8000191';
$arrivialStation = '8010404';
search($departuretime, $departureStation, $arrivialStation, $array2);
function search($departuretime, $departureStation, $arrivialStation, $array2) {
$routes = array();
//$checkedStations[] = $departureStation;
$checkedStations = array();
$stationsToCheck[] = ['station' => $departureStation,
'time' => $departuretime];
for($i = 0; $i < count($stationsToCheck); $i++) {
$checkedStations[] = ['station' => $stationsToCheck[$i]['station'],
'time' => $stationsToCheck[$i]['time']];
//echo $i;
if($stationsToCheck[$i]['station'] == $arrivialStation) {
echo "\nFound a way! ";
echo "Time: " . $stationsToCheck[$i]['time'];
echo "Prev: " . $stationsToCheck[$i]['previousstop'];
echo "Pref train: " . $stationsToCheck[$i]['prevtrain'];
//print_r (getdepraturefortraininstation($array2, $departureStation, explode(",", $stationsToCheck[$i]['prevtrain'])[1]));
search($stationsToCheck[$i]['time']+1, $departureStation, $arrivialStation, $array2);
//break;
}
$result = gothroughlistandreturnnextstopsintwohours($stationsToCheck[$i]['station'], $stationsToCheck[$i]['time'], $array2, $checkedStations, $stationsToCheck, $routes);
//$routes = $result['routes'];
$stationsToCheck = $result['stationstocheck'];
/*
for($j = 0; $j < count($result); $j++) {
//echo $result[$j][STATIONNAMENEXT] . "\n";
if(in_array($result[$j][STATIONNAMENEXT], $checkedStations) === FALSE) {
//echo $i;
$stationsToCheck[] = ['station' => $result[$j][STATIONNAMENEXT],
'time' => $result[$j][2] + $result[$j][3]];
}
}*/
//$checkedStationsPath[] =
}
}
//print_r( $routes);
//print_r($checkedStations);
/*
//print_r($array);
$result = mitEinemUmstieg("Stuttgart Hbf", "Karlsruhe Hbf", $array);
print_r($result);
function ohneUmstieg($starthalt, $zielhalt, $array) {
$returnarray = array();
// Gehe durch alle Zugläufe
for($i = 0; $i < count($array); $i++) {
// Überprüfe ob Zuglauf überhaupt existiert
if(isset($array[$i]['stops'][0])) {
// Gehe durch alle Stops in einem Zuglauf
for($j = 0; $j < count($array[$i]['stops']); $j++) {
// Prüfe ob der ein Starthalt ist
if($array[$i]['stops'][$j]['station']['title'] == $starthalt) {
// Gehe dann durch die verbleiben Stops und schaue ob Zielhalt ist.
for($k = $j; $k < count($array[$i]['stops']); $k++) {
if($array[$i]['stops'][$k]['station']['title'] == $zielhalt) {
//Direktverbindung gefunden
$returnarray[] = $array[$i];
}
}
}
}
}
}
return $returnarray;
}
function mitEinemUmstieg($starthalt, $zielhalt, $array) {
$returnarray = array();
$allezugemitstart = array();
$allezugemitziel = array();
// Gehe durch alle Zugläufe
for($i = 0; $i < count($array); $i++) {
// Überprüfe ob Zuglauf überhaupt existiert
if(isset($array[$i]['stops'][0])) {
// Gehe durch alle Stops in einem Zuglauf
for($j = 0; $j < count($array[$i]['stops']); $j++) {
// Prüfe ob der ein Starthalt ist
if($array[$i]['stops'][$j]['station']['title'] == $starthalt) {
$allezugemitstart[] = $array[$i];
}
if($array[$i]['stops'][$j]['station']['title'] == $zielhalt) {
$allezugemitziel[] = $array[$i];
}
}
}
}
for($i = 0; $i < count($allezugemitstart); $i++) {
for($j = 0; $j < count($allezugemitstart[$i]['stops']); $j++) {
for($k = 0; $k < count($allezugemitziel); $k++) {
for($l = 0; $l < count($allezugemitziel[$k]['stops']); $l++) {
if(($allezugemitstart[$i]['stops'][$j]['station']['title']) == ($allezugemitziel[$k]['stops'][$l]['station']['title'])) {
for($m = 0; $m < count($allezugemitstart[$i]['stops']); $m++) {
if($allezugemitstart[$i]['stops'][$m]['station']['title'] == $starthalt) {
$startzeit = $allezugemitstart[$i]['stops'][$m]['departure']['scheduledTime'];
}
}
for($m = 0; $m < count($allezugemitziel[$i]['stops']); $m++) {
if($allezugemitziel[$i]['stops'][$m]['station']['title'] == $zielhalt) {
$zielzeit = $allezugemitziel[$i]['stops'][$m]['arrival']['scheduledTime'];
}
}
if(isset($returnarray[10])) {
$timeten = $returnarray[10]['duration'];
if(strtotime($timeten) > (strtotime($zielzeit) - strtotime($startzeit))) {
$returnarray[] = ['duration' => date("H:i", (strtotime($zielzeit) - strtotime($startzeit))),
'1' => $allezugemitstart[$i],
'2' => $allezugemitziel[$k]];
//usort($returnarray, "cmp");
}
} else {
$returnarray[] = ['duration' => date("H:i", (strtotime($zielzeit) - strtotime($startzeit))),
'1' => $allezugemitstart[$i],
'2' => $allezugemitziel[$k]];
// usort($returnarray, "cmp");
}
}
}
}
}
}
$returnhelparray = array();
$duration = strtotime("23:59");
for($i = 0; $i < count($returnarray); $i++) {
if($returnarray[$i]['duration'] < $duration) {
$duration = $returnarray[$i]['duration'];
$returnhelparray[] = $returnarray[$i];
if(count($returnhelparray) > 10) {
unset($returnhelparray[0]);
array_values($returnhelparray);
}
}
}
return $returnhelparray;
}
function cmp($a, $b)
{
if (strtotime($a['duration']) == strtotime($b['duration'])) {
return 0;
}
return (strtotime($a['duration']) < strtotime($b['duration'])) ? -1 : 1;
}
*/
function cmp2($a, $b)
{
$a = explode(" ", $a)[2];
$b = explode(" ", $b)[2];
if ($a == $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment