Chess clock flag fall
EasyOnline Assessment
Replay the thinking times of a chess game on a Fischer-increment clock and report who, if anyone, lost on time.
Both players start with base seconds on their clock. White moves first and the players alternate. spent[i] is how many seconds the player making half-move i + 1 kept their own clock running for that move.
When a player makes a move:
- Their clock goes down by the seconds spent. If it reaches 0 or below, they have flagged and the game ends at once; nothing is added to a clock that has flagged.
- Otherwise
incrementseconds are added to their clock. - Then, if this was that player's
bonusMove-th move,bonusextra seconds are also added to their clock. Each player gets the bonus after their ownbonusMove-th move (not after thebonusMove-th half-move of the game).
Moves are numbered the way chess players do: White's and Black's first moves are both move 1, their second moves are move 2, and so on.
Input: base, increment, bonusMove and bonus as integers, and spent, a list of integers.
Output: a string. If a player flagged, return "flag W n" or "flag B n", where n is their move number. If everyone survives all of spent, return "clocks w b" with White's and then Black's remaining seconds.
Example 1
- Input:
- [60,2,40,0,[10,12,15,9,20]]
- Output:
- "clocks 21 43"
- Explanation
- White: 60 - 10 + 2 = 52, then 52 - 15 + 2 = 39, then 39 - 20 + 2 = 21. Black: 60 - 12 + 2 = 50, then 50 - 9 + 2 = 43. Nobody ran out, so the clocks read 21 and 43.
Example 2
- Input:
- [10,5,40,0,[3,9,3,6]]
- Output:
- "flag B 2"
- Explanation
- White's first move leaves 10 - 3 + 5 = 12 and Black's leaves 10 - 9 + 5 = 6. White's second move leaves 12 - 3 + 5 = 14. Black then has 6 seconds and thinks for 6: the clock reaches exactly 0, which is a flag, and no increment is given. It is Black's second move.
Example 3
- Input:
- [20,1,3,30,[5,4,6,4,8,6,5,12]]
- Output:
- "clocks 30 28"
- Explanation
- After three moves each, White has 4 and Black 9 before bonuses. White's third move (half-move 5) ends with 11 - 8 + 1 + 30 = 34 and Black's third move (half-move 6) with 14 - 6 + 1 + 30 = 39. White's fourth move uses 5: 34 - 5 + 1 = 30. Black's fourth uses 12: 39 - 12 + 1 = 28. Without the bonus Black would have flagged. The clocks read 30 and 28.
Constraints
1 <= base <= 10,0000 <= increment <= 601 <= bonusMove <= 1000 <= bonus <= 10,0000 <= spent.length <= 3000 <= spent[i] <= 3,000