Skip to content

Instantly share code, notes, and snippets.

@ahmed4end
Created March 6, 2020 10:56
Show Gist options
  • Save ahmed4end/6a18ecaef7f7a8b6bf9c1413c74396a2 to your computer and use it in GitHub Desktop.
Save ahmed4end/6a18ecaef7f7a8b6bf9c1413c74396a2 to your computer and use it in GitHub Desktop.
board = "RBYYBBRRB"
hand = "YRBGB"
import re
def play(board=list(board+" "), hand=list(hand), trac=[], c=0):
try:
hand[c]
except:
trac.append(["".join(board[:-1]), c])
if "".join(board[:-1]) == "":
trac.append(["".join(board[:-1]), c])
for f in hand[c:]:
for i,j in enumerate(board):
board.insert(i, f)
move = re.sub(r"([A-Z])\1\1", r"", "".join(board[:-1]))
explain = re.sub(r"([A-Z])\1\1", r"[\1\1\1]", "".join(board[:-1]))
if len("".join(board[:-1])) != len(move) or len("".join(board[:-1]))<3:
auto_remove = re.sub(r"([A-Z])\1{2,}", "", move)
if len(move) != len(auto_remove):
move = auto_remove
play(board=list(move+" "), hand=hand, trac=trac, c=c+1)
del board[i]
return trac
trac = play()
shortest_moves = min([i for i in trac if len(i[0])==len(min(trac, key=lambda x:len(x[0]))[0])], key=lambda x: x[1])
print(shortest_moves[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment