Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for R-mahjong Ⅰ: break hand into sets by alexdraga
from copy import deepcopy
def get_pairs(hand):
for tile in hand:
new_hand = deepcopy(hand)
new_hand.remove(tile)
if tile in new_hand:
yield tile + tile[1], new_hand
def get_sets(hand):
for tile in hand:
if hand.count(tile) == 3:
new_hand = deepcopy(hand)
new_hand.remove(tile)
new_hand.remove(tile)
new_hand.remove(tile)
yield f'{tile}{tile[1]}{tile[1]}', new_hand
if tile[0] in ['m', 's', 'p']:
new_hand = deepcopy(hand)
digit = int(tile[1])
next_tile = f'{tile[0]}{digit + 1}'
next_next = f'{tile[0]}{digit + 2}'
new_hand.remove(tile)
if next_tile in new_hand and next_next in new_hand:
new_hand.remove(next_tile)
new_hand.remove(next_next)
yield f'{tile}{digit + 1}{digit + 2}', new_hand
def riichi_mahjong_sets(hand: list) -> list:
for set1, new_hand1 in get_sets(hand):
for set2, new_hand2 in get_sets(new_hand1):
for set3, new_hand3 in get_sets(new_hand2):
for set4, new_hand4 in get_sets(new_hand3):
for pair1, new_hand5 in get_pairs(new_hand4):
return sorted([set1, set2, set3, set4, pair1])
return sorted([])
Jan. 25, 2024
Comments: