-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday9.php
More file actions
executable file
·224 lines (181 loc) · 5.83 KB
/
day9.php
File metadata and controls
executable file
·224 lines (181 loc) · 5.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
ini_set("memory_limit","256M");
/**
* dispacer, ktory bude manazovat vytvaraenie ciest a vyhodnocovat najkratsie
*/
class dispacer
{
protected $roadNet = null;
protected $cities = array();
protected $roads = array();
function __construct($roadNet, $cities)
{
$this->roadNet = $roadNet;
$this->cities = $cities;
}
public function getRoadNet() {
return $this->roadNet;
}
public function addRoad($start, $end, $numberOfVisitedCities, $roadDistance) {
$this->roads[$start->getName()][] = array(
// 'start' => $start,
// 'end' => $end,
'numberOfVisitedCities' => $numberOfVisitedCities,
'roadDistance' => $roadDistance,
'allCitiesVisited' => ($numberOfVisitedCities == count($this->cities)) ? TRUE : FALSE,
);
}
public function computeRoads() {
foreach ($this->cities as $cityName => $justTrue) {
$start = new Node($cityName, $this);
$start->buildPath();
}
}
public function getShortestRoad() {
$minDistance = 9999999999999;
$minRoad = null;
foreach ($this->roads as $startCityName => $roadsFromCity) {
foreach ($roadsFromCity as $road) {
if ($road['allCitiesVisited']) {
if ($road['roadDistance'] < $minDistance) {
$minDistance = $road['roadDistance'];
$minRoad = $road;
}
}
}
}
if ($minRoad == null) {
throw new \RuntimeException("No road found!");
}
return $minRoad;
}
public function getLongestRoad() {
$maxDistance = 0;
$maxRoad = null;
foreach ($this->roads as $startCityName => $roadsFromCity) {
foreach ($roadsFromCity as $road) {
if ($road['allCitiesVisited']) {
if ($maxDistance < $road['roadDistance']) {
$maxDistance = $road['roadDistance'];
$maxRoad = $road;
}
}
}
}
if ($maxRoad == null) {
throw new \RuntimeException("No road found!");
}
return $maxRoad;
}
}
/**
* bod v strome
*/
class node
{
protected $name = null;
protected $parentNode = null;
protected $childNodes = array();
protected $dispacer = null;
function __construct($name, &$dispacer) {
$this->name = $name;
$this->dispacer = $dispacer;
}
public function getName(){
return $this->name;
}
public function setParentNode(&$parent) {
$this->parentNode = $parent;
}
public function getRoadDistance(){
return $this->roadDistance;
}
public function addChildNode(&$node) {
$this->childNodes[$node->getName()] = $node;
}
public function isRoadEnd() {
if (empty($this->childNodes)) {
return true;
} else {
return false;
}
}
private function getRoadStart() {
if ($this->parentNode === null) {
return $this;
} else {
return $this->parentNode->getRoadStart();
}
}
private function isOneOfParents($nodeName) {
if ($this->parentNode === null) {
return false;
}
if ($this->parentNode->getName() == $nodeName) {
return true;
} else {
return $this->parentNode->isOneOfParents($nodeName);
}
}
// vrati vzdialenost od zaciatku cesty do tohoto mesta
// pocita sa rekurzivne ako vzdialenost k predkovi + vzdialenost od predka po
// zaciatok
public function getRoadDistanceToStart() {
if ($this->parentNode == null) {
return 0;
} else {
$roadNet = $this->dispacer->getRoadNet();
$distanceToParent = $roadNet[$this->name][$this->parentNode->getName()];
return $this->parentNode->getRoadDistanceToStart() + $distanceToParent;
}
}
public function getNumberOfVisitedCitiesToStart(){
if ($this->parentNode == null) {
return 1; //navstivil som sam seba
} else {
return $this->parentNode->getNumberOfVisitedCitiesToStart() + 1;
}
}
//
public function buildPath() {
$roadNet = $this->dispacer->getRoadNet();
foreach ($roadNet[$this->name] as $cityName => $distance) {
if ($this->isOneOfParents($cityName)) {
// v tomto meste sme uz boli
} else {
$newNode = new node($cityName, $this->dispacer);
$newNode->setParentNode($this);
$this->addChildNode($newNode);
$newNode->buildPath();
}
}
// ak je toto mesto posledne v ceste, a boli prejdene vsetky mesta,
// moze sa tato cesta poznacit.
if ($this->isRoadEnd()) {
$start = $this->getRoadStart();
$end = $this;
$numberOfVisitedCities = $this->getNumberOfVisitedCitiesToStart();
$roadDistance = $this->getRoadDistanceToStart();
$this->dispacer->addRoad($start, $end, $numberOfVisitedCities, $roadDistance);
}
}
}
$inputFile = fopen("input9.txt",'r');
$roadNet = array();
$cities = array();
while ($row = trim(fgets($inputFile))) {
$pom = explode(' ', $row);
// vytvori sa cestna siet
$roadNet[$pom[0]][$pom[2]] = (int)$pom[4];
$roadNet[$pom[2]][$pom[0]] = (int)$pom[4];
// naplni sa pole vsetkych miest
$cities[$pom[0]] = true;
$cities[$pom[2]] = true;
}
fclose($inputFile);
$dispacer = new Dispacer($roadNet, $cities);
$dispacer->computeRoads();
$shortestRoad = $dispacer->getShortestRoad();
var_dump($shortestRoad);
$longestRoad = $dispacer->getLongestRoad();
var_dump($longestRoad);