-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComputer-Smuggler
79 lines (60 loc) · 1.6 KB
/
Computer-Smuggler
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
$parts = $_GET['list'];
$parts = str_replace(' ', '', $parts);
$partsArray = explode(',', $parts);
$groupedParts = [];
foreach($partsArray as $part) {
if(!empty($part)) {
if(!isset($groupedParts[$part])) {
$groupedParts[$part] = 0;
}
$groupedParts[$part]++;
}
}
$prices = array(
'CPU' => '85',
'RAM' => '35',
'VIA' => '45',
'ROM' => '45',
);
$totalPartsPrice = get_total_price($groupedParts, $prices);
$computers = $leftParts = $profit = $leftPartsPrice = 0;
$minComputers = min($groupedParts);
for($i = 0; $i < $minComputers; $i++) {
if((isset($groupedParts['CPU']) && $groupedParts['CPU'] > 0) &&
(isset($groupedParts['RAM']) && $groupedParts['RAM'] > 0) &&
(isset($groupedParts['VIA']) && $groupedParts['VIA'] > 0) &&
(isset($groupedParts['ROM']) && $groupedParts['ROM'] > 0)) {
$computers++;
$groupedParts['CPU']--;
$groupedParts['RAM']--;
$groupedParts['VIA']--;
$groupedParts['ROM']--;
}
}
foreach($groupedParts as $key=>$leftPart) {
$leftParts += $leftPart;
$leftPartsPrice += $leftPart * ($prices[$key] / 2);
}
echo '<ul>';
echo '<li>'.$computers.' computers assembled</li>';
echo '<li>'.$leftParts.' parts left</li>';
echo '</ul>';
$income = ($computers * 420) + $leftPartsPrice;
$profit = $income - $totalPartsPrice;
if($profit > 0) {
echo '<p>Nakov gained '.$profit.' leva</p>';
} else {
echo '<p>Nakov lost '. $profit.' leva</p>';
}
function get_total_price($parts, $prices) {
$total = 0;
foreach($parts as $key=>$part) {
if($part > 4) {
$total += $part * ($prices[$key] / 2);
} else {
$total += $part * $prices[$key];
}
}
return $total;
}
?>