Skip to content

Instantly share code, notes, and snippets.

@mugi-uno
Created April 16, 2016 08:01
Show Gist options
  • Save mugi-uno/fc3048a914df32cb6fcff1da14fef76e to your computer and use it in GitHub Desktop.
Save mugi-uno/fc3048a914df32cb6fcff1da14fef76e to your computer and use it in GitHub Desktop.
module Q2
PATTERN = [
[1], [2], [3], [4], [5], [6], [7], [8], [9],
[1, 2],
[2, 3],
[3, 6],
[6, 9],
[9, 8],
[8, 7],
[7, 4],
[4, 1]
]
def self.hit(board, target)
# いずれかが存在しなければ不正な投球
target.each { |n| return 0 unless board.include?(n) }
hit_board = board - target
# 空になったら終わり
return 1 if hit_board.empty?
# 残りのパターンを網羅
return PATTERN.map { |target| self.hit(hit_board, target) }.inject(:+)
end
def self.strike
PATTERN.map { |target| self.hit((1..9).to_a, target) }.inject(:+)
end
end
puts "answer : #{Q2.strike}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment