This file contains 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 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
//: Permutations | |
// based on https://www.objc.io/blog/2014/12/08/functional-snippet-10-permutations/ | |
// but updated for Swift 2.0 (Xcode 7.0) | |
extension Array { | |
func decompose() -> (Generator.Element, [Generator.Element])? { | |
guard let x = first else { return nil } | |
return (x, Array(self[1..<count])) | |
} | |
} |
This file contains 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 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
// Swift 2.2 | |
extension NSColor { | |
convenience init(rgb: String) { | |
guard rgb.characters.count == 6 else { fatalError("Invalid rgb value: \(rgb)") } | |
let scanner = NSScanner(string: rgb) | |
var hexValue: UInt32 = 0 |
This file contains 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
//: [Merging Overlapping Intervals](https://www.shiftedup.com/2015/05/17/programming-challenge-merging-overlapping-intervals) | |
//: Xcode 7.0, Swift 2.0 | |
/*: Given a collection of intervals, write a function that merges all overlapping intervals and prints them out. | |
For example, given [1, 3], [2, 6], [8, 10], and [7, 11], the function should print [1, 6], [7, 11]. Or given [5, 12], and [8, 10] the function should print [5, 12]. | |
You can assume that the first element of each interval is always less or equal than the second element of the interval. | |
*/ | |
//: Note: The challenge suggests that each interval is an `Array<Int>` where `interval.count == 2` and `interval[0] <= interval[1]`. In Swift, these intervals should be modeled by the `Range<Int>` type. I am altering the challenge to suit the language, I know. [TKO] |
This file contains 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))") | |
} |
This file contains 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
//: Rotate a matrix 90 degrees in place | |
// "Write a function to rotate an NxN matrix by 90 degrees. You should rotate it in place, meaning you can't use another matrix to perform the rotation, but instead you have to use the same given structure." | |
// https://www.shiftedup.com/2015/05/10/programming-challenge-rotating-a-matrix-90-degrees-in-place | |
// This implementation of a matrix is taken from Apple's own. | |
// https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Subscripts.html | |
// The extensions are my own. | |
// Xcode 7.0, Swift 2.0 |
This file contains 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 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 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 |
NewerOlder