Last active
May 8, 2016 02:17
-
-
Save mizuhara/3020a2a3c53178787c6cbfe063db61f8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
using System; | |
using System.Linq; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.Text.RegularExpressions; | |
namespace ESM | |
{ | |
public interface IHand | |
{ | |
bool Beat (IHand other); | |
bool BeatRock (); | |
bool BeatPaper (); | |
bool BeatScissors (); | |
} | |
public class Rock : IHand | |
{ | |
public bool Beat (IHand other) | |
{ | |
return !other.BeatRock (); | |
} | |
public bool BeatRock () | |
{ | |
return true; | |
} | |
public bool BeatPaper () | |
{ | |
return false; | |
} | |
public bool BeatScissors () | |
{ | |
return true; | |
} | |
} | |
public class Paper : IHand | |
{ | |
public bool Beat (IHand other) | |
{ | |
return !other.BeatPaper (); | |
} | |
public bool BeatRock () | |
{ | |
return true; | |
} | |
public bool BeatPaper () | |
{ | |
return true; | |
} | |
public bool BeatScissors () | |
{ | |
return false; | |
} | |
} | |
public class Scissors : IHand | |
{ | |
public bool Beat (IHand other) | |
{ | |
return !other.BeatScissors (); | |
} | |
public bool BeatRock () | |
{ | |
return false; | |
} | |
public bool BeatPaper () | |
{ | |
return true; | |
} | |
public bool BeatScissors () | |
{ | |
return true; | |
} | |
} | |
public class HandFactory | |
{ | |
public static IHand Create (string src) | |
{ | |
if (src.Equals ("R")) { | |
return new Rock (); | |
} | |
if (src.Equals ("P")) { | |
return new Paper (); | |
} | |
return new Scissors (); | |
} | |
} | |
class MainClass | |
{ | |
public static void Main (string[] args) | |
{ | |
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)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)(S)(SSP)(RPPP)(RPSP)(RPRR)(R)(SRRSS)(RSR)(PS)(PRP)(SSSS)(S)(SSSR)(SS)(PSP)(RS)(PSRSR)(SR)", "(SR)"); | |
Test ("(RSPSS)(RRSSR)(S)(RRS)(PSSRR)(S)(RPRRP)(RS)(PS)(RR)(R)(PSRR)(RPPRP)(SSS)(S)(R)(R)(SRSS)(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)"); | |
} | |
private static void Test (string src, string expected) | |
{ | |
var actual = Solve (src); | |
Console.WriteLine ((actual.Equals (expected)) ? "OK" : "***NG***"); | |
} | |
private static string Solve (string src) | |
{ | |
var tournament = MakeTournament ( | |
Regex.Matches (src, "[RSP]+").Cast<Match> ().Select (m => m.Value).ToArray ()); | |
while (tournament.Count () != 1) { | |
tournament = MakeTournament ( | |
tournament.Select (players => Battle (players.Item1, players.Item2)).ToArray ()); | |
} | |
return string.Concat ("(", Battle (tournament.First ().Item1, tournament.First ().Item2), ")"); | |
} | |
private static IEnumerable<Tuple<string, string>> MakeTournament (string[] src) | |
{ | |
var seed = Math.Pow (2, (Math.Ceiling (Math.Log (src.Length, 2)))) - src.Length; | |
for (var i = 0; i < src.Length; ++i) { | |
if (i < seed) { | |
yield return Tuple.Create (src [i], src [i]); | |
} else { | |
yield return Tuple.Create (src [i], src [++i]); | |
} | |
} | |
} | |
private static string Battle (string left, string right) | |
{ | |
var leftPlayer = Repeat (left, right.Length); | |
var rightPlayer = Repeat (right, left.Length); | |
for (var i = 0; i < leftPlayer.Length; ++i) { | |
var leftHand = HandFactory.Create (leftPlayer.Substring (i, 1)); | |
var rightHand = HandFactory.Create (rightPlayer.Substring (i, 1)); | |
var leftBeatsRight = leftHand.Beat (rightHand); | |
var rightBeatsLeft = rightHand.Beat (leftHand); | |
if (leftBeatsRight) { | |
return left; | |
} | |
if (rightBeatsLeft) { | |
return right; | |
} | |
} | |
return left; | |
} | |
private static string Repeat (string src, int count) | |
{ | |
return string.Concat (Enumerable.Repeat (src, count)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment