Skip to content

Instantly share code, notes, and snippets.

@willnet
Created October 16, 2012 13:15
Show Gist options
  • Save willnet/3899215 to your computer and use it in GitHub Desktop.
Save willnet/3899215 to your computer and use it in GitHub Desktop.
オフラインリアルタイムどう書く四回をrubyで解いてみた
require 'test/unit'
class TM
def self.calc(ary)
ary.sort!.map!(&:to_i)
touch_info = ary.permutation(2).map { |a,b| check_touch(a, b) }
touch_numbers = touch_info.each_slice(3).map {|i| i.compact.length }
return 'T' if touch_numbers.max == 3
return 'O' if touch_numbers.all? { |i| i == 2 }
if touch_numbers.max == 2 && touch_numbers.count(2) == 2
ils = case touch_info.each_slice(3).count {|i| touch_xy? i }
when 0; 'I'
when 1; 'L'
when 2; 'S'
end
return ils
end
'-'
end
def self.touch_xy?(ary)
ary.compact.count('X') > 0 && ary.compact.count('Y') > 0
end
def self.check_touch(a, b)
return 'X' if a - 1 == b || a + 1 == b
return 'Y' if a - 10 == b || a + 10 == b
nil
end
end
class TestTM < Test::Unit::TestCase
def test_calc_ramdom
assert_equal(TM.calc(%w(03 11 13 01)), '-')
end
def test_calc_dup
assert_equal(TM.calc(%w(55 55 55 55)), '-')
end
def test_calc_L
assert_equal(TM.calc(%w(07 17 06 05)), 'L')
end
def test_calc_T
assert_equal(TM.calc(%w(89 99 79 88)), 'T')
end
def test_calc_O
assert_equal(TM.calc(%w(68 57 58 67)), 'O')
end
def test_calc_S
assert_equal(TM.calc(%w(43 54 53 42)), 'S')
end
def test_calc_I
assert_equal(TM.calc(%w(24 22 25 23)), 'I')
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment