2
2

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.

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

Posted at

問題はこちら
http://nabetani.sakura.ne.jp/hena/ord26tribo/

2つの方法で解きました。

解答1

NB=[
    [/bc/,/ef/,/gh/,/jk/,/lm/,/no/,/qr/,/st/,/uv/,/wx/],
    [/cd/,/fg/,/hi/,/kl/,/mn/,/op/,/rs/,/tu/,/vw/,/xy/],
    [/a.*c/,/b.*f/,/d.*h/,/e.*k/,/g.*m/,/i.*o/,/j.*r/,/l.*t/,/n.*v/,/p.*x/],
]

def solve(q)
  NB.zip([q.size]*3).map{|nb, n|
    nb.each{|r|
      n -= 2 if q =~ r
    }
    n
  }*(',')
end

DATA.readlines.each do |line|
  no,q,a = line.strip.split(/\s+/)
  ans = solve(q)
  print no + "\t" + ans
  puts ans == a ? ' o' : ' x'
end
__END__
0	bdelmnouy	5,7,9	
1	a	1,1,1	
2	q	1,1,1	
3	t	1,1,1	
4	i	1,1,1	
5	fg	2,0,2	
6	gh	0,2,2	
7	gm	2,2,0	
8	fgh	1,1,3	
9	fghm	2,2,2	
10	fhm	3,3,3	

解法2

TR=[
    ['-','-','-','-','-','-','-','-','-','-','-'],
    ['-','-','-','-','-','a','-','-','-','-','-'],
    ['-','-','-','-','b','c','d','-','-','-','-'],
    ['-','-','-','e','f','g','h','i','-','-','-'],
    ['-','-','j','k','l','m','n','o','p','-','-'],
    ['-','q','r','s','t','u','v','w','x','y','-'],
    ['-','-','-','-','-','-','-','-','-','-','-'],
]

def get_y_x_d(c)
  pos = TR.flatten.index(c)
  y, x = TR.flatten.index(c).divmod(TR[0].size)
  d = (x + y).even?
  [y,x,d]
end

def solve(q)
  a = [0, 0, 0]
  q.chars.each{|c|
    y, x, d = get_y_x_d(c)
    if d
      a[0] += 1 unless q.include?(TR[y][x+1])
      a[1] += 1 unless q.include?(TR[y][x-1])
      a[2] += 1 unless q.include?(TR[y+1][x])
    else
      a[0] += 1 unless q.include?(TR[y][x-1])
      a[1] += 1 unless q.include?(TR[y][x+1])
      a[2] += 1 unless q.include?(TR[y+-1][x])
    end
  }
  a*(',')
end

DATA.readlines.each do |line|
  no,q,a = line.strip.split(/\s+/)
  ans = solve(q)
  print no + "\t" + ans
  puts ans == a ? ' o' : ' x'
end
__END__
0	bdelmnouy	5,7,9	
2
2
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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?