14
2
Idea thanks to @MartinBüttner from a discussion in chat
Mahjong is a tile game that is immensely popular in Asia. It is typically played with four players, and the goal of the game is to be the first person to complete a valid hand using the tiles. For this challenge, we will consider a simplified version of the game — PPCG mahjong.
In PPCG mahjong, there are three suits – m
, p
and s
– and the tiles are numbered from 1
to 9
. There are exactly four copies of each tile, and the tiles are denoted by its number followed by its suit (e.g. 3m
, 9s
).
A completed PPCG mahjong hand consists of four sets of three and a pair, for a total of 14 tiles.
A set of three can be either:
- Three of the same tile (e.g.
4s 4s 4s
, but not4m 4p 4s
), or - A sequence of three consecutive tiles in the same suit (e.g.
1s 2s 3s
or6p 7p 8p
but not3s 4m 5m
or3p 5p 7p
). Sequences do not wrap (so9m 1m 2m
is invalid).
A pair is simply two identical tiles (e.g. 5s 5s
).
The challenge
Your program will receive a space-separated hand of 13 tiles, with each tile appearing no more than four times. You may write either a full program or a function which takes in a string.
Your task is to find all possible 14th tiles ("waits") which, when added to the hand, would form a completed PPCG mahjong hand. The outputted tiles should be space-separated, but can be in any order. Leading or trailing whitespace is allowed.
Your program should run in a reasonable amount of time, no longer than a minute.
Examples
Input: 1m 1m 1m 4s 4s 4s 7p 7p 7p 3m 3m 3m 9s
Output: 9s
Input: 1m 1m 1m 3m 3m 3m 5m 5m 5m 2s 3s 7p 8p
Output:
Input: 1m 2m 2m 3m 3m 3m 3m 4m 1s 1s 9s 9s 9s
Output: 1s
Input: 1m 1m 1m 2m 3m 4m 5m 6m 7m 8m 9m 9m 9m
Output: 1m 2m 3m 4m 5m 6m 7m 8m 9m
Input: 1m 1m 1m 5p 2m 3m 5p 7s 8s 5p 9s 9s 9s
Output: 1m 4m 6s 9s
In the first example, the 1m 4s 7p 3m
all form existing triplets, leaving the lone 9s
to form a pair.
In the second example, the 2s 3s
and 7p 8p
can only form sequences, and the remaining tiles can only form triplets. Hence no pair can be formed, and there is no output.
In the third example, the hand splits into 1m2m3m 2m3m4m 3m3m 1s1s 9s9s9s
. Normally this would be a wait for 3m 1s
, but as all four 3m
have been used, the only available wait is 1s
.
In the fourth example, all of the m
tiles complete the hand. For example, for 1m
, one could have 1m1m1m 1m2m3m 4m5m6m 7m8m9m 9m9m
which is a completed hand.
Try to work out the rest of the fourth example and the fifth example :)
Scoring
This is code-golf, so the solution in the fewest bytes wins. Standard loopholes apply.
What about seven pairs? thirteen orphans? you could do something even more complex with honors :) Do you think it's out-of-purpose if I create a codegolf that asks to calculate the shanten (minimal number of tiles needed before getting tenpai - ready to win) of a hand? – V. Courtois – 2017-07-20T07:41:34.047
@VCourtois It's been a while, but I recall specifically excluding seven pairs, thirteen orphans, honours and already made calls so as to not overcomplicate the challenge for people new to the game. I think I considered making a shanten challenge later after that but never did in the end - if you'd like to post one I think it'd make a fine challenge. – Sp3000 – 2017-07-20T08:18:59.910
9Thank you for actually doing Mahjong, rather than the (IMO annoying) solitaire using the tiles that westerners seem to think of whenever they hear the word "Mahjong". – Justin – 2014-10-16T05:00:24.757
@Quincunx Fun fact: This challenge came about because I wanted to do a challenge with an ASCII representation of Mahjong solitaire (which I might still do at some point...). I did call it "Mahjong solitaire" though. ;) – Martin Ender – 2014-10-16T10:43:42.470
2@Quincunx: I don't think it's their fault. It's game developers' fault for calling their "Mahjong solitaire" games "Mahjong" and nothing else. – Joe Z. – 2014-10-20T18:50:22.887