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
func product<A: Collection, B: Collection>(_ a: A, _ b: B) -> CartesianProductCollection<A, B> { | |
return CartesianProductCollection(a, b) | |
} | |
struct CartesianProductCollection<A: Collection, B: Collection> { | |
let a: A | |
let b: B | |
init(_ a: A, _ b: B) { | |
self.a = a |
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
extension String { | |
init<T>(dumping x: T) { | |
self.init() | |
dump(x, to: &self) | |
} | |
} | |
func assertDumpsEqual<T>(_ lhs: @autoclosure () -> T, _ rhs: @autoclosure () -> T, file: StaticString = #file, line: UInt = #line) { | |
assert(String(dumping: lhs()) == String(dumping: rhs()), "Expected dumps to be equal.", file: file, line: line) | |
} |
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
extension Sequence { | |
func min(_ n: Int, by areInIncreasingOrder: (Element, Element) -> Bool) -> [Element] { | |
var iterator = makeIterator() | |
guard let first = iterator.next() else { return [] } | |
return withoutActuallyEscaping(areInIncreasingOrder) { areInIncreasingOrder in | |
var heap = NonEmptyMaxHeap(root: first, by: areInIncreasingOrder) | |
var heapSize = 1 | |
while heapSize < n, let element = iterator.next() { |
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
// Monoid | |
precedencegroup MonoidPrecedence { | |
associativity: left | |
} | |
infix operator <> : MonoidPrecedence | |
protocol Monoid { | |
static var empty: Self { get } |
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
import Foundation | |
extension Notification { | |
struct TypedName<Value> { | |
let rawValue: String | |
init(_ rawValue: String) { | |
self.rawValue = rawValue | |
} | |
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
struct AnyEncodable: Encodable { | |
let base: Encodable | |
init(_ value: Encodable) { | |
base = value | |
} | |
func encode(to encoder: Encoder) throws { | |
try base.encode(to: encoder) | |
} |
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
let totalSize = 300 | |
func powerLevel(x: Int, y: Int, serialNumber: Int) -> Int { | |
let rackID = x + 10 | |
return ((rackID * y + serialNumber) * rackID / 100) % 10 - 5 | |
} | |
func part2(serialNumber: Int) -> String { | |
let grid = (1...totalSize).map { y in | |
(1...totalSize).map { x in |
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
extension ClosedRange where Bound == Int { | |
init(_ string: Substring) { | |
if let range = string.range(of: "..") { | |
let x = Int(string.prefix(through: range.lowerBound).dropLast())! | |
let y = Int(string.suffix(from: range.upperBound))! | |
self = x...y | |
} else { | |
let x = Int(string)! | |
self = x...x |
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
struct NonEmptyMaxHeap<Element: Comparable> { | |
private(set) var elements: [Element] | |
init(root: Element) { | |
self.elements = [root] | |
} | |
} | |
extension NonEmptyMaxHeap { | |
mutating func insert(_ element: Element) { |
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
use std::mem::ManuallyDrop; | |
use std::ops::{Deref, DerefMut}; | |
use std::ptr; | |
struct NonEmptyBinaryHeap<T> { | |
data: Vec<T>, | |
} | |
impl<T: Ord> NonEmptyBinaryHeap<T> { | |
fn with_root_and_capacity(root: T, capacity: usize) -> Self { |
OlderNewer