Last active
May 6, 2016 12:59
-
-
Save mtsmfm/3e28be981ab985dfa042 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
| "use strict"; | |
| class Doukaku { | |
| seedCountFor(members: Array<string>): number { | |
| return Math.pow(2, Math.ceil(Math.log2(members.length))) - members.length; | |
| } | |
| eachSlice (array, size, callback) { | |
| for (let i = 0, l = array.length; i < l; i += size) { | |
| callback.call(this, array.slice(i, i + size)); | |
| } | |
| } | |
| battle(left: string, right: string): string { | |
| let left2 = left.repeat(right.length); | |
| let right2 = right.repeat(left.length); | |
| if (left2 === right2) { | |
| return left; | |
| } else { | |
| for (let i = 0; i < left2.length; i++) { | |
| let l = left2[i]; | |
| let r = right2[i]; | |
| if (l === r) { | |
| // noop | |
| } else if (l === "R" && r === "S") { | |
| return left; | |
| } else if (l === "P" && r === "R") { | |
| return left; | |
| } else if (l === "S" && r === "P") { | |
| return left; | |
| } else { | |
| return right; | |
| } | |
| }; | |
| } | |
| } | |
| process(members: Array<string>): Array<string> { | |
| let nextMembers = []; | |
| this.eachSlice(members, 2, (e) => { | |
| nextMembers.push(this.battle(e[0], e[1])); | |
| }); | |
| return nextMembers; | |
| } | |
| solve(input: string): string { | |
| let members = input.match(/\((.*?)\)/g).map((e) => e.replace(/[()]/g, "")); | |
| let seedMembers = members.slice(0, this.seedCountFor(members)); | |
| let notSeedMembers = members.slice(- members.length + this.seedCountFor(members)); | |
| members = seedMembers.concat(this.process(notSeedMembers)); | |
| while (members.length > 1) { | |
| members = this.process(members); | |
| } | |
| return `(${members[0]})`; | |
| } | |
| } | |
| let test = (input: string, expected: string) => { | |
| let doukaku: Doukaku = new Doukaku(); | |
| let actual: string = doukaku.solve(input); | |
| if (actual === expected) { | |
| console.log("ok"); | |
| } else { | |
| console.log("ng"); | |
| console.log(`expected: ${expected}`); | |
| console.log(`actual: ${actual}`); | |
| throw new Error("error"); | |
| } | |
| }; | |
| 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)"); |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", "(RRPS)"); | |
| test("(S)(PRSRR)(PP)(PSSSS)(SR)(SRRP)(PRRPR)(PRSS)(SPPS)(SS)(SPPR)(SSRSR)(PSRPP)(RSP)(R)(P)(PPP)(SS)(SP)(SSSS)(RRSR)", "(SRRP)"); | |
| test("(PS)(R)(R)(S)(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)"); |
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
| { | |
| "name": "js", | |
| "version": "1.0.0", | |
| "description": "", | |
| "main": "index.js", | |
| "scripts": { | |
| "test": "tsc doukaku.ts -t es6 && node --harmony doukaku.js", | |
| "tsc": "tsc" | |
| }, | |
| "devDependencies": { | |
| "typescript": "latest", | |
| "typings": "latest" | |
| }, | |
| "author": "", | |
| "license": "ISC" | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment