I hereby claim:
- I am proxpero on github.
- I am proxpero (https://keybase.io/proxpero) on keybase.
- I have a public key whose fingerprint is ABAF 07DE 0D7E EA58 7C60 1131 CB36 31B5 E8CA 11C7
To claim this, I am signing this object:
// Swift 2.1 | |
extension MutableCollectionType where Self.Generator.Element : Comparable { | |
/// Return a new sorted collection of the elements in `source` | |
/// using [Selection Sort](https://en.wikipedia.org/wiki/Selection_sort). | |
/// | |
/// Complexity O(n^2). | |
@warn_unused_result(mutable_variant="selectionSortInPlace") | |
public func selectionSort() -> Self { |
// Swift 2.1 | |
extension MutableCollectionType where Self.Generator.Element : Comparable { | |
/// Return a new sorted collection of the elements in `source` | |
/// using [Insertion Sort](https://en.wikipedia.org/wiki/Insertion_sort). | |
/// | |
/// Complexity O(n^2). | |
@warn_unused_result(mutable_variant="insertionSortInPlace") | |
public func insertionSort() -> Self { |
// Swift 2.1 | |
extension MutableCollectionType where Self.Generator.Element : Comparable, Self.Index == Int { | |
/// Return a new sorted collection of the elements in `source` | |
/// using [Shell Sort](https://en.wikipedia.org/wiki/Shellsort). | |
/// | |
/// Complexity O(n^(3/2)). | |
@warn_unused_result(mutable_variant="shellSortInPlace") |
// Swift 2.1 | |
extension Array where Element : Comparable { | |
private func merge(var left: Array<Element>, var _ right: Array<Element>) -> Array<Element> { | |
var result: Array<Element> = [] | |
while !left.isEmpty && !right.isEmpty { | |
// As described in: http://proxpero.com/2017/02/14/an-implementation-of-md5/ | |
extension Collection where Iterator.Element == UInt8 { | |
/// Returns the md5 hash of self. | |
public var md5: String { | |
return self.md5Digest.lazy.reduce("") { | |
var s = String($1, radix: 16) | |
if s.characters.count == 1 { | |
s = "0" + s |
I hereby claim:
To claim this, I am signing this object:
// Accompanying blog post: http://127.0.0.1:4000/2017/07/11/encoding-and-decoding-custom-enums-with-associated-values-in-swift-4 | |
// Also: https://gist.github.com/proxpero/189a723fb96bb88fac5bf9e11d6cf9e2 | |
enum Barcode { | |
case upc(Int, Int, Int, Int) | |
case qrCode(String) | |
} | |
extension Barcode: Codable { | |
private enum CodingKeys: String, CodingKey { |
// Part 1 | |
extension String { | |
var characterCounts: [Character: Int] { | |
return self.reduce(into: [Character: Int]()) { $0[$1, default: 0] += 1 } | |
} | |
} | |
extension Bool { | |
var intValue: Int { |
// prepare the puzzle input | |
let input = loadFile(named: "day1") // convenience function in resources | |
.split(separator: "\n") | |
.compactMap(Int.init) // extension on Int to init? with String.Subsequence | |
// part 1 | |
let part1 = input.reduce(0, +) | |
// part 2 |
// See http://proxpero.com/2017/07/11/encoding-and-decoding-custom-enums-with-associated-values-in-swift-4/ | |
enum Barcode { | |
case upc(Int, Int, Int, Int) | |
case qrCode(String) | |
} | |
extension Barcode: Codable { | |
init(from decoder: Decoder) throws { | |
self = try Barcode.Coding.init(from: decoder).barcode() | |
} |