LoginSignup
1
1

More than 5 years have passed since last update.

円周上のCrossing

Last updated at Posted at 2013-09-21
# http://nabetani.sakura.ne.jp/hena/ord14crosscircle/
# http://qiita.com/Nabetani/items/66806c9dc14a96f2fd42
#

def count_ring_crossing(input)

  input
    .split("")
    .zip((1..input.length).to_a)
    .group_by { |p| p[0] }
    .values
    .map { |v|
      v.transpose[1]
    }
    .inject([]) { |lines, pg|
      lines + pg.combination(2).to_a
    }
    .combination(2)
    .count { |pair|
      pair[0]
        .product(pair[1])
        .inject(1) { |sgn , d|
          sgn * (d[0] <=> d[1])
        } < 0
    }
    .to_s

end

def test(input, expected)

  result = count_ring_crossing(input)
  print (result == expected) ? "." : "E"

end

test( "aabbca1bcb", "14" )                                                                   #  0
test( "111ZZZ", "0" )                                                                        #  1
test( "v", "0" )                                                                             #  2
test( "ww", "0" )                                                                            #  3
test( "xxx", "0" )                                                                           #  4
test( "yyyy", "1" )                                                                          #  5
test( "zzzzz", "5" )                                                                         #  6
test( "abcdef", "0" )                                                                        #  7
test( "abcaef", "0" )                                                                        #  8
test( "abbaee", "0" )                                                                        #  9
test( "abcacb", "2" )                                                                        # 10
test( "abcabc", "3" )                                                                        # 11
test( "abcdabcd", "6" )                                                                      # 12
test( "abcadeabcade", "23" )                                                                 # 13
test( "abcdeedcba", "0" )                                                                    # 14
test( "abcdeaedcba", "8" )                                                                   # 15
test( "abcdeaedcbad", "16" )                                                                 # 16
test( "QQQQXXXX", "2" )                                                                      # 17
test( "QwQQmQXmXXwX", "14" )                                                                 # 18
test( "111222333", "0" )                                                                     # 19
test( "aaAAaA", "4" )                                                                        # 20
test( "121232313", "12" )                                                                    # 21
test( "1ab1b", "1" )                                                                         # 22
test( "abcdefbadcfe", "12" )                                                                 # 23
test( "abxcdefbadcfex", "14" )                                                               # 24
test( "dtnwtkt", "0" )                                                                       # 25
test( "mvubvpp", "0" )                                                                       # 26
test( "moggscd", "0" )                                                                       # 27
test( "kzkjzpkw", "2" )                                                                      # 28
test( "fbifybre", "1" )                                                                      # 29
test( "rrrfjryki", "1" )                                                                     # 30
test( "wrbbdwsdwtx", "2" )                                                                   # 31
test( "vvucugvxbvgx", "9" )                                                                  # 32
test( "ojkjzyasjwbfjj", "5" )                                                                # 33
test( "ggffyuxnkyypifff", "5" )                                                              # 34
test( "vcgtcqlwrepwvkkogl", "4" )                                                            # 35
test( "xeqtmmgppwcjpcisogxbs", "4" )                                                         # 36
test( "lukltpeucrqfvcupnpxwmoj", "6" )                                                       # 37
test( "zpzswlkkoqwwndwpfdpkhtzgtn", "31" )                                                   # 38
test( "bkfeflagfvluelududqjcvfyvytfw", "45" )                                                # 39
test( "rvqbhfmcjjqlpqzulzerxgyowiwrfkmhw", "26" )                                            # 40
test( "qyxvpdtoeexbqsethwjwmqszcxxjnsdoeaet", "144" )                                        # 41
test( "rjmsgmswhcolmpbhmpncziymydyalrcnevsrespj", "133" )                                    # 42
test( "oxetnyjzjbysnwktfwzndlejfndsqeetsnjvsicyjehd", "395" )                                # 43
test( "wzvddnddzogywcqxbyvagbzmsmtcmrrlbnebmvhaemjouaqim", "219" )                           # 44
test( "karhphxcxqgsyorhusbumbqzocuzvnwzwcpxgsksrviihxrgsrhji", "461" )                       # 45
test( "oxgbononhqdxzmkysgijwvxljpaazmgkurkpffeuwywwuyxhyfkicgyzyc", "441" )                  # 46
test( "sdgsrddwsrwqthhdvhrjhgtxwgurgyiygtktgtughtogzaqmcafkljgpniddsvb", "1077" )            # 47
test( "qemhecchkgzhxmdcsltwhpoyhkapckkkzosmklcvzkiiucrvzzznmhjfcdumuflavxik", "1711" )       # 48
test( "ffqmsirwpxrzfkbvmmfeptkbhnrvfcywthkwkbycmayhhkgvuyecbwwofwthlmzruphrcujwhr", "2440" ) # 49

1
1
4

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
1
1