「オフラインリアルタイムどう書く第14回(眠れるモンスターを狩る)」の問題を解いてみた


[横浜へなちょこプログラミング勉強会](https://yhpg.doorkeeper.jp/)にて過去に出題された[眠れるモンスターを狩る](http://nabetani.sakura.ne.jp/hena/ord14linedung/)を解いてみた。
回答にかかった時間は10分程度。

考えなければいけないことは何もないので、ひたすら手持ちの武器を持ち替えて、その武器が弱点のモンスターを刈り尽くしていく方法を取った。

```ruby
class Field
  WEAKNESS  = {a: :B, c: :D, e: :F, g: :H, i: :J, k: :L}
  DROPTABLE = {B: :c, D: :e, F: :g, H: :i, J: :k, L: :a}

  def initialize input
    @monsters = []
    @weapons = []

    input.chars.each{|chr|
      case chr
      when chr.upcase   then @monsters.push chr.to_sym
      when chr.downcase then @weapons.push chr.to_sym
      end
    }
  end

  def hunt
    start = @monsters.size
    while weapon = @weapons.shift do
      killed = @monsters.delete WEAKNESS[weapon]
      @weapons.push DROPTABLE[killed] unless killed.nil?
    end
    start - @monsters.size
  end
end

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

require 'minitest/autorun'

describe 'MonsterHunt' do
  test.split("\n").each do |line|
    t, n, input, expect = line.match(/^\/\*(\d+)\*\/\s*test\(\s*"([^"]+)",\s*"([^"]+)"\s*\);\s*$/).to_a
    field = Field.new input
    it input do
      assert_equal expect, field.hunt.to_s
    end
  end
end
```