28
6
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
1I recommend removing gender-specific pronouns and replacing them with gender-neutral ones (or restructuring sentences): gamers do not have to be male. – Greg Martin – 2017-03-06T22:13:09.093
1@GregMartin Of course. Did I catch them all? – Martin Ender – 2017-03-06T22:14:41.430
2Look like it; thank you for thinking about it! Also, is Terra Mystica as awesome as I've heard? – Greg Martin – 2017-03-06T22:16:38.643
4@GregMartin yes. :) – Martin Ender – 2017-03-06T22:16:58.033
1@GregMartin I confirm :) – Leo – 2017-03-06T22:42:44.720
1It looks like Settlers of Catan meets Civilization V. – mbomb007 – 2017-03-06T22:47:36.343
5No power burns from bowl 2? This just feels so incomplete. – moreON – 2017-03-07T04:24:32.190
seconding @moreON. I want answers with sacrificing power! – Sparr – 2017-03-07T05:34:20.913
@GregMartin: No. It's móre awesome than you've heard. – Erik – 2017-03-07T06:33:51.397
@moreON I thought a while about including it but couldn't find a good way to do so that didn't feel tacked on. – Martin Ender – 2017-03-07T07:06:53.163
I'm tempted to try this in BrainFuck this evening. No promises that I'll ever actually accomplish a solution, but given that BF doesn't support negative numbers, would you be OK with me using a sign-and-magnitude representation for
P
? – ymbirtt – 2017-03-07T08:47:36.107@ymbirtt Yes, but you could also treat the tape cells as 2's complement so
255
would represent-1
etc. – Martin Ender – 2017-03-07T08:49:27.240I'm not quite sure which would make my life easier at this point, but I'll bear both options in mind. – ymbirtt – 2017-03-07T08:51:59.440
Is it safe to assume no one will try and spend more power than they have? – aslum – 2017-03-07T15:53:49.220
@aslum "(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)" – Martin Ender – 2017-03-07T15:55:37.110
@ymbirtt I'm pretty sure there are BF implementations that have negatives, but you can't take a negative number as input. – mbomb007 – 2017-03-07T16:38:07.773
@mbomb007, The standard I/O formats say nothing about BF specifically, but does allow stack-based languages to start with their input on the stack and leave their output on the stack. It also allows TMs to start with their input on the tape and leave their output on the tape. I think it's reasonable for BF to start with its input on the tape and leave its output on the tape, but obviously OP can have the final say here.
– ymbirtt – 2017-03-07T16:44:32.453@ymbirtt This has already been discussed (can't find where). BF is not a Turing machine. Maybe Martin can give his opinion, since I think he's the one that told me that. Considering that BF's standard method of I/O is
,
and.
, those should be used. – mbomb007 – 2017-03-07T16:53:28.080