Skip to content

Instantly share code, notes, and snippets.

Tim Vermeulen timvermeulen

Block or report user

Report or block timvermeulen

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View RotatedCollection.swift
struct RotatedCollection<Base: Collection> {
let base: Base
let index: Base.Index
init(base: Base, shiftingToStart index: Base.Index) {
self.base = base
self.index = index
}
}
View countWhereBranching.swift
import Foundation
func measure<T>(_ block: () -> T) -> T {
let start = Date()
let result = block()
print("elapsed time: \(-start.timeIntervalSinceNow)")
return result
}
extension Sequence {
View non_empty_binary_heap.rs
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 {
View NonEmptyMaxHeap.swift
struct NonEmptyMaxHeap<Element: Comparable> {
private(set) var elements: [Element]
init(root: Element) {
self.elements = [root]
}
}
extension NonEmptyMaxHeap {
mutating func insert(_ element: Element) {
View Aoc2018Day17.swift
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
View Aoc2018Day11.swift
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
View EncodableDictionary.swift
struct AnyEncodable: Encodable {
let base: Encodable
init(_ value: Encodable) {
base = value
}
func encode(to encoder: Encoder) throws {
try base.encode(to: encoder)
}
View Notification+TypedName.swift
import Foundation
extension Notification {
struct TypedName<Value> {
let rawValue: String
init(_ rawValue: String) {
self.rawValue = rawValue
}
View SortDescriptor.swift
// Monoid
precedencegroup MonoidPrecedence {
associativity: left
}
infix operator <> : MonoidPrecedence
protocol Monoid {
static var empty: Self { get }
View smallestN.swift
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() {
You can’t perform that action at this time.