Skip to content

Instantly share code, notes, and snippets.

@techno-tanoC
Last active April 7, 2016 13:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save techno-tanoC/7194872 to your computer and use it in GitHub Desktop.
Save techno-tanoC/7194872 to your computer and use it in GitHub Desktop.
簡単な世代間血液型割合の変化のシミュレート。
class Blood
def initialize f, g
@blood = [f, g].sort
end
def type
case @blood
when %w(A A) then "A"
when %w(A O) then "A"
when %w(B B) then "B"
when %w(B O) then "B"
when %w(O O) then "O"
when %w(A B) then "AB"
end
end
def blood
@blood.join
end
def meiosis
@blood.sample
end
end
class Pair
attr_reader :pair
def initialize x, y
@pair = [x, y]
end
def children
Blood.new(*@pair.map(&:meiosis))
end
end
class Japan
# 300
A, B, O, AB = [38, 22, 31, 9].map(&3.method(:*))
AA = A / 3
AO = (A / 3) * 2
BB = B / 3
BO = (B / 3) * 2
def initialize
aa = [Blood.new("A", "A")] * AA
ao = [Blood.new("A", "O")] * AO
bb = [Blood.new("B", "B")] * BB
bo = [Blood.new("B", "O")] * BO
oo = [Blood.new("O", "O")] * O
ab = [Blood.new("A", "B")] * AB
@people = [*aa, *ao, *bb, *bo, *oo, *ab] * 10
end
def step
@people = @people.shuffle.each_cons(2).map {|x, y|
Pair.new(x, y).children
}.flatten
end
def show
size = @people.size
bloods = %w(AA AO BB BO OO AB)
bloods.each do |blood|
puts "blood #{blood}: #{@people.select{|person| person.blood == blood}.size.to_f / size}"
end
types = %w(A B O AB)
types.each do |type|
puts "type #{type}: #{@people.select{|person| person.type == type}.size.to_f / size}"
end
end
end
class Generations
def initialize
japan = Japan.new
n = 100
n.times do
japan.step
end
puts "#{300}人"
puts n.to_s + "ステップ"
japan.show
end
end
Generations.new
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment