眠れるモンスターを狩る

```
# 眠れるモンスターを狩る
# http://nabetani.sakura.ne.jp/hena/ord14linedung/
# http://qiita.com/Nabetani/items/0597bd3af481e5834ae1
#

class Monster < Struct.new(:name, :wreq, :wgain)
end

MONSTERS = [
	Monster.new("B", "a", "c"),
	Monster.new("D", "c", "e"),
	Monster.new("F", "e", "g"),
	Monster.new("H", "g", "i"),
	Monster.new("J", "i", "k"),
	Monster.new("L", "k", "a"),
]

def monster_conv(input)
	
	total = 0
	
	begin
		input0 = input
		MONSTERS.each { |m|
			next unless input.match(/#{m.wreq}/)
			total += input.scan(/#{m.name}/).size
			input = input.gsub(/#{m.name}/, m.wgain)
		}
	end while input != input0
	
	return total
end

def test(input, expected)
	result = monster_conv(input).to_s
	print (result == expected) ? "." : "E"
end

test( "gLDLBgBgHDaD", "6" )                           # 0 
test( "DBcDLaLgDBH", "6" )                            # 1 
test( "JJca", "0" )                                   # 2 
test( "FJDLBH", "0" )                                 # 3 
test( "HJBLFDg", "6" )                                # 4 
test( "HBaDLFJ", "6" )                                # 5 
test( "DJaHLB", "2" )                                 # 6 
test( "gDLHJF", "3" )                                 # 7 
test( "cJFgLHD", "5" )                                # 8 
test( "FFBJaJJ", "1" )                                # 9 
test( "FJeJFBJ", "2" )                                # 10
test( "iJFFJJB", "3" )                                # 11
test( "JBJiLFJF", "5" )                               # 12
test( "JDiFLFBJJ", "8" )                              # 13
test( "BDFDFFDFFLLFFJFDBFDFFFFDDFaDBFFB", "28" )      # 14
test( "DDFBFcBDFFFFFFLBFDFFBFLFDFDJDFDF", "24" )      # 15
test( "FDLBFDDBFFFeFFFFFDFBLDDFDDFBFFJF", "16" )      # 16
test( "FDBFFLFDFFDBBDFFBJDLFgDFFFDFFDFF", "0" )       # 17
test( "FDiFLDFFFFBDDJDDBFBFDFFFBFFDFLFF", "31" )      # 18
test( "FDFDJBLBLBFFDDFFFDFFFFFDDFBkFDFF", "30" )      # 19
test( "HBkFFFFHBLH", "3" )                            # 20
test( "FBHHFFFHLaB", "2" )                            # 21
test( "LFHFBBcHFHF", "0" )                            # 22
test( "LFBHFFeFHBH", "7" )                            # 23
test( "LgFHHHBFBFF", "3" )                            # 24
test( "FFiFHBHLBFH", "0" )                            # 25
test( "BFHHFFHBeFLk", "10" )                          # 26
test( "FHFaBBHFHLFg", "5" )                           # 27
test( "FFgacaFg", "0" )                               # 28
test( "JHDaDcBJiiHccBHDBDH", "9" )                    # 29
test( "FHJJLckFckFJHDFF", "12" )                      # 30
test( "DeDHJHDFHJBLHDLLDHJLBDD", "22" )               # 31
test( "gJLLLJgJgJLJL", "0" )                          # 32
test( "DaaaDDD", "0" )                                # 33
test( "HFeJFHiBiiBJeJBBFFB", "9" )                    # 34
test( "FJFFJDBHBHaLJBHJHDLHkLLLFFFgJgHJLHkJkB", "32" )# 35
test( "giFLBiBJLLJgHBFJigJJJBLHFLDLL", "23" )         # 36
test( "cgkLJcLJJJJgJc", "2" )                         # 37
test( "LDFHJHcFBDBLJBLFLcFJcDFBL", "22" )             # 38
test( "JJHHHkHJkHLJk", "1" )                          # 39
test( "kHHBBaBgHagHgaHBBB", "11" )                    # 40
test( "HDBFFDHHHDFLDcHHLFDcJD", "20" )                # 41
test( "HFFFHeFFee", "7" )                             # 42
test( "gLLDHgDLgFL", "1" )                            # 43
test( "JJJBBaBBHBBHaLBHJ", "7" )                      # 44
test( "FBFBgJBDBDgF", "0" )                           # 45
test( "LLLLakakLakLL", "7" )                          # 46
test( "HeJHeJe", "0" )                                # 47
test( "LDFLBLLeBLDBBFFBLFBB", "4" )                   # 48
```