Skip to content

Instantly share code, notes, and snippets.

@shokai
Created January 3, 2012 22:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save shokai/1557352 to your computer and use it in GitHub Desktop.
Save shokai/1557352 to your computer and use it in GitHub Desktop.
清一色のアガリ判定

判定

% ruby gen.rb | ruby agari.rb

アガリを数える

% ruby gen.rb | ruby agari.rb | grep 'o' -c
=> 13277
#!/usr/bin/env ruby
def pair7?(pais)
v = pais.values
v.delete(0)
v.count(2) == 7 and v.min == 2 and v.max == 2
end
def chinitsu?(pais, pairs=4)
v = pais.values
v.delete(0)
return true if v.min == 2 and v.count(2) == 1 and (pairs == 0 or (v.max == 3 and v.count(3) == pairs))
(1..7).each do |k|
next if pais[k]==0 or pais[k+1]==0 or pais[k+2]==0
pais_ = pais.clone
pais_[k] -= 1
pais_[k+1] -= 1
pais_[k+2] -= 1
return true if chinitsu?(pais_, pairs-1)
end
return false
end
def agari?(pais)
pair7?(pais) or chinitsu?(pais)
end
ARGF.each do |line|
counts = line.strip!.split(/\s*,\s*/).map{|i| i.to_i}
next if counts.size != 9 or counts.inject{|a,b| a+b} != 14 # 9種、合計14枚
pais = Hash.new(0)
(1..9).each{|i|
pais[i] = counts[i-1]
}
puts "#{line} " + (agari?(pais) ? 'o' : 'x')
end
#!/usr/bin/env ruby
for i in 0..(("4"*9).to_i(5))
res = i.to_s(5)
if res.split('').map{|j| j.to_i}.inject{|a,b| a+b} == 14
puts ('0'*(9-res.size)+res).split('').join(',')
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment