[Solved] Seize the Fire - PHP Exam Task

  

5
Topic starter

The group of adventurists have gone on their first task. Now they have to walk through fire - literally. They have to use all of the water they have left. Your task is to help them survive.

Create a program that calculates the water that is needed to put out a "fire cell", based on the given information about its "fire level" and how much it gets affected by water.

First, you will be given the level of fire inside the cell with the integer value of the cell, which represents the needed water to put out the fire.  They will be given in the following format:

"{typeOfFire} = {valueOfCell}#{typeOfFire} = {valueOfCell}#{typeOfFire} = {valueOfCell}……"

Afterwards you will receive the amount of water you have for putting out the fires. There is a range of fire for each of the fire types, and if a cell’s value is below or exceeds it, it is invalid and you don’t need to put it out:

seize the fire php exam task

If a cell is valid, you have to put it out by reducing the water with its value.

Putting out fire also takes effort and you need to calculate it. Its value is equal to 25% of the cell’s value.

In the end you will have to print the total effort.

Keep putting out cells until you run out of water. If you don’t have enough water to put out a given cell – skip it and try the next one. In the end, print the cells you have put out in the following format:

"Cells:
 - {cell1}
 - {cell2}
 - {cell3}
……
 - {cellN}"
"Effort: {effort}"

In the end, print the total fire you have put out from all of the cells in the following format: "Total Fire: {totalFire}"

Input / Constraints:

  • On the 1st line you are going to receive the fires with their cells in the format described above – integer numbers in the range [1…500]
  • On the 2nd line, you are going to be given the water – an integer number in the range [0….100000]

Output:

  • Print the cells, which you have put out in the following format:

"Cells:
 - {cell}
 - {cell2}
 - {cell3}
 - {cell5}
……
 - {cellN}"

  • Print the effort, rounded 2 digits after the decimal separator in the following format:

"Effort: {effort}"

  • Print the total fire put out:

"Total Fire: {totalFire}"

Examples:

seize the fire php task example 1

seize the fire php task example 2

2 Answers
4

Here is my solution:

<?php
 
$fire = explode("#", readline());
$water = intval(readline());
 
$validCells = [];
$effort = 0;
$totalFire = $water;
 
for ($i = 0; $i < count($fire); $i++) {
 
    $args = explode(" = ", $fire[$i]);
 
    $typeFire = $args[0];
    $strengthFire = $args[1];
 
    switch ($typeFire) {
        case 'High':
            if ($strengthFire >= 81 && $strengthFire <= 125 && $water >= $strengthFire) {
                $validCells[] = $strengthFire;
                $water -= $strengthFire;
                $effort += $strengthFire * 0.25;
            }
            break;
        case 'Medium':
            if ($strengthFire >= 51 && $strengthFire <= 80 && $water >= $strengthFire) {
                $validCells[] = $strengthFire;
                $water -= $strengthFire;
                $effort += $strengthFire * 0.25;
            }
            break;
        case 'Low':
            if ($strengthFire >= 1 && $strengthFire <= 50 && $water >= $strengthFire) {
                $validCells[] = $strengthFire;
                $water -= $strengthFire;
                $effort += $strengthFire * 0.25;
            }
            break;
    }
}
 
echo "Cells:" . PHP_EOL;
for ($i = 0; $i < count($validCells); $i++) {
    echo " - " . $validCells[$i] . PHP_EOL;
}
 
printf("Effort: %.2f", $effort);
echo PHP_EOL;
echo "Total Fire: " . ($totalFire - $water);
2

Here is another solution with custom function (isValid):

<?php
 
$fireCells = explode("#", readline());
$water = intval(readline());
$effort = 0;
$totalFire = 0;
$print = [];
 
foreach ($fireCells as $cellProperty) {
    $args = explode(" = ", $cellProperty);
 
    $typeOfFire = $args[0];
    $valueOfFire = $args[1];
 
    if (isValid($typeOfFire, $valueOfFire) && ($water >= $valueOfFire)) {
        $water -= $valueOfFire;
        $totalFire += $valueOfFire;
        $effort += $valueOfFire * 0.25;
        $print[] = $valueOfFire;
    }
}
 
 
function isValid($typeOfFire, $valueOfFire) {
    $valid = false;
 
    switch ($typeOfFire) {
        case 'High':
            if ($valueOfFire >= 81 && $valueOfFire <= 125) {
                $valid = true;
            }
            break;
        case 'Medium':
            if ($valueOfFire >= 51 && $valueOfFire <= 80) {
                $valid = true;
            }
            break;
        case 'Low':
            if ($valueOfFire >= 1 && $valueOfFire <= 50) {
                $valid = true;
            }
            break;
    }
 
    return $valid;
}
 
echo "Cells: " . PHP_EOL;
foreach ($print as $cell) {
    echo " - " . $cell . PHP_EOL;
}
 
printf("Effort: %.2f" . PHP_EOL, $effort);
echo "Total Fire: $totalFire";
Share: