Skip to content

Instantly share code, notes, and snippets.

@SamSaffron
Forked from shokai/README.md
Created January 3, 2012 23:33
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 SamSaffron/1557569 to your computer and use it in GitHub Desktop.
Save SamSaffron/1557569 to your computer and use it in GitHub Desktop.
清一色のアガリのみ出力する
#!/usr/bin/env ruby
def agari?(pais, pairs=4)
h = Hash.new(0)
pais.each do |x|
h[x] += 1
end
v = h.values
if v.min == 2 and v.count(2) == 1 and v.max == 3 and v.count(3) == pairs
return true
else
(1..7).to_a.map{|i|
h[i]!=0 and h[i+1]!=0 and h[i+2]!=0 ? [i, i+1, i+2] : nil
}.delete_if{|k|
k.class != Array
}.each{|k|
pais_ = h.to_a.map{|j|[j[0]]*(k.include?(j[0]) ? j[1]-1 : j[1])}.flatten
return true if agari?(pais_, pairs-1)
}
return false
end
end
ARGF.each do |line|
line.strip!
counts = line.split(/\s*,\s*/).map{|i| i.to_i}
next if counts.inject{|a,b| a+b} != 14 # 合計14枚
pais = Array.new
(1..9).to_a.each{|i|
pais << [i]*counts[i-1]
}
pais.flatten!
puts pais.join(',') if agari?(pais)
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