Created
August 29, 2017 02:53
-
-
Save sooop/92f2385ec6df90a40ff3e3cc75a44396 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
| #!/usr/bin/swift | |
| import Foundation | |
| func timeit(_ f:() -> ()) { | |
| let s = Date() | |
| f() | |
| let e = String(format:"time: %.4fms", s.timeIntervalSinceNow * -1000) | |
| print(e) | |
| } | |
| timeit{ | |
| guard let url = URL(string: "http://euler.synap.co.kr/files/words.txt"), | |
| let contents = try? String(contentsOf: url) | |
| else { return } | |
| func wordPoint(_ s: Substring) -> Int { | |
| let x = s.trimmingCharacters(in: CharacterSet(charactersIn: "\"")) | |
| .utf8.flatMap{ Int($0) - 64 } | |
| .reduce(0, +) | |
| return x | |
| } | |
| let make_t:(Int) -> Int = { $0 * ($0 + 1) / 2 } | |
| let ts = Set((1...1000).map(make_t)) | |
| let result = contents.split(separator:",") | |
| .map(wordPoint) .filter{ ts.contains($0) }.count | |
| print(result) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment