1 ! Copyright (c) 2009 Aaron Schaefer.
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: arrays io.encodings.ascii io.files kernel math.order
4 poker project-euler.common sequences ;
7 ! https://projecteuler.net/problem=54
12 ! In the card game poker, a hand consists of five cards and are
13 ! ranked, from lowest to highest, in the following way:
15 ! * High Card: Highest value card.
16 ! * One Pair: Two cards of the same value.
17 ! * Two Pairs: Two different pairs.
18 ! * Three of a Kind: Three cards of the same value.
19 ! * Straight: All cards are consecutive values.
20 ! * Flush: All cards of the same suit.
21 ! * Full House: Three of a kind and a pair.
22 ! * Four of a Kind: Four cards of the same value.
23 ! * Straight Flush: All cards are consecutive values of same
25 ! * Royal Flush: Ten, Jack, Queen, King, Ace, in same suit.
27 ! The cards are valued in the order:
28 ! 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, Ace.
30 ! If two players have the same ranked hands then the rank made
31 ! up of the highest value wins; for example, a pair of eights
32 ! beats a pair of fives (see example 1 below). But if two ranks
33 ! tie, for example, both players have a pair of queens, then
34 ! highest cards in each hand are compared (see example 4 below);
35 ! if the highest cards tie then the next highest cards are
36 ! compared, and so on.
38 ! Consider the following five hands dealt to two players:
40 ! Hand Player 1 Player 2 Winner
41 ! ---------------------------------------------------------
42 ! 1 5H 5C 6S 7S KD 2C 3S 8S 8D TD
43 ! Pair of Fives Pair of Eights Player 2
45 ! 2 5D 8C 9S JS AC 2C 5C 7D 8S QH
46 ! Highest card Ace Highest card Queen Player 1
48 ! 3 2D 9C AS AH AC 3D 6D 7D TD QD
49 ! Three Aces Flush with Diamonds Player 2
51 ! 4 4D 6S 9H QH QC 3D 6D 7H QD QS
52 ! Pair of Queens Pair of Queens
53 ! Highest card Nine Highest card Seven Player 1
55 ! 5 2H 2D 4C 4D 4S 3C 3D 3S 9S 9D
56 ! Full House Full House
57 ! With Three Fours With Three Threes Player 1
59 ! The file, poker.txt, contains one-thousand random hands dealt
60 ! to two players. Each line of the file contains ten cards
61 ! (separated by a single space): the first five are Player 1's
62 ! cards and the last five are Player 2's cards. You can assume
63 ! that all hands are valid (no invalid characters or repeated
64 ! cards), each player's hand is in no specific order, and in
65 ! each hand there is a clear winner.
67 ! How many hands does Player 1 win?
75 : source-054 ( -- seq )
76 "resource:extra/project-euler/054/poker.txt" ascii file-lines
77 [ [ 14 head-slice ] [ 14 tail-slice* ] bi 2array ] map ;
81 : euler054 ( -- answer )
82 source-054 [ [ string>value ] map first2 before? ] count ;
84 ! [ euler054 ] 100 ave-time
85 ! 34 ms ave run time - 2.65 SD (100 trials)