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.
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.
🀇🀇🀇🀎🀎🀎🀓🀓🀙🀚🀛🀟🀠 : 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) ,
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.