Power Grid Resource Costs 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((&lt;units available&gt; - 1) / 3). A single unit of Uranium costs 13 - &lt;units available&gt; if there are 5 or more units available, and 18 - (2 * &lt;units available&gt;) 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 or C=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
&gt; result


Test cases:

C, 24, 4
&gt; 5

C, 20, 4
&gt; 10

O, 3, 3
&gt; 24

U, 1, 1
&gt; 16

C, 1, 1
&gt; 8

G, 0, 0
&gt; 0

O, 10, 7
&gt; 44

U, 12, 4
&gt; 10

G, 11, 4
&gt; 22
Python 3, 71 69 bytes
Thanks to @xnor for -2 bytes
f=lambda r,a,b:b and[8-int(~-a/3),max(18-2*a,13-a)][r&gt;2]+f(r,a-1,b-1)

A function that takes input via argument of the zero-indexed resource type r, the amount available a and the amount to buy b, and returns the cost.
This makes use of the fact that True and False equate to 1 and 0 in Python, allowing the use of Boolean expressions to index into lists.
How it works
f=lambda r,a,b           Function with input resource type r, amount available a and amount
                         to buy b
b and...                 Base case: return 0 if b=0
[8-int(~-a/3),...][r&gt;2]  If not uranium, yield the unit cost 8-floor((a-1)/3)...
max(18-2*a,13-a)         ..else yield the current uranium unit cost
...f(r,a-1,b-1)          Decrement a and b, then pass to function
...+...                  Add the cost of each unit to give the total cost
:...                     Return the above

Try it on Ideone