Babylon Stacking Game
Asked at Google
Reported by the problem's creator, not verified
Two players play a game with colored tiles. BabylonGame(tiles) takes a string with one letter per tile, such as "YYYWWWGGGBBB": twelve tiles in four colors. Every tile starts as its own stack of height 1, and the stacks are numbered from 0 in the order of the string.
The players alternate, and player 0 moves first. A move puts the whole stack source on top of a different stack target. It is legal when the two stacks have the same height or the same top color. The merged stack has the sum of the two heights and the top color of the stack that was moved. It takes the place of target in the list, and source is removed, so every stack after source gets an index one lower. A player with no legal move on their turn loses.
Examples
Example 1
Input: init =
["YWYG"], operations =[["legal_moves",[]],["play",[0,2]],["state",[]],["current_player",[]],["legal_moves",[]],["play",[0,1]],["play",[1,0]],["play",[2,0]],["state",[]],["winner",[]],["current_player",[]],["legal_moves",[]]]Output:
[[[0,1],[0,2],[0,3],[1,0],[1,2],[1,3],[2,0],[2,1],[2,3],[3,0],[3,1],[3,2]],true,[[1,"W"],[2,"Y"],[1,"G"]],1,[[0,2],[2,0]],false,false,true,[[2,"G"],[2,"Y"]],-1,0,[[0,1],[1,0]]]Explanation: At the start every stack has height 1, so any pair is legal. Playing 0 onto 2 puts
YonYat position 2 with height 2 and removes stack 0, so the state is[1,W],[2,Y],[1,G]and it is player 1's turn. OnlyWandGmatch now, by height. Playing 0 onto 1 or 1 onto 0 mixes height 1 and 2 with different colors, so both returnfalse. Playing 2 onto 0 mergesGontoWinto[2,G], leaving[2,G],[2,Y]. The game goes on because those two have the same height, sowinner()is-1and it is player 0's turn.