9
1
Introduction to Mahjong tiles
Mahjong (麻雀) is a board game that originates from China. Mahjong tiles used in this challenge are in Unicode points U+1F000 – U+1F021:
They are categorized as:
Winds (風牌): (East Wind), (South Wind), (West Wind), (North Wind)
Dragons (三元牌): (Red Dragon), (Green Dragon), (White Dragon)
Ten Thousands (萬子): (One of the Ten Thousands) through (Nine of the Ten Thousands)
Bamboos (索子): (One of the Bamboos; note that it depicts a bird) through (Nine of the Bamboos)
Circles (筒子): (One of the Circles) through (Nine of the Circles)
Winds and Dragons are together called Honors (字牌).
Ten Thousands, Bamboos and Circles are each called a suit and together called Numbers (数牌). They have ranks from 1 to 9.
Among Numbers, the Ones and the Nines are called Terminals (老頭牌). The Twos through the Eights are called Simples (中張牌).
Honors and Terminals are together called Orphans (幺九牌).
Every Mahjong tiles have 4 copies of themselves.
Mahjong Hand
A legal Hand consists of:
A Head (対子), which is 2 identical tiles.
4 Bodies (面子), each which is either:
A Sequence (順子), which is 3 consecutive Numbers with same suit.
A Triplet (刻子), which is 3 identical tiles.
Hence, a Hand consists of 14 tiles.
There are exceptional valid Hands. Seven Heads (七対子), which is 7 distinct Heads, and Thirteen Orphans (国士無双), which collects all 13 Orphans with an additional Orphan.
Tenpais and Shantens
13 Mahjong tiles will be given as the input. If an additional tile would make a valid Hand, this is called Tenpai (聽牌). Tenpai is zero Shantens.
If a tile must be exchanged to be Tenpai, this is 1-Shanten (1向聴).
If a tile must be exchanged to be 1-Shanten, this is 2-Shantens, and so on.
The objective is to output the number of Shantens.
Examples
: Tenpai waiting for or to make a Sequence. An example of Two-sided Wait (兩面待機).
: Tenpai waiting for or to make a Triplet. An example of Wait for a Triplet (双碰待機).
: Tenpai waiting for to make a Sequence. An example of One-sided Wait (邊張待機).
: Tenpai waiting for to make a Sequence. An example of Wait for the Middle (坎張待機).
: Tenpai waiting for to make a Head. An example of Wait for a Head (單騎待機).
: Tenpai waiting for , , or . This example demonstrates how multiple ways of waiting can be combined.
: Tenpai waiting for any of Ten Thousands. This is called Nine Gates (九蓮宝燈), and has the highest possible number of tiles to wait for to be a "normal" Hand.
: Tenpai waiting for to make a Head. An example of Seven Heads.
: Tenpai waiting for . An example of Thirteen Orphans.
: Tenpai waiting for any of Orphans. Another example of Thirteen Orphans.
: 1-Shanten. Note that this is not Tenpai waiting for because it would be a fifth .
: 1-Shanten. Note that this is not Tenpai for Seven Heads because getting will result in a duplicated Head.
: 2-Shantens.
: 6-Shantens. This is the highest possible number of Shantens.
You can demonstrate arbitrary examples here. (for 13 or 14 tiles)
Rules about code golf
Input type and format doesn't matter, but it must consist of the Unicode characters above. In C++, valid examples include
std::u8string
(sorted or not) ,u32char_t[13]
, andstd::multiset<u32char_t>
.Output type and format doesn't matter either.
Invalid input (not exactly 13 tiles, contains 5 copies of the same tile, contains a character not in U+1F000 – U+1F021, etc) falls into don't care situation.
1@KevinCruijssen Thanks. Since I want to see how golfing languages handle Unicode, I removed that rule. – Dannyu NDos – 2020-02-10T09:01:18.700
FYI: for some reason, the Mahjong tile characters are hard to read on (my version of) Firefox which alternates between small monochrome characters and larger color characters. Weirdly enough, the monochrome and color versions of the same character are both used depending on its position in the post. No problem on Chrome on the same laptop. – Arnauld – 2020-02-10T10:23:23.833
@Arnauld Really? I'm using Firefox on Ubuntu, and to me, only is displayed as emoji, and all other tiles are displayed as monochrome characters. I'm afraid I don't know a solution. – Dannyu NDos – 2020-02-10T10:26:55.733
It is quite painful to convert from Unicode to the mpsz notation used by the tool linked in the challenge, or the other way around. So ...
– Arnauld – 2020-02-11T02:21:36.220... here are two scripts to perform both conversions.
– Arnauld – 2020-02-11T02:21:46.9071Also, I think we need more non-tenpai test cases, especially some tricky ones where it's not obvious to decide which groups should be removed or completed to get the optimal N-shantens. – Arnauld – 2020-02-11T02:27:53.683
Man, seeing this challenge brought me back to when I played some games for fun (no actual monetary stakes). I was a beginner and wanted to be super sure I would have the yaku needed to win with the hand I had, and somehow ended up on tenpai for a closed chin'itsu-tan'yao-ryanpeikou, which (if I won with it) has some ridiculous score value... I didn't win the round, but when I showed what I had after the game the table basically flipped out with how insane my hand was lmao – Value Ink – 2020-02-14T08:17:05.833