0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

第14回オフラインリアルタイムどう書くの問題をRubyで解く

Posted at

リアルタイム現場で頂いた意見で少し修正しています。

def battle(ms, ws)
  mh = {:B=>:a,:D=>:c,:F=>:e,:H=>:g,:J=>:i,:L=>:k}
  wh = {:B=>:c,:D=>:e,:F=>:g,:H=>:i,:J=>:k,:L=>:a}
  ms_k, ms_a = ms.partition{|m| ws.include?(mh[m])}
  return 0 if ms_k.size == 0
  ws_a = ms_k.map{|m| wh[m]}
  battle(ms_a, ws_a) + ms_k.size
end

def solve(q)
  ms, ws = q.chars.map(&:to_sym).partition{|e| (:A..:Z).include?(e)}
  battle(ms, ws)
end

DATA.readlines.each do |line|
  no,q,a = line.chop.split(/\s+/)
  ans = solve(q)
  print no + "\t" + ans.to_s
  puts ans == a.to_i ? ' o' : ' x'
end
__END__
0	gLDLBgBgHDaD	6
1	DBcDLaLgDBH	6
2	JJca	0
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?