vbe_elvis

2021 Day5

Dec 5th, 2021 (edited)
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 1.10 KB | None | 0 0
  1. class Line(_start:Pair<Int, Int>, _end:Pair<Int, Int>) {
  2.     val diagonal = _start.first != _end.first && _start.second != _end.second
  3.     private val inverted = !diagonal &&  (_start.first > _end.first || _start.second > _end.second)
  4.     private val start = if (inverted) _end else _start
  5.     private val end = if (inverted) _start else _end
  6.     val points = when {
  7.         diagonal -> pointsOnDiagonal()
  8.         start.first == end.first -> (start.second..end.second).map { start.first to it }
  9.         else -> (start.first..end.first).map { it to start.second }
  10.     }
  11.  
  12.     private fun pointsOnDiagonal(): List<Pair<Int, Int>> {
  13.         val first = start.first range end.first
  14.         val second = (start.second range end.second).toList()
  15.         return first.mapIndexed{index, x -> x to second[index]}
  16.     }
  17. }
  18.  
  19. private infix fun Int.range(other: Int) = if (this < other) (this .. other) else (this downTo other)
  20.  
  21. private val String.asLine: Line
  22.     get() = this.split(" -> ").map { pair -> pair.split(",").map { coord -> coord.toInt() }}
  23.         .let { Line(it[0][0] to it[0][1], it[1][0] to it [1][1]) }
Advertisement
Add Comment
Please, Sign In to add comment