Skip to content

Instantly share code, notes, and snippets.

@baweaver
Created January 14, 2021 04:13
Show Gist options
  • Save baweaver/2a470cdebb53958ccea84ba9aad18218 to your computer and use it in GitHub Desktop.
Save baweaver/2a470cdebb53958ccea84ba9aad18218 to your computer and use it in GitHub Desktop.
Forgive me, for I have done bad things to Ruby again. Credits to @philferne on Twitter for pointing out this might work
RANKS = [*2..10, *%w(J Q K A)].map(&:to_s).freeze
RANKS_SCORES = RANKS.each_with_index.to_h
Card = Struct.new(:suit, :rank)
straight = [Card['H', RANKS.first], *RANKS[1..4].map { Card['S', _1] }]
not_straight = [Card['H', RANKS.last], *RANKS[1..4].map { Card['S', _1] }]
def add_rank(r, n) = RANKS[RANKS_SCORES[r] + n]
def straight?(hand)
hand in [
Card[*, r],
Card[*, "#{add_rank(r, 1)}"],
Card[*, "#{add_rank(r, 2)}"],
Card[*, "#{add_rank(r, 3)}"],
Card[*, "#{add_rank(r, 4)}"],
]
end
straight?(straight)
# => true
straight?(not_straight)
# => false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment