import kotlin.math.abs

object Day15 {
    const val EXPECTED_PART1_CHECK_ANSWER = 26
    const val EXPECTED_PART2_CHECK_ANSWER = 56000011L

    const val PART1_CHECK_ROW = 10
    const val PART1_ROW = 2_000_000
    const val PART2_CHECK_MAX = 20
    const val PART2_MAX = 4_000_000
    const val PART2_MULTIPLY_VALUE = 4_000_000
}

fun main() {

    fun String.parseSensorAndBeaconCoordinatess(): Pair<Coordinates, Coordinates> {
        val (sensorX, sensorY, beaconX, beaconY) = Regex("Sensor at x=([-\\d]+), y=([-\\d]+): closest beacon is at x=([-\\d]+), y=([-\\d]+)")
            .matchEntire(this)
            ?.destructured ?: error("Should match regex")
        return Coordinates(sensorX.toInt(), sensorY.toInt()) to Coordinates(beaconX.toInt(), beaconY.toInt())
    }

    data class RowInfo(val positionsCovered: Int, val notCovered: Coordinates?)

    fun rowInfo(
        row: Int,
        gridMin: Int,
        gridMax: Int,
        sensorAndBeaconCoordinates: List<Pair<Coordinates, Coordinates>>
    ): RowInfo {
        val coveredRangesOnRow = mutableListOf<IntRange>()
        for (sensorAndBeaconCoordinate in sensorAndBeaconCoordinates) {
            val sensorLocation = sensorAndBeaconCoordinate.first
            val beaconLocation = sensorAndBeaconCoordinate.second
            val distance = abs(sensorLocation.x - beaconLocation.x) + abs(sensorLocation.y - beaconLocation.y)
            val positionsCoveredAtSensorRow = distance * 2 + 1
            val yDistanceToCheckRow = abs(sensorLocation.y - row)
            val positionsCoveredAtCheckRow = positionsCoveredAtSensorRow - yDistanceToCheckRow * 2
            if (positionsCoveredAtCheckRow > 0) {
                val xFrom = maxOf(gridMin, sensorLocation.x - (positionsCoveredAtCheckRow / 2))
                val xTo = minOf(gridMax, sensorLocation.x + (positionsCoveredAtCheckRow / 2))
                val range = xFrom..xTo

                coveredRangesOnRow.add(range)
                val overlappingRanges =
                    coveredRangesOnRow
                        .filter { range.first in it || range.last in it || it.first in range || it.last in range }
                if (overlappingRanges.isNotEmpty()) {
                    val mergedRange = overlappingRanges.reduce { acc, intRange ->
                        minOf(acc.first, intRange.first)..maxOf(
                            acc.last,
                            intRange.last
                        )
                    }
                    coveredRangesOnRow.add(mergedRange)
                    overlappingRanges.forEach { coveredRangesOnRow.remove(it) }
                }
            }
        }

        val coveredCount = coveredRangesOnRow.sumOf { it.count() }

        val notCovered = if (gridMax - gridMin - coveredCount >= 0) {
            // find in ranges the open position..
            if (coveredRangesOnRow.size == 1) {
                if (coveredRangesOnRow[0].first > gridMin) {
                    gridMin
                } else {
                    gridMax
                }
            } else {
                coveredRangesOnRow
                    .sortedBy { it.first }
                    .windowed(2)
                    .first { it.first().last + 1 < it[1].first }
                    .let { it.first().last + 1 }
            }
        } else null

        return RowInfo(coveredCount, if (notCovered != null) Coordinates(notCovered, row) else null)
    }

    fun part1(input: List<String>, checkRow: Int): Int {
        val sensorAndBeaconCoordinates = input.map(String::parseSensorAndBeaconCoordinatess)
        val (min, max) = sensorAndBeaconCoordinates.flatMap { listOf(it.first, it.second) }.findMinAndMax()
        val beaconCoordinates = sensorAndBeaconCoordinates.map { it.second }
        val beaconsOnCheckRow = beaconCoordinates.toSet().filter { it.y == checkRow }.size

        return rowInfo(checkRow, min.x, max.x, sensorAndBeaconCoordinates).positionsCovered - beaconsOnCheckRow
    }

    fun part2(input: List<String>, max: Int): Long {
        val sensorAndBeaconCoordinates = input.map(String::parseSensorAndBeaconCoordinatess)
        var notCoveredRow: RowInfo? = null
        for (row in max downTo 0) {
            val rowInfo = rowInfo(row, 0, max, sensorAndBeaconCoordinates)
            println("Row $row")
            if (rowInfo.notCovered != null) {
                notCoveredRow = rowInfo
                break
            }
        }
        if (notCoveredRow != null) {
            return notCoveredRow.notCovered!!.x.toLong() * Day15.PART2_MULTIPLY_VALUE + notCoveredRow.notCovered!!.y
        }
        return -1
    }

    // test if implementation meets criteria from the description, like:
    val testInput = readInput("Day15_test")
    println("Checking")
    check(part1(testInput, Day15.PART1_CHECK_ROW) == Day15.EXPECTED_PART1_CHECK_ANSWER) { "Part 1 failed" }
    check(part2(testInput, Day15.PART2_CHECK_MAX) == Day15.EXPECTED_PART2_CHECK_ANSWER) { "Part 2 failed" }

    println("On real data")

    val input = readInput("Day15")
    println(part1(input, Day15.PART1_ROW))
    println(part2(input, Day15.PART2_MAX))
}

Compiled from "Day15.kt"
public final class Day15 {
  public static final Day15 INSTANCE;

  public static final int EXPECTED_PART1_CHECK_ANSWER;

  public static final long EXPECTED_PART2_CHECK_ANSWER;

  public static final int PART1_CHECK_ROW;

  public static final int PART1_ROW;

  public static final int PART2_CHECK_MAX;

  public static final int PART2_MAX;

  public static final int PART2_MULTIPLY_VALUE;

  private Day15();
    Code:
       0: aload_0
       1: invokespecial #8                  // Method java/lang/Object."<init>":()V
       4: return

  static {};
    Code:
       0: new           #2                  // class Day15
       3: dup
       4: invokespecial #12                 // Method "<init>":()V
       7: putstatic     #15                 // Field INSTANCE:LDay15;
      10: return
}

Compiled from "Day15.kt"
public final class Day15Kt {
  public static final void main();
    Code:
       0: ldc           #8                  // String Day15_test
       2: invokestatic  #14                 // Method UtilsKt.readInput:(Ljava/lang/String;)Ljava/util/List;
       5: astore_0
       6: ldc           #16                 // String Checking
       8: getstatic     #22                 // Field java/lang/System.out:Ljava/io/PrintStream;
      11: swap
      12: invokevirtual #28                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
      15: aload_0
      16: bipush        10
      18: invokestatic  #32                 // Method main$part1:(Ljava/util/List;I)I
      21: bipush        26
      23: if_icmpne     30
      26: iconst_1
      27: goto          31
      30: iconst_0
      31: ifne          51
      34: iconst_0
      35: istore_2
      36: ldc           #34                 // String Part 1 failed
      38: astore_2
      39: new           #36                 // class java/lang/IllegalStateException
      42: dup
      43: aload_2
      44: invokevirtual #40                 // Method java/lang/Object.toString:()Ljava/lang/String;
      47: invokespecial #44                 // Method java/lang/IllegalStateException."<init>":(Ljava/lang/String;)V
      50: athrow
      51: aload_0
      52: bipush        20
      54: invokestatic  #48                 // Method main$part2:(Ljava/util/List;I)J
      57: ldc2_w        #49                 // long 56000011l
      60: lcmp
      61: ifne          68
      64: iconst_1
      65: goto          69
      68: iconst_0
      69: ifne          89
      72: iconst_0
      73: istore_2
      74: ldc           #52                 // String Part 2 failed
      76: astore_2
      77: new           #36                 // class java/lang/IllegalStateException
      80: dup
      81: aload_2
      82: invokevirtual #40                 // Method java/lang/Object.toString:()Ljava/lang/String;
      85: invokespecial #44                 // Method java/lang/IllegalStateException."<init>":(Ljava/lang/String;)V
      88: athrow
      89: ldc           #54                 // String On real data
      91: getstatic     #22                 // Field java/lang/System.out:Ljava/io/PrintStream;
      94: swap
      95: invokevirtual #28                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
      98: ldc           #56                 // String Day15
     100: invokestatic  #14                 // Method UtilsKt.readInput:(Ljava/lang/String;)Ljava/util/List;
     103: astore_1
     104: aload_1
     105: ldc           #57                 // int 2000000
     107: invokestatic  #32                 // Method main$part1:(Ljava/util/List;I)I
     110: istore_2
     111: getstatic     #22                 // Field java/lang/System.out:Ljava/io/PrintStream;
     114: iload_2
     115: invokevirtual #60                 // Method java/io/PrintStream.println:(I)V
     118: aload_1
     119: ldc           #61                 // int 4000000
     121: invokestatic  #48                 // Method main$part2:(Ljava/util/List;I)J
     124: lstore_2
     125: getstatic     #22                 // Field java/lang/System.out:Ljava/io/PrintStream;
     128: lload_2
     129: invokevirtual #64                 // Method java/io/PrintStream.println:(J)V
     132: return

  public static void main(java.lang.String[]);
    Code:
       0: invokestatic  #75                 // Method main:()V
       3: return

  private static final kotlin.Pair<Coordinates, Coordinates> main$parseSensorAndBeaconCoordinatess(java.lang.String);
    Code:
       0: new           #82                 // class kotlin/text/Regex
       3: dup
       4: ldc           #84                 // String Sensor at x=([-\\d]+), y=([-\\d]+): closest beacon is at x=([-\\d]+), y=([-\\d]+)
       6: invokespecial #85                 // Method kotlin/text/Regex."<init>":(Ljava/lang/String;)V
       9: aload_0
      10: checkcast     #87                 // class java/lang/CharSequence
      13: invokevirtual #91                 // Method kotlin/text/Regex.matchEntire:(Ljava/lang/CharSequence;)Lkotlin/text/MatchResult;
      16: astore_2
      17: aload_2
      18: ifnull        36
      21: aload_2
      22: invokeinterface #97,  1           // InterfaceMethod kotlin/text/MatchResult.getDestructured:()Lkotlin/text/MatchResult$Destructured;
      27: astore_3
      28: aload_3
      29: ifnull        36
      32: aload_3
      33: goto          49
      36: new           #36                 // class java/lang/IllegalStateException
      39: dup
      40: ldc           #99                 // String Should match regex
      42: invokevirtual #40                 // Method java/lang/Object.toString:()Ljava/lang/String;
      45: invokespecial #44                 // Method java/lang/IllegalStateException."<init>":(Ljava/lang/String;)V
      48: athrow
      49: astore_1
      50: aload_1
      51: invokevirtual #105                // Method kotlin/text/MatchResult$Destructured.getMatch:()Lkotlin/text/MatchResult;
      54: invokeinterface #109,  1          // InterfaceMethod kotlin/text/MatchResult.getGroupValues:()Ljava/util/List;
      59: iconst_1
      60: invokeinterface #113,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      65: checkcast     #115                // class java/lang/String
      68: astore_2
      69: aload_1
      70: invokevirtual #105                // Method kotlin/text/MatchResult$Destructured.getMatch:()Lkotlin/text/MatchResult;
      73: invokeinterface #109,  1          // InterfaceMethod kotlin/text/MatchResult.getGroupValues:()Ljava/util/List;
      78: iconst_2
      79: invokeinterface #113,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      84: checkcast     #115                // class java/lang/String
      87: astore_3
      88: aload_1
      89: invokevirtual #105                // Method kotlin/text/MatchResult$Destructured.getMatch:()Lkotlin/text/MatchResult;
      92: invokeinterface #109,  1          // InterfaceMethod kotlin/text/MatchResult.getGroupValues:()Ljava/util/List;
      97: iconst_3
      98: invokeinterface #113,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     103: checkcast     #115                // class java/lang/String
     106: astore        4
     108: aload_1
     109: invokevirtual #105                // Method kotlin/text/MatchResult$Destructured.getMatch:()Lkotlin/text/MatchResult;
     112: invokeinterface #109,  1          // InterfaceMethod kotlin/text/MatchResult.getGroupValues:()Ljava/util/List;
     117: iconst_4
     118: invokeinterface #113,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     123: checkcast     #115                // class java/lang/String
     126: astore        5
     128: new           #117                // class Coordinates
     131: dup
     132: aload_2
     133: invokestatic  #123                // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
     136: aload_3
     137: invokestatic  #123                // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
     140: invokespecial #126                // Method Coordinates."<init>":(II)V
     143: new           #117                // class Coordinates
     146: dup
     147: aload         4
     149: invokestatic  #123                // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
     152: aload         5
     154: invokestatic  #123                // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
     157: invokespecial #126                // Method Coordinates."<init>":(II)V
     160: invokestatic  #132                // Method kotlin/TuplesKt.to:(Ljava/lang/Object;Ljava/lang/Object;)Lkotlin/Pair;
     163: areturn

  private static final Day15Kt$main$RowInfo main$rowInfo(int, int, int, java.util.List<kotlin.Pair<Coordinates, Coordinates>>);
    Code:
       0: new           #143                // class java/util/ArrayList
       3: dup
       4: invokespecial #145                // Method java/util/ArrayList."<init>":()V
       7: checkcast     #72                 // class java/util/List
      10: astore        4
      12: aload_3
      13: invokeinterface #149,  1          // InterfaceMethod java/util/List.iterator:()Ljava/util/Iterator;
      18: astore        5
      20: aload         5
      22: invokeinterface #155,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
      27: ifeq          691
      30: aload         5
      32: invokeinterface #159,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      37: checkcast     #161                // class kotlin/Pair
      40: astore        6
      42: aload         6
      44: invokevirtual #164                // Method kotlin/Pair.getFirst:()Ljava/lang/Object;
      47: checkcast     #117                // class Coordinates
      50: astore        7
      52: aload         6
      54: invokevirtual #167                // Method kotlin/Pair.getSecond:()Ljava/lang/Object;
      57: checkcast     #117                // class Coordinates
      60: astore        8
      62: aload         7
      64: invokevirtual #171                // Method Coordinates.getX:()I
      67: aload         8
      69: invokevirtual #171                // Method Coordinates.getX:()I
      72: isub
      73: invokestatic  #177                // Method java/lang/Math.abs:(I)I
      76: aload         7
      78: invokevirtual #180                // Method Coordinates.getY:()I
      81: aload         8
      83: invokevirtual #180                // Method Coordinates.getY:()I
      86: isub
      87: invokestatic  #177                // Method java/lang/Math.abs:(I)I
      90: iadd
      91: istore        9
      93: iload         9
      95: iconst_2
      96: imul
      97: iconst_1
      98: iadd
      99: istore        10
     101: aload         7
     103: invokevirtual #180                // Method Coordinates.getY:()I
     106: iload_0
     107: isub
     108: invokestatic  #177                // Method java/lang/Math.abs:(I)I
     111: istore        11
     113: iload         10
     115: iload         11
     117: iconst_2
     118: imul
     119: isub
     120: istore        12
     122: iload         12
     124: ifle          20
     127: iload_1
     128: aload         7
     130: invokevirtual #171                // Method Coordinates.getX:()I
     133: iload         12
     135: iconst_2
     136: idiv
     137: isub
     138: invokestatic  #184                // Method java/lang/Math.max:(II)I
     141: istore        13
     143: iload_2
     144: aload         7
     146: invokevirtual #171                // Method Coordinates.getX:()I
     149: iload         12
     151: iconst_2
     152: idiv
     153: iadd
     154: invokestatic  #187                // Method java/lang/Math.min:(II)I
     157: istore        14
     159: new           #189                // class kotlin/ranges/IntRange
     162: dup
     163: iload         13
     165: iload         14
     167: invokespecial #190                // Method kotlin/ranges/IntRange."<init>":(II)V
     170: astore        15
     172: aload         4
     174: aload         15
     176: invokeinterface #194,  2          // InterfaceMethod java/util/List.add:(Ljava/lang/Object;)Z
     181: pop
     182: aload         4
     184: checkcast     #196                // class java/lang/Iterable
     187: astore        17
     189: nop
     190: iconst_0
     191: istore        18
     193: aload         17
     195: astore        19
     197: new           #143                // class java/util/ArrayList
     200: dup
     201: invokespecial #145                // Method java/util/ArrayList."<init>":()V
     204: checkcast     #198                // class java/util/Collection
     207: astore        20
     209: iconst_0
     210: istore        21
     212: aload         19
     214: invokeinterface #199,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     219: astore        22
     221: aload         22
     223: invokeinterface #155,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
     228: ifeq          459
     231: aload         22
     233: invokeinterface #159,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     238: astore        23
     240: aload         23
     242: checkcast     #189                // class kotlin/ranges/IntRange
     245: astore        24
     247: iconst_0
     248: istore        25
     250: aload         24
     252: invokevirtual #201                // Method kotlin/ranges/IntRange.getFirst:()I
     255: istore        26
     257: aload         24
     259: invokevirtual #204                // Method kotlin/ranges/IntRange.getLast:()I
     262: istore        27
     264: aload         15
     266: invokevirtual #201                // Method kotlin/ranges/IntRange.getFirst:()I
     269: istore        28
     271: iload         26
     273: iload         28
     275: if_icmpgt     293
     278: iload         28
     280: iload         27
     282: if_icmpgt     289
     285: iconst_1
     286: goto          294
     289: iconst_0
     290: goto          294
     293: iconst_0
     294: ifne          438
     297: aload         24
     299: invokevirtual #201                // Method kotlin/ranges/IntRange.getFirst:()I
     302: istore        26
     304: aload         24
     306: invokevirtual #204                // Method kotlin/ranges/IntRange.getLast:()I
     309: istore        27
     311: aload         15
     313: invokevirtual #204                // Method kotlin/ranges/IntRange.getLast:()I
     316: istore        28
     318: iload         26
     320: iload         28
     322: if_icmpgt     340
     325: iload         28
     327: iload         27
     329: if_icmpgt     336
     332: iconst_1
     333: goto          341
     336: iconst_0
     337: goto          341
     340: iconst_0
     341: ifne          438
     344: aload         15
     346: invokevirtual #201                // Method kotlin/ranges/IntRange.getFirst:()I
     349: istore        26
     351: aload         15
     353: invokevirtual #204                // Method kotlin/ranges/IntRange.getLast:()I
     356: istore        27
     358: aload         24
     360: invokevirtual #201                // Method kotlin/ranges/IntRange.getFirst:()I
     363: istore        28
     365: iload         26
     367: iload         28
     369: if_icmpgt     387
     372: iload         28
     374: iload         27
     376: if_icmpgt     383
     379: iconst_1
     380: goto          388
     383: iconst_0
     384: goto          388
     387: iconst_0
     388: ifne          438
     391: aload         15
     393: invokevirtual #201                // Method kotlin/ranges/IntRange.getFirst:()I
     396: istore        26
     398: aload         15
     400: invokevirtual #204                // Method kotlin/ranges/IntRange.getLast:()I
     403: istore        27
     405: aload         24
     407: invokevirtual #204                // Method kotlin/ranges/IntRange.getLast:()I
     410: istore        28
     412: iload         26
     414: iload         28
     416: if_icmpgt     434
     419: iload         28
     421: iload         27
     423: if_icmpgt     430
     426: iconst_1
     427: goto          435
     430: iconst_0
     431: goto          435
     434: iconst_0
     435: ifeq          442
     438: iconst_1
     439: goto          443
     442: iconst_0
     443: ifeq          221
     446: aload         20
     448: aload         23
     450: invokeinterface #205,  2          // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     455: pop
     456: goto          221
     459: aload         20
     461: checkcast     #72                 // class java/util/List
     464: nop
     465: astore        16
     467: aload         16
     469: checkcast     #198                // class java/util/Collection
     472: invokeinterface #208,  1          // InterfaceMethod java/util/Collection.isEmpty:()Z
     477: ifne          484
     480: iconst_1
     481: goto          485
     484: iconst_0
     485: ifeq          20
     488: aload         16
     490: checkcast     #196                // class java/lang/Iterable
     493: astore        18
     495: iconst_0
     496: istore        19
     498: aload         18
     500: invokeinterface #199,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     505: astore        20
     507: aload         20
     509: invokeinterface #155,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
     514: ifne          527
     517: new           #210                // class java/lang/UnsupportedOperationException
     520: dup
     521: ldc           #212                // String Empty collection can\'t be reduced.
     523: invokespecial #213                // Method java/lang/UnsupportedOperationException."<init>":(Ljava/lang/String;)V
     526: athrow
     527: aload         20
     529: invokeinterface #159,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     534: astore        21
     536: aload         20
     538: invokeinterface #155,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
     543: ifeq          608
     546: aload         21
     548: aload         20
     550: invokeinterface #159,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     555: checkcast     #189                // class kotlin/ranges/IntRange
     558: astore        22
     560: checkcast     #189                // class kotlin/ranges/IntRange
     563: astore        23
     565: iconst_0
     566: istore        24
     568: new           #189                // class kotlin/ranges/IntRange
     571: dup
     572: aload         23
     574: invokevirtual #201                // Method kotlin/ranges/IntRange.getFirst:()I
     577: aload         22
     579: invokevirtual #201                // Method kotlin/ranges/IntRange.getFirst:()I
     582: invokestatic  #187                // Method java/lang/Math.min:(II)I
     585: nop
     586: aload         23
     588: invokevirtual #204                // Method kotlin/ranges/IntRange.getLast:()I
     591: aload         22
     593: invokevirtual #204                // Method kotlin/ranges/IntRange.getLast:()I
     596: invokestatic  #184                // Method java/lang/Math.max:(II)I
     599: invokespecial #190                // Method kotlin/ranges/IntRange."<init>":(II)V
     602: nop
     603: astore        21
     605: goto          536
     608: aload         21
     610: checkcast     #189                // class kotlin/ranges/IntRange
     613: astore        17
     615: aload         4
     617: aload         17
     619: invokeinterface #194,  2          // InterfaceMethod java/util/List.add:(Ljava/lang/Object;)Z
     624: pop
     625: aload         16
     627: checkcast     #196                // class java/lang/Iterable
     630: astore        18
     632: iconst_0
     633: istore        19
     635: aload         18
     637: invokeinterface #199,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     642: astore        20
     644: aload         20
     646: invokeinterface #155,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
     651: ifeq          687
     654: aload         20
     656: invokeinterface #159,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     661: astore        21
     663: aload         21
     665: checkcast     #189                // class kotlin/ranges/IntRange
     668: astore        22
     670: iconst_0
     671: istore        23
     673: aload         4
     675: aload         22
     677: invokeinterface #216,  2          // InterfaceMethod java/util/List.remove:(Ljava/lang/Object;)Z
     682: pop
     683: nop
     684: goto          644
     687: nop
     688: goto          20
     691: aload         4
     693: checkcast     #196                // class java/lang/Iterable
     696: astore        6
     698: iconst_0
     699: istore        7
     701: aload         6
     703: invokeinterface #199,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     708: astore        8
     710: aload         8
     712: invokeinterface #155,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
     717: ifeq          763
     720: aload         8
     722: invokeinterface #159,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     727: astore        9
     729: iload         7
     731: aload         9
     733: checkcast     #189                // class kotlin/ranges/IntRange
     736: astore        10
     738: istore        29
     740: iconst_0
     741: istore        11
     743: aload         10
     745: checkcast     #196                // class java/lang/Iterable
     748: invokestatic  #222                // Method kotlin/collections/CollectionsKt.count:(Ljava/lang/Iterable;)I
     751: istore        30
     753: iload         29
     755: iload         30
     757: iadd
     758: istore        7
     760: goto          710
     763: iload         7
     765: istore        5
     767: iload_2
     768: iload_1
     769: isub
     770: iload         5
     772: isub
     773: iflt          978
     776: aload         4
     778: invokeinterface #225,  1          // InterfaceMethod java/util/List.size:()I
     783: iconst_1
     784: if_icmpne     813
     787: aload         4
     789: iconst_0
     790: invokeinterface #113,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     795: checkcast     #189                // class kotlin/ranges/IntRange
     798: invokevirtual #201                // Method kotlin/ranges/IntRange.getFirst:()I
     801: iload_1
     802: if_icmple     809
     805: iload_1
     806: goto          972
     809: iload_2
     810: goto          972
     813: aload         4
     815: checkcast     #196                // class java/lang/Iterable
     818: astore        7
     820: nop
     821: iconst_0
     822: istore        8
     824: aload         7
     826: new           #227                // class Day15Kt$main$rowInfo$$inlined$sortedBy$1
     829: dup
     830: invokespecial #228                // Method Day15Kt$main$rowInfo$$inlined$sortedBy$1."<init>":()V
     833: checkcast     #230                // class java/util/Comparator
     836: invokestatic  #234                // Method kotlin/collections/CollectionsKt.sortedWith:(Ljava/lang/Iterable;Ljava/util/Comparator;)Ljava/util/List;
     839: checkcast     #196                // class java/lang/Iterable
     842: iconst_2
     843: iconst_0
     844: iconst_0
     845: bipush        6
     847: aconst_null
     848: invokestatic  #238                // Method kotlin/collections/CollectionsKt.windowed$default:(Ljava/lang/Iterable;IIZILjava/lang/Object;)Ljava/util/List;
     851: checkcast     #196                // class java/lang/Iterable
     854: astore        7
     856: nop
     857: iconst_0
     858: istore        8
     860: aload         7
     862: invokeinterface #199,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     867: astore        9
     869: aload         9
     871: invokeinterface #155,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
     876: ifeq          941
     879: aload         9
     881: invokeinterface #159,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     886: astore        10
     888: aload         10
     890: checkcast     #72                 // class java/util/List
     893: astore        11
     895: iconst_0
     896: istore        12
     898: aload         11
     900: invokestatic  #242                // Method kotlin/collections/CollectionsKt.first:(Ljava/util/List;)Ljava/lang/Object;
     903: checkcast     #189                // class kotlin/ranges/IntRange
     906: invokevirtual #204                // Method kotlin/ranges/IntRange.getLast:()I
     909: iconst_1
     910: iadd
     911: aload         11
     913: iconst_1
     914: invokeinterface #113,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     919: checkcast     #189                // class kotlin/ranges/IntRange
     922: invokevirtual #201                // Method kotlin/ranges/IntRange.getFirst:()I
     925: if_icmpge     932
     928: iconst_1
     929: goto          933
     932: iconst_0
     933: ifeq          869
     936: aload         10
     938: goto          951
     941: new           #244                // class java/util/NoSuchElementException
     944: dup
     945: ldc           #246                // String Collection contains no element matching the predicate.
     947: invokespecial #247                // Method java/util/NoSuchElementException."<init>":(Ljava/lang/String;)V
     950: athrow
     951: checkcast     #72                 // class java/util/List
     954: astore        8
     956: iconst_0
     957: istore        9
     959: aload         8
     961: invokestatic  #242                // Method kotlin/collections/CollectionsKt.first:(Ljava/util/List;)Ljava/lang/Object;
     964: checkcast     #189                // class kotlin/ranges/IntRange
     967: invokevirtual #204                // Method kotlin/ranges/IntRange.getLast:()I
     970: iconst_1
     971: iadd
     972: invokestatic  #251                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     975: goto          979
     978: aconst_null
     979: astore        6
     981: new           #253                // class Day15Kt$main$RowInfo
     984: dup
     985: iload         5
     987: aload         6
     989: ifnull        1008
     992: new           #117                // class Coordinates
     995: dup
     996: aload         6
     998: invokevirtual #256                // Method java/lang/Integer.intValue:()I
    1001: iload_0
    1002: invokespecial #126                // Method Coordinates."<init>":(II)V
    1005: goto          1009
    1008: aconst_null
    1009: invokespecial #259                // Method Day15Kt$main$RowInfo."<init>":(ILCoordinates;)V
    1012: areturn

  private static final int main$part1(java.util.List<java.lang.String>, int);
    Code:
       0: aload_0
       1: checkcast     #196                // class java/lang/Iterable
       4: astore_3
       5: iconst_0
       6: istore        4
       8: aload_3
       9: astore        5
      11: new           #143                // class java/util/ArrayList
      14: dup
      15: aload_3
      16: bipush        10
      18: invokestatic  #317                // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
      21: invokespecial #319                // Method java/util/ArrayList."<init>":(I)V
      24: checkcast     #198                // class java/util/Collection
      27: astore        6
      29: iconst_0
      30: istore        7
      32: aload         5
      34: invokeinterface #199,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      39: astore        8
      41: aload         8
      43: invokeinterface #155,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
      48: ifeq          91
      51: aload         8
      53: invokeinterface #159,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      58: astore        9
      60: aload         6
      62: aload         9
      64: checkcast     #115                // class java/lang/String
      67: astore        10
      69: astore        17
      71: iconst_0
      72: istore        11
      74: aload         10
      76: invokestatic  #321                // Method main$parseSensorAndBeaconCoordinatess:(Ljava/lang/String;)Lkotlin/Pair;
      79: aload         17
      81: swap
      82: invokeinterface #205,  2          // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
      87: pop
      88: goto          41
      91: aload         6
      93: checkcast     #72                 // class java/util/List
      96: nop
      97: astore_2
      98: aload_2
      99: checkcast     #196                // class java/lang/Iterable
     102: astore        4
     104: iconst_0
     105: istore        5
     107: aload         4
     109: astore        6
     111: new           #143                // class java/util/ArrayList
     114: dup
     115: invokespecial #145                // Method java/util/ArrayList."<init>":()V
     118: checkcast     #198                // class java/util/Collection
     121: astore        7
     123: iconst_0
     124: istore        8
     126: aload         6
     128: invokeinterface #199,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     133: astore        9
     135: aload         9
     137: invokeinterface #155,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
     142: ifeq          209
     145: aload         9
     147: invokeinterface #159,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     152: astore        10
     154: aload         10
     156: checkcast     #161                // class kotlin/Pair
     159: astore        11
     161: iconst_0
     162: istore        12
     164: iconst_2
     165: anewarray     #117                // class Coordinates
     168: astore        13
     170: aload         13
     172: iconst_0
     173: aload         11
     175: invokevirtual #164                // Method kotlin/Pair.getFirst:()Ljava/lang/Object;
     178: aastore
     179: aload         13
     181: iconst_1
     182: aload         11
     184: invokevirtual #167                // Method kotlin/Pair.getSecond:()Ljava/lang/Object;
     187: aastore
     188: aload         13
     190: invokestatic  #325                // Method kotlin/collections/CollectionsKt.listOf:([Ljava/lang/Object;)Ljava/util/List;
     193: checkcast     #196                // class java/lang/Iterable
     196: astore        11
     198: aload         7
     200: aload         11
     202: invokestatic  #329                // Method kotlin/collections/CollectionsKt.addAll:(Ljava/util/Collection;Ljava/lang/Iterable;)Z
     205: pop
     206: goto          135
     209: aload         7
     211: checkcast     #72                 // class java/util/List
     214: nop
     215: invokestatic  #333                // Method UtilsKt.findMinAndMax:(Ljava/util/List;)Lkotlin/Pair;
     218: astore_3
     219: aload_3
     220: invokevirtual #336                // Method kotlin/Pair.component1:()Ljava/lang/Object;
     223: checkcast     #117                // class Coordinates
     226: astore        4
     228: aload_3
     229: invokevirtual #339                // Method kotlin/Pair.component2:()Ljava/lang/Object;
     232: checkcast     #117                // class Coordinates
     235: astore        5
     237: aload_2
     238: checkcast     #196                // class java/lang/Iterable
     241: astore        7
     243: iconst_0
     244: istore        8
     246: aload         7
     248: astore        9
     250: new           #143                // class java/util/ArrayList
     253: dup
     254: aload         7
     256: bipush        10
     258: invokestatic  #317                // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
     261: invokespecial #319                // Method java/util/ArrayList."<init>":(I)V
     264: checkcast     #198                // class java/util/Collection
     267: astore        10
     269: iconst_0
     270: istore        11
     272: aload         9
     274: invokeinterface #199,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     279: astore        12
     281: aload         12
     283: invokeinterface #155,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
     288: ifeq          334
     291: aload         12
     293: invokeinterface #159,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     298: astore        13
     300: aload         10
     302: aload         13
     304: checkcast     #161                // class kotlin/Pair
     307: astore        14
     309: astore        17
     311: iconst_0
     312: istore        15
     314: aload         14
     316: invokevirtual #167                // Method kotlin/Pair.getSecond:()Ljava/lang/Object;
     319: checkcast     #117                // class Coordinates
     322: aload         17
     324: swap
     325: invokeinterface #205,  2          // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     330: pop
     331: goto          281
     334: aload         10
     336: checkcast     #72                 // class java/util/List
     339: nop
     340: astore        6
     342: aload         6
     344: checkcast     #196                // class java/lang/Iterable
     347: invokestatic  #343                // Method kotlin/collections/CollectionsKt.toSet:(Ljava/lang/Iterable;)Ljava/util/Set;
     350: checkcast     #196                // class java/lang/Iterable
     353: astore        8
     355: iconst_0
     356: istore        9
     358: aload         8
     360: astore        10
     362: new           #143                // class java/util/ArrayList
     365: dup
     366: invokespecial #145                // Method java/util/ArrayList."<init>":()V
     369: checkcast     #198                // class java/util/Collection
     372: astore        11
     374: iconst_0
     375: istore        12
     377: aload         10
     379: invokeinterface #199,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     384: astore        13
     386: aload         13
     388: invokeinterface #155,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
     393: ifeq          445
     396: aload         13
     398: invokeinterface #159,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     403: astore        14
     405: aload         14
     407: checkcast     #117                // class Coordinates
     410: astore        15
     412: iconst_0
     413: istore        16
     415: aload         15
     417: invokevirtual #180                // Method Coordinates.getY:()I
     420: iload_1
     421: if_icmpne     428
     424: iconst_1
     425: goto          429
     428: iconst_0
     429: ifeq          386
     432: aload         11
     434: aload         14
     436: invokeinterface #205,  2          // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     441: pop
     442: goto          386
     445: aload         11
     447: checkcast     #72                 // class java/util/List
     450: nop
     451: invokeinterface #225,  1          // InterfaceMethod java/util/List.size:()I
     456: istore        7
     458: iload_1
     459: aload         4
     461: invokevirtual #171                // Method Coordinates.getX:()I
     464: aload         5
     466: invokevirtual #171                // Method Coordinates.getX:()I
     469: aload_2
     470: invokestatic  #345                // Method main$rowInfo:(IIILjava/util/List;)LDay15Kt$main$RowInfo;
     473: invokevirtual #348                // Method Day15Kt$main$RowInfo.getPositionsCovered:()I
     476: iload         7
     478: isub
     479: ireturn

  private static final long main$part2(java.util.List<java.lang.String>, int);
    Code:
       0: aload_0
       1: checkcast     #196                // class java/lang/Iterable
       4: astore_3
       5: iconst_0
       6: istore        4
       8: aload_3
       9: astore        5
      11: new           #143                // class java/util/ArrayList
      14: dup
      15: aload_3
      16: bipush        10
      18: invokestatic  #317                // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
      21: invokespecial #319                // Method java/util/ArrayList."<init>":(I)V
      24: checkcast     #198                // class java/util/Collection
      27: astore        6
      29: iconst_0
      30: istore        7
      32: aload         5
      34: invokeinterface #199,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      39: astore        8
      41: aload         8
      43: invokeinterface #155,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
      48: ifeq          91
      51: aload         8
      53: invokeinterface #159,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      58: astore        9
      60: aload         6
      62: aload         9
      64: checkcast     #115                // class java/lang/String
      67: astore        10
      69: astore        12
      71: iconst_0
      72: istore        11
      74: aload         10
      76: invokestatic  #321                // Method main$parseSensorAndBeaconCoordinatess:(Ljava/lang/String;)Lkotlin/Pair;
      79: aload         12
      81: swap
      82: invokeinterface #205,  2          // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
      87: pop
      88: goto          41
      91: aload         6
      93: checkcast     #72                 // class java/util/List
      96: nop
      97: astore_2
      98: aconst_null
      99: astore_3
     100: iload_1
     101: istore        4
     103: iconst_m1
     104: iload         4
     106: if_icmpge     167
     109: iload         4
     111: iconst_0
     112: iload_1
     113: aload_2
     114: invokestatic  #345                // Method main$rowInfo:(IIILjava/util/List;)LDay15Kt$main$RowInfo;
     117: astore        5
     119: new           #369                // class java/lang/StringBuilder
     122: dup
     123: invokespecial #370                // Method java/lang/StringBuilder."<init>":()V
     126: ldc_w         #372                // String Row
     129: invokevirtual #376                // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     132: iload         4
     134: invokevirtual #379                // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
     137: invokevirtual #380                // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
     140: getstatic     #22                 // Field java/lang/System.out:Ljava/io/PrintStream;
     143: swap
     144: invokevirtual #28                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
     147: aload         5
     149: invokevirtual #384                // Method Day15Kt$main$RowInfo.getNotCovered:()LCoordinates;
     152: ifnull        161
     155: aload         5
     157: astore_3
     158: goto          167
     161: iinc          4, -1
     164: goto          103
     167: aload_3
     168: ifnull        201
     171: aload_3
     172: invokevirtual #384                // Method Day15Kt$main$RowInfo.getNotCovered:()LCoordinates;
     175: dup
     176: invokestatic  #389                // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
     179: invokevirtual #171                // Method Coordinates.getX:()I
     182: i2l
     183: ldc           #61                 // int 4000000
     185: i2l
     186: lmul
     187: aload_3
     188: invokevirtual #384                // Method Day15Kt$main$RowInfo.getNotCovered:()LCoordinates;
     191: dup
     192: invokestatic  #389                // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
     195: invokevirtual #180                // Method Coordinates.getY:()I
     198: i2l
     199: ladd
     200: lreturn
     201: ldc2_w        #390                // long -1l
     204: lreturn
}

Compiled from "Comparisons.kt"
public final class Day15Kt$main$rowInfo$$inlined$sortedBy$1<T> implements java.util.Comparator {
  public Day15Kt$main$rowInfo$$inlined$sortedBy$1();
    Code:
       0: aload_0
       1: invokespecial #16                 // Method java/lang/Object."<init>":()V
       4: return

  public final int compare(T, T);
    Code:
       0: aload_1
       1: checkcast     #23                 // class kotlin/ranges/IntRange
       4: astore_3
       5: iconst_0
       6: istore        4
       8: aload_3
       9: invokevirtual #27                 // Method kotlin/ranges/IntRange.getFirst:()I
      12: invokestatic  #33                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      15: checkcast     #35                 // class java/lang/Comparable
      18: aload_2
      19: checkcast     #23                 // class kotlin/ranges/IntRange
      22: astore_3
      23: astore        5
      25: iconst_0
      26: istore        4
      28: aload_3
      29: invokevirtual #27                 // Method kotlin/ranges/IntRange.getFirst:()I
      32: invokestatic  #33                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      35: aload         5
      37: swap
      38: checkcast     #35                 // class java/lang/Comparable
      41: invokestatic  #41                 // Method kotlin/comparisons/ComparisonsKt.compareValues:(Ljava/lang/Comparable;Ljava/lang/Comparable;)I
      44: ireturn
}

Compiled from "Day15.kt"
public final class Day15Kt$main$RowInfo {
  private final int positionsCovered;

  private final Coordinates notCovered;

  public Day15Kt$main$RowInfo(int, Coordinates);
    Code:
       0: aload_0
       1: invokespecial #9                  // Method java/lang/Object."<init>":()V
       4: aload_0
       5: iload_1
       6: putfield      #13                 // Field positionsCovered:I
       9: aload_0
      10: aload_2
      11: putfield      #17                 // Field notCovered:LCoordinates;
      14: return

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

  public final Coordinates getNotCovered();
    Code:
       0: aload_0
       1: getfield      #17                 // Field notCovered:LCoordinates;
       4: areturn

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

  public final Coordinates component2();
    Code:
       0: aload_0
       1: getfield      #17                 // Field notCovered:LCoordinates;
       4: areturn

  public final Day15Kt$main$RowInfo copy(int, Coordinates);
    Code:
       0: new           #2                  // class Day15Kt$main$RowInfo
       3: dup
       4: iload_1
       5: aload_2
       6: invokespecial #29                 // Method "<init>":(ILCoordinates;)V
       9: areturn

  public static Day15Kt$main$RowInfo copy$default(Day15Kt$main$RowInfo, int, Coordinates, int, java.lang.Object);
    Code:
       0: iload_3
       1: iconst_1
       2: iand
       3: ifeq          11
       6: aload_0
       7: getfield      #13                 // Field positionsCovered:I
      10: istore_1
      11: iload_3
      12: iconst_2
      13: iand
      14: ifeq          22
      17: aload_0
      18: getfield      #17                 // Field notCovered:LCoordinates;
      21: astore_2
      22: aload_0
      23: iload_1
      24: aload_2
      25: invokevirtual #33                 // Method copy:(ILCoordinates;)LDay15Kt$main$RowInfo;
      28: areturn

  public java.lang.String toString();
    Code:
       0: new           #37                 // class java/lang/StringBuilder
       3: dup
       4: invokespecial #38                 // Method java/lang/StringBuilder."<init>":()V
       7: ldc           #40                 // String RowInfo(positionsCovered=
       9: invokevirtual #44                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      12: aload_0
      13: getfield      #13                 // Field positionsCovered:I
      16: invokevirtual #47                 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
      19: ldc           #49                 // String , notCovered=
      21: invokevirtual #44                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      24: aload_0
      25: getfield      #17                 // Field notCovered:LCoordinates;
      28: invokevirtual #52                 // Method java/lang/StringBuilder.append:(Ljava/lang/Object;)Ljava/lang/StringBuilder;
      31: bipush        41
      33: invokevirtual #55                 // Method java/lang/StringBuilder.append:(C)Ljava/lang/StringBuilder;
      36: invokevirtual #57                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
      39: areturn

  public int hashCode();
    Code:
       0: aload_0
       1: getfield      #13                 // Field positionsCovered:I
       4: invokestatic  #63                 // Method java/lang/Integer.hashCode:(I)I
       7: istore_1
       8: iload_1
       9: bipush        31
      11: imul
      12: aload_0
      13: getfield      #17                 // Field notCovered:LCoordinates;
      16: ifnonnull     23
      19: iconst_0
      20: goto          30
      23: aload_0
      24: getfield      #17                 // Field notCovered:LCoordinates;
      27: invokevirtual #67                 // Method Coordinates.hashCode:()I
      30: iadd
      31: istore_1
      32: iload_1
      33: 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 Day15Kt$main$RowInfo
      11: ifne          16
      14: iconst_0
      15: ireturn
      16: aload_1
      17: checkcast     #2                  // class Day15Kt$main$RowInfo
      20: astore_2
      21: aload_0
      22: getfield      #13                 // Field positionsCovered:I
      25: aload_2
      26: getfield      #13                 // Field positionsCovered:I
      29: if_icmpeq     34
      32: iconst_0
      33: ireturn
      34: aload_0
      35: getfield      #17                 // Field notCovered:LCoordinates;
      38: aload_2
      39: getfield      #17                 // Field notCovered:LCoordinates;
      42: invokestatic  #76                 // Method kotlin/jvm/internal/Intrinsics.areEqual:(Ljava/lang/Object;Ljava/lang/Object;)Z
      45: ifne          50
      48: iconst_0
      49: ireturn
      50: iconst_1
      51: ireturn
}