Advertisement
Guest User

HydrothermalVenture

a guest
Dec 6th, 2021
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 5.45 KB | None | 0 0
  1. private fun main() {
  2.     fun part1(input: List<String>): Int {
  3.         val ventLines = mutableListOf<VentLine>()
  4.         input.forEach {
  5.             val (startX, startY) = it.substringBefore(" -> ").split(",").map(String::toInt)
  6.             val (endX, endY) = it.substringAfter(" -> ").split(",").map(String::toInt)
  7.             val startPoint = Point(startX, startY)
  8.             val endPoint = Point(endX, endY)
  9.             ventLines.add(VentLine(startPoint, endPoint))
  10.         }
  11.  
  12.         val diagram = mutableMapOf<Point, Int>()
  13.         ventLines.filter {
  14.             it.firstEnd.x == it.secondEnd.x || it.firstEnd.y == it.secondEnd.y
  15.         }.forEach { ventLine ->
  16.             ventLine.run {
  17.                 if (firstEnd.isHorizontallyAlignedWith(secondEnd)) {
  18.                     val range =
  19.                         if (firstEnd <= secondEnd) firstEnd.y..secondEnd.y else firstEnd.y downTo secondEnd.y
  20.                     for (i in range) {
  21.                         val point = Point(firstEnd.x, i)
  22.                         diagram[point] = diagram[point]?.inc() ?: 1
  23.                     }
  24.                 } else if (firstEnd.isVerticallyAlignedWith(secondEnd)) {
  25.                     val range =
  26.                         if (firstEnd <= secondEnd) firstEnd.x..secondEnd.x else firstEnd.x downTo secondEnd.x
  27.                     for (i in range) {
  28.                         val point = Point(i, firstEnd.y)
  29.                         diagram[point] = diagram[point]?.inc() ?: 1
  30.                     }
  31.                 }
  32.             }
  33.         }
  34.  
  35.         return diagram.values.count { it >= 2 }
  36.     }
  37.  
  38.     fun part2(input: List<String>): Int {
  39.         val ventLines = mutableListOf<VentLine>()
  40.         input.forEach {
  41.             val (startX, startY) = it.substringBefore(" -> ").split(",").map(String::toInt)
  42.             val (endX, endY) = it.substringAfter(" -> ").split(",").map(String::toInt)
  43.             val startPoint = Point(startX, startY)
  44.             val endPoint = Point(endX, endY)
  45.             ventLines.add(VentLine(startPoint, endPoint))
  46.         }
  47.  
  48.         val diagram = mutableMapOf<Point, Int>()
  49.         ventLines.forEach { ventLine ->
  50.             ventLine.run {
  51.                 if (firstEnd.isHorizontallyAlignedWith(secondEnd)) {
  52.                     val range =
  53.                         if (firstEnd <= secondEnd) firstEnd.y..secondEnd.y else firstEnd.y downTo secondEnd.y
  54.                     for (i in range) {
  55.                         val point = Point(firstEnd.x, i)
  56.                         diagram[point] = diagram[point]?.inc() ?: 1
  57.                     }
  58.                 } else if (firstEnd.isVerticallyAlignedWith(secondEnd)) {
  59.                     val range =
  60.                         if (firstEnd <= secondEnd) firstEnd.x..secondEnd.x else firstEnd.x downTo secondEnd.x
  61.                     for (i in range) {
  62.                         val point = Point(i, firstEnd.y)
  63.                         diagram[point] = diagram[point]?.inc() ?: 1
  64.                     }
  65.                 } else if (firstEnd.isDiagonallyAlignedWith(secondEnd)) {
  66.                     if (firstEnd <= secondEnd) {
  67.                         var x = firstEnd.x
  68.                         var y = firstEnd.y
  69.                         if (firstEnd.y < secondEnd.y) {
  70.                             while (x <= secondEnd.x && y <= secondEnd.y) {
  71.                                 val point = Point(x, y)
  72.                                 diagram[point] = diagram[point]?.inc() ?: 1
  73.                                 x++
  74.                                 y++
  75.                             }
  76.                         } else {
  77.                             while (x <= secondEnd.x && y >= secondEnd.y) {
  78.                                 val point = Point(x, y)
  79.                                 diagram[point] = diagram[point]?.inc() ?: 1
  80.                                 x++
  81.                                 y--
  82.                             }
  83.                         }
  84.                     } else {
  85.                         var x = firstEnd.x
  86.                         var y = firstEnd.y
  87.                         if (firstEnd.y > secondEnd.y) {
  88.                             while (x >= secondEnd.x && y >= secondEnd.y) {
  89.                                 val point = Point(x, y)
  90.                                 diagram[point] = diagram[point]?.inc() ?: 1
  91.                                 x--
  92.                                 y--
  93.                             }
  94.                         } else {
  95.                             while (x >= secondEnd.x && y <= secondEnd.y) {
  96.                                 val point = Point(x, y)
  97.                                 diagram[point] = diagram[point]?.inc() ?: 1
  98.                                 x--
  99.                                 y++
  100.                             }
  101.                         }
  102.                     }
  103.                 }
  104.             }
  105.         }
  106.  
  107.         return diagram.values.count { it >= 2 }
  108.     }
  109. }
  110.  
  111. data class Point(val x: Int, val y: Int) : Comparable<Point> {
  112.     override fun compareTo(other: Point): Int {
  113.         if (this.x < other.x) return -1
  114.         if (this.x > other.x) return 1
  115.         if (this.y < other.y) return -1
  116.         if (this.y > other.y) return 1
  117.         return 0
  118.     }
  119.  
  120.     fun isHorizontallyAlignedWith(other: Point) = x == other.x
  121.  
  122.     fun isVerticallyAlignedWith(other: Point) = y == other.y
  123.  
  124.     fun isDiagonallyAlignedWith(other: Point) = abs(x - other.x) == abs(y - other.y)
  125. }
  126.  
  127. data class VentLine(val firstEnd: Point, val secondEnd: Point)
  128.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement