solution to problem 54

master
Evan Hemsley 2016-09-04 16:00:58 -07:00
parent c591b75442
commit 38412d7d78
7 changed files with 1760 additions and 0 deletions

1000
input/p054_poker.txt Normal file

File diff suppressed because it is too large Load Diff

2
ruby/.rspec Normal file
View File

@ -0,0 +1,2 @@
--color
--require spec_helper

3
ruby/Gemfile Normal file
View File

@ -0,0 +1,3 @@
source 'https://rubygems.org'
gem 'rspec'

26
ruby/Gemfile.lock Normal file
View File

@ -0,0 +1,26 @@
GEM
remote: https://rubygems.org/
specs:
diff-lcs (1.2.5)
rspec (3.5.0)
rspec-core (~> 3.5.0)
rspec-expectations (~> 3.5.0)
rspec-mocks (~> 3.5.0)
rspec-core (3.5.3)
rspec-support (~> 3.5.0)
rspec-expectations (3.5.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.5.0)
rspec-mocks (3.5.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.5.0)
rspec-support (3.5.0)
PLATFORMS
ruby
DEPENDENCIES
rspec
BUNDLED WITH
1.12.5

245
ruby/euler054.rb Normal file
View File

@ -0,0 +1,245 @@
VALUES = [:two, :three, :four, :five, :six, :seven, :eight, :nine, :ten, :jack, :queen, :king, :ace]
SUITS = [:heart, :diamond, :spade, :club]
STRING_TO_SUIT_MAP = { 'H' => :heart,
'D' => :diamond,
'S' => :spade,
'C' => :club }
STRING_TO_VALUE_MAP = {'A' => :ace,
'K' => :king,
'Q' => :queen,
'J' => :jack,
'10' => :ten,
'T' => :ten,
'9' => :nine,
'8' => :eight,
'7' => :seven,
'6' => :six,
'5' => :five,
'4' => :four,
'3' => :three,
'2' => :two}
class Card
include Comparable
attr_reader :suit, :value
def initialize(suit, value)
@suit = STRING_TO_SUIT_MAP[suit]
@value = STRING_TO_VALUE_MAP[value]
end
def succ_value
if value == :ace
:two
else
VALUES[VALUES.index(value) + 1]
end
end
def succ? other
succ_value == other.value
end
def <=> other
VALUES.index(value) <=> VALUES.index(other.value)
end
def === other
suit == other.suit and value == other.value
end
def ace?
value == :ace
end
end
class Hand
include Comparable
attr_reader :cards
def initialize(*cards)
if cards.first.is_a? String
card_strings = cards.first.split(' ')
@cards = card_strings.map { |str| str.split('') }.map { |value, suit| Card.new(suit, value) }
else
@cards = cards
end
end
def flush?
suits.uniq.one?
end
def straight?
if ace?
all_successors? cards.sort or all_successors? aces_low
else
all_successors? cards.sort
end
end
def straight_flush?
straight? and flush?
end
def royal_flush?
flush? and
values.include?(:ten) and
values.include?(:jack) and
values.include?(:queen) and
values.include?(:king) and
values.include?(:ace)
end
def four_of_a_kind?
same_of_value?(4)
end
def three_of_a_kind?
same_of_value?(3)
end
def full_house?
same_of_value?(3) and same_of_value?(2)
end
def two_pair?
collapsed == 2 and same_of_value?(2)
end
def pair?
same_of_value?(2)
end
def values
cards.map(&:value)
end
def suits
cards.map(&:suit)
end
def aces_low
sorted = cards.sort
while sorted.last.ace?
ace = sorted.pop
sorted.insert(0, ace)
end
sorted
end
def ace?
cards.any? { |card| card.ace? }
end
def high_card
cards.sort.last
end
def full_house_high_value
cards.group_by { |card| card.value }.detect { |k, v| v.count == 3 }.first
end
def full_house_low_value
cards.group_by { |card| card.value }.detect { |k, v| v.count == 2 }.first
end
def high_match_value
cards.group_by { |card| card.value }.select { |k, v| v.count > 1 }.keys.max { |a, b| VALUES.index(a) <=> VALUES.index(b) }
end
def low_match_value
cards.group_by { |card| card.value }.select { |k, v| v.count > 1 }.keys.min { |a, b| VALUES.index(a) <=> VALUES.index(b) }
end
def kicker
removed_value(high_match_value).sort.last
end
def <=> other
poker_score <=> other.poker_score
end
def poker_score
if royal_flush?
900000
elsif straight_flush?
800000 + high_card_score
elsif four_of_a_kind?
700000 + high_match_score * 100 + kicker_score
elsif full_house?
600000 + full_house_high_score * 1000 + full_house_low_score * 10
elsif flush?
500000 + high_card_score
elsif straight?
400000 + high_card_score
elsif three_of_a_kind?
300000 + high_match_score * 100 + kicker_score
elsif two_pair?
200000 + high_match_score * 1000 + low_match_score * 10 + kicker_score
elsif pair?
100000 + high_match_score * 100 + kicker_score
else
high_card_score
end
end
private
def kicker_score
VALUES.index(kicker.value)
end
def high_card_score
VALUES.index(high_card.value)
end
def high_match_score
VALUES.index(high_match_value)
end
def low_match_score
VALUES.index(low_match_value)
end
def full_house_high_score
VALUES.index(full_house_high_value)
end
def full_house_low_score
VALUES.index(full_house_low_value)
end
def removed_value(value)
cards.reject { |card| card.value == value }
end
def collapsed
cards.size - values.uniq.size
end
def same_of_value(n)
cards.detect { |card| cards.count { |c| c.value == card.value } == n }.value
end
def same_of_value?(n)
cards.any? { |card| cards.count { |c| c.value == card.value } == n }
end
def all_successors?(cards)
cards.all? {|card| card === cards.last || card.succ?(cards[cards.index(card) + 1]) }
end
end
def games
game_lines = File.readlines("../input/p054_poker.txt")
game_lines.map { |game_line| [game_line[0..13], game_line[15..28]] }.map { |one, two| [Hand.new(one), Hand.new(two)] }
end
def solution
games.map { |one, two| one > two }.count(true)
end

381
ruby/spec/euler054_spec.rb Normal file
View File

@ -0,0 +1,381 @@
require_relative '../euler054.rb'
describe Card, 'succ_value' do
context 'with an ace' do
card = Card.new('S', 'A')
it 'returns two' do
expect(card.succ_value).to eql :two
end
end
context 'with no ace' do
card = Card.new('D', '9')
it 'returns ten' do
expect(card.succ_value).to eql :ten
end
end
end
describe Card, 'succ?' do
context 'with an ace' do
card = Card.new('S', 'A')
context 'with a two' do
card_two = Card.new('S', '2')
it 'returns true' do
expect(card.succ?(card_two)).to eql true
end
end
context 'otherwise' do
card_two = Card.new('S', '5')
it 'returns false' do
expect(card.succ?(card_two)).to eql false
end
end
end
context 'with a five' do
card = Card.new('S', '5')
context 'with a six' do
card_two = Card.new('S', '6')
it 'returns true' do
expect(card.succ?(card_two)).to eql true
end
end
context 'otherwise' do
card_two = Card.new('S', '8')
it 'returns false' do
expect(card.succ?(card_two)).to eql false
end
end
end
end
describe Card, '===' do
card = Card.new('S', '6')
context 'is the same card' do
card_two = Card.new('S', '6')
it 'returns true' do
expect(card === card_two).to eql true
end
end
context 'is not the same card' do
card_two = Card.new('D', '9')
it 'returns false' do
expect(card === card_two).to eql false
end
end
end
describe Hand, 'initialize' do
context 'given a string' do
hand = Hand.new('2C 5C 7D 8S QH')
it 'generates the proper cards' do
expect(hand.values).to include(:two, :five, :seven, :eight, :queen)
end
end
end
describe Hand, 'flush?' do
context 'is a flush' do
hand = Hand.new(Card.new('H', '2'), Card.new('H', '5'), Card.new('H', '9'), Card.new('H', 'J'), Card.new('H', 'K'))
it 'returns true' do
expect(hand.flush?).to eql true
end
end
context 'is not a flush' do
hand = Hand.new(Card.new('S', '2'), Card.new('H', '5'), Card.new('H', '9'), Card.new('H', 'J'), Card.new('H', 'K'))
it 'returns false' do
expect(hand.flush?).to eql false
end
end
end
describe Hand, 'straight?' do
context 'is a straight' do
context 'there is an ace' do
hand = Hand.new(Card.new('D', 'A'), Card.new('S', '2'), Card.new('S', '3'), Card.new('S', '4'), Card.new('S', '5'))
it 'returns true' do
expect(hand.straight?).to eql true
end
end
context 'there is no ace' do
hand = Hand.new(Card.new('S', '3'), Card.new('S', '4'), Card.new('D', '5'), Card.new('C', '6'), Card.new('D', '7'))
it 'returns true' do
expect(hand.straight?).to eql true
end
end
end
context 'is not a straight' do
hand = Hand.new(Card.new('D', 'A'), Card.new('C', '6'), Card.new('S', '8'), Card.new('C', '10'), Card.new('S', 'J'))
it 'returns false' do
expect(hand.straight?).to eql false
end
end
end
describe Hand, 'straight_flush?' do
context 'is a straight flush' do
hand = Hand.new(Card.new('S', '8'), Card.new('S', '9'), Card.new('S', '10'), Card.new('S', 'J'), Card.new('S', 'Q'))
it 'returns true' do
expect(hand.straight_flush?).to eql true
end
end
context 'is not a straight flush' do
hand = Hand.new(Card.new('D', 'A'), Card.new('C', '6'), Card.new('S', '8'), Card.new('C', '10'), Card.new('S', 'J'))
it 'returns false' do
expect(hand.straight_flush?).to eql false
end
end
end
describe Hand, 'royal_flush?' do
context 'is a royal flush' do
hand = Hand.new(Card.new('S', '10'), Card.new('S', 'J'), Card.new('S', 'Q'), Card.new('S', 'K'), Card.new('S', 'A'))
it 'returns true' do
expect(hand.royal_flush?).to eql true
end
end
context 'is not a royal flush' do
hand = Hand.new(Card.new('D', 'A'), Card.new('C', '6'), Card.new('S', '8'), Card.new('C', '10'), Card.new('S', 'J'))
it 'returns false' do
expect(hand.royal_flush?).to eql false
end
end
end
describe Hand, 'four_of_a_kind' do
context 'contains a four of a kind' do
hand = Hand.new(Card.new('S', '9'), Card.new('D', '9'), Card.new('C', '9'), Card.new('H', '9'), Card.new('S', '2'))
it 'returns true' do
expect(hand.four_of_a_kind?).to eql true
end
end
context 'does not contain a four of a kind' do
hand = Hand.new(Card.new('D', 'A'), Card.new('C', '6'), Card.new('S', '8'), Card.new('C', '10'), Card.new('S', 'J'))
it 'returns false' do
expect(hand.four_of_a_kind?).to eql false
end
end
end
describe Hand, 'three_of_a_kind' do
context 'contains a three of a kind' do
hand = Hand.new(Card.new('S', '9'), Card.new('D', '9'), Card.new('C', '9'), Card.new('D', 'J'), Card.new('S', '2'))
it 'returns true' do
expect(hand.three_of_a_kind?).to eql true
end
end
context 'does not contain a three of a kind' do
hand = Hand.new(Card.new('D', 'A'), Card.new('C', '6'), Card.new('S', '8'), Card.new('C', '10'), Card.new('S', 'J'))
it 'returns false' do
expect(hand.three_of_a_kind?).to eql false
end
end
end
describe Hand, 'full_house?' do
context 'is a full house' do
hand = Hand.new(Card.new('S', '9'), Card.new('D', '9'), Card.new('C', '9'), Card.new('D', '2'), Card.new('S', '2'))
it 'returns true' do
expect(hand.full_house?).to eql true
end
end
context 'is not a full house' do
hand = Hand.new(Card.new('D', 'A'), Card.new('C', '6'), Card.new('S', '8'), Card.new('C', '10'), Card.new('S', 'J'))
it 'returns false' do
expect(hand.full_house?).to eql false
end
end
end
describe Hand, 'two_pair?' do
context 'contains a two pair' do
hand = Hand.new(Card.new('S', '3'), Card.new('D', '3'), Card.new('C', '7'), Card.new('H', '7'), Card.new('S', 'K'))
it 'returns true' do
expect(hand.two_pair?).to eql true
end
end
context 'does not contain a two pair' do
hand = Hand.new(Card.new('D', 'A'), Card.new('C', '6'), Card.new('S', '8'), Card.new('C', '10'), Card.new('S', 'J'))
it 'returns false' do
expect(hand.two_pair?).to eql false
end
end
end
describe Hand, 'pair?' do
context 'contains a pair' do
hand = Hand.new(Card.new('S', '3'), Card.new('D', '3'), Card.new('C', '8'), Card.new('H', '7'), Card.new('S', 'K'))
it 'returns true' do
expect(hand.pair?).to eql true
end
end
context 'does not contain a pair' do
hand = Hand.new(Card.new('D', 'A'), Card.new('C', '6'), Card.new('S', '8'), Card.new('C', '10'), Card.new('S', 'J'))
it 'returns false' do
expect(hand.pair?).to eql false
end
end
end
describe Hand, 'aces_low' do
context 'hand contains an ace' do
ace = Card.new('S', 'A')
hand = Hand.new(ace, Card.new('D', '4'), Card.new('S', '9'), Card.new('C', '10'), Card.new('C', 'K'))
it 'returns a list with the ace first' do
expect(hand.aces_low.first).to eql ace
end
end
context 'hand contains two aces' do
first_ace = Card.new('S', 'A')
second_ace = Card.new('D', 'A')
hand = Hand.new(first_ace, second_ace, Card.new('S', '9'), Card.new('C', '10'), Card.new('C', 'K'))
it 'returns a list with an ace first' do
expect(hand.aces_low.first.value).to eql :ace
end
it 'returns a list with an ace second' do
expect(hand.aces_low[1].value).to eql :ace
end
end
end
describe Hand, 'ace?' do
context 'hand contains an ace' do
hand = Hand.new(Card.new('S', 'A'), Card.new('D', '4'), Card.new('S', '9'), Card.new('C', '10'), Card.new('C', 'K'))
it 'returns true' do
expect(hand.ace?).to eql true
end
end
context 'hand does not contain an ace' do
hand = Hand.new(Card.new('C', '5'), Card.new('D', '4'), Card.new('S', '9'), Card.new('C', '10'), Card.new('C', 'K'))
it 'returns false' do
expect(hand.ace?).to eql false
end
end
end
describe Hand, 'high_card' do
context 'highest card is a king' do
hand_high_card = Card.new('C', 'K')
hand = Hand.new(Card.new('S', '5'), Card.new('D', '4'), Card.new('S', '9'), Card.new('C', '10'), hand_high_card)
it 'returns the king' do
expect(hand.high_card).to eql hand_high_card
end
end
end
describe Hand, 'high_match_value' do
context 'highest pair in two pair is kings' do
hand = Hand.new(Card.new('S', 'K'), Card.new('D', '4'), Card.new('S', '4'), Card.new('C', '10'), Card.new('C', 'K'))
it 'returns king value' do
expect(hand.high_match_value).to eql :king
end
end
end
describe Hand, 'full_house_high_value' do
context 'three pair in a full house is kings' do
hand = Hand.new(Card.new('S', 'K'), Card.new('D', '4'), Card.new('S', '4'), Card.new('H', 'K'), Card.new('C', 'K'))
it 'returns king value' do
expect(hand.full_house_high_value).to eql :king
end
end
end
describe Hand, 'full_house_low_value' do
context 'full house with three kings and two jacks' do
hand = Hand.new(Card.new('S', 'K'), Card.new('D', 'K'), Card.new('C', 'J'), Card.new('H', 'J'), Card.new('H', 'K'))
it 'returns jack value' do
expect(hand.low_match_value).to eql :jack
end
end
end
describe Hand, 'low_match_value' do
context 'two pair with kings and jacks' do
hand = Hand.new(Card.new('S', 'K'), Card.new('D', 'K'), Card.new('C', 'J'), Card.new('H', 'J'), Card.new('D', '2'))
it 'returns jack value' do
expect(hand.low_match_value).to eql :jack
end
end
end
describe Hand, 'kicker' do
context 'four of a kind with king kicker' do
king_card = Card.new('S', 'K')
hand = Hand.new(Card.new('S', '9'), Card.new('D', '9'), Card.new('C', '9'), Card.new('H', '9'), king_card)
it 'returns the king kicker' do
expect(hand.kicker).to eql king_card
end
end
context 'three of a kind with king kicker' do
king_card = Card.new('S', 'K')
hand = Hand.new(Card.new('S', '9'), Card.new('D', '9'), Card.new('C', '9'), Card.new('H', '7'), king_card)
it 'returns the king kicker' do
expect(hand.kicker).to eql king_card
end
end
context 'two pair with king kicker' do
end
end
describe Hand, '<=>' do
context 'pair of fives vs pair of eights' do
hand_one = Hand.new(Card.new('H', '5'), Card.new('C', '5'), Card.new('S', '6'), Card.new('S', '7'), Card.new('D', 'K'))
hand_two = Hand.new(Card.new('C', '2'), Card.new('S', '3'), Card.new('S', '8'), Card.new('D', '8'), Card.new('D', '10'))
it 'pair of eights is higher' do
expect(hand_two).to be > hand_one
end
end
context 'ace high vs queen high' do
hand_one = Hand.new('5D 8C 9S JS AC')
hand_two = Hand.new('2C 5C 7D 8S QH')
it 'ace high is higher' do
expect(hand_one).to be > hand_two
end
end
context 'three aces vs diamond flush' do
hand_one = Hand.new('2D 9C AS AH AC')
hand_two = Hand.new('3D 6D 7D TD QD')
it 'flush is higher' do
expect(hand_two).to be > hand_one
end
end
context 'pair of queens nine high vs pair of queens seven high' do
hand_one = Hand.new('4D 6S 9H QH QC')
hand_two = Hand.new('3D 6D 7H QD QS')
it 'nine high is higher' do
expect(hand_one).to be > hand_two
end
end
context 'full house with three fours vs full house with three threes' do
hand_one = Hand.new('2H 2D 4C 4D 4S')
hand_two = Hand.new('3C 3D 3S 9S 9D')
it 'seven high is higher' do
expect(hand_one).to be > hand_two
end
end
end

103
ruby/spec/spec_helper.rb Normal file
View File

@ -0,0 +1,103 @@
# This file was generated by the `rspec --init` command. Conventionally, all
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
# The generated `.rspec` file contains `--require spec_helper` which will cause
# this file to always be loaded, without a need to explicitly require it in any
# files.
#
# Given that it is always loaded, you are encouraged to keep this file as
# light-weight as possible. Requiring heavyweight dependencies from this file
# will add to the boot time of your test suite on EVERY test run, even for an
# individual file that may not need all of that loaded. Instead, consider making
# a separate helper file that requires the additional dependencies and performs
# the additional setup, and require it from the spec files that actually need
# it.
#
# The `.rspec` file also contains a few flags that are not defaults but that
# users commonly want.
#
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
RSpec.configure do |config|
# rspec-expectations config goes here. You can use an alternate
# assertion/expectation library such as wrong or the stdlib/minitest
# assertions if you prefer.
config.expect_with :rspec do |expectations|
# This option will default to `true` in RSpec 4. It makes the `description`
# and `failure_message` of custom matchers include text for helper methods
# defined using `chain`, e.g.:
# be_bigger_than(2).and_smaller_than(4).description
# # => "be bigger than 2 and smaller than 4"
# ...rather than:
# # => "be bigger than 2"
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end
# rspec-mocks config goes here. You can use an alternate test double
# library (such as bogus or mocha) by changing the `mock_with` option here.
config.mock_with :rspec do |mocks|
# Prevents you from mocking or stubbing a method that does not exist on
# a real object. This is generally recommended, and will default to
# `true` in RSpec 4.
mocks.verify_partial_doubles = true
end
# This option will default to `:apply_to_host_groups` in RSpec 4 (and will
# have no way to turn it off -- the option exists only for backwards
# compatibility in RSpec 3). It causes shared context metadata to be
# inherited by the metadata hash of host groups and examples, rather than
# triggering implicit auto-inclusion in groups with matching metadata.
config.shared_context_metadata_behavior = :apply_to_host_groups
# The settings below are suggested to provide a good initial experience
# with RSpec, but feel free to customize to your heart's content.
=begin
# This allows you to limit a spec run to individual examples or groups
# you care about by tagging them with `:focus` metadata. When nothing
# is tagged with `:focus`, all examples get run. RSpec also provides
# aliases for `it`, `describe`, and `context` that include `:focus`
# metadata: `fit`, `fdescribe` and `fcontext`, respectively.
config.filter_run_when_matching :focus
# Allows RSpec to persist some state between runs in order to support
# the `--only-failures` and `--next-failure` CLI options. We recommend
# you configure your source control system to ignore this file.
config.example_status_persistence_file_path = "spec/examples.txt"
# Limits the available syntax to the non-monkey patched syntax that is
# recommended. For more details, see:
# - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
# - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
# - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
config.disable_monkey_patching!
# This setting enables warnings. It's recommended, but in some cases may
# be too noisy due to issues in dependencies.
config.warnings = true
# Many RSpec users commonly either run the entire suite or an individual
# file, and it's useful to allow more verbose output when running an
# individual spec file.
if config.files_to_run.one?
# Use the documentation formatter for detailed output,
# unless a formatter has already been configured
# (e.g. via a command-line flag).
config.default_formatter = 'doc'
end
# Print the 10 slowest examples and example groups at the
# end of the spec run, to help surface which specs are running
# particularly slow.
config.profile_examples = 10
# Run specs in random order to surface order dependencies. If you find an
# order dependency and want to debug it, you can fix the order by providing
# the seed, which is printed after each run.
# --seed 1234
config.order = :random
# Seed global randomization in this process using the `--seed` CLI option.
# Setting this allows you to use `--seed` to deterministically reproduce
# test failures related to randomization by passing the same `--seed` value
# as the one that triggered the failure.
Kernel.srand config.seed
=end
end