Skip to content

Instantly share code, notes, and snippets.

@sooop
Created August 29, 2017 02:53
Show Gist options
  • Select an option

  • Save sooop/92f2385ec6df90a40ff3e3cc75a44396 to your computer and use it in GitHub Desktop.

Select an option

Save sooop/92f2385ec6df90a40ff3e3cc75a44396 to your computer and use it in GitHub Desktop.
#!/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