PyPE solution to PE problem 54
PyPE solution to PE problem 54
Blueprint information
- Status:
- Complete
- Approver:
- None
- Priority:
- Undefined
- Drafter:
- None
- Direction:
- Needs approval
- Assignee:
- None
- Definition:
- Approved
- Series goal:
- None
- Implementation:
- Implemented
- Milestone target:
- None
- Started by
- Scott Armitage
- Completed by
- Scott Armitage
Related branches
Related bugs
Sprints
Whiteboard
ProjectEule
===
In the card game poker, a hand consists of five cards and are ranked, from
lowest to highest, in the following way:
0 High Card: Highest value card.
1 One Pair: Two cards of the same value.
2 Two Pairs: Two different pairs.
3 Three of a Kind: Three cards of the same value.
4 Straight: All cards are consecutive values.
5 Flush: All cards of the same suit.
6 Full House: Three of a kind and a pair.
7 Four of a Kind: Four cards of the same value.
8 Straight Flush: All cards are consecutive values of same suit.
9 Royal Flush: Ten, Jack, Queen, King, Ace, in same suit.
The cards are valued in the order:
2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, Ace.
If two players have the same ranked hands then the rank made up of the
highest value wins; for example, a pair of eights beats a pair of fives
(see example 1 below). But if two ranks tie, for example, both players
have a pair of queens, then highest cards in each hand are compared (see
example 4 below); if the highest cards tie then the next highest cards
are compared, and so on.
Consider the following five hands dealt to two players:
Hand Player 1 Player 2 Winner
1 5H 5C 6S 7S KD 2C 3S 8S 8D TD Player 2
-
2 5D 8C 9S JS AC 2C 5C 7D 8S QH Player 1
-
3 2D 9C AS AH AC 3D 6D 7D TD QD Player 2
-
4 4D 6S 9H QH QC 3D 6D 7H QD QS Player 1
-
5 2H 2D 4C 4D 4S 3C 3D 3S 9S 9D Player 1
The file, poker.txt, contains one-thousand random hands dealt to two
players. Each line of the file contains ten cards (separated by a single
space): the first five are Player 1's cards and the last five are Player 2's
cards. You can assume that all hands are valid (no invalid characters or
repeated cards), each player's hand is in no specific order, and in each
hand there is a clear winner.
How many hands does Player 1 win?
Solution
--------
This is a brute-force solution, the most difficult part of which is
generating a hand's "score". The motif used here is that each type of hand
is ranked from 0 (high-card) to 8 (straight-flush). Then, a list of sub-
scores is provided that allow one to uniquely identify a hand's score
against any other hand (except for a perfect tie). For example, for a
straight or straight-flush, the highest card in the straight is provided
as a secondary score. For a flush, all of the cards are provided in
decreasing order.
Answer
------
376