The boardgame Terra Mystica has some very interesting mechanics for one of the primary resources, power. Instead of gaining and spending units of power from a bank, each player starts the game with exactly 12 units of power which are distributed over three "bowls", which are labelled I, II and III. Gaining and spending power then simply shifts power between these bowls:
- To spend a unit of power, move it from bowl III to bowl I (provided you have a unit in bowl III).
- When you gain a unit of power, if there is a unit in bowl I, move it to bowl II. If there are no units in bowl I, but there is a unit in bowl II, move it to bowl III. If all units are already in bowl III, nothing happens.
- When you gain or spend multiple units at once, they are processed one unit at a time.
Here is an example. Say, a player starts with the following power distribution (given in order I | II | III
):
5 | 7 | 0
Their power changes as follows if they gain and spend power a few times:
5 | 7 | 0
Gain 3 ==> 2 | 10 | 0
Gain 6 ==> 0 | 8 | 4 (move 2 power from I to II,
then the remaining 4 from II to III)
Gain 7 ==> 0 | 1 | 11
Spend 4 ==> 4 | 1 | 7
Gain 1 ==> 3 | 2 | 7
Spend 7 ==> 10 | 2 | 0
Gain 12 ==> 0 | 10 | 2 (move 10 power from I to II,
then the remaining 2 from II to III)
Gain 12 ==> 0 | 0 | 12 (the two excess units go to waste)
Your task is to compute the result of one such gaining or spending event.
The Challenge
You are given four integers as input. The first three, I
, II
, III
, represent the amount of power in each of the three bowls. They will be non-negative, and they will sum to 12. The fourth number, P
, is the amount of power gained or spent, and will be in the inclusive range [-III, 24]
(so you may assume that the player will never try to spend more power than they currently can, but they might be gaining more power than they need to move all power into bowl III).
You may take these numbers in any consistent order, as separate arguments, as a list of integers, or as a string containing these integers. You can also take P
as one argument, as I
, II
, III
as a separate list argument.
You should output three integers I'
, II'
, III'
which represent the amount of power in each bowl after P
units were gained or spent, following the rules explained above.
You may write a program or a function and use any of the our standard methods of receiving input and providing output.
You may use any programming language, but note that these loopholes are forbidden by default.
This is code-golf, so the shortest valid answer – measured in bytes – wins.
Test Cases
I II III P => I' II' III'
5 7 0 3 => 2 10 0
2 10 0 6 => 0 8 4
0 8 4 7 => 0 1 11
0 1 11 -4 => 4 1 7
4 1 7 0 => 4 1 7
4 1 7 1 => 3 2 7
3 2 7 -7 => 10 2 0
10 2 0 12 => 0 10 2
0 10 2 12 => 0 0 12