第13回オフラインリアルタイムどう書くの参考問題回答例（Ruby）

問題：http://nabetani.sakura.ne.jp/hena/ord13updowndouble/
回答例：http://qiita.com/Nabetani/items/89fb0e2e712d4b396535

```ruby:hoge.rb
#coding:utf-8

def button n
  return 2 if n==3
  return n+1 if ((n+1)%4).zero?  
  return n-1 if ((n-1)%4).zero?
  return n/2
end

def calc n
  cnt = 0
  while !(n.zero?) do
    n = button n
    cnt += 1
  end
  cnt
end

DATA.each do |d|
  n,src,dst = d.split.map{|m|m.to_i}
  r = calc src
  puts "#{n} : (#{src}) #{r} != #{dst}" unless r==dst
end

__END__
0	59	9	 +1 ×2 ×2 ×2 ×2 -1 ×2 ×2 -1
1	10	5	 +1 +1 ×2 +1 ×2
2	11	6	 +1 ×2 ×2 +1 ×2 +1
3	12	5	 +1 +1 +1 ×2 ×2
4	13	6	 +1 ×2 +1 ×2 ×2 +1
5	14	6	 +1 +1 ×2 ×2 -1 ×2
6	15	6	 +1 ×2 ×2 ×2 ×2 -1
7	16	5	 +1 +1 ×2 ×2 ×2
8	17	6	 +1 +1 ×2 ×2 ×2 +1
9	18	6	 +1 +1 ×2 ×2 +1 ×2
10	27	8	 +1 +1 ×2 ×2 -1 ×2 ×2 -1
11	28	7	 +1 ×2 +1 ×2 +1 ×2 ×2
12	29	8	 +1 ×2 ×2 ×2 ×2 -1 ×2 -1
13	30	7	 +1 +1 ×2 ×2 ×2 -1 ×2
14	31	7	 +1 ×2 ×2 ×2 ×2 ×2 -1
15	32	6	 +1 +1 ×2 ×2 ×2 ×2
16	33	7	 +1 ×2 ×2 ×2 ×2 ×2 +1
17	34	7	 +1 ×2 ×2 ×2 ×2 +1 ×2
18	35	8	 +1 +1 ×2 ×2 ×2 +1 ×2 +1
19	41	8	 +1 +1 ×2 +1 ×2 ×2 ×2 +1
20	71	9	 +1 +1 ×2 ×2 +1 ×2 ×2 ×2 -1
21	1023	12	 +1 +1 ×2 ×2 ×2 ×2 ×2 ×2 ×2 ×2 ×2 -1
22	1024	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