Last active
April 7, 2018 15:00
-
-
Save yancya/5e804e90fbd87b30058a853285b477b4 to your computer and use it in GitHub Desktop.
オフラインリアルタイムどう書くE23 への yancya の回答 http://nabetani.sakura.ne.jp/hena/orde23nokoch/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'pg' | |
RULES = { | |
'a' => [0, 1, -1, 0], | |
'b' => [0, 1, 0, -1, 0] | |
} | |
ANS = '0+-' | |
def main(str) | |
n, inputs = str.split(',').yield_self { |n, inputs| | |
[n.to_i - 1, inputs.chars] | |
} | |
total_size = inputs.map { |s| s == 'a' ? 4 : 5 }.reduce(:*) | |
return 'x' if n.to_i > total_size | |
inputs.reduce([0, total_size, n]) { |(ang, size, n), symbol| | |
rule = RULES[symbol] | |
next [ang + rule[n], nil, nil] if size == rule.size | |
next_size = size / rule.size | |
index, next_n = n.divmod(next_size) | |
[ang + rule[index], next_size, next_n] | |
}.yield_self { |(ang, _, _)| ANS[ang % 3] } | |
end | |
require 'test-unit' | |
class HogeTest < Test::Unit::TestCase | |
TEST_CASES = DATA.each_line.map { |line| | |
%r[^/\*(?<id>\d+)\*/.*test\((?<act_exp>.*)\)] =~ line | |
act, exp = act_exp.scan(%r["(.+?)"]).flatten | |
[id, [act, exp]] | |
}.to_h | |
data(TEST_CASES) | |
test "hoge" do |(input, expected)| | |
assert { main(input) == expected } | |
end | |
end | |
__END__ | |
/*0*/ test( "120,aabb", "0" ); | |
/*1*/ test( "100,a", "x" ); | |
/*2*/ test( "3,a", "-" ); | |
/*3*/ test( "3,b", "0" ); | |
/*4*/ test( "9,aa", "-" ); | |
/*5*/ test( "10,bb", "+" ); | |
/*6*/ test( "11,ab", "-" ); | |
/*7*/ test( "12,ba", "0" ); | |
/*8*/ test( "7,aaa", "0" ); | |
/*9*/ test( "17,baa", "+" ); | |
/*10*/ test( "28,bba", "-" ); | |
/*11*/ test( "82,bba", "+" ); | |
/*12*/ test( "35,baa", "-" ); | |
/*13*/ test( "254,babb", "+" ); | |
/*14*/ test( "462,abba", "x" ); | |
/*15*/ test( "226,bbba", "0" ); | |
/*16*/ test( "345,bbba", "0" ); | |
/*17*/ test( "256,aaaa", "0" ); | |
/*18*/ test( "11,aaab", "-" ); | |
/*19*/ test( "241,abaab", "-" ); | |
/*20*/ test( "490,aabaa", "0" ); | |
/*21*/ test( "1305,bbbaa", "0" ); | |
/*22*/ test( "1102,ababa", "-" ); | |
/*23*/ test( "1077,abbab", "-" ); | |
/*24*/ test( "281,aabaa", "-" ); | |
/*25*/ test( "2218,abbaaa", "+" ); | |
/*26*/ test( "4095,bbabbb", "+" ); | |
/*27*/ test( "2750,abbaab", "+" ); | |
/*28*/ test( "5573,bbaaba", "+" ); | |
/*29*/ test( "6644,aaabba", "x" ); | |
/*30*/ test( "8109,bbbbba", "+" ); | |
/*31*/ test( "3860,aaaabbb", "+" ); | |
/*32*/ test( "59222,bbbbbba", "0" ); | |
/*33*/ test( "14956,baabbab", "-" ); | |
/*34*/ test( "14894,ababbba", "+" ); | |
/*35*/ test( "3163,aaaaaab", "-" ); | |
/*36*/ test( "21917,babaaaa", "+" ); | |
/*37*/ test( "178620,aabbbaab", "x" ); | |
/*38*/ test( "96709,babbaaaa", "0" ); | |
/*39*/ test( "74116,abababaa", "-" ); | |
/*40*/ test( "22025,abbbbabb", "0" ); | |
/*41*/ test( "8612,aaaabbaa", "-" ); | |
/*42*/ test( "153868,bbbabbab", "-" ); | |
/*43*/ test( "747769,abbabaaba", "x" ); | |
/*44*/ test( "541589,baabbbbab", "-" ); | |
/*45*/ test( "787443,ababbbbab", "-" ); | |
/*46*/ test( "129549,ababaaaaa", "0" ); | |
/*47*/ test( "837323,aabbbabab", "x" ); | |
/*48*/ test( "140592,bbbbabbab", "+" ); | |
/*49*/ test( "219669,ababbabbab", "-" ); | |
/*50*/ test( "500261,bbababaabb", "-" ); | |
/*51*/ test( "966503,aaabababbb", "0" ); | |
/*52*/ test( "443603,baabaababb", "+" ); | |
/*53*/ test( "3912,aabbababaa", "0" ); | |
/*54*/ test( "2926358,bbabbbbaba", "0" ); | |
/*55*/ test( "18104279,bbbaababbab", "-" ); | |
/*56*/ test( "3849980,aaabaaaaaba", "0" ); | |
/*57*/ test( "9276072,baabaabaaab", "0" ); | |
/*58*/ test( "11202113,baaaaabbbba", "0" ); | |
/*59*/ test( "5432578,abaabbaaaaa", "-" ); | |
/*60*/ test( "17363025,bbaabababbb", "0" ); | |
/*61*/ test( "24147656,baabaabbbbab", "0" ); | |
/*62*/ test( "1078733,bbbaaaabbbbb", "+" ); | |
/*63*/ test( "38623426,abaabababbaa", "-" ); | |
/*64*/ test( "19312285,bbaababbaaba", "+" ); | |
/*65*/ test( "11485959,baaaaababaaa", "-" ); | |
/*66*/ test( "36831104,babbbbbbabab", "+" ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment