This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
jag's profile picture

[solved] Simple Math Problem

Started by jag, 17 November 2013 - 08:21 AM
jag #1
Posted 17 November 2013 - 09:21 AM
So yea I am a bit tired and so my math is not the best, so I figured I can ask you guys.

This is the situation: I want a program that moves fuel and items from a chest based the fuels burntime.
If the fuel and item burned 1:1 it would be peace á cake, but I got different fuel types supported.

This is what you get to know in the function:
fuel quantity, fuel burn time, item quantity

Burn time is a number of how many items the fuel burns, for example coal/charcoal burns 8.

example:
I got 5 coal and 26 iron in a chest, this is the optimal amounts for this situation:
  • fuel to move: 3
  • items to move: 24
How would a algorithm look for this?
If you come up with one it does not need to be optimized and small, just that it works.
Edited on 17 November 2013 - 09:18 AM
Cozzimoto #2
Posted 17 November 2013 - 09:28 AM
well you would need to get how many items you have first, then get what fuel you are using then divide how many items you have against the burn amount of the fuel source you are using

FuelNeeded = ItemAmount / FuelBurntime

and if you have several types of fuels you can store all your fuel sources in an array/table with the value being what the burn time is EXAMPLE


  local fuels = { coal=8, wood=4 }

fuels.coal would return 8
and fuels.wood would return 4
Edited on 17 November 2013 - 08:28 AM
jag #3
Posted 17 November 2013 - 09:28 AM

fueltomove = math.floor(itemamount / burntime)
itemstomove = itemamount - (itemamount % burntime)
Would that work?
jag #4
Posted 17 November 2013 - 09:30 AM

  local fuels = { coal=8, wood=4 }

fuels.coal would return 8
and fuels.wood would return 4

Ye no shit dude.
But would the algorythm above work ya think?
Cozzimoto #5
Posted 17 November 2013 - 09:32 AM
well im just covering my basis. the OP was asking for help so i wanted to make sure that everything i was saying was easily interpreted.

and yes that should work just fine. idk if you need the modulus equation as you can just take the amount of fuel needed and divide that by the number of items you have so you dont stick too much in the furnace
jag #6
Posted 17 November 2013 - 09:34 AM
Hmm but that doesn't work if you got less fuel then required does it
Cozzimoto #7
Posted 17 November 2013 - 09:38 AM
lets say you have 3 coal but a stack of ore to process
you would need to figure out how much to move according to ho much you have

3 * 8 = 24
so you would stick in the 3 coal you have and 24 ore as well for the even burn amount

the other way around lets say you have 12 coal and only 32 ore to process
first you would need to find how much coal you need

32 / 8 = 4
since you have 32 ore and the burn time for coal is 8
then stick 32 ore in the furnace as well as 4 coal
Edited on 17 November 2013 - 08:38 AM
jag #8
Posted 17 November 2013 - 09:55 AM
Jeez having a hard time converting it to code. Can't seem to wrap my mind around it
–snip–
good reply btw.


maxburn = fuelamount*burntime
if itemamount > maxburn then
    -- # of items are more then we can burn
    fueltomove = fuelamount
    itemstomove = maxburn
else
    -- we have more or enough fuel for all items
    fueltomove = math.floor(itemamount/burntime)
    itemstomove = itemamount - (itemamount%burntime)
end
Finnaly got a code together
This works, thanks for the help dude!
Cozzimoto #9
Posted 17 November 2013 - 10:18 AM
your welcome, im glad i can be of help. =)

usually sometimes i have to step back for a min to clear my mind then come back and i usually find a way to do what im trying to do
jag #10
Posted 17 November 2013 - 10:19 AM
your welcome, im glad i can be of help. =)

usually sometimes i have to step back for a min to clear my mind then come back and i usually find a way to do what im trying to do
Yea I had a huge problem on focusing, each step I got closer to the answer I forgot it and had to rethink all over again. Glad I got it done at least.

I tried inserting the code into the turtle and it works like a charm