Skip to content

Instantly share code, notes, and snippets.

@heinrisch
heinrisch / day3.kt
Created December 4, 2023 09:51
Advent of Code 2023 Day 3
import java.awt.Point
import java.io.File
fun main() {
data class Digit(val p: Point, val value: Char)
// List of digits is a number (always in right order because of parsing order)
fun List<Digit>.value(): Long = this.map { it.value }.joinToString("").toLong()
val matrix = File("input/day3.txt").readLines()
@heinrisch
heinrisch / UICollectionview.swift
Created March 21, 2017 12:05
Nicer register class and dequeue
extension UICollectionView {
func dequeueCell<T: UICollectionViewCell>(_ indexPath: IndexPath) -> T {
let reuseIdentifier = T.classForCoder().description()
guard let cell = dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as? T else {
fatalError("Could not dequeue cell with identifier \(reuseIdentifier)")
}
return cell
}
@heinrisch
heinrisch / UINavigationController+StatusbarColorChange.swift
Created July 21, 2016 08:53
Change statusbar background color iOS
import Foundation
extension UINavigationController {
public override class func initialize() {
struct Static {
static var token: dispatch_once_t = 0
}
if self !== UINavigationController.self {
return
@heinrisch
heinrisch / Lookups.kt
Last active May 9, 2016 10:39
Easy lookup creation from lists
import java.util.*
// Already in Kotlin std lib: list.mapTo(mutableSetOf(), keyGenerator)
fun <K, V> List<V>.createLookupSet(keyGenerator: (V) -> K) : HashSet<K> {
return fold(HashSet<K>(), {set, value -> set.add(keyGenerator(value)); set})
}
// Already in Kotlin std lib: list.associateBy(keyGenerator)
fun <K, V> List<V>.createLookupMap(keyGenerator: (V) -> K) : HashMap<K, V> {
return fold(HashMap<K, V>(), {map, value -> map.put(keyGenerator(value), value); map})
@heinrisch
heinrisch / RecyclingMergeAdapter.kt
Last active January 25, 2017 15:16
Now it works with animations! Reset the index before calling notifyItemXXX
package co.leftorright.android.util
import android.support.v7.widget.RecyclerView
import android.view.View
import android.view.ViewGroup
/**
* Created by henrik on 02/08/16.
*/
private class LazyView<T>(view : View, res : Int) : ReadWriteProperty<Any?, T> {
private var mView: View = view
private var res : Int = res
private var mValue: T? = null
override fun getValue(thisRef: Any?, property: KProperty<*>): T {
mValue = mValue ?: mView.findViewById(res) as T
return mValue!!
}
@heinrisch
heinrisch / hitTest.swift
Created August 10, 2015 11:00
Grow hit area
override func hitTest(point: CGPoint, withEvent event: UIEvent?) -> UIView? {
let errorMargin : CGFloat = 30;
let largerFrame = CGRectMake(0 - errorMargin, 0 - errorMargin, frame.size.width + 2*errorMargin, frame.size.height + 2*errorMargin);
return largerFrame.contains(point) ? self : nil
}
@heinrisch
heinrisch / about.swift
Created August 7, 2015 15:04
Very useful operator for not being precise..
prefix operator ~ {}
prefix func ~ (number: Float) -> Float {
let random = Float(arc4random()) / Float(UINT32_MAX)
return number + (0.1*number - 0.2*number*random) // +-10%
}