Created
May 27, 2017 00:17
-
-
Save qsona/22f42b7f23b72552b9171ba3cc5b97f4 to your computer and use it in GitHub Desktop.
mizogu-
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 2〜99のカードの中から2枚取り、その2つの数の和だけをAさんに、積だけをBさんに教えた時の会話が | |
# A「Bさん、わかりませんね」 | |
# B「お、ならわかった」 | |
# A「なら僕もわかった」 | |
# となった場合、2つの数字はそれぞれ何でしょう。なお、AさんとBさんはそれぞれ完璧に合理的に発言しています。 | |
# (Aさんが一言目をしゃべっているとき、すでに「Bさんがまだわかっていないこと」を察してる前提) | |
class Candidate | |
def initialize(low, high) | |
@low = low | |
@high = high | |
@sum = low + high | |
@product = low * high | |
end | |
attr_reader :low, :high, :sum, :product | |
def to_s | |
"(#{low}, #{high} | #{sum} | #{product})" | |
end | |
end | |
candidates = [] | |
(2..98).each do |low| | |
(low+1..99).each do |high| | |
candidates << Candidate.new(low, high) | |
end | |
end | |
puts "stage0 #{candidates.size}" | |
### | |
# 1. filter Bわからん | |
### | |
candidates = candidates. | |
group_by(&:product). | |
values. | |
reject { |arr| arr.size == 1 }. | |
flatten | |
puts "stage1 #{candidates.size}" | |
### | |
# 2. A「Bさん、わかりませんねえ」 | |
# filter Aわからん | |
### | |
# for logging | |
reduced = candidates. | |
group_by(&:sum). | |
values. | |
select { |arr| arr.size == 1 }. | |
flatten | |
candidates = candidates. | |
group_by(&:sum). | |
values. | |
reject { |arr| arr.size == 1 }. | |
flatten | |
puts "stage2 #{candidates.size} reduced" | |
puts reduced | |
### | |
# 3. B「お、ならわかった」 | |
# filter Bわかった | |
### | |
candidates = candidates. | |
group_by(&:product). | |
values. | |
select { |arr| arr.size == 1 }. | |
flatten | |
puts "stage3 #{candidates.size} candidates:" | |
puts candidates | |
### | |
# 4. A「なら、僕もわかりました」 | |
# filter Aわかった | |
### | |
candidates = candidates. | |
group_by(&:sum). | |
values. | |
select { |arr| arr.size == 1 }. | |
flatten | |
puts "stage4 #{candidates.size} candidates:" | |
puts candidates |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment