]> gitweb.factorcode.org Git - factor.git/blob - extra/project-euler/054/054.factor
Switch to https urls
[factor.git] / extra / project-euler / 054 / 054.factor
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 poker
4     project-euler.common sequences ;
5 IN: project-euler.054
6
7 ! https://projecteuler.net/index.php?section=problems&id=54
8
9 ! DESCRIPTION
10 ! -----------
11
12 ! In the card game poker, a hand consists of five cards and are ranked, from
13 ! lowest to highest, in the following way:
14
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 suit.
24 !     * Royal Flush: Ten, Jack, Queen, King, Ace, in same suit.
25
26 ! The cards are valued in the order:
27 !     2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, Ace.
28
29 ! If two players have the same ranked hands then the rank made up of the
30 ! highest value wins; for example, a pair of eights beats a pair of fives (see
31 ! example 1 below). But if two ranks tie, for example, both players have a pair
32 ! of queens, then highest cards in each hand are compared (see example 4
33 ! below); if the highest cards tie then the next highest cards are compared,
34 ! and so on.
35
36 ! Consider the following five hands dealt to two players:
37
38 !     Hand   Player 1            Player 2              Winner
39 !     ---------------------------------------------------------
40 !     1      5H 5C 6S 7S KD      2C 3S 8S 8D TD
41 !            Pair of Fives       Pair of Eights        Player 2
42
43 !     2      5D 8C 9S JS AC      2C 5C 7D 8S QH
44 !            Highest card Ace    Highest card Queen    Player 1
45
46 !     3      2D 9C AS AH AC      3D 6D 7D TD QD
47 !            Three Aces          Flush with Diamonds   Player 2
48
49 !     4      4D 6S 9H QH QC      3D 6D 7H QD QS
50 !            Pair of Queens      Pair of Queens
51 !            Highest card Nine   Highest card Seven    Player 1
52
53 !     5      2H 2D 4C 4D 4S      3C 3D 3S 9S 9D
54 !            Full House          Full House
55 !            With Three Fours    With Three Threes     Player 1
56
57 ! The file, poker.txt, contains one-thousand random hands dealt to two players.
58 ! Each line of the file contains ten cards (separated by a single space): the
59 ! first five are Player 1's cards and the last five are Player 2's cards. You
60 ! can assume that all hands are valid (no invalid characters or repeated
61 ! cards), each player's hand is in no specific order, and in each hand there is
62 ! a clear winner.
63
64 ! How many hands does Player 1 win?
65
66
67 ! SOLUTION
68 ! --------
69
70 <PRIVATE
71
72 : source-054 ( -- seq )
73     "resource:extra/project-euler/054/poker.txt" ascii file-lines
74     [ [ 14 head-slice ] [ 14 tail-slice* ] bi 2array ] map ;
75
76 PRIVATE>
77
78 : euler054 ( -- answer )
79     source-054 [ [ string>value ] map first2 before? ] count ;
80
81 ! [ euler054 ] 100 ave-time
82 ! 34 ms ave run time - 2.65 SD (100 trials)
83
84 SOLUTION: euler054