-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeavyRain.php
More file actions
75 lines (61 loc) · 1.89 KB
/
HeavyRain.php
File metadata and controls
75 lines (61 loc) · 1.89 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
<?php
require 'CityBuilder.php';
/**
*
*/
class HeavyRain extends CityBuilder
{
const AJUSTER_COORD = 1;
function __construct(){}
public function exec($rand = false)
{
$city = $rand ? $this->randomCity() : $this->staticCity();
//$city = [1, 2, 1, 5, 2, 4, 1, 0, 1, 2, 6, 4, 5, 2, 3, 4, 1, 2];
/* add your code here */
$borderOne = $this->locatePrimaryBorder($city);
$borderTwo = $this->locateSecondBorder($city);
$rightBorder = ($borderOne[0] > $borderTwo[0] ) ? $borderOne : $borderTwo;
$leftBorder = ($borderOne[0] < $borderTwo[0] ) ? $borderOne : $borderTwo;
$result = $this->floodPerimeter($city, $leftBorder, $rightBorder);
echo json_encode($city) . " => " . $result . "\n";
}
/**
* @param array $city
* @return array
*/
private function locatePrimaryBorder(array $city)
{
return [array_keys($city, max($city))[0], max($city) ];
}
/**
* @param array $city
* @return array
*/
private function locateSecondBorder(array $city)
{
$max = max($city);
$key = array_keys($city, $max);
unset($city[$key[0]]);
return [array_keys($city, max($city))[0],max($city)];
}
/**
* @param array $city
* @param array $leftBorder
* @param array $rightBorder
* @return float|int|mixed
*/
private function floodPerimeter(array $city, array $leftBorder, array $rightBorder)
{
$largeurPerimeter = $rightBorder[0] - $leftBorder[0] - self::AJUSTER_COORD;
$perimeter = $leftBorder[1] * $largeurPerimeter;
$i = 0;
$perimeterIn = 0;
while ($i < count($city)) {
if (($i > $leftBorder[0]) && ($i < $rightBorder[0])) {
$perimeterIn += $city[$i];
}
$i++;
}
return ($perimeter - $perimeterIn);
}
}