Skip to content

Instantly share code, notes, and snippets.

@withs
Last active June 5, 2021 09:40
Show Gist options
  • Save withs/6de5702c2f5f12168a84ce837b2c0f7d to your computer and use it in GitHub Desktop.
Save withs/6de5702c2f5f12168a84ce837b2c0f7d to your computer and use it in GitHub Desktop.
Two functions wich generate dice combinations for diceware list
/*
Two functions wich generate dice combinations for diceware list, the firstone is oriented to be compact and the seconds oriented to be efficient
- results: withMaxInt: 6 and andMinLenght: 5 (Imac 2013, i5, swift 5.3.2)
Efficient done in -> 62.49544095993042 seconds
Compact done in -> 76.10342800617218 seconds
- results: withMaxInt: 6 and andMinLenght: 5 (Macbook air 2020, M1, swift 5.4)
Efficient done in -> 0.06685197353363037 seconds
Compact done in -> 0.23580491542816162 seconds
- results: withMaxInt: 6 and andMinLenght: 5 (Ipad pro 2018, A12X, swift playground)
Efficient done in -> 0.10886907577514648 seconds
Compact done in -> 0.39872801303863525 seconds
*/
import Foundation
func dice(withMaxInt: Int, andMinLenght: Int) -> [Int] {
var base: Int = Int(String((1...andMinLenght).map {_ in "1"}))!
var re: [Int] = [base]
while base != Int(String((1...andMinLenght).map {_ in Character(String(withMaxInt))}))!{
base += 1
if !(((withMaxInt + 1)...9).map({String($0)}).map{ String(base).contains(String($0)) }.contains(true)) {
re.append(base)
}
}
return re
}
func diceTwo(withMaxInt: Int, andMinLenght: Int) -> [Int] {
var base: Int = Int(String((1...andMinLenght).map {_ in "1"}))!
let end: Int = Int(String((1...andMinLenght).map {_ in Character(String(withMaxInt))}))!
var re: [Int] = [base]
let toEx: [String] = ((withMaxInt + 1)...9).map({String($0)})
while base != end {
base += 1
if !(toEx.map{ String(base).contains(String($0)) }.contains(true)) {
re.append(base)
}
}
return re
}
var startTime = Date().timeIntervalSinceReferenceDate
let dec = dice(withMaxInt: 6, andMinLenght: 5)
var endTime = Date().timeIntervalSinceReferenceDate
print("Done -> \(endTime - startTime) seconds")
startTime = Date().timeIntervalSinceReferenceDate
let dec2 = diceTwo(withMaxInt: 6, andMinLenght: 5)
endTime = Date().timeIntervalSinceReferenceDate
print("Done -> \(endTime - startTime) seconds")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment