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
// 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() | |
} |
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
// 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 { |
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
/// Creates a sequence of tuple-3s built out of three underlying sequences. | |
/// Based on Zip2Seqence from the Swift Standard Library. | |
/// https://github.com/apple/swift/blob/9361a6b66f6f8351e89c090f604d7e1f42e2a045/stdlib/public/core/Zip.swift | |
/// | |
/// - Parameters: | |
/// - sequence1: The first sequence or collection to zip. | |
/// - sequence2: The second sequence or collection to zip. | |
/// - sequence3: The third sequence or collection to zip. | |
/// - Returns: A sequence of tuple triples, where the elements of each triplet are | |
/// corresponding elements of `sequence1`, `sequence2`, and `sequence3`. |
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
// taken 2018-03-19 from wikipedia. https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes | |
public enum Iso639_1: String { | |
case ab = "AB" | |
case aa = "AA" | |
case af = "AF" | |
case ak = "AK" | |
case sq = "SQ" | |
case am = "AM" | |
case ar = "AR" |
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
// taken 2018-03-19 from wikipedia. https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2 | |
enum Iso3166_1a2: String { | |
case af = "AF" | |
case ax = "AX" | |
case al = "AL" | |
case dz = "DZ" | |
case `as` = "AS" | |
case ad = "AD" | |
case ao = "AO" |
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
// 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 |
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
// Part 1 | |
extension String { | |
var characterCounts: [Character: Int] { | |
return self.reduce(into: [Character: Int]()) { $0[$1, default: 0] += 1 } | |
} | |
} | |
extension Bool { | |
var intValue: Int { |
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
public func ack(_ m: Int, _ n: Int) -> Int { | |
if m == 0 { return n + 1 } | |
if n == 0 { return ack(m - 1, 1) } | |
return ack(m - 1, ack(m, n - 1)) | |
} | |
for i in (0...5) { | |
for j in (0...5) { | |
print("ack(\(i), \(j)) is \(ack(i, j))") | |
} |
OlderNewer