The Snoring Giant of Hobbenfell
Posted by the Lantern Guild of Hobbenfell. High on Hobbenfell ridge, the giant Gorrund sleeps in his hall beside a table heaped with gold coins. A band of thieves will slip in and carry out a fixed list of moves, one after another. Before anyone risks their neck, the Guild wants a reckoning: how much gold comes home, and whether Gorrund wakes.
Write giantHeist(actions, hoard).
actionsis a list of strings, each exactly"grab","stash"or"wait", in the order they are performed.hoardis a non-negative integer: the number of coins on the table at the start.
The raid has this state:
- the giant's restlessness, which starts at 0;
- the sack, which starts empty and can hold at most 3 coins;
- the stash by the door, which starts at 0 coins.
Process the actions in order. For each action:
1. The giant is lightly asleep if his restlessness is at least 4 at that moment, before this action. Otherwise he is deeply asleep. 2. The action's base noise is: - "grab": 2, or 3 if the action immediately before it in the list was also "grab"; - "stash": 1; - "wait": 0. If the giant is lightly asleep, the noise is doubled. 3. Add the noise to his restlessness. If his restlessness is now 9 or more, he wakes: this action does not take effect, every coin in the sack is lost, and all remaining actions are ignored. 4. Otherwise the action takes effect: - "grab": if the sack holds fewer than 3 coins and the table still has a coin, move one coin from the table into the sack. Otherwise no coin moves. Either way the action still made its noise and still counts as a grab for the action after it. - "stash": move every coin in the sack to the stash; the sack becomes empty. - "wait": reduce his restlessness by 3, but not below 0.
Return a list of two integers [coins, woke]:
coinsis the number of coins in the stash, plus the coins in the sack if the giant never woke. If he woke, sack coins do not count.wokeis the 0-based index of the action that woke the giant, or -1 if he slept through every action.
examples
- case 1
actions of grab, grab, stash, wait, grab; hoard of 5[3, -1] - restlessness climbs 2, 5, 7, drops to 4 after the wait, then the light-sleep grab brings it to 8; 2 coins stashed plus 1 still in the sack - case 2
actions of grab, grab, stash, wait, grab, stash; hoard of 5[2, 5] - the final stash raises restlessness from 8 to 10, so it never happens and the coin in the sack is lost - case 3
actions of grab, grab, grab; hoard of 10[0, 2] - the third grab follows a grab (base 3) while he is lightly asleep at 5, so it adds 6 and wakes him at 11 - case 4
actions of wait, wait, grab, grab, wait, grab, grab; hoard of 4[0, 6] - waiting at restlessness 0 leaves it at 0, never below
- reward
- 540 XP
- signature
giantHeist
TypeScript, run as ES2022 — types are stripped, not checked. If a JS engine would run it, so will we.
- sleeps through, sack counts at end
- waking stash loses the sack
- grab streak doubled in light sleep
- exactly nine wakes him
- no actions
- empty table
- sack capacity of three
- hoard runs out, unstashed coin kept
- restlessness never below zero