Add 2016 solutions
This commit is contained in:
parent
ddf5ec2274
commit
8146a794a3
8 changed files with 4006 additions and 0 deletions
2016/AoC2016.playground/Pages/Day 5.xcplaygroundpage
|
@ -0,0 +1,37 @@
|
|||
import CryptoKit
|
||||
import Foundation
|
||||
|
||||
func char(_ string: String) -> (String, String)? {
|
||||
let md5 = Insecure.MD5.hash(data: Data(string.utf8))
|
||||
return md5.withUnsafeBytes { ptr in
|
||||
guard ptr[0] == 0 && ptr[1] == 0 && ptr[2] & 0xf0 == 0 else { return nil }
|
||||
return (
|
||||
String(ptr[2] & 0x0f, radix: 16, uppercase: true),
|
||||
String((ptr[3] >> 4) & 0x0f, radix: 16, uppercase: true)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
var result1 = ""
|
||||
var result2 = "________"
|
||||
for i in 0... {
|
||||
if let (first, second) = char("abbhdwsy\(i)") {
|
||||
if result1.count < 8 {
|
||||
result1 += first
|
||||
}
|
||||
|
||||
if let i = Int(first), 0..<8 ~= i {
|
||||
let index = result2.index(result2.startIndex, offsetBy: i)
|
||||
if result2[index] == "_" {
|
||||
result2.replaceSubrange(index...index, with: second)
|
||||
}
|
||||
}
|
||||
|
||||
if result1.count == 8 && !result2.contains("_") {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
result1
|
||||
result2
|
Loading…
Add table
Add a link
Reference in a new issue