Last active
April 9, 2018 05:04
-
-
Save yancya/5ba05e428323ea0fe85119c3da51189c 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' | |
def main(str) | |
n, inputs = str.split(',') | |
inputs_sql = inputs.chars.map { |s| "('#{s}')" }.join(', ') | |
sql = <<~SQL | |
WITH RECURSIVE rules(symbol, "no", angle) AS ( | |
VALUES('a', 1, 0), ('a', 2, 60), ('a', 3, -60), ('a', 4, 0), | |
('b', 1, 0), ('b', 2, 60), ('b', 3, 0), ('b', 4, -60), ('b', 5, 0)) | |
, inputs AS ( | |
SELECT ROW_NUMBER() OVER() AS "no" | |
, symbol | |
FROM (VALUES#{inputs_sql}) AS symbols(symbol)) | |
, t AS ( | |
SELECT Array[0] AS no | |
, 0 AS angle | |
UNION ALL | |
SELECT t."no" || rules."no" | |
, t.angle + rules.angle | |
FROM t | |
JOIN inputs ON ARRAY_LENGTH(t."no", 1) = inputs."no" | |
JOIN rules ON rules.symbol = inputs.symbol) | |
SELECT angle % 360 AS angle | |
FROM t | |
WHERE ARRAY_LENGTH(t."no", 1) - 1 = #{inputs.size} | |
ORDER BY t."no" | |
OFFSET #{n} - 1 | |
LIMIT 1 | |
SQL | |
res = PG.connect.exec(sql).first | |
return 'x' if res.nil? | |
case res['angle'] | |
when '0', '180', '-180' | |
'0' | |
when '60', '-300', '-120', '240' | |
'+' | |
when '-60', '300', '120', '-240' | |
'-' | |
end | |
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", "+" ); |
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
-- case 13 254,babb の例 | |
WITH RECURSIVE rules(symbol, "no", angle) AS ( | |
VALUES('a', 1, 0), ('a', 2, 60), ('a', 3, -60), ('a', 4, 0), | |
('b', 1, 0), ('b', 2, 60), ('b', 3, 0), ('b', 4, -60), ('b', 5, 0)) | |
, inputs AS ( | |
SELECT ROW_NUMBER() OVER() AS "no" | |
, symbol | |
FROM (VALUES('b'), ('a'), ('b'), ('b')) AS symbols(symbol)) | |
, t AS ( | |
SELECT Array[0] AS no | |
, 0 AS angle | |
UNION ALL | |
SELECT t."no" || rules."no" | |
, t.angle + rules.angle | |
FROM t | |
JOIN inputs ON ARRAY_LENGTH(t."no", 1) = inputs."no" | |
JOIN rules ON rules.symbol = inputs.symbol) | |
SELECT angle % 360 AS angle | |
FROM t | |
WHERE ARRAY_LENGTH(t."no", 1) - 1 = 4 | |
ORDER BY t."no" | |
OFFSET 254 - 1 | |
LIMIT 1 | |
; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment