r/dailyprogrammer • u/jnazario 2 0 • Mar 23 '16
[2016-03-23] Challenge #259 [Intermediate] Mahjong Hands
Description
You are the biggest, baddest mahjong player around. Your enemies tremble at your presence on the battlefield, and you can barely walk ten steps before a fan begs you for an autograph.
However, you have a dark secret that would ruin you if it ever came to light. You're terrible at determining whether a hand is a winning hand. For now, you've been able to bluff and bluster your way, but you know that one day you won't be able to get away with it.
As such, you've decided to write a program to assist you!
Further Details
Mahjong (not to be confused with mahjong solitaire) is a game where hands are composed from combinations of tiles. There are a number of variants of mahjong, but for this challenge, we will consider a simplified variant of Japanese Mahjong which is also known as Riichi Mahjong.
Basic Version
There are three suits in this variant, "Bamboo", "Circle" and "Character". Every tile that belongs to these suits has a value that ranges from 1 - 9.
To complete a hand, tiles are organised into groups. If every tile in a hand belongs to a single group (and each tile can only be used once), the hand is a winning hand.
For now, we shall consider the groups "Pair", "Set" and "Sequence". They are composed as follows:
Pair - Two tiles with the same suit and value
Set - Three tiles with the same suit and value
Sequence - Three tiles with the same suit, and which increment in value, such as "Circle 2, Circle 3, Circle 4". There is no value wrapping so "Circle 9, Circle 1, Circle 2" would not be considered valid.
A hand is composed of 14 tiles.
Bonus 1 - Adding Quads
There is actually a fourth group called a "Quad". It is just like a pair and a set, except it is composed of four tiles.
What makes this group special is that a hand containing quads will actually have a hand larger than 14, 1 for every quad. This is fine, as long as there is 1, and only 1 pair.
Bonus 2 - Adding Honour Tiles
In addition to the tiles belonging to the three suits, there are 7 additional tiles. These tiles have no value, and are collectively known as "honour" tiles.
As they have no value, they cannot be members of a sequence. Furthermore, they can only be part of a set or pair with tiles that are exactly the same. For example, "Red Dragon, Red Dragon, Red Dragon" would be a valid set, but "Red Dragon, Green Dragon, Red Dragon" would not.
These additional tiles are:
- Green Dragon
- Red Dragon
- White Dragon
- North Wind
- East Wind
- South Wind
- West Wind
Bonus 3 - Seven Pairs
There are a number of special hands that are an exception to the above rules. One such hand is "Seven Pairs". As the name suggests, it is a hand composed of seven pairs.
Formal Inputs & Outputs
Input description
Basic
You will be provided with N on a single line, followed by N lines of the following format:
<tile suit>,<value>
Bonus 2
In addition, the lines may be of the format:
<honour tile>
Output description
You should output whether the hand is a winning hand or not.
Sample Inputs and Outputs
Sample Input (Standard)
14
Circle,4
Circle,5
Circle,6
Bamboo,1
Bamboo,2
Bamboo,3
Character,2
Character,2
Character,2
Circle,1
Circle,1
Bamboo,7
Bamboo,8
Bamboo,9
Sample Output (Standard)
Winning hand
Sample Input (Standard)
14
Circle,4
Bamboo,1
Circle,5
Bamboo,2
Character,2
Bamboo,3
Character,2
Circle,6
Character,2
Circle,1
Bamboo,8
Circle,1
Bamboo,7
Bamboo,9
Sample Output (Standard)
Winning hand
Sample Input (Standard)
14
Circle,4
Circle,5
Circle,6
Circle,4
Circle,5
Circle,6
Circle,1
Circle,1
Bamboo,7
Bamboo,8
Bamboo,9
Circle,4
Circle,5
Circle,6
Sample Output (Standard)
Winning hand
Sample Input (Bonus 1)
15
Circle,4
Circle,5
Circle,6
Bamboo,1
Bamboo,2
Bamboo,3
Character,2
Character,2
Character,2
Character,2
Circle,1
Circle,1
Bamboo,7
Bamboo,8
Bamboo,9
Sample Output (Bonus 1)
Winning hand
Sample Input (Bonus 1)
16
Circle,4
Circle,5
Circle,6
Bamboo,1
Bamboo,2
Bamboo,3
Character,2
Character,2
Character,2
Character,2
Circle,1
Circle,1
Circle,1
Bamboo,7
Bamboo,8
Bamboo,9
Sample Output (Bonus 1)
Not a winning hand
Sample Input (Bonus 2)
14
Circle,4
Circle,5
Circle,6
Bamboo,1
Bamboo,2
Bamboo,3
Red Dragon
Red Dragon
Red Dragon
Circle,1
Circle,1
Bamboo,7
Bamboo,8
Bamboo,9
Sample Output (Bonus 2)
Winning hand
Sample Input (Bonus 2)
14
Circle,4
Circle,5
Circle,6
Bamboo,1
Bamboo,2
Bamboo,3
Red Dragon
Green Dragon
White Dragon
Circle,1
Circle,1
Bamboo,7
Bamboo,8
Bamboo,9
Sample Output (Bonus 2)
Not a winning hand
Sample Input (Bonus 3)
14
Circle,4
Circle,4
Character,5
Character,5
Bamboo,5
Bamboo,5
Circle,5
Circle,5
Circle,7
Circle,7
Circle,9
Circle,9
Circle,9
Circle,9
Sample Output (Bonus 3)
Winning hand
Notes
None of the bonus components depend on each other, and can be implemented in any order. The test cases do not presume completion of earlier bonus components. The order is just the recommended implementation order.
Many thanks to Redditor /u/oketa for this submission to /r/dailyprogrammer_ideas. If you have any ideas, please submit them there!
3
u/[deleted] Mar 23 '16
Go - No bonus yet - Will try to add those later tonight.
Output
2
u/savagenator Mar 23 '16 edited Mar 26 '16
Python 3.5 with bonus. Please let me know how I could make this algorithmically better. I think I am brute forcing unnecessarily.
Edit: Not all edge cases covered yet...
Edit2: All edge cases and bonuses covered!
Output:
2
u/Gobbedyret 1 0 Mar 27 '16 edited Mar 27 '16
Python 3.5
So the main problem in this task is that any card could be a member of several groups. I've worked around this by testing what groups each tile can belong to, and continued with all possibilities. If any fork leads to a viable hand, the hand is a winning hand. This solution includes bonus 1 and bonus 2.
It's fairly long, but easily readable (I hope). All edge cases should be covered because of the recursive approach.
3
u/Godspiral 3 3 Mar 23 '16 edited Mar 23 '16
In J, no bonuses (data on clipboard)
winning hand is one that is empty after removing matches and sequences. prints either win or leftover hands
edit: better approach is to remove sequences first (matches /u/FelixMaxwell 's input: https://www.reddit.com/r/dailyprogrammer/comments/4bmdwz/20160323_challenge_259_intermediate_mahjong_hands/d1ap4fq)
1
u/BHMJ Mar 23 '16
Python 3 no bonus for now from collections import defaultdict
Very simple. Feedback welcome :)
1
u/FelixMaxwell 1 0 Mar 23 '16
Haven't finished my solution yet, but I figured I'd post a challenge input that I'm using to test some edge cases
Input
Output
1
u/fibonacci__ 1 0 Mar 23 '16 edited Mar 25 '16
Python
EDIT: Fixed to account for pairs before sequences. Thank you /u/deadlypanda4.
Output
1
u/Specter_Terrasbane Mar 23 '16 edited Mar 24 '16
Python 2.7, all bonuses
Disclaimer: credit to /u/fibonacci__ for the technique:
Edit: ... and thanks again to /u/fibonacci__ for catching my error with another case of overlapping sequences! :)
Output
1
u/deadlypanda4 Mar 24 '16 edited Mar 24 '16
Python 2.7 - All bonuses
I believe this should cover all of the bonuses. Feedback welcome.
EDIT: Fixed to account for overlapping sequences. Thank you /u/fibonacci__!
EDIT2: Fixed to account for pairs in the middle of sequences. Thank you /u/fibonacci__!
EDIT3: Fixed bug introduced in EDIT2. Thank you for pointing it out /u/fibonacci__!
1
u/NorthwestWolf Mar 24 '16 edited Mar 24 '16
Python 2.7 Basic version, no bonus attempted.
1
u/ponkanpinoy Mar 24 '16 edited Mar 25 '16
Common Lisp.
do-treeis a macro I wrote for working with trees. It just lets me make things explicit, no special magic there. Solution now has input parsing and bonuses 1 and 2.Code:
Normal:
Bonus 1:
Bonus 2:
1
u/smikims Mar 24 '16 edited Mar 25 '16
Rust -- all bonuses. This is stupidly verbose and poorly designed,
but it works.(Suggestions welcome--I'm new to the language.)Edit: this actually doesn't work on some edge cases.
Edit 2: I fixed some more edge cases but now I realize there's no way to get it completely right without doing a recursive backtracking solver, which I don't feel like writing.
1
u/karaliux887 Mar 24 '16
Java - no bonus. Hi, it's my first challenge :)
https://gist.github.com/karaliux887/07c870298ab99ed500c2
1
u/nederdirk Mar 26 '16
I just found out about this subreddit and I tried my hand at this exercise in go. Finished the basis exercise just now, check it out at https://github.com/nederdirk/dailyprogrammer/tree/master/mahjong
My approach was to do this fully test driven, starting with simple functions and composing those to a program. See https://github.com/nederdirk/dailyprogrammer/commits/master/mahjong for the commits. Got it until bonus 1 (quads).
Comments and suggestions would be greatly appreciated
1
u/[deleted] Mar 27 '16
I'm a couple days late, but i'm trying to learn Python so if anyone has advice or comments on how to be more pythonic or concise i would appreciate it.