Last active
April 20, 2017 00:47
-
-
Save watr/026062da8877d1d8a89009bad28d4e39 to your computer and use it in GitHub Desktop.
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
//: Playground - noun: a place where people can play | |
import Foundation | |
var testCases: [(input: String, expectedOutput: String)] = [] | |
func addTestCase(_ input: String, _ expectedOutput: String) { | |
testCases.append((input, expectedOutput)) | |
} | |
/*0*/ addTestCase( "165", "3445" ); | |
/*1*/ addTestCase( "80", "48" ); | |
/*2*/ addTestCase( "255", "33333333" ); | |
/*3*/ addTestCase( "68", "55" ); | |
/*4*/ addTestCase( "200", "355" ); | |
/*5*/ addTestCase( "82", "455" ); | |
/*6*/ addTestCase( "164", "455" ); | |
/*7*/ addTestCase( "73", "455" ); | |
/*8*/ addTestCase( "146", "455" ); | |
/*9*/ addTestCase( "37", "455" ); | |
/*10*/ addTestCase( "74", "455" ); | |
/*11*/ addTestCase( "148", "455" ); | |
/*12*/ addTestCase( "41", "455" ); | |
/*13*/ addTestCase( "38", "355" ); | |
/*14*/ addTestCase( "76", "355" ); | |
/*15*/ addTestCase( "152", "355" ); | |
/*16*/ addTestCase( "49", "355" ); | |
/*17*/ addTestCase( "98", "355" ); | |
/*18*/ addTestCase( "196", "355" ); | |
/*19*/ addTestCase( "137", "355" ); | |
/*20*/ addTestCase( "19", "355" ); | |
/*21*/ addTestCase( "20", "48" ); | |
/*22*/ addTestCase( "9", "57" ); | |
/*23*/ addTestCase( "209", "3345" ); | |
/*24*/ addTestCase( "121", "33345" ); | |
/*25*/ addTestCase( "239", "3333334" ); | |
/*26*/ addTestCase( "26", "347" ); | |
/*27*/ addTestCase( "111", "333344" ); | |
/*28*/ addTestCase( "95", "333344" ); | |
/*29*/ addTestCase( "85", "4444" ); | |
/*30*/ addTestCase( "24", "39" ); | |
/*31*/ addTestCase( "97", "347" ); | |
/*32*/ addTestCase( "234", "33444" ); | |
/*33*/ addTestCase( "59", "33345" ); | |
/*34*/ addTestCase( "187", "333344" ); | |
/*35*/ addTestCase( "34", "55" ); | |
/*36*/ addTestCase( "249", "333335" ); | |
/*37*/ addTestCase( "43", "3445" ); | |
/*38*/ addTestCase( "143", "33335" ); | |
/*39*/ addTestCase( "28", "338" ); | |
/*40*/ addTestCase( "79", "33345" ); | |
/*41*/ addTestCase( "173", "33444" ); | |
/*42*/ addTestCase( "55", "33345" ); | |
/*43*/ addTestCase( "77", "3445" ); | |
/*44*/ addTestCase( "35", "355" ); | |
/*45*/ addTestCase( "153", "3355" ); | |
/*46*/ addTestCase( "30", "3337" ); | |
/*47*/ addTestCase( "228", "3355" ); | |
/*48*/ addTestCase( "177", "3345" ); | |
/*49*/ addTestCase( "162", "445" ); | |
/*50*/ addTestCase( "184", "3345" ); | |
func solve(input: String) -> String? { | |
guard let inputValue: Int = Int(input) else { | |
return nil | |
} | |
let sideCount = 8// 8角形 | |
var polygons: [Int] = []//みつけた多角形(n角形)を n: Int として保存しておく | |
do { | |
func zeroFilledCount(value: Int) -> Int { | |
for shift in 0..<sideCount { | |
if (value & (1 << shift)) != 0 { | |
return shift | |
} | |
} | |
return sideCount | |
} | |
func polygon(startIndex: Int, endIndex: Int) -> Int? { | |
let distance = (endIndex - startIndex) | |
if distance == 0 { | |
return nil | |
} | |
return (2 + distance) - (((distance * 2) == sideCount) ? 1 : 0) | |
} | |
let shifted = (inputValue >> zeroFilledCount(value: inputValue)) | |
var startIndex = 0 | |
var endIndex = 0 | |
for shift in 0...sideCount { // スタートまでやる必要があるので 🙆"0...sideCount" 🙅"0..<sideCount" | |
if ((shifted & (1 << shift)) != 0) || (shift == sideCount) { | |
endIndex = shift | |
if let polygon = polygon(startIndex: startIndex, endIndex: endIndex) { | |
polygons.append(polygon) | |
} | |
startIndex = shift | |
} | |
} | |
} | |
return polygons.sorted().map({String($0)}).joined() | |
} | |
func runTests() { | |
var testedCount = 0 | |
var okCount = 0 | |
var ngCount = 0 | |
for testCase in testCases { | |
let solved = solve(input: testCase.input) | |
if solved == testCase.expectedOutput { | |
okCount += 1 | |
} | |
else { | |
ngCount += 1 | |
print("## NG") | |
print("solved: ", solved ?? "(nil)") | |
print("test: ", testCase) | |
} | |
testedCount += 1 | |
} | |
print("########") | |
print("tests have done") | |
print("total tests: ", testedCount) | |
print("total ok: ", okCount) | |
print("total ng: ", ngCount) | |
} | |
runTests() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment