Skip to content

Instantly share code, notes, and snippets.

@keiji-mu
Created November 22, 2013 14:03
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 keiji-mu/7600345 to your computer and use it in GitHub Desktop.
Save keiji-mu/7600345 to your computer and use it in GitHub Desktop.
CodeIQ テトロミノ+ビンゴ! https://codeiq.jp/ace/nabetani_takenori/q524 の解答です。 コメントが「テトラ」ミノになっているのは数学ガールの影響?
#! /usr/bin/ruby
# テトラミノビンゴ
# 指定位置から連続する穴の位置リスト(4個以下なら正確)
def run(pos)
x,y = pos;
return [] if !$hole[pos] || $visit[pos]
$visit[pos]=1;
l = [pos]
l += run([x-1,y]) if x > 0 && l.size <= 4
l += run([x+1,y]) if x < 4 && l.size <= 4
l += run([x,y-1]) if y > 0 && l.size <= 4
l += run([x,y+1]) if y < 4 && l.size <= 4
return l
end
# 読み上げる数入力
num = gets.split(',').map(&:to_i)
# カードを入力しつつ集計
a = Hash.new(0);
while c = gets
# 各カードの数の位置表
pos=[]
c.split('/').each.with_index{|l,y|
l.split(',').each.with_index{|v,x| pos[v.to_i]=[x,y]}
}
# 読み上げる度にその数の位置から連続する穴を調べ、個数が4ならビンゴ
$hole={}; l=[]
next if num.each{|i| next if !pos[i]; $hole[pos[i]]=1;$visit={};l=run(pos[i]); break if l.size == 4}
x,y = l.transpose.map{|v|[v.min,v.max]} # x,y の 最小・最大
a[((dx=x[1]-x[0]) == 0 || dx == 3) ? 'I' :
((dy=y[1]-y[0]) == 1 && dx == 1) ? 'O' :
(dx == 2) ? ($hole[[x[0]+1,y[0]]] != $hole[[x[0]+1,y[0]+1]] ? 'L' :
$hole[[x[0],y[0]]] != $hole[[x[0]+2,y[0]]] ? 'S' : 'T')
: ($hole[[x[0],y[0]+1]] != $hole[[x[0]+1,y[0]+1]] ? 'L' :
$hole[[x[0],y[0]]] != $hole[[x[0],y[0]+2]] ? 'S' : 'T')] += 1;
end
# 出力
puts 'ILOST'.chars.map{|c|[c,a[c]]*?:}*?,
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment