/extra/project-euler/054/054.factor

http://github.com/abeaumont/factor · Factor · 84 lines · 11 code · 22 blank · 51 comment · 0 complexity · dcc94826e072745b6095d295ddb19a73 MD5 · raw file

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