Power Grid Resource Costs
Introduction
In the board game Power Grid, an integral part of the game is the act of buying resources to fuel your power stations. There are four types of resource used in the game (five, if you include renewable energy, but obviously you can't buy resources for that). These are Coal, Oil, Garbage, and Uranium. The resource market looks like this:
1---- | 2---- | 3---- | 4---- | 5---- | 6---- | 7---- | 8---- | 10 | 12
CCC | CCC | CCC | CCC | CCC | CCC | CCC | CCC | U | U
OOO U | OOO U | OOO U | OOO U | OOO U | OOO U | OOO U | OOO U | 14 | 16
GGG | GGG | GGG | GGG | GGG | GGG | GGG | GGG | U | U
When resources are bought, they are taken from the left of the market, where they are cheapest. When new resources are added to the market, they are added from the right. As can be seen, this produces a supply and demand balance - if more resources of a particular type are being bought than are being replenished each round, the price of that resource will rise. Similarly, if it's less, the cost will reduce.
Coal, Oil, and Garbage all scale up in cost at the same rate, while Uranium scales much faster. A single unit of a non-Uranium resource costs 8 - floor((<units available> - 1) / 3)
. A single unit of Uranium costs 13 - <units available>
if there are 5 or more units available, and 18 - (2 * <units available>)
otherwise.
For example, at the start of the game there are all 24 units of Coal available to buy. If the first player would like to buy 4 units of Coal, the first three units would cost 1 each, and the 4th would cost 2, making the total cost 5. This leaves 20 units available. If the second player would also like to buy 4 units of Coal, the cost would be (2*2 + 2*3) = 10.
The Challenge
Your challenge is to write a program or function which calculates the cost of buying a given amount of a particular resource, assuming that there is a given amount of that resource present in the market.
Your submission should accept, in any reasonable input format and in any order, the following parameters:
- The resource type - guaranteed to be one of [C, O, G, U].
- The amount of that resource that is present in the market - guaranteed to be a non-negative integer. If the resource type is not U, it will not be greater than 24. If the resource type is U, it will not be greater than 12.
- The amount of that resource that a player would like to buy - guaranteed to be a non-negative integer which is less than or equal to the amount already present in the market.
The output should be the cost of the requested resources.
Additional Rules
- The formulae given are for illustrative purposes only, feel free to use any method of calculating the costs you like.
- The letter codes for the different resource types (C, O, G, U) may be replaced by their lower case equivalents. You may also replace the letters for numbers, either in the form
C=0, O=1, G=2, U=3
orC=1, O=2, G=3, U=4
. - This is code-golf, so the smallest submission in bytes will be the winner.
- If your submission requires the input in a different order or format than specified in the previous section, you should provide details.
Test cases
Test case format:
resource type, amount in market, amount to buy
> result
Test cases:
C, 24, 4
> 5
C, 20, 4
> 10
O, 3, 3
> 24
U, 1, 1
> 16
C, 1, 1
> 8
G, 0, 0
> 0
O, 10, 7
> 44
U, 12, 4
> 10
G, 11, 4
> 22
f("O",1,5)
? \$\endgroup\$f(G, 11, 4) = 22
andf(O, 10, 7) = 44
. \$\endgroup\$