Created
May 6, 2016 09:47
-
-
Save kunitoo/93f72f9dba1aeecb0265522b2b187560 to your computer and use it in GitHub Desktop.
ESM オフラインリアルタイムどう書く 拡大版 Ruby 実装例
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
def solve(input) | |
players = input.scan(/\w+/) | |
tournament = make_tournament(players) | |
"(#{winner(tournament)})" | |
end | |
def make_tournament(players) | |
sub = (2 ** (Math.log2(players.size).ceil)) - players.size | |
players.map.with_index(0) {|p, i| sub > i ? [p, p] : p }.flatten | |
end | |
def winner(tournament) | |
if tournament.size == 1 | |
tournament.first | |
else | |
next_tournament = [] | |
tournament.each_slice(2) do |p1, p2| | |
next_tournament << vs(p1, p2) | |
end | |
winner(next_tournament) | |
end | |
end | |
def vs(p1, p2) | |
hand1 = p1 * p2.size | |
hand2 = p2 * p1.size | |
if hand1 == hand2 | |
p1 | |
else | |
win = nil | |
(p1.size * p2.size).times.each do |i| | |
if hand1[i] != hand2[i] | |
case hand1[i] | |
when 'R' | |
win = hand2[i] == 'S' ? p1 : p2 | |
when 'S' | |
win = hand2[i] == 'P' ? p1 : p2 | |
when 'P' | |
win = hand2[i] == 'R' ? p1 : p2 | |
end | |
return win | |
end | |
end | |
end | |
end | |
def test(input, expect) | |
result = solve(input) | |
if expect == result | |
print '.' | |
else | |
puts | |
puts "test(#{input}, #{expect}) #=> got #{result}" | |
end | |
end | |
test("(RSP)(R)(RPS)(SP)", "(RPS)") | |
test("(RPS)(R)(RSP)(SP)(RSSP)", "(RSSP)") | |
test("(RRS)(S)(PSSRP)(PRP)(PSS)", "(PRP)") | |
test("(PRS)(PSPP)(PRSP)(S)(RR)(SSPR)", "(PRS)") | |
test("(PSRP)(PR)(RPRPR)(PSSPP)(SP)(SRPP)(PR)", "(SP)") | |
test("(SPS)(R)(RP)(RRS)(PPRRS)(R)(RS)(RRRRP)", "(PPRRS)") | |
test("(PPSRPSPRR)(SP)(PPPRSSR)(PS)(P)(PRSPS)(PP)(RSSR)", "(SP)") | |
test("(SRPRS)(SRPSRS)(SPP)(RSPRS)(S)(SRPSPS)(RSPPSSS)(SRRPRRPSSP)", "(RSPPSSS)") | |
test("(SRSPSPRS)(RRPRRS)(PRRRRS)(RSSPSSRPS)(PPSSPPRR)(PPSPPS)(PSPSPSSSP)(RPPRPS)", "(PRRRRS)") | |
test("(S)(PRS)(RSRP)(S)(PPRR)(PP)(RSSS)(P)(RSR)", "(PP)") | |
test("(RPR)(P)(PSPR)(SRSRP)(SR)(RPPR)(RRS)(S)(SSPR)(PRPR)", "(RPPR)") | |
test("(PSR)(PPPRR)(S)(SP)(S)(PR)(SPSRP)(PPSRR)(PRPPR)(RRRSP)(SR)", "(S)") | |
test("(PPRPP)(RSS)(PRS)(R)(RPRP)(SPSSS)(RR)(PPRP)(RSSS)(RSRS)(RP)", "(PPRPP)") | |
test("(P)(PPPRR)(RRRS)(RR)(RPRSS)(PRSPS)(PP)(R)(PSR)(RPPP)(RP)(SSSR)", "(PSR)") | |
test("(SR)(P)(RRPRP)(RSPS)(PSS)(SPPSP)(RRPS)(PR)(RRRSR)(PRR)(SSS)(RRRSS)(P)", "(SR)") | |
test("(PS)(RS)(RR)(RPR)(SR)(SP)(PRP)(PPS)(R)(PRSP)(SSPRR)(SP)(PPR)(RSRR)", "(SSPRR)") | |
test("(RRRRS)(SRPRR)(PPSS)(SSPPS)(R)(R)(P)(P)(PSSPR)(S)(RRPP)(SPRR)(S)(RR)(S)", "(PSSPR)") | |
test("(RRPSSRP)(SSSSSP)(RRSPSS)(PRSRRSRP)(SSRRRRR)(SS)(SSSSSSPPRP)(R)(SRRSR)(PPPSRSP)(RPRS)(RSRPPRS)(RPPPPRPR)(PRRSR)(RPRRSR)", "(PPPSRSP)") | |
test("(SSSRS)(SRPSS)(RSPRP)(RPPPP)(S)(PPRPS)(RRR)(PS)(RPSPS)(SPP)(PSRS)(P)(P)(RR)(S)(PSP)", "(RSPRP)")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SS)(PR)(S)(RRPPS)(S)(SSPRR)", "(RRS)") | |
test("(PSSS)(RRRPR)(PRPP)(RSSS)(RR)(RP)(PPS)(PSR)(SPS)(SRSS)(R)(RR)(SPRSR)(RSPRP)(RRSP)(SSRRP)(RSSSR)(PPSS)(PRS)(RRSRS)(PS)(SS)(P)(SPR)", "(PRPP)") | |
test("(RSRPSS)(RPPRPRRSP)(PRPSRSRPPP)(SSRSSRS)(RPS)(SP)(PPPPPSSP)(RRRPSR)(PSR)(SRSRSSR)(RPSSSRP)(RRSPSSSPPR)(RS)(SRRRSPRP)(PR)(RSSRPSSS)(PPRRRRRR)(RRSRP)(RRR)(PSPRSSPRP)(PRPPRSSRP)(SPPSPSS)(PSS)(RPS)(P)(RRSRSP)(PS)(RRPSSSRR)(RR)(PPPSPRPR)(PS)(PRSSRPR)(RRP)(PSRPR)(PS)(R)(RRPP)(SSPPSS)(SRPSSS)(RRSRRPRPP)", "(SPPSPSS)") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment