Time Question

orangepineapple

New member
Joined
Feb 15, 2020
Messages
3
So I'm a little stuck on this.

A plant needs to be watered four times over the course of minimum 48 hours as it's growth increases by percentage up to 100%.
This means that for every 12 hours, the plant needs to be watered once. If it has not been watered, the percentage will stop at it's limit (i.e. if it has been watered only once, it will stop at 25%) and it's growth will stop increasing by the second. I need to write a piece of code which will calculate this every second, and return the percentage growth. Instead of having the code check for 48 hours and update the database with new growth every second, I want to use math and calculate this instead.

What we know is:
- the time the plant began growing
- the last time it was watered
- the amount of times it's been watered.

Here's the code I'm working with, if anyone understands it:
Code:
function GetMaturity()
    on_going = time - time_planted
    maturity = round(on_going / 172800, 3)
  
    if maturity > 1 then
        return 1.0
    else
        return maturity
    end
end

function GetNeedsWater()
    on_going = time - time_planted
    maturity = round(on_going / 172800, 3)
    percentage = maturity / 0.25
    water_level = round(1 - percentage - number_of_times_watered, 3)
    if water_level < 0 then
        return 0.0
    else
        return water_level
    end
end

So far, I've calculated the percentage growth using (current time - begin time / end time). How can I dynamically limit the growth and calculate this percentage by the number of time's it's been watered, and resume it from that point once the plant has been watered using the last time it was watered? Essentially I need an equation which will do all of this using the information provided, if it's even possible.

Any help would be appreciated c:
 
Last edited:
So I'm a little stuck on this.

A plant needs to be watered four times over the course of minimum 48 hours as it's growth increases by percentage up to 100%.
This means that for every 12 hours, the plant needs to be watered once. If it has not been watered, the percentage will stop at it's limit (i.e. if it has been watered only once, it will stop at 25%) and it's growth will stop increasing by the second. I need to write a piece of code which will calculate this every second, and return the percentage growth. Instead of having the code check for 48 hours and update the database with new growth every second, I want to use math and calculate this instead.

What we know is:
- the time the plant began growing
- the last time it was watered
- the amount of times it's been watered.

Here's the code I'm working with, if anyone understands it:
Code:
function GetMaturity()
    on_going = time - time_planted
    maturity = round(on_going / 172800, 3)
 
    if maturity > 1 then
        return 1.0
    else
        return maturity
    end
end

function GetNeedsWater()
    on_going = time - time_planted
    maturity = round(on_going / 172800, 3)
    percentage = maturity / 0.25
    water_level = round(1 - percentage - number_of_times_watered, 3)
    if water_level < 0 then
        return 0.0
    else
        return water_level
    end
end

So far, I've calculated the percentage growth using (current time - begin time / end time). How can I dynamically limit the growth and calculate this percentage by the number of time's it's been watered, and resume it from that point once the plant has been watered using the last time it was watered? Essentially I need an equation which will do all of this using the information provided, if it's even possible.

Any help would be appreciated c:
I would say:

With a combination of IF - THEN - WHEN statements.

Please try it and show us where you are stuck.
 
So I'm a little stuck on this.

A plant needs to be watered four times over the course of minimum 48 hours as it's growth increases by percentage up to 100%.
This means that for every 12 hours, the plant needs to be watered once. If it has not been watered, the percentage will stop at it's limit (i.e. if it has been watered only once, it will stop at 25%) and it's growth will stop increasing by the second. I need to write a piece of code which will calculate this every second, and return the percentage growth. Instead of having the code check for 48 hours and update the database with new growth every second, I want to use math and calculate this instead.

What we know is:
- the time the plant began growing
- the last time it was watered
- the amount of times it's been watered.
I'd think your simulation code would need as input each of the actual times when the plant is watered. And I wouldn't think the final result would depend only on how many times it was watered over the 48 hours; watering it four times in the last minute would not make up for leaving it dry until then.

One result of this is that I don't think you'll be able to skip the simulation entirely and do one calculation at the end, if that's what you're looking for. You should, however, be able to do one calculation each time it's watered (well, actually one algorithm) to determine its growth over that time. So four runs of a little program should do the trick, rather than 172,800 runs.

Of course, you said "I need to write a piece of code which will calculate this every second", so maybe you aren't allowed to do this. I can't tell what the real requirements are. (Your code doesn't appear to do nearly what you say you need to do.)
 
I'd think your simulation code would need as input each of the actual times when the plant is watered. And I wouldn't think the final result would depend only on how many times it was watered over the 48 hours; watering it four times in the last minute would not make up for leaving it dry until then.

One result of this is that I don't think you'll be able to skip the simulation entirely and do one calculation at the end, if that's what you're looking for. You should, however, be able to do one calculation each time it's watered (well, actually one algorithm) to determine its growth over that time. So four runs of a little program should do the trick, rather than 172,800 runs.

Of course, you said "I need to write a piece of code which will calculate this every second", so maybe you aren't allowed to do this. I can't tell what the real requirements are. (Your code doesn't appear to do nearly what you say you need to do.)
Thanks for your response. Like you’ve said, I’d much prefer only updating the database over those four runs as this seems like the most optimised solution for such problem. I’d also be able to get an input for each time it is watered, and there isn’t really any real requirements aside from displaying a live percentage to the player by a calculation every second.

Linking this back to the question, what’s the most plausible way of writing an algorithm for this? Given that you shouldn’t be able to water it four times in the last minute, and it’s possible to produce this simulation with a percentage without 172,800 runs to update the database, like you’ve said.

I just can’t wrap my head around the algorithm itself.
 
You haven't told us the context of your question. I presumed it was for a class (so that you were told what you "need" to do), and not for something real (running a greenhouse, say); now it appears to be a game, since you mention a "player". Are you designing the game, or did someone else tell you what you have to do?

It's essential in programming to know exactly what the inputs are, and what the outputs have to be. Is it running in real time? Are you given a signal when the plant is watered? or are you just told at the end how many times it was? I'd expect to have been told something like how the growth rate depends on the cumulative amount of water in the plant, which could be calculated from the time since last watering together with the amount of water present at that time. The rules you stated are far too simplistic.

There is so much missing, that it's impossible to help you yet. But it should be easy to work out the plant's growth at a given time after the last watering, if the rules were clearly stated.
 
You haven't told us the context of your question. I presumed it was for a class (so that you were told what you "need" to do), and not for something real (running a greenhouse, say); now it appears to be a game, since you mention a "player". Are you designing the game, or did someone else tell you what you have to do?

It's essential in programming to know exactly what the inputs are, and what the outputs have to be. Is it running in real time? Are you given a signal when the plant is watered? or are you just told at the end how many times it was? I'd expect to have been told something like how the growth rate depends on the cumulative amount of water in the plant, which could be calculated from the time since last watering together with the amount of water present at that time. The rules you stated are far too simplistic.

There is so much missing, that it's impossible to help you yet. But it should be easy to work out the plant's growth at a given time after the last watering, if the rules were clearly stated.
My apologies for the lack of information, I've ended up running the simulation and updating values every minute, then saving them every 30 minutes. I think what I wanted was too complex for something that doesn't require a running simulation because of the factors in consideration. But yes, this was for an online video game and I wanted to optimize it by using an algorithm instead.

By running this as a simulation which updates each plant per minute, I've also been able to implement other important factors into the growth rate so I guess it was the best solution after all.
 
Top