Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 3.17 KB | None | 0 0
  1. package advent
  2.  
  3. import java.awt.Point
  4. import kotlin.math.abs
  5. import kotlin.math.max
  6. import kotlin.math.min
  7.  
  8. class Day3 {
  9.  
  10.     private class Line(
  11.         val x1 : Int,
  12.         val y1 : Int,
  13.         val x2 : Int,
  14.         val y2 : Int
  15.     ) {
  16.         fun isHorizontal() = y1 == y2
  17.  
  18.         override fun toString() = "[$x1, $y1] -> [$x2, $y2]"
  19.     }
  20.  
  21.     private class Step(
  22.         val direction : String,
  23.         val steps : Int
  24.  
  25.     ) {
  26.         override fun toString() = "$direction$steps"
  27.     }
  28.  
  29.     companion object {
  30.  
  31.         private fun getInput() : Pair<List<Step>, List<Step>> {
  32.             val lines = javaClass
  33.                 .getResource("/Day3Input")
  34.                 .readText()
  35.                 .lines()
  36.                 .map { line ->
  37.                     line.split(',').map { Step(it.substring(0, 1), it.substring(1).toInt()) }
  38.                 }
  39.             return Pair(lines[0], lines[1])
  40.         }
  41.  
  42.         private fun getWire(steps : List<Step>) : MutableList<Line> {
  43.             var (x, y) = Pair(0, 0)
  44.             val lines = mutableListOf<Line>()
  45.             for (step in steps) {
  46.                 val (x0, y0) = Pair(x, y)
  47.                 when (step.direction) {
  48.                     "U" -> y -= step.steps
  49.                     "D" -> y += step.steps
  50.                     "L" -> x -= step.steps
  51.                     "R" -> x += step.steps
  52.                 }
  53.                 lines.add(Line(min(x0, x), min(y0, y), max(x0, x), max(y0, y)))
  54.             }
  55.             return lines
  56.         }
  57.  
  58.         private fun areLinesIntersecting(line1 : Line, line2 : Line) : Point? {
  59.             val hLine : Line
  60.             val vLine : Line
  61.             if (line1.isHorizontal() && !line2.isHorizontal()) {
  62.                 hLine = line1
  63.                 vLine = line2
  64.             } else if (!line1.isHorizontal() && line2.isHorizontal()) {
  65.                 hLine = line2
  66.                 vLine = line1
  67.             } else {
  68.                 return null
  69.             }
  70.             if (hLine.y1 >= vLine.y1 && hLine.y1 <= vLine.y2 && vLine.x1 >= hLine.x1 && vLine.x1 <= hLine.x2) {
  71.                 return Point(vLine.x1, hLine.y1)
  72.             }
  73.             return null
  74.         }
  75.  
  76.         private fun getIntersections(wire1 : List<Line>, wire2 : List<Line>) : MutableList<Point> {
  77.             val intersections = mutableListOf<Point>()
  78.             for (line1 in wire1) {
  79.                 for (line2 in wire2) {
  80.                     areLinesIntersecting(line1, line2)?.let { intersections.add(it) }
  81.                 }
  82.             }
  83.             return intersections
  84.         }
  85.  
  86.         @JvmStatic
  87.         fun main(args : Array<String>) {
  88.             val input = getInput()
  89.             val wire1 = getWire(input.first)
  90.             val wire2 = getWire(input.second)
  91.             val intersections = getIntersections(wire1, wire2)
  92.             var closestDistance= Int.MAX_VALUE
  93.             intersections.forEach {
  94.                 val distance = abs(it.x) + abs(it.y)
  95.                 if (distance in 1 until closestDistance) {
  96.                     closestDistance = distance
  97.                 }
  98.             }
  99.             println(closestDistance)
  100.         }
  101.  
  102.     }
  103.  
  104.  
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement