settingsAccountsettings
Menusettings

Q: Spice Must Flow - JavaScript task

+2 votes

Spice is Love, Spice is Life. And most importantly, Spice must flow. It must be extracted from the scorching sands of Arrakis, under constant threat of giant  sand worms. To make the work as efficient as possible, the Duke has tasked you with the creation of a management software.

Write a JS program that calculates the total amount  of spice that can be extracted from a source. The source has a starting  yield, which indicates how much spice can be mined on the  first  day.  After it has been mined for a day, the yield drops by 10, meaning on the second day it’ll produce 10 less spice than on the first, on the third day 10 less than on the second, and so on (see examples). A source is considered profitable only while its yield is at least 100 – when less than 100 spice is expected in a day, abandon the source.

The mining crew consumes 26 spice every day at the end of their shift and an additional 26 after the mine has been exhausted. Note that the workers cannot consume more spice than there is in storage.

When the operation is complete, print on the console on two separate lines how many days the mine has operated and the total amount of spice extracted.

Input:
You will receive an array with a single string element that must be parsed as a  number, representing the  starting  yield of the source.

Output:
Print on the  console  on two separate lines how many  days  the mine has operated and the  total amount  of spice extracted.

Constraints:
The starting yield will be an integer within range [0…2[on 28]]

Examples:

Input:
111

Output:
2
134

Day 1:  we extract 111 spice  and at the end of the shift, the workers consume 26, leaving 85. The yield drops by 10 to 101.

Day 2: we extract 101 spice, the workers consume 26, leaving  75. The total is 160 and the yield has dropped to 91.

Since  the  expected  yield  is  less  than  100,  we  abandon  the  source.  The  workers  take another 26, leaving 134. The mine has operated 2 days.


Input:
450

Output:
36
8938


Input:
200

Output:
11
1338

asked in JavaScript category by user ak47seo

1 Answer

+1 vote

Here is my js solution:

function spiceMustFlow(input) {
    let currentYield = Number(input[0]);

    let extractedSpice = 0;
    let days = 0;

    while (currentYield >= 100) {
        extractedSpice += currentYield - 26;
        currentYield -= 10;
        days++;
    }

    if (extractedSpice >= 26) {
        extractedSpice -= 26;
    }

    console.log(days);
    console.log(extractedSpice);
}

//spiceMustFlow(["111"]);
//spiceMustFlow(["450"]);
spiceMustFlow(["200"]);

 

answered by user andrew
...