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
| func grayEncode(_ i: Int) -> Int { | |
| return (i >> 1) ^ i | |
| } | |
| func grayDecode(_ i: Int) -> Int { | |
| switch i { | |
| case 0: | |
| return 0 | |
| case _: | |
| return i ^ grayDecode(i >> 1) |
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
| typealias FourBit = (Int, Int, Int, Int) | |
| func halfAdder(_ a: Int, _ b: Int) -> (Int, Int) { | |
| return (a ^ b, a & b) | |
| } | |
| func fullAdder(_ a: Int, _ b: Int, carry: Int) -> (Int, Int) { | |
| let (s0, c0) = halfAdder(a, b) | |
| let (s1, c1) = halfAdder(s0, carry) |
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 struct Cell: Hashable { | |
| public var x: Int | |
| public var y: Int | |
| public var neighbors: [Cell] { | |
| [ | |
| Cell(x: x - 1, y: y - 1), | |
| Cell(x: x, y: y - 1), | |
| Cell(x: x + 1, y: y - 1), | |
| Cell(x: x - 1, y: y), |
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
| extension Dictionary where Key == ClosedRange<Int> { | |
| subscript(n: Int) -> Value { | |
| get { | |
| guard let key = keys.first(where: { $0.contains(n) }) else { | |
| fatalError("dict does not contain range for \(n)") | |
| } | |
| return self[key]! | |
| } |
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
| import Foundation | |
| import Playground | |
| private let lock = DispatchSemaphore(value: 1) | |
| private let queue = DispatchQueue(label: "worker", attributes: .concurrent) | |
| private let nWorkers = 5 | |
| private let stopAt = 1 << 30 | |
| private var i = 33550336 | |
| private var running = nWorkers |
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
| print("Enter the number of resources: ", terminator: "") | |
| guard let resources = Int(readLine(strippingNewline: true)!) else { | |
| fatalError() | |
| } | |
| print("Enter the number of processes: ", terminator: "") | |
| guard let processes = Int(readLine(strippingNewline: true)!) else { | |
| fatalError() |
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
| import Foundation | |
| public extension String { | |
| func textBetween(_ startDelim: String, and endDelim: String) -> String { | |
| precondition(!startDelim.isEmpty && !endDelim.isEmpty) | |
| let startIdx: String.Index | |
| let endIdx: String.Index | |
| if startDelim == "start" { |
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
| func maxNugget(limit: Int) -> Int { | |
| var (max, sixes, nines, twenties, i) = (0, 0, 0, 0, 0) | |
| mainLoop: while i < limit { | |
| sixes = 0 | |
| while sixes * 6 < i { | |
| if sixes * 6 == i { | |
| i += 1 | |
| continue mainLoop |
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
| import Foundation | |
| public struct MTRandom: RandomNumberGenerator { | |
| private var index = 312 | |
| private var mt = [UInt64](repeating: 0, count: 312) | |
| public init(seed: UInt64 = .random(in: .min ... .max)) { | |
| mt[0] = seed | |
| for i in 1..<312 { |
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
| import Foundation | |
| public final class AtomicBuckets: CustomStringConvertible { | |
| public var count: Int { | |
| return buckets.count | |
| } | |
| public var description: String { | |
| return withBucketsLocked { "\(buckets)" } | |
| } |