1
0
Fork 0

day(05): introduce helper for creating range

Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
Matej Focko 2021-12-05 11:24:34 +01:00
parent 19ee5441e5
commit 0492a86f08

View file

@ -7,6 +7,12 @@ data class Point(val x: Int, val y: Int) {
operator fun times(other: Int): Point = Point(other * x, other * y) operator fun times(other: Int): Point = Point(other * x, other * y)
} }
fun getRange(from: Int, to: Int): Iterable<Int> = if (from <= to) {
from..to
} else {
(to..from).reversed()
}
data class Vector(val from: Point, val to: Point) { data class Vector(val from: Point, val to: Point) {
val horizontal: Boolean val horizontal: Boolean
get() = from.y == to.y get() = from.y == to.y
@ -18,10 +24,10 @@ data class Vector(val from: Point, val to: Point) {
get() = horizontal || vertical get() = horizontal || vertical
private val xs: Iterable<Int> private val xs: Iterable<Int>
get() = if (from.x <= to.x) (from.x..to.x) else (to.x..from.x).reversed() get() = getRange(from.x, to.x)
private val ys: Iterable<Int> private val ys: Iterable<Int>
get() = if (from.y <= to.y) (from.y..to.y) else (to.y..from.y).reversed() get() = getRange(from.y, to.y)
val points: Iterable<Point> val points: Iterable<Point>
get() = if (horizontalOrVertical) { get() = if (horizontalOrVertical) {