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 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() |
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 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 | |
} |
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 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}) |
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
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. | |
*/ |
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
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!! | |
} |
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
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 | |
} |
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
prefix operator ~ {} | |
prefix func ~ (number: Float) -> Float { | |
let random = Float(arc4random()) / Float(UINT32_MAX) | |
return number + (0.1*number - 0.2*number*random) // +-10% | |
} |