Малък проблем със задача Feed the Animals от демо изпита
Здравейте колеги, от PHP групата съм и решавам въпросната задача и judge отпуска само 40 точки. Така и не успях да си намеря грешката та бих се радвал на малко помощ!
<?php
$animals = [];
$area = [];
$input = readline();
while ($input !== 'Last Info') {
$args = explode(":", $input);
$action = $args[0];
$animalName = $args[1];
$areaName = $args[3];
if ($action == 'Add') {
$dailyFoodLimit = intval($args[2]);
if (!key_exists($animalName, $animals)) {
$animals[$animalName] = intval($dailyFoodLimit);
if (!key_exists($animalName, $area)) {
$area[$areaName] = 1;
} else
$area[$areaName] += 1;
} else {
$animals[$animalName] += intval($dailyFoodLimit);
}
} else if ($action == 'Feed') {
$givenFood = intval($args[2]);
if (key_exists($animalName, $animals)) {
$animals[$animalName] -= intval($givenFood);
if ($animals[$animalName] <= 0) {
echo "$animalName was successfully fed" . PHP_EOL;
//unset($animals[$animalName]);
$area[$areaName] -= 1;
}
}
}
$input = readline();
}
//sort animals
krsort($animals);
arsort($animals);
//output
echo 'Animals:' . PHP_EOL;
foreach ($animals as $hungryAnimal => $foodToGive) {
if ($foodToGive > 0) {
echo $hungryAnimal . " -> " . $foodToGive . 'g' . PHP_EOL;
}
}
//sort areas
arsort($area);
echo 'Areas with hungry animals:' . PHP_EOL;
foreach ($area as $anArea => $count) {
if ($count > 0) {
echo $anArea . " : " . $count . PHP_EOL;
}
}