package aoc2018

class Day10(input: List<String>) {

    private val lights = input.map(Light::from)

    fun solvePart1(): String {
        val message = Sky(lights).getMessageWithLeastArea()
        println(message.second)

        return message.first
    }

    fun solvePart2(): Int {
        val message = Sky(lights).getMessageWithLeastArea()

        return message.second
    }

    data class Light(var x: Int, var y: Int, val velocityX: Int, val velocityY: Int) {

        fun move(direction: Direction) = when (direction) {
            Direction.Forward -> {
                x += velocityX
                y += velocityY
            }

            Direction.Backward -> {
                x -= velocityX
                y -= velocityY
            }
        }

        companion object {
            fun from(line: String): Light {
                val (x, y) = line.substringAfter("<").substringBefore(">").trimAndSplitBy(",")
                val (velocityX, velocityY) = line.substringAfterLast("<").substringBeforeLast(">").trimAndSplitBy(",")

                return Light(x.toInt(), y.toInt(), velocityX.toInt(), velocityY.toInt())
            }
        }
    }

    enum class Direction {
        Forward, Backward
    }

    data class Sky(val lights: List<Light>) {

        /**
         * For this to make sense, we assume that the message shows itself when the area of all the lights are the smallest
         * This might be naïve, but it seems to work with this task.
         * It would've failed if the stars all started from the center of the sky
         */
        fun getMessageWithLeastArea(): Pair<String, Int> {
            var lastArea = Long.MAX_VALUE
            var currentArea = calculateCurrentArea()
            var seconds = 0

            while (lastArea > currentArea) {
                moveLights()
                seconds += 1

                lastArea = currentArea
                currentArea = calculateCurrentArea()
            }

            // We're out of the loop since the lastArea is smaller than the current area,
            // so that means we're 1 second too far ahead... Luckily we can just go back in time.
            moveLights(direction = Direction.Backward)

            return this.toString() to seconds - 1
        }

        private fun moveLights(direction: Direction = Direction.Forward) = lights.forEach { it.move(direction) }

        private val rangeY
            get() = IntRange(lights.minOf { it.y }, lights.maxOf { it.y })
        private val rangeX
            get() = IntRange(lights.minOf { it.x }, lights.maxOf { it.x })

        private fun calculateCurrentArea(): Long =
            (rangeX.last - rangeX.first).toLong() * (rangeY.last - rangeY.first).toLong()

        override fun toString(): String {
            val lightSet = lights.map { it.x to it.y }.toSet()

            return rangeY.joinToString(separator = "\n") { y ->
                rangeX.map { x ->
                    if (x to y in lightSet) '#' else '.'
                }.joinToString("")
            }
        }
    }
}

private fun String.trimAndSplitBy(delimiter: String) = replace("\\s".toRegex(), "").split(delimiter)

Compiled from "Day10.kt"
public final class aoc2018.Day10$Light$WhenMappings {
  public static final int[] $EnumSwitchMapping$0;

  static {};
    Code:
       0: invokestatic  #14                 // Method aoc2018/Day10$Direction.values:()[Laoc2018/Day10$Direction;
       3: arraylength
       4: newarray       int
       6: astore_0
       7: nop
       8: aload_0
       9: getstatic     #18                 // Field aoc2018/Day10$Direction.Forward:Laoc2018/Day10$Direction;
      12: invokevirtual #22                 // Method aoc2018/Day10$Direction.ordinal:()I
      15: iconst_1
      16: iastore
      17: goto          21
      20: astore_1
      21: nop
      22: aload_0
      23: getstatic     #25                 // Field aoc2018/Day10$Direction.Backward:Laoc2018/Day10$Direction;
      26: invokevirtual #22                 // Method aoc2018/Day10$Direction.ordinal:()I
      29: iconst_2
      30: iastore
      31: goto          35
      34: astore_1
      35: aload_0
      36: putstatic     #29                 // Field $EnumSwitchMapping$0:[I
      39: return
    Exception table:
       from    to  target type
           7    17    20   Class java/lang/NoSuchFieldError
          21    31    34   Class java/lang/NoSuchFieldError
}

Compiled from "Day10.kt"
public final class aoc2018.Day10$Light$Companion {
  private aoc2018.Day10$Light$Companion();
    Code:
       0: aload_0
       1: invokespecial #8                  // Method java/lang/Object."<init>":()V
       4: return

  public final aoc2018.Day10$Light from(java.lang.String);
    Code:
       0: aload_1
       1: ldc           #15                 // String line
       3: invokestatic  #21                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_1
       7: ldc           #23                 // String <
       9: aconst_null
      10: iconst_2
      11: aconst_null
      12: invokestatic  #29                 // Method kotlin/text/StringsKt.substringAfter$default:(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;ILjava/lang/Object;)Ljava/lang/String;
      15: ldc           #31                 // String >
      17: aconst_null
      18: iconst_2
      19: aconst_null
      20: invokestatic  #34                 // Method kotlin/text/StringsKt.substringBefore$default:(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;ILjava/lang/Object;)Ljava/lang/String;
      23: ldc           #36                 // String ,
      25: invokestatic  #42                 // Method aoc2018/Day10Kt.access$trimAndSplitBy:(Ljava/lang/String;Ljava/lang/String;)Ljava/util/List;
      28: astore_2
      29: aload_2
      30: iconst_0
      31: invokeinterface #48,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      36: checkcast     #50                 // class java/lang/String
      39: astore_3
      40: aload_2
      41: iconst_1
      42: invokeinterface #48,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      47: checkcast     #50                 // class java/lang/String
      50: astore        4
      52: aload_1
      53: ldc           #23                 // String <
      55: aconst_null
      56: iconst_2
      57: aconst_null
      58: invokestatic  #53                 // Method kotlin/text/StringsKt.substringAfterLast$default:(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;ILjava/lang/Object;)Ljava/lang/String;
      61: ldc           #31                 // String >
      63: aconst_null
      64: iconst_2
      65: aconst_null
      66: invokestatic  #56                 // Method kotlin/text/StringsKt.substringBeforeLast$default:(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;ILjava/lang/Object;)Ljava/lang/String;
      69: ldc           #36                 // String ,
      71: invokestatic  #42                 // Method aoc2018/Day10Kt.access$trimAndSplitBy:(Ljava/lang/String;Ljava/lang/String;)Ljava/util/List;
      74: astore        5
      76: aload         5
      78: iconst_0
      79: invokeinterface #48,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      84: checkcast     #50                 // class java/lang/String
      87: astore        6
      89: aload         5
      91: iconst_1
      92: invokeinterface #48,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      97: checkcast     #50                 // class java/lang/String
     100: astore        7
     102: new           #58                 // class aoc2018/Day10$Light
     105: dup
     106: aload_3
     107: invokestatic  #64                 // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
     110: aload         4
     112: invokestatic  #64                 // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
     115: aload         6
     117: invokestatic  #64                 // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
     120: aload         7
     122: invokestatic  #64                 // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
     125: invokespecial #67                 // Method aoc2018/Day10$Light."<init>":(IIII)V
     128: areturn

  public aoc2018.Day10$Light$Companion(kotlin.jvm.internal.DefaultConstructorMarker);
    Code:
       0: aload_0
       1: invokespecial #74                 // Method "<init>":()V
       4: return
}

Compiled from "Day10.kt"
public final class aoc2018.Day10$Direction extends java.lang.Enum<aoc2018.Day10$Direction> {
  public static final aoc2018.Day10$Direction Forward;

  public static final aoc2018.Day10$Direction Backward;

  private static final aoc2018.Day10$Direction[] $VALUES;

  private static final kotlin.enums.EnumEntries $ENTRIES;

  private aoc2018.Day10$Direction();
    Code:
       0: aload_0
       1: aload_1
       2: iload_2
       3: invokespecial #10                 // Method java/lang/Enum."<init>":(Ljava/lang/String;I)V
       6: return

  public static aoc2018.Day10$Direction[] values();
    Code:
       0: getstatic     #22                 // Field $VALUES:[Laoc2018/Day10$Direction;
       3: invokevirtual #28                 // Method java/lang/Object.clone:()Ljava/lang/Object;
       6: checkcast     #29                 // class "[Laoc2018/Day10$Direction;"
       9: areturn

  public static aoc2018.Day10$Direction valueOf(java.lang.String);
    Code:
       0: ldc           #2                  // class aoc2018/Day10$Direction
       2: aload_0
       3: invokestatic  #34                 // Method java/lang/Enum.valueOf:(Ljava/lang/Class;Ljava/lang/String;)Ljava/lang/Enum;
       6: checkcast     #2                  // class aoc2018/Day10$Direction
       9: areturn

  public static kotlin.enums.EnumEntries<aoc2018.Day10$Direction> getEntries();
    Code:
       0: getstatic     #43                 // Field $ENTRIES:Lkotlin/enums/EnumEntries;
       3: areturn

  private static final aoc2018.Day10$Direction[] $values();
    Code:
       0: iconst_2
       1: anewarray     #2                  // class aoc2018/Day10$Direction
       4: astore_0
       5: aload_0
       6: iconst_0
       7: getstatic     #47                 // Field Forward:Laoc2018/Day10$Direction;
      10: aastore
      11: aload_0
      12: iconst_1
      13: getstatic     #50                 // Field Backward:Laoc2018/Day10$Direction;
      16: aastore
      17: aload_0
      18: areturn

  static {};
    Code:
       0: new           #2                  // class aoc2018/Day10$Direction
       3: dup
       4: ldc           #52                 // String Forward
       6: iconst_0
       7: invokespecial #53                 // Method "<init>":(Ljava/lang/String;I)V
      10: putstatic     #47                 // Field Forward:Laoc2018/Day10$Direction;
      13: new           #2                  // class aoc2018/Day10$Direction
      16: dup
      17: ldc           #54                 // String Backward
      19: iconst_1
      20: invokespecial #53                 // Method "<init>":(Ljava/lang/String;I)V
      23: putstatic     #50                 // Field Backward:Laoc2018/Day10$Direction;
      26: invokestatic  #56                 // Method $values:()[Laoc2018/Day10$Direction;
      29: putstatic     #22                 // Field $VALUES:[Laoc2018/Day10$Direction;
      32: getstatic     #22                 // Field $VALUES:[Laoc2018/Day10$Direction;
      35: checkcast     #58                 // class "[Ljava/lang/Enum;"
      38: invokestatic  #64                 // Method kotlin/enums/EnumEntriesKt.enumEntries:([Ljava/lang/Enum;)Lkotlin/enums/EnumEntries;
      41: putstatic     #43                 // Field $ENTRIES:Lkotlin/enums/EnumEntries;
      44: return
}

Compiled from "Day10.kt"
public final class aoc2018.Day10 {
  private final java.util.List<aoc2018.Day10$Light> lights;

  public aoc2018.Day10(java.util.List<java.lang.String>);
    Code:
       0: aload_1
       1: ldc           #10                 // String input
       3: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: invokespecial #19                 // Method java/lang/Object."<init>":()V
      10: aload_0
      11: aload_1
      12: checkcast     #21                 // class java/lang/Iterable
      15: astore_2
      16: getstatic     #27                 // Field aoc2018/Day10$Light.Companion:Laoc2018/Day10$Light$Companion;
      19: astore_3
      20: astore        12
      22: iconst_0
      23: istore        4
      25: aload_2
      26: astore        5
      28: new           #29                 // class java/util/ArrayList
      31: dup
      32: aload_2
      33: bipush        10
      35: invokestatic  #35                 // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
      38: invokespecial #38                 // Method java/util/ArrayList."<init>":(I)V
      41: checkcast     #40                 // class java/util/Collection
      44: astore        6
      46: iconst_0
      47: istore        7
      49: aload         5
      51: invokeinterface #44,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      56: astore        8
      58: aload         8
      60: invokeinterface #50,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      65: ifeq          109
      68: aload         8
      70: invokeinterface #54,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      75: astore        9
      77: aload         6
      79: aload         9
      81: checkcast     #56                 // class java/lang/String
      84: astore        10
      86: astore        13
      88: iconst_0
      89: istore        11
      91: aload_3
      92: aload         10
      94: invokevirtual #62                 // Method aoc2018/Day10$Light$Companion.from:(Ljava/lang/String;)Laoc2018/Day10$Light;
      97: aload         13
      99: swap
     100: invokeinterface #66,  2           // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     105: pop
     106: goto          58
     109: aload         6
     111: checkcast     #68                 // class java/util/List
     114: nop
     115: aload         12
     117: swap
     118: putfield      #72                 // Field lights:Ljava/util/List;
     121: return

  public final java.lang.String solvePart1();
    Code:
       0: new           #91                 // class aoc2018/Day10$Sky
       3: dup
       4: aload_0
       5: getfield      #72                 // Field lights:Ljava/util/List;
       8: invokespecial #93                 // Method aoc2018/Day10$Sky."<init>":(Ljava/util/List;)V
      11: invokevirtual #97                 // Method aoc2018/Day10$Sky.getMessageWithLeastArea:()Lkotlin/Pair;
      14: astore_1
      15: aload_1
      16: invokevirtual #102                // Method kotlin/Pair.getSecond:()Ljava/lang/Object;
      19: checkcast     #104                // class java/lang/Number
      22: invokevirtual #108                // Method java/lang/Number.intValue:()I
      25: istore_2
      26: getstatic     #114                // Field java/lang/System.out:Ljava/io/PrintStream;
      29: iload_2
      30: invokevirtual #119                // Method java/io/PrintStream.println:(I)V
      33: aload_1
      34: invokevirtual #122                // Method kotlin/Pair.getFirst:()Ljava/lang/Object;
      37: checkcast     #56                 // class java/lang/String
      40: areturn

  public final int solvePart2();
    Code:
       0: new           #91                 // class aoc2018/Day10$Sky
       3: dup
       4: aload_0
       5: getfield      #72                 // Field lights:Ljava/util/List;
       8: invokespecial #93                 // Method aoc2018/Day10$Sky."<init>":(Ljava/util/List;)V
      11: invokevirtual #97                 // Method aoc2018/Day10$Sky.getMessageWithLeastArea:()Lkotlin/Pair;
      14: astore_1
      15: aload_1
      16: invokevirtual #102                // Method kotlin/Pair.getSecond:()Ljava/lang/Object;
      19: checkcast     #104                // class java/lang/Number
      22: invokevirtual #108                // Method java/lang/Number.intValue:()I
      25: ireturn
}

Compiled from "Day10.kt"
public final class aoc2018.Day10$Sky {
  private final java.util.List<aoc2018.Day10$Light> lights;

  public aoc2018.Day10$Sky(java.util.List<aoc2018.Day10$Light>);
    Code:
       0: aload_1
       1: ldc           #10                 // String lights
       3: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: invokespecial #19                 // Method java/lang/Object."<init>":()V
      10: aload_0
      11: aload_1
      12: putfield      #22                 // Field lights:Ljava/util/List;
      15: return

  public final java.util.List<aoc2018.Day10$Light> getLights();
    Code:
       0: aload_0
       1: getfield      #22                 // Field lights:Ljava/util/List;
       4: areturn

  public final kotlin.Pair<java.lang.String, java.lang.Integer> getMessageWithLeastArea();
    Code:
       0: ldc2_w        #31                 // long 9223372036854775807l
       3: lstore_1
       4: aload_0
       5: invokespecial #36                 // Method calculateCurrentArea:()J
       8: lstore_3
       9: iconst_0
      10: istore        5
      12: lload_1
      13: lload_3
      14: lcmp
      15: ifle          38
      18: aload_0
      19: aconst_null
      20: iconst_1
      21: aconst_null
      22: invokestatic  #40                 // Method moveLights$default:(Laoc2018/Day10$Sky;Laoc2018/Day10$Direction;ILjava/lang/Object;)V
      25: iinc          5, 1
      28: lload_3
      29: lstore_1
      30: aload_0
      31: invokespecial #36                 // Method calculateCurrentArea:()J
      34: lstore_3
      35: goto          12
      38: aload_0
      39: getstatic     #46                 // Field aoc2018/Day10$Direction.Backward:Laoc2018/Day10$Direction;
      42: invokespecial #50                 // Method moveLights:(Laoc2018/Day10$Direction;)V
      45: aload_0
      46: invokevirtual #54                 // Method toString:()Ljava/lang/String;
      49: iload         5
      51: iconst_1
      52: isub
      53: invokestatic  #60                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      56: invokestatic  #66                 // Method kotlin/TuplesKt.to:(Ljava/lang/Object;Ljava/lang/Object;)Lkotlin/Pair;
      59: areturn

  private final void moveLights(aoc2018.Day10$Direction);
    Code:
       0: aload_0
       1: getfield      #22                 // Field lights:Ljava/util/List;
       4: checkcast     #73                 // class java/lang/Iterable
       7: astore_2
       8: iconst_0
       9: istore_3
      10: aload_2
      11: invokeinterface #77,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      16: astore        4
      18: aload         4
      20: invokeinterface #83,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      25: ifeq          57
      28: aload         4
      30: invokeinterface #87,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      35: astore        5
      37: aload         5
      39: checkcast     #89                 // class aoc2018/Day10$Light
      42: astore        6
      44: iconst_0
      45: istore        7
      47: aload         6
      49: aload_1
      50: invokevirtual #92                 // Method aoc2018/Day10$Light.move:(Laoc2018/Day10$Direction;)V
      53: nop
      54: goto          18
      57: nop
      58: return

  static void moveLights$default(aoc2018.Day10$Sky, aoc2018.Day10$Direction, int, java.lang.Object);
    Code:
       0: iload_2
       1: iconst_1
       2: iand
       3: ifeq          10
       6: getstatic     #104                // Field aoc2018/Day10$Direction.Forward:Laoc2018/Day10$Direction;
       9: astore_1
      10: aload_0
      11: aload_1
      12: invokespecial #50                 // Method moveLights:(Laoc2018/Day10$Direction;)V
      15: return

  private final kotlin.ranges.IntRange getRangeY();
    Code:
       0: aload_0
       1: getfield      #22                 // Field lights:Ljava/util/List;
       4: checkcast     #73                 // class java/lang/Iterable
       7: invokeinterface #77,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      12: astore_2
      13: aload_2
      14: invokeinterface #83,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      19: ifne          30
      22: new           #108                // class java/util/NoSuchElementException
      25: dup
      26: invokespecial #109                // Method java/util/NoSuchElementException."<init>":()V
      29: athrow
      30: aload_2
      31: invokeinterface #87,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      36: checkcast     #89                 // class aoc2018/Day10$Light
      39: astore_3
      40: iconst_0
      41: istore        4
      43: aload_3
      44: invokevirtual #113                // Method aoc2018/Day10$Light.getY:()I
      47: istore_3
      48: aload_2
      49: invokeinterface #83,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      54: ifeq          90
      57: aload_2
      58: invokeinterface #87,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      63: checkcast     #89                 // class aoc2018/Day10$Light
      66: astore        4
      68: iconst_0
      69: istore        5
      71: aload         4
      73: invokevirtual #113                // Method aoc2018/Day10$Light.getY:()I
      76: istore        4
      78: iload_3
      79: iload         4
      81: if_icmple     48
      84: iload         4
      86: istore_3
      87: goto          48
      90: iload_3
      91: aload_0
      92: getfield      #22                 // Field lights:Ljava/util/List;
      95: checkcast     #73                 // class java/lang/Iterable
      98: astore_1
      99: istore        6
     101: aload_1
     102: invokeinterface #77,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     107: astore_2
     108: aload_2
     109: invokeinterface #83,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     114: ifne          125
     117: new           #108                // class java/util/NoSuchElementException
     120: dup
     121: invokespecial #109                // Method java/util/NoSuchElementException."<init>":()V
     124: athrow
     125: aload_2
     126: invokeinterface #87,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     131: checkcast     #89                 // class aoc2018/Day10$Light
     134: astore_3
     135: iconst_0
     136: istore        4
     138: aload_3
     139: invokevirtual #113                // Method aoc2018/Day10$Light.getY:()I
     142: istore_3
     143: aload_2
     144: invokeinterface #83,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     149: ifeq          185
     152: aload_2
     153: invokeinterface #87,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     158: checkcast     #89                 // class aoc2018/Day10$Light
     161: astore        4
     163: iconst_0
     164: istore        5
     166: aload         4
     168: invokevirtual #113                // Method aoc2018/Day10$Light.getY:()I
     171: istore        4
     173: iload_3
     174: iload         4
     176: if_icmpge     143
     179: iload         4
     181: istore_3
     182: goto          143
     185: iload_3
     186: istore        7
     188: iload         6
     190: iload         7
     192: istore        8
     194: istore        9
     196: new           #115                // class kotlin/ranges/IntRange
     199: dup
     200: iload         9
     202: iload         8
     204: invokespecial #118                // Method kotlin/ranges/IntRange."<init>":(II)V
     207: areturn

  private final kotlin.ranges.IntRange getRangeX();
    Code:
       0: aload_0
       1: getfield      #22                 // Field lights:Ljava/util/List;
       4: checkcast     #73                 // class java/lang/Iterable
       7: invokeinterface #77,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      12: astore_2
      13: aload_2
      14: invokeinterface #83,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      19: ifne          30
      22: new           #108                // class java/util/NoSuchElementException
      25: dup
      26: invokespecial #109                // Method java/util/NoSuchElementException."<init>":()V
      29: athrow
      30: aload_2
      31: invokeinterface #87,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      36: checkcast     #89                 // class aoc2018/Day10$Light
      39: astore_3
      40: iconst_0
      41: istore        4
      43: aload_3
      44: invokevirtual #124                // Method aoc2018/Day10$Light.getX:()I
      47: istore_3
      48: aload_2
      49: invokeinterface #83,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      54: ifeq          90
      57: aload_2
      58: invokeinterface #87,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      63: checkcast     #89                 // class aoc2018/Day10$Light
      66: astore        4
      68: iconst_0
      69: istore        5
      71: aload         4
      73: invokevirtual #124                // Method aoc2018/Day10$Light.getX:()I
      76: istore        4
      78: iload_3
      79: iload         4
      81: if_icmple     48
      84: iload         4
      86: istore_3
      87: goto          48
      90: iload_3
      91: aload_0
      92: getfield      #22                 // Field lights:Ljava/util/List;
      95: checkcast     #73                 // class java/lang/Iterable
      98: astore_1
      99: istore        6
     101: aload_1
     102: invokeinterface #77,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     107: astore_2
     108: aload_2
     109: invokeinterface #83,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     114: ifne          125
     117: new           #108                // class java/util/NoSuchElementException
     120: dup
     121: invokespecial #109                // Method java/util/NoSuchElementException."<init>":()V
     124: athrow
     125: aload_2
     126: invokeinterface #87,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     131: checkcast     #89                 // class aoc2018/Day10$Light
     134: astore_3
     135: iconst_0
     136: istore        4
     138: aload_3
     139: invokevirtual #124                // Method aoc2018/Day10$Light.getX:()I
     142: istore_3
     143: aload_2
     144: invokeinterface #83,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     149: ifeq          185
     152: aload_2
     153: invokeinterface #87,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     158: checkcast     #89                 // class aoc2018/Day10$Light
     161: astore        4
     163: iconst_0
     164: istore        5
     166: aload         4
     168: invokevirtual #124                // Method aoc2018/Day10$Light.getX:()I
     171: istore        4
     173: iload_3
     174: iload         4
     176: if_icmpge     143
     179: iload         4
     181: istore_3
     182: goto          143
     185: iload_3
     186: istore        7
     188: iload         6
     190: iload         7
     192: istore        8
     194: istore        9
     196: new           #115                // class kotlin/ranges/IntRange
     199: dup
     200: iload         9
     202: iload         8
     204: invokespecial #118                // Method kotlin/ranges/IntRange."<init>":(II)V
     207: areturn

  private final long calculateCurrentArea();
    Code:
       0: aload_0
       1: invokespecial #128                // Method getRangeX:()Lkotlin/ranges/IntRange;
       4: invokevirtual #131                // Method kotlin/ranges/IntRange.getLast:()I
       7: aload_0
       8: invokespecial #128                // Method getRangeX:()Lkotlin/ranges/IntRange;
      11: invokevirtual #134                // Method kotlin/ranges/IntRange.getFirst:()I
      14: isub
      15: i2l
      16: aload_0
      17: invokespecial #136                // Method getRangeY:()Lkotlin/ranges/IntRange;
      20: invokevirtual #131                // Method kotlin/ranges/IntRange.getLast:()I
      23: aload_0
      24: invokespecial #136                // Method getRangeY:()Lkotlin/ranges/IntRange;
      27: invokevirtual #134                // Method kotlin/ranges/IntRange.getFirst:()I
      30: isub
      31: i2l
      32: lmul
      33: lreturn

  public java.lang.String toString();
    Code:
       0: aload_0
       1: getfield      #22                 // Field lights:Ljava/util/List;
       4: checkcast     #73                 // class java/lang/Iterable
       7: astore_2
       8: iconst_0
       9: istore_3
      10: aload_2
      11: astore        4
      13: new           #138                // class java/util/ArrayList
      16: dup
      17: aload_2
      18: bipush        10
      20: invokestatic  #144                // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
      23: invokespecial #147                // Method java/util/ArrayList."<init>":(I)V
      26: checkcast     #149                // class java/util/Collection
      29: astore        5
      31: iconst_0
      32: istore        6
      34: aload         4
      36: invokeinterface #77,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      41: astore        7
      43: aload         7
      45: invokeinterface #83,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      50: ifeq          107
      53: aload         7
      55: invokeinterface #87,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      60: astore        8
      62: aload         5
      64: aload         8
      66: checkcast     #89                 // class aoc2018/Day10$Light
      69: astore        9
      71: astore        11
      73: iconst_0
      74: istore        10
      76: aload         9
      78: invokevirtual #124                // Method aoc2018/Day10$Light.getX:()I
      81: invokestatic  #60                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      84: aload         9
      86: invokevirtual #113                // Method aoc2018/Day10$Light.getY:()I
      89: invokestatic  #60                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      92: invokestatic  #66                 // Method kotlin/TuplesKt.to:(Ljava/lang/Object;Ljava/lang/Object;)Lkotlin/Pair;
      95: aload         11
      97: swap
      98: invokeinterface #153,  2          // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     103: pop
     104: goto          43
     107: aload         5
     109: checkcast     #155                // class java/util/List
     112: nop
     113: checkcast     #73                 // class java/lang/Iterable
     116: invokestatic  #159                // Method kotlin/collections/CollectionsKt.toSet:(Ljava/lang/Iterable;)Ljava/util/Set;
     119: astore_1
     120: aload_0
     121: invokespecial #136                // Method getRangeY:()Lkotlin/ranges/IntRange;
     124: checkcast     #73                 // class java/lang/Iterable
     127: ldc           #161                // String \n
     129: checkcast     #163                // class java/lang/CharSequence
     132: aconst_null
     133: aconst_null
     134: iconst_0
     135: aconst_null
     136: aload_0
     137: aload_1
     138: invokedynamic #183,  0            // InvokeDynamic #0:invoke:(Laoc2018/Day10$Sky;Ljava/util/Set;)Lkotlin/jvm/functions/Function1;
     143: bipush        30
     145: aconst_null
     146: invokestatic  #187                // Method kotlin/collections/CollectionsKt.joinToString$default:(Ljava/lang/Iterable;Ljava/lang/CharSequence;Ljava/lang/CharSequence;Ljava/lang/CharSequence;ILjava/lang/CharSequence;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Ljava/lang/String;
     149: areturn

  public final java.util.List<aoc2018.Day10$Light> component1();
    Code:
       0: aload_0
       1: getfield      #22                 // Field lights:Ljava/util/List;
       4: areturn

  public final aoc2018.Day10$Sky copy(java.util.List<aoc2018.Day10$Light>);
    Code:
       0: aload_1
       1: ldc           #10                 // String lights
       3: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: new           #2                  // class aoc2018/Day10$Sky
       9: dup
      10: aload_1
      11: invokespecial #203                // Method "<init>":(Ljava/util/List;)V
      14: areturn

  public static aoc2018.Day10$Sky copy$default(aoc2018.Day10$Sky, java.util.List, int, java.lang.Object);
    Code:
       0: iload_2
       1: iconst_1
       2: iand
       3: ifeq          11
       6: aload_0
       7: getfield      #22                 // Field lights:Ljava/util/List;
      10: astore_1
      11: aload_0
      12: aload_1
      13: invokevirtual #207                // Method copy:(Ljava/util/List;)Laoc2018/Day10$Sky;
      16: areturn

  public int hashCode();
    Code:
       0: aload_0
       1: getfield      #22                 // Field lights:Ljava/util/List;
       4: invokevirtual #210                // Method java/lang/Object.hashCode:()I
       7: ireturn

  public boolean equals(java.lang.Object);
    Code:
       0: aload_0
       1: aload_1
       2: if_acmpne     7
       5: iconst_1
       6: ireturn
       7: aload_1
       8: instanceof    #2                  // class aoc2018/Day10$Sky
      11: ifne          16
      14: iconst_0
      15: ireturn
      16: aload_1
      17: checkcast     #2                  // class aoc2018/Day10$Sky
      20: astore_2
      21: aload_0
      22: getfield      #22                 // Field lights:Ljava/util/List;
      25: aload_2
      26: getfield      #22                 // Field lights:Ljava/util/List;
      29: invokestatic  #216                // Method kotlin/jvm/internal/Intrinsics.areEqual:(Ljava/lang/Object;Ljava/lang/Object;)Z
      32: ifne          37
      35: iconst_0
      36: ireturn
      37: iconst_1
      38: ireturn

  private static final java.lang.CharSequence toString$lambda$7(aoc2018.Day10$Sky, java.util.Set, int);
    Code:
       0: aload_0
       1: invokespecial #128                // Method getRangeX:()Lkotlin/ranges/IntRange;
       4: checkcast     #73                 // class java/lang/Iterable
       7: astore_3
       8: iconst_0
       9: istore        4
      11: aload_3
      12: astore        5
      14: new           #138                // class java/util/ArrayList
      17: dup
      18: aload_3
      19: bipush        10
      21: invokestatic  #144                // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
      24: invokespecial #147                // Method java/util/ArrayList."<init>":(I)V
      27: checkcast     #149                // class java/util/Collection
      30: astore        6
      32: iconst_0
      33: istore        7
      35: aload         5
      37: invokeinterface #77,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      42: astore        8
      44: aload         8
      46: invokeinterface #83,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      51: ifeq          118
      54: aload         8
      56: checkcast     #219                // class kotlin/collections/IntIterator
      59: invokevirtual #222                // Method kotlin/collections/IntIterator.nextInt:()I
      62: istore        9
      64: aload         6
      66: iload         9
      68: istore        10
      70: astore        12
      72: iconst_0
      73: istore        11
      75: aload_1
      76: iload         10
      78: invokestatic  #60                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      81: iload_2
      82: invokestatic  #60                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      85: invokestatic  #66                 // Method kotlin/TuplesKt.to:(Ljava/lang/Object;Ljava/lang/Object;)Lkotlin/Pair;
      88: invokeinterface #227,  2          // InterfaceMethod java/util/Set.contains:(Ljava/lang/Object;)Z
      93: ifeq          101
      96: bipush        35
      98: goto          103
     101: bipush        46
     103: invokestatic  #232                // Method java/lang/Character.valueOf:(C)Ljava/lang/Character;
     106: aload         12
     108: swap
     109: invokeinterface #153,  2          // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     114: pop
     115: goto          44
     118: aload         6
     120: checkcast     #155                // class java/util/List
     123: nop
     124: checkcast     #73                 // class java/lang/Iterable
     127: ldc           #234                // String
     129: checkcast     #163                // class java/lang/CharSequence
     132: aconst_null
     133: aconst_null
     134: iconst_0
     135: aconst_null
     136: aconst_null
     137: bipush        62
     139: aconst_null
     140: invokestatic  #187                // Method kotlin/collections/CollectionsKt.joinToString$default:(Ljava/lang/Iterable;Ljava/lang/CharSequence;Ljava/lang/CharSequence;Ljava/lang/CharSequence;ILjava/lang/CharSequence;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Ljava/lang/String;
     143: checkcast     #163                // class java/lang/CharSequence
     146: areturn
}

Compiled from "Day10.kt"
public final class aoc2018.Day10$Light {
  public static final aoc2018.Day10$Light$Companion Companion;

  private int x;

  private int y;

  private final int velocityX;

  private final int velocityY;

  public aoc2018.Day10$Light(int, int, int, int);
    Code:
       0: aload_0
       1: invokespecial #9                  // Method java/lang/Object."<init>":()V
       4: aload_0
       5: iload_1
       6: putfield      #13                 // Field x:I
       9: aload_0
      10: iload_2
      11: putfield      #16                 // Field y:I
      14: aload_0
      15: iload_3
      16: putfield      #19                 // Field velocityX:I
      19: aload_0
      20: iload         4
      22: putfield      #22                 // Field velocityY:I
      25: return

  public final int getX();
    Code:
       0: aload_0
       1: getfield      #13                 // Field x:I
       4: ireturn

  public final void setX(int);
    Code:
       0: aload_0
       1: iload_1
       2: putfield      #13                 // Field x:I
       5: return

  public final int getY();
    Code:
       0: aload_0
       1: getfield      #16                 // Field y:I
       4: ireturn

  public final void setY(int);
    Code:
       0: aload_0
       1: iload_1
       2: putfield      #16                 // Field y:I
       5: return

  public final int getVelocityX();
    Code:
       0: aload_0
       1: getfield      #19                 // Field velocityX:I
       4: ireturn

  public final int getVelocityY();
    Code:
       0: aload_0
       1: getfield      #22                 // Field velocityY:I
       4: ireturn

  public final void move(aoc2018.Day10$Direction);
    Code:
       0: aload_1
       1: ldc           #38                 // String direction
       3: invokestatic  #44                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_1
       7: getstatic     #50                 // Field aoc2018/Day10$Light$WhenMappings.$EnumSwitchMapping$0:[I
      10: swap
      11: invokevirtual #55                 // Method aoc2018/Day10$Direction.ordinal:()I
      14: iaload
      15: tableswitch   { // 1 to 2
                     1: 36
                     2: 65
               default: 94
          }
      36: aload_0
      37: aload_0
      38: getfield      #13                 // Field x:I
      41: aload_0
      42: getfield      #19                 // Field velocityX:I
      45: iadd
      46: putfield      #13                 // Field x:I
      49: aload_0
      50: aload_0
      51: getfield      #16                 // Field y:I
      54: aload_0
      55: getfield      #22                 // Field velocityY:I
      58: iadd
      59: putfield      #16                 // Field y:I
      62: goto          102
      65: aload_0
      66: aload_0
      67: getfield      #13                 // Field x:I
      70: aload_0
      71: getfield      #19                 // Field velocityX:I
      74: isub
      75: putfield      #13                 // Field x:I
      78: aload_0
      79: aload_0
      80: getfield      #16                 // Field y:I
      83: aload_0
      84: getfield      #22                 // Field velocityY:I
      87: isub
      88: putfield      #16                 // Field y:I
      91: goto          102
      94: new           #57                 // class kotlin/NoWhenBranchMatchedException
      97: dup
      98: invokespecial #58                 // Method kotlin/NoWhenBranchMatchedException."<init>":()V
     101: athrow
     102: return

  public final int component1();
    Code:
       0: aload_0
       1: getfield      #13                 // Field x:I
       4: ireturn

  public final int component2();
    Code:
       0: aload_0
       1: getfield      #16                 // Field y:I
       4: ireturn

  public final int component3();
    Code:
       0: aload_0
       1: getfield      #19                 // Field velocityX:I
       4: ireturn

  public final int component4();
    Code:
       0: aload_0
       1: getfield      #22                 // Field velocityY:I
       4: ireturn

  public final aoc2018.Day10$Light copy(int, int, int, int);
    Code:
       0: new           #2                  // class aoc2018/Day10$Light
       3: dup
       4: iload_1
       5: iload_2
       6: iload_3
       7: iload         4
       9: invokespecial #67                 // Method "<init>":(IIII)V
      12: areturn

  public static aoc2018.Day10$Light copy$default(aoc2018.Day10$Light, int, int, int, int, int, java.lang.Object);
    Code:
       0: iload         5
       2: iconst_1
       3: iand
       4: ifeq          12
       7: aload_0
       8: getfield      #13                 // Field x:I
      11: istore_1
      12: iload         5
      14: iconst_2
      15: iand
      16: ifeq          24
      19: aload_0
      20: getfield      #16                 // Field y:I
      23: istore_2
      24: iload         5
      26: iconst_4
      27: iand
      28: ifeq          36
      31: aload_0
      32: getfield      #19                 // Field velocityX:I
      35: istore_3
      36: iload         5
      38: bipush        8
      40: iand
      41: ifeq          50
      44: aload_0
      45: getfield      #22                 // Field velocityY:I
      48: istore        4
      50: aload_0
      51: iload_1
      52: iload_2
      53: iload_3
      54: iload         4
      56: invokevirtual #71                 // Method copy:(IIII)Laoc2018/Day10$Light;
      59: areturn

  public java.lang.String toString();
    Code:
       0: new           #75                 // class java/lang/StringBuilder
       3: dup
       4: invokespecial #76                 // Method java/lang/StringBuilder."<init>":()V
       7: ldc           #78                 // String Light(x=
       9: invokevirtual #82                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      12: aload_0
      13: getfield      #13                 // Field x:I
      16: invokevirtual #85                 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
      19: ldc           #87                 // String , y=
      21: invokevirtual #82                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      24: aload_0
      25: getfield      #16                 // Field y:I
      28: invokevirtual #85                 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
      31: ldc           #89                 // String , velocityX=
      33: invokevirtual #82                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      36: aload_0
      37: getfield      #19                 // Field velocityX:I
      40: invokevirtual #85                 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
      43: ldc           #91                 // String , velocityY=
      45: invokevirtual #82                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      48: aload_0
      49: getfield      #22                 // Field velocityY:I
      52: invokevirtual #85                 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
      55: bipush        41
      57: invokevirtual #94                 // Method java/lang/StringBuilder.append:(C)Ljava/lang/StringBuilder;
      60: invokevirtual #96                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
      63: areturn

  public int hashCode();
    Code:
       0: aload_0
       1: getfield      #13                 // Field x:I
       4: invokestatic  #102                // Method java/lang/Integer.hashCode:(I)I
       7: istore_1
       8: iload_1
       9: bipush        31
      11: imul
      12: aload_0
      13: getfield      #16                 // Field y:I
      16: invokestatic  #102                // Method java/lang/Integer.hashCode:(I)I
      19: iadd
      20: istore_1
      21: iload_1
      22: bipush        31
      24: imul
      25: aload_0
      26: getfield      #19                 // Field velocityX:I
      29: invokestatic  #102                // Method java/lang/Integer.hashCode:(I)I
      32: iadd
      33: istore_1
      34: iload_1
      35: bipush        31
      37: imul
      38: aload_0
      39: getfield      #22                 // Field velocityY:I
      42: invokestatic  #102                // Method java/lang/Integer.hashCode:(I)I
      45: iadd
      46: istore_1
      47: iload_1
      48: ireturn

  public boolean equals(java.lang.Object);
    Code:
       0: aload_0
       1: aload_1
       2: if_acmpne     7
       5: iconst_1
       6: ireturn
       7: aload_1
       8: instanceof    #2                  // class aoc2018/Day10$Light
      11: ifne          16
      14: iconst_0
      15: ireturn
      16: aload_1
      17: checkcast     #2                  // class aoc2018/Day10$Light
      20: astore_2
      21: aload_0
      22: getfield      #13                 // Field x:I
      25: aload_2
      26: getfield      #13                 // Field x:I
      29: if_icmpeq     34
      32: iconst_0
      33: ireturn
      34: aload_0
      35: getfield      #16                 // Field y:I
      38: aload_2
      39: getfield      #16                 // Field y:I
      42: if_icmpeq     47
      45: iconst_0
      46: ireturn
      47: aload_0
      48: getfield      #19                 // Field velocityX:I
      51: aload_2
      52: getfield      #19                 // Field velocityX:I
      55: if_icmpeq     60
      58: iconst_0
      59: ireturn
      60: aload_0
      61: getfield      #22                 // Field velocityY:I
      64: aload_2
      65: getfield      #22                 // Field velocityY:I
      68: if_icmpeq     73
      71: iconst_0
      72: ireturn
      73: iconst_1
      74: ireturn

  static {};
    Code:
       0: new           #111                // class aoc2018/Day10$Light$Companion
       3: dup
       4: aconst_null
       5: invokespecial #114                // Method aoc2018/Day10$Light$Companion."<init>":(Lkotlin/jvm/internal/DefaultConstructorMarker;)V
       8: putstatic     #118                // Field Companion:Laoc2018/Day10$Light$Companion;
      11: return
}

Compiled from "Day10.kt"
public final class aoc2018.Day10Kt {
  private static final java.util.List<java.lang.String> trimAndSplitBy(java.lang.String, java.lang.String);
    Code:
       0: aload_0
       1: checkcast     #9                  // class java/lang/CharSequence
       4: astore_2
       5: new           #11                 // class kotlin/text/Regex
       8: dup
       9: ldc           #13                 // String \\s
      11: invokespecial #17                 // Method kotlin/text/Regex."<init>":(Ljava/lang/String;)V
      14: astore_3
      15: ldc           #19                 // String
      17: astore        4
      19: aload_3
      20: aload_2
      21: aload         4
      23: invokevirtual #23                 // Method kotlin/text/Regex.replace:(Ljava/lang/CharSequence;Ljava/lang/String;)Ljava/lang/String;
      26: checkcast     #9                  // class java/lang/CharSequence
      29: iconst_1
      30: anewarray     #25                 // class java/lang/String
      33: astore_2
      34: aload_2
      35: iconst_0
      36: aload_1
      37: aastore
      38: aload_2
      39: iconst_0
      40: iconst_0
      41: bipush        6
      43: aconst_null
      44: invokestatic  #31                 // Method kotlin/text/StringsKt.split$default:(Ljava/lang/CharSequence;[Ljava/lang/String;ZIILjava/lang/Object;)Ljava/util/List;
      47: areturn

  public static final java.util.List access$trimAndSplitBy(java.lang.String, java.lang.String);
    Code:
       0: aload_0
       1: aload_1
       2: invokestatic  #37                 // Method trimAndSplitBy:(Ljava/lang/String;Ljava/lang/String;)Ljava/util/List;
       5: areturn
}


package com.jackchapman.codingchallenges.hackerrank

import kotlin.math.abs

/**
 * Problem: [Queen's Attack II](https://www.hackerrank.com/challenges/queens-attack-2/)
 *
 * Given a chess board and a list of obstacles, find the number of valid moves a queen can make
 * @param size The size of the chess board
 * @param k The number of obstacles on the board
 * @param obstacles An array of obstacles represented as an array of two integers (it's y and x coordinate)
 * @return The number of valid moves a queen could make
 */
fun queensAttack(size: Int, k: Int, y: Int, x: Int, obstacles: Array<IntArray>): Int {
    val closest = obstacles.asSequence()
        .filter { it[0] == y || it[1] == x || abs(it[0] - y) == abs(it[1] - x) }
        .groupBy { (if (it[0] > y) 1 else if (it[0] < y) 2 else 0) or (if (it[1] > x) 4 else if (it[1] < x) 8 else 0) }
        .mapValues { (dir, arr) ->
            when {
                dir and 1 != 0 -> arr.minByOrNull { it[0] }!!
                dir and 2 != 0 -> arr.maxByOrNull { it[0] }!!
                dir and 4 != 0 -> arr.minByOrNull { it[1] }!!
                dir and 8 != 0 -> arr.maxByOrNull { it[1] }!!
                else -> throw IllegalStateException()
            }
        }

    return arrayOf(1, 2, 4, 5, 6, 8, 9, 10).sumOf {
        val obstacle = closest[it] ?: return@sumOf minOf(
            if (it and 1 != 0) size - y else Int.MAX_VALUE,
            if (it and 2 != 0) y - 1 else Int.MAX_VALUE,
            if (it and 4 != 0) size - x else Int.MAX_VALUE,
            if (it and 8 != 0) x - 1 else Int.MAX_VALUE,
        )
        maxOf(abs(obstacle[0] - y), abs(obstacle[1] - x)) - 1
    }
}

Compiled from "QueensAttack.kt"
public final class com.jackchapman.codingchallenges.hackerrank.QueensAttackKt {
  public static final int queensAttack(int, int, int, int, int[][]);
    Code:
       0: aload         4
       2: ldc           #9                  // String obstacles
       4: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       7: aload         4
       9: checkcast     #17                 // class "[Ljava/lang/Object;"
      12: invokestatic  #23                 // Method kotlin/collections/ArraysKt.asSequence:([Ljava/lang/Object;)Lkotlin/sequences/Sequence;
      15: iload_2
      16: iload_3
      17: invokedynamic #43,  0             // InvokeDynamic #0:invoke:(II)Lkotlin/jvm/functions/Function1;
      22: invokestatic  #49                 // Method kotlin/sequences/SequencesKt.filter:(Lkotlin/sequences/Sequence;Lkotlin/jvm/functions/Function1;)Lkotlin/sequences/Sequence;
      25: astore        6
      27: nop
      28: iconst_0
      29: istore        7
      31: aload         6
      33: astore        8
      35: new           #51                 // class java/util/LinkedHashMap
      38: dup
      39: invokespecial #55                 // Method java/util/LinkedHashMap."<init>":()V
      42: checkcast     #57                 // class java/util/Map
      45: astore        9
      47: iconst_0
      48: istore        10
      50: aload         8
      52: invokeinterface #63,  1           // InterfaceMethod kotlin/sequences/Sequence.iterator:()Ljava/util/Iterator;
      57: astore        11
      59: aload         11
      61: invokeinterface #69,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      66: ifeq          221
      69: aload         11
      71: invokeinterface #73,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      76: astore        12
      78: aload         12
      80: checkcast     #75                 // class "[I"
      83: astore        13
      85: iconst_0
      86: istore        14
      88: aload         13
      90: iconst_0
      91: iaload
      92: iload_2
      93: if_icmple     100
      96: iconst_1
      97: goto          113
     100: aload         13
     102: iconst_0
     103: iaload
     104: iload_2
     105: if_icmpge     112
     108: iconst_2
     109: goto          113
     112: iconst_0
     113: aload         13
     115: iconst_1
     116: iaload
     117: iload_3
     118: if_icmple     125
     121: iconst_4
     122: goto          139
     125: aload         13
     127: iconst_1
     128: iaload
     129: iload_3
     130: if_icmpge     138
     133: bipush        8
     135: goto          139
     138: iconst_0
     139: ior
     140: invokestatic  #81                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     143: astore        15
     145: aload         9
     147: astore        16
     149: iconst_0
     150: istore        17
     152: aload         16
     154: aload         15
     156: invokeinterface #84,  2           // InterfaceMethod java/util/Map.get:(Ljava/lang/Object;)Ljava/lang/Object;
     161: astore        18
     163: aload         18
     165: ifnonnull     200
     168: iconst_0
     169: istore        19
     171: new           #86                 // class java/util/ArrayList
     174: dup
     175: invokespecial #87                 // Method java/util/ArrayList."<init>":()V
     178: checkcast     #89                 // class java/util/List
     181: astore        19
     183: aload         16
     185: aload         15
     187: aload         19
     189: invokeinterface #93,  3           // InterfaceMethod java/util/Map.put:(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
     194: pop
     195: aload         19
     197: goto          202
     200: aload         18
     202: nop
     203: checkcast     #89                 // class java/util/List
     206: astore        20
     208: aload         20
     210: aload         12
     212: invokeinterface #97,  2           // InterfaceMethod java/util/List.add:(Ljava/lang/Object;)Z
     217: pop
     218: goto          59
     221: aload         9
     223: nop
     224: astore        6
     226: nop
     227: iconst_0
     228: istore        7
     230: aload         6
     232: astore        8
     234: new           #51                 // class java/util/LinkedHashMap
     237: dup
     238: aload         6
     240: invokeinterface #101,  1          // InterfaceMethod java/util/Map.size:()I
     245: invokestatic  #107                // Method kotlin/collections/MapsKt.mapCapacity:(I)I
     248: invokespecial #110                // Method java/util/LinkedHashMap."<init>":(I)V
     251: checkcast     #57                 // class java/util/Map
     254: astore        9
     256: iconst_0
     257: istore        10
     259: aload         8
     261: invokeinterface #114,  1          // InterfaceMethod java/util/Map.entrySet:()Ljava/util/Set;
     266: checkcast     #116                // class java/lang/Iterable
     269: astore        11
     271: iconst_0
     272: istore        12
     274: aload         11
     276: invokeinterface #117,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     281: astore        13
     283: aload         13
     285: invokeinterface #69,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     290: ifeq          963
     293: aload         13
     295: invokeinterface #73,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     300: astore        14
     302: aload         9
     304: aload         14
     306: checkcast     #119                // class java/util/Map$Entry
     309: astore        15
     311: astore        16
     313: iconst_0
     314: istore        17
     316: aload         15
     318: invokeinterface #122,  1          // InterfaceMethod java/util/Map$Entry.getKey:()Ljava/lang/Object;
     323: aload         16
     325: swap
     326: aload         14
     328: checkcast     #119                // class java/util/Map$Entry
     331: astore        18
     333: astore        31
     335: astore        30
     337: iconst_0
     338: istore        19
     340: aload         18
     342: invokeinterface #122,  1          // InterfaceMethod java/util/Map$Entry.getKey:()Ljava/lang/Object;
     347: checkcast     #124                // class java/lang/Number
     350: invokevirtual #127                // Method java/lang/Number.intValue:()I
     353: istore        20
     355: aload         18
     357: invokeinterface #130,  1          // InterfaceMethod java/util/Map$Entry.getValue:()Ljava/lang/Object;
     362: checkcast     #89                 // class java/util/List
     365: astore        21
     367: nop
     368: iload         20
     370: iconst_1
     371: iand
     372: ifeq          510
     375: aload         21
     377: checkcast     #116                // class java/lang/Iterable
     380: astore        22
     382: iconst_0
     383: istore        23
     385: aload         22
     387: invokeinterface #117,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     392: astore        24
     394: aload         24
     396: invokeinterface #69,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     401: ifne          408
     404: aconst_null
     405: goto          500
     408: aload         24
     410: invokeinterface #73,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     415: astore        25
     417: aload         24
     419: invokeinterface #69,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     424: ifne          432
     427: aload         25
     429: goto          500
     432: aload         25
     434: checkcast     #75                 // class "[I"
     437: astore        26
     439: iconst_0
     440: istore        27
     442: aload         26
     444: iconst_0
     445: iaload
     446: istore        26
     448: aload         24
     450: invokeinterface #73,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     455: astore        27
     457: aload         27
     459: checkcast     #75                 // class "[I"
     462: astore        28
     464: iconst_0
     465: istore        29
     467: aload         28
     469: iconst_0
     470: iaload
     471: istore        28
     473: iload         26
     475: iload         28
     477: if_icmple     488
     480: aload         27
     482: astore        25
     484: iload         28
     486: istore        26
     488: aload         24
     490: invokeinterface #69,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     495: ifne          448
     498: aload         25
     500: dup
     501: invokestatic  #134                // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
     504: checkcast     #75                 // class "[I"
     507: goto          945
     510: iload         20
     512: iconst_2
     513: iand
     514: ifeq          652
     517: aload         21
     519: checkcast     #116                // class java/lang/Iterable
     522: astore        22
     524: iconst_0
     525: istore        23
     527: aload         22
     529: invokeinterface #117,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     534: astore        24
     536: aload         24
     538: invokeinterface #69,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     543: ifne          550
     546: aconst_null
     547: goto          642
     550: aload         24
     552: invokeinterface #73,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     557: astore        25
     559: aload         24
     561: invokeinterface #69,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     566: ifne          574
     569: aload         25
     571: goto          642
     574: aload         25
     576: checkcast     #75                 // class "[I"
     579: astore        26
     581: iconst_0
     582: istore        27
     584: aload         26
     586: iconst_0
     587: iaload
     588: istore        26
     590: aload         24
     592: invokeinterface #73,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     597: astore        27
     599: aload         27
     601: checkcast     #75                 // class "[I"
     604: astore        28
     606: iconst_0
     607: istore        29
     609: aload         28
     611: iconst_0
     612: iaload
     613: istore        28
     615: iload         26
     617: iload         28
     619: if_icmpge     630
     622: aload         27
     624: astore        25
     626: iload         28
     628: istore        26
     630: aload         24
     632: invokeinterface #69,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     637: ifne          590
     640: aload         25
     642: dup
     643: invokestatic  #134                // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
     646: checkcast     #75                 // class "[I"
     649: goto          945
     652: iload         20
     654: iconst_4
     655: iand
     656: ifeq          794
     659: aload         21
     661: checkcast     #116                // class java/lang/Iterable
     664: astore        22
     666: iconst_0
     667: istore        23
     669: aload         22
     671: invokeinterface #117,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     676: astore        24
     678: aload         24
     680: invokeinterface #69,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     685: ifne          692
     688: aconst_null
     689: goto          784
     692: aload         24
     694: invokeinterface #73,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     699: astore        25
     701: aload         24
     703: invokeinterface #69,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     708: ifne          716
     711: aload         25
     713: goto          784
     716: aload         25
     718: checkcast     #75                 // class "[I"
     721: astore        26
     723: iconst_0
     724: istore        27
     726: aload         26
     728: iconst_1
     729: iaload
     730: istore        26
     732: aload         24
     734: invokeinterface #73,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     739: astore        27
     741: aload         27
     743: checkcast     #75                 // class "[I"
     746: astore        28
     748: iconst_0
     749: istore        29
     751: aload         28
     753: iconst_1
     754: iaload
     755: istore        28
     757: iload         26
     759: iload         28
     761: if_icmple     772
     764: aload         27
     766: astore        25
     768: iload         28
     770: istore        26
     772: aload         24
     774: invokeinterface #69,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     779: ifne          732
     782: aload         25
     784: dup
     785: invokestatic  #134                // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
     788: checkcast     #75                 // class "[I"
     791: goto          945
     794: iload         20
     796: bipush        8
     798: iand
     799: ifeq          937
     802: aload         21
     804: checkcast     #116                // class java/lang/Iterable
     807: astore        22
     809: iconst_0
     810: istore        23
     812: aload         22
     814: invokeinterface #117,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     819: astore        24
     821: aload         24
     823: invokeinterface #69,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     828: ifne          835
     831: aconst_null
     832: goto          927
     835: aload         24
     837: invokeinterface #73,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     842: astore        25
     844: aload         24
     846: invokeinterface #69,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     851: ifne          859
     854: aload         25
     856: goto          927
     859: aload         25
     861: checkcast     #75                 // class "[I"
     864: astore        26
     866: iconst_0
     867: istore        27
     869: aload         26
     871: iconst_1
     872: iaload
     873: istore        26
     875: aload         24
     877: invokeinterface #73,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     882: astore        27
     884: aload         27
     886: checkcast     #75                 // class "[I"
     889: astore        28
     891: iconst_0
     892: istore        29
     894: aload         28
     896: iconst_1
     897: iaload
     898: istore        28
     900: iload         26
     902: iload         28
     904: if_icmpge     915
     907: aload         27
     909: astore        25
     911: iload         28
     913: istore        26
     915: aload         24
     917: invokeinterface #69,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     922: ifne          875
     925: aload         25
     927: dup
     928: invokestatic  #134                // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
     931: checkcast     #75                 // class "[I"
     934: goto          945
     937: new           #136                // class java/lang/IllegalStateException
     940: dup
     941: invokespecial #137                // Method java/lang/IllegalStateException."<init>":()V
     944: athrow
     945: nop
     946: astore        32
     948: aload         30
     950: aload         31
     952: aload         32
     954: invokeinterface #93,  3           // InterfaceMethod java/util/Map.put:(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
     959: pop
     960: goto          283
     963: aload         9
     965: nop
     966: nop
     967: astore        5
     969: bipush        8
     971: anewarray     #77                 // class java/lang/Integer
     974: astore        6
     976: aload         6
     978: iconst_0
     979: iconst_1
     980: invokestatic  #81                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     983: aastore
     984: aload         6
     986: iconst_1
     987: iconst_2
     988: invokestatic  #81                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     991: aastore
     992: aload         6
     994: iconst_2
     995: iconst_4
     996: invokestatic  #81                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     999: aastore
    1000: aload         6
    1002: iconst_3
    1003: iconst_5
    1004: invokestatic  #81                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
    1007: aastore
    1008: aload         6
    1010: iconst_4
    1011: bipush        6
    1013: invokestatic  #81                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
    1016: aastore
    1017: aload         6
    1019: iconst_5
    1020: bipush        8
    1022: invokestatic  #81                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
    1025: aastore
    1026: aload         6
    1028: bipush        6
    1030: bipush        9
    1032: invokestatic  #81                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
    1035: aastore
    1036: aload         6
    1038: bipush        7
    1040: bipush        10
    1042: invokestatic  #81                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
    1045: aastore
    1046: aload         6
    1048: astore        6
    1050: iconst_0
    1051: istore        7
    1053: iconst_0
    1054: istore        8
    1056: aload         6
    1058: arraylength
    1059: istore        9
    1061: iload         8
    1063: iload         9
    1065: if_icmpge     1238
    1068: aload         6
    1070: iload         8
    1072: aaload
    1073: astore        10
    1075: iload         7
    1077: aload         10
    1079: checkcast     #124                // class java/lang/Number
    1082: invokevirtual #127                // Method java/lang/Number.intValue:()I
    1085: istore        11
    1087: istore        30
    1089: iconst_0
    1090: istore        12
    1092: aload         5
    1094: iload         11
    1096: invokestatic  #81                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
    1099: invokeinterface #84,  2           // InterfaceMethod java/util/Map.get:(Ljava/lang/Object;)Ljava/lang/Object;
    1104: checkcast     #75                 // class "[I"
    1107: dup
    1108: ifnonnull     1198
    1111: pop
    1112: iload         11
    1114: iconst_1
    1115: iand
    1116: ifeq          1125
    1119: iload_0
    1120: iload_2
    1121: isub
    1122: goto          1127
    1125: ldc           #138                // int 2147483647
    1127: iconst_3
    1128: newarray       int
    1130: astore        13
    1132: aload         13
    1134: iconst_0
    1135: iload         11
    1137: iconst_2
    1138: iand
    1139: ifeq          1148
    1142: iload_2
    1143: iconst_1
    1144: isub
    1145: goto          1150
    1148: ldc           #138                // int 2147483647
    1150: iastore
    1151: aload         13
    1153: iconst_1
    1154: iload         11
    1156: iconst_4
    1157: iand
    1158: ifeq          1167
    1161: iload_0
    1162: iload_3
    1163: isub
    1164: goto          1169
    1167: ldc           #138                // int 2147483647
    1169: iastore
    1170: aload         13
    1172: iconst_2
    1173: iload         11
    1175: bipush        8
    1177: iand
    1178: ifeq          1187
    1181: iload_3
    1182: iconst_1
    1183: isub
    1184: goto          1189
    1187: ldc           #138                // int 2147483647
    1189: iastore
    1190: aload         13
    1192: invokestatic  #144                // Method kotlin/comparisons/ComparisonsKt.minOf:(I[I)I
    1195: goto          1223
    1198: astore        14
    1200: aload         14
    1202: iconst_0
    1203: iaload
    1204: iload_2
    1205: isub
    1206: invokestatic  #149                // Method java/lang/Math.abs:(I)I
    1209: aload         14
    1211: iconst_1
    1212: iaload
    1213: iload_3
    1214: isub
    1215: invokestatic  #149                // Method java/lang/Math.abs:(I)I
    1218: invokestatic  #153                // Method java/lang/Math.max:(II)I
    1221: iconst_1
    1222: isub
    1223: istore        31
    1225: iload         30
    1227: iload         31
    1229: iadd
    1230: istore        7
    1232: iinc          8, 1
    1235: goto          1061
    1238: iload         7
    1240: ireturn

  private static final boolean queensAttack$lambda$0(int, int, int[]);
    Code:
       0: aload_2
       1: ldc           #214                // String it
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_2
       7: iconst_0
       8: iaload
       9: iload_0
      10: if_icmpeq     39
      13: aload_2
      14: iconst_1
      15: iaload
      16: iload_1
      17: if_icmpeq     39
      20: aload_2
      21: iconst_0
      22: iaload
      23: iload_0
      24: isub
      25: invokestatic  #149                // Method java/lang/Math.abs:(I)I
      28: aload_2
      29: iconst_1
      30: iaload
      31: iload_1
      32: isub
      33: invokestatic  #149                // Method java/lang/Math.abs:(I)I
      36: if_icmpne     43
      39: iconst_1
      40: goto          44
      43: iconst_0
      44: ireturn
}