Skip to content

Instantly share code, notes, and snippets.

@camertron
Created December 21, 2016 07:31
Show Gist options
  • Save camertron/f8b4cb63037c911086e9bda2482df77b to your computer and use it in GitHub Desktop.
Save camertron/f8b4cb63037c911086e9bda2482df77b to your computer and use it in GitHub Desktop.
Given the number of points earned in a football game, determine all the possible scoring sequences
# name: human-readable name
# point_value: number of points earned
# priority: how likely the score is to actually happen
ScoreType = Struct.new(:name, :point_value, :priority)
SCORE_TYPES = [
ScoreType.new('safety', 2, 5),
ScoreType.new('field goal', 3, 2),
ScoreType.new('touchdown', 6, 3),
ScoreType.new('touchdown with extra point', 7, 1),
ScoreType.new('touchdown with 2-point conversion', 8, 4)
]
def possible_scoring_sequences_for(points, current_sequence)
return [current_sequence] if points == 0
SCORE_TYPES.each_with_object([]) do |score_type, ret|
if points >= score_type.point_value
ret.concat(
possible_scoring_sequences_for(
points - score_type.point_value, current_sequence + [score_type]
)
)
end
end
end
sequences = possible_scoring_sequences_for(ARGV[0].to_i, [])
.map { |seq| seq.sort { |a, b| a.name <=> b.name } }.uniq
.sort do |a, b|
a_priority = a.inject(0) { |ret, score_type| ret + score_type.priority }
b_priority = b.inject(0) { |ret, score_type| ret + score_type.priority }
a_priority <=> b_priority
end
sequences.each do |sequence|
puts sequence.map(&:name).join(', ')
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment