package day15

import java.io.File
import kotlin.math.abs
import kotlin.system.measureTimeMillis

fun manhattanDistance(x1: Long, y1: Long, x2: Long, y2: Long) = abs(x1-x2) + abs(y1-y2)

class LineScan(
    initStart: Long,
    initEnd: Long
) {
    val start = minOf(initStart, initEnd)
    val end = maxOf(initStart, initEnd)

    fun union(other: LineScan): List<LineScan> =
        if(other.start-1 > end || other.end+1 < start) listOf(this, other)
        else listOf(LineScan(
            minOf(other.start, start),
            maxOf(other.end, end)))

    fun union(others: List<LineScan>): List<LineScan> =
        others.fold(Pair(this, emptyList<LineScan>())) { acc, it ->
            val united = acc.first.union(it)
            Pair(
                united.first(),
                acc.second + united.drop(1)
            )
        }.let {listOf(it.first) + it.second    }

}

class SensorScan(
    val x: Long,
    val y: Long,
    private val beaconX: Long,
    private val beaconY: Long
) {
    val radius = manhattanDistance(x, y, beaconX, beaconY)

    fun contains(newX: Long, newY: Long) = manhattanDistance(x, y, newX, newY) <= radius
    fun hasBeacon(newX: Long, newY: Long) = newX == beaconX && newY == beaconY

    fun lineScanAtHeight(height: Long) = LineScan(
        x - (radius - abs(y-height)),
        x + (radius - abs(y-height))
    )

}

fun List<SensorScan>.findXBoundaries() =
    this.fold(Pair(this.first(), this.first())) { acc, scan ->
        Pair(
            scan.takeIf { it.x < acc.first.x } ?: acc.first,
            scan.takeIf { it.x > acc.second.x } ?: acc.second
        )
    }.let { Pair(it.first.x - it.first.radius, it.second.x + it.second.radius) }

fun List<SensorScan>.uniqueExcludedPosition(y: Long) =
    this.filter { abs(y-it.y) <= it.radius }
        .fold(emptyList<LineScan>()) { acc, it ->
            it.lineScanAtHeight(y).union(acc)
        }

fun parseInputFile() =
    File("src/main/kotlin/day15/input.txt")
        .readLines()
        .fold(emptyList<SensorScan>()) { acc, it ->
            Regex("Sensor at x=(-?[0-9]+), y=(-?[0-9]+): closest beacon is at x=(-?[0-9]+), y=(-?[0-9]+)")
                .find(it)!!
                .let{
                    acc + SensorScan(
                        it.groupValues[1].toLong(),
                        it.groupValues[2].toLong(),
                        it.groupValues[3].toLong(),
                        it.groupValues[4].toLong()
                    )
                }
        }

fun findBeaconFreePoints(row: Long) =
    parseInputFile()
        .let { scans ->
            val boundaries = scans.findXBoundaries()
            (boundaries.first  .. boundaries.second).fold(0) { acc, it ->
                if ( scans.any{ scan -> scan.contains(it, row) } && scans.all { scan -> !scan.hasBeacon(it, row) }) acc + 1
                else acc
            }
        }

fun findBeacon(range: Long) =
    parseInputFile()
        .let {scans ->
            (0L .. range).fold(emptyList<Long>()) { acc, y ->
                val lineScan = scans.uniqueExcludedPosition(y)
                if(lineScan.size == 1 && lineScan.first().start <= 0L && lineScan.first().end >= range ) acc
                else if(lineScan.size == 1 && lineScan.first().start > 0L) acc + listOf(y + lineScan.first().start*4000000L)
                else if(lineScan.size == 1 && lineScan.first().end < range) acc + listOf(y + lineScan.first().end*4000000L)
                else acc + listOf(y + (lineScan.minBy { it.end }.end+1)*4000000L)

            }
        }

fun main() {
    measureTimeMillis {
        println("In the row where y=2000000, the  positions cannot contain a beacon are: ${findBeaconFreePoints(10)}")
        val beacons = findBeacon(4000000L)
        if (beacons.size > 1) println("I did not find any beacon")
        else println("The beacon tuning frequency is: ${beacons.first()}")
    }.let {
        println("The whole process took: $it milliseconds")
    }
}

Compiled from "Solution.kt"
public final class day15.SolutionKt {
  public static final long manhattanDistance(long, long, long, long);
    Code:
       0: lload_0
       1: lload         4
       3: lsub
       4: invokestatic  #12                 // Method java/lang/Math.abs:(J)J
       7: lload_2
       8: lload         6
      10: lsub
      11: invokestatic  #12                 // Method java/lang/Math.abs:(J)J
      14: ladd
      15: lreturn

  public static final kotlin.Pair<java.lang.Long, java.lang.Long> findXBoundaries(java.util.List<day15.SensorScan>);
    Code:
       0: aload_0
       1: ldc           #23                 // String <this>
       3: invokestatic  #29                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: checkcast     #31                 // class java/lang/Iterable
      10: astore_1
      11: new           #33                 // class kotlin/Pair
      14: dup
      15: aload_0
      16: invokestatic  #39                 // Method kotlin/collections/CollectionsKt.first:(Ljava/util/List;)Ljava/lang/Object;
      19: aload_0
      20: invokestatic  #39                 // Method kotlin/collections/CollectionsKt.first:(Ljava/util/List;)Ljava/lang/Object;
      23: invokespecial #43                 // Method kotlin/Pair."<init>":(Ljava/lang/Object;Ljava/lang/Object;)V
      26: astore_2
      27: iconst_0
      28: istore_3
      29: aload_2
      30: astore        4
      32: aload_1
      33: invokeinterface #47,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      38: astore        5
      40: aload         5
      42: invokeinterface #53,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      47: ifeq          218
      50: aload         5
      52: invokeinterface #57,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      57: astore        6
      59: aload         4
      61: aload         6
      63: checkcast     #59                 // class day15/SensorScan
      66: astore        7
      68: astore        8
      70: iconst_0
      71: istore        9
      73: aload         7
      75: astore        10
      77: aload         10
      79: astore        11
      81: iconst_0
      82: istore        12
      84: aload         11
      86: invokevirtual #63                 // Method day15/SensorScan.getX:()J
      89: aload         8
      91: invokevirtual #66                 // Method kotlin/Pair.getFirst:()Ljava/lang/Object;
      94: checkcast     #59                 // class day15/SensorScan
      97: invokevirtual #63                 // Method day15/SensorScan.getX:()J
     100: lcmp
     101: ifge          108
     104: iconst_1
     105: goto          109
     108: iconst_0
     109: ifeq          117
     112: aload         10
     114: goto          118
     117: aconst_null
     118: dup
     119: ifnonnull     131
     122: pop
     123: aload         8
     125: invokevirtual #66                 // Method kotlin/Pair.getFirst:()Ljava/lang/Object;
     128: checkcast     #59                 // class day15/SensorScan
     131: aload         7
     133: astore        10
     135: aload         10
     137: astore        11
     139: astore        13
     141: iconst_0
     142: istore        12
     144: aload         11
     146: invokevirtual #63                 // Method day15/SensorScan.getX:()J
     149: aload         8
     151: invokevirtual #69                 // Method kotlin/Pair.getSecond:()Ljava/lang/Object;
     154: checkcast     #59                 // class day15/SensorScan
     157: invokevirtual #63                 // Method day15/SensorScan.getX:()J
     160: lcmp
     161: ifle          168
     164: iconst_1
     165: goto          169
     168: iconst_0
     169: istore        14
     171: aload         13
     173: iload         14
     175: ifeq          183
     178: aload         10
     180: goto          184
     183: aconst_null
     184: dup
     185: ifnonnull     197
     188: pop
     189: aload         8
     191: invokevirtual #69                 // Method kotlin/Pair.getSecond:()Ljava/lang/Object;
     194: checkcast     #59                 // class day15/SensorScan
     197: astore        15
     199: astore        16
     201: new           #33                 // class kotlin/Pair
     204: dup
     205: aload         16
     207: aload         15
     209: invokespecial #43                 // Method kotlin/Pair."<init>":(Ljava/lang/Object;Ljava/lang/Object;)V
     212: nop
     213: astore        4
     215: goto          40
     218: aload         4
     220: astore_2
     221: iconst_0
     222: istore_3
     223: new           #33                 // class kotlin/Pair
     226: dup
     227: aload_2
     228: invokevirtual #66                 // Method kotlin/Pair.getFirst:()Ljava/lang/Object;
     231: checkcast     #59                 // class day15/SensorScan
     234: invokevirtual #63                 // Method day15/SensorScan.getX:()J
     237: aload_2
     238: invokevirtual #66                 // Method kotlin/Pair.getFirst:()Ljava/lang/Object;
     241: checkcast     #59                 // class day15/SensorScan
     244: invokevirtual #72                 // Method day15/SensorScan.getRadius:()J
     247: lsub
     248: invokestatic  #78                 // Method java/lang/Long.valueOf:(J)Ljava/lang/Long;
     251: aload_2
     252: invokevirtual #69                 // Method kotlin/Pair.getSecond:()Ljava/lang/Object;
     255: checkcast     #59                 // class day15/SensorScan
     258: invokevirtual #63                 // Method day15/SensorScan.getX:()J
     261: aload_2
     262: invokevirtual #69                 // Method kotlin/Pair.getSecond:()Ljava/lang/Object;
     265: checkcast     #59                 // class day15/SensorScan
     268: invokevirtual #72                 // Method day15/SensorScan.getRadius:()J
     271: ladd
     272: invokestatic  #78                 // Method java/lang/Long.valueOf:(J)Ljava/lang/Long;
     275: invokespecial #43                 // Method kotlin/Pair."<init>":(Ljava/lang/Object;Ljava/lang/Object;)V
     278: nop
     279: areturn

  public static final java.util.List<day15.LineScan> uniqueExcludedPosition(java.util.List<day15.SensorScan>, long);
    Code:
       0: aload_0
       1: ldc           #23                 // String <this>
       3: invokestatic  #29                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: checkcast     #31                 // class java/lang/Iterable
      10: astore_3
      11: iconst_0
      12: istore        4
      14: aload_3
      15: astore        5
      17: new           #104                // class java/util/ArrayList
      20: dup
      21: invokespecial #107                // Method java/util/ArrayList."<init>":()V
      24: checkcast     #109                // class java/util/Collection
      27: astore        6
      29: iconst_0
      30: istore        7
      32: aload         5
      34: invokeinterface #47,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      39: astore        8
      41: aload         8
      43: invokeinterface #53,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      48: ifeq          110
      51: aload         8
      53: invokeinterface #57,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      58: astore        9
      60: aload         9
      62: checkcast     #59                 // class day15/SensorScan
      65: astore        10
      67: iconst_0
      68: istore        11
      70: lload_1
      71: aload         10
      73: invokevirtual #112                // Method day15/SensorScan.getY:()J
      76: lsub
      77: invokestatic  #12                 // Method java/lang/Math.abs:(J)J
      80: aload         10
      82: invokevirtual #72                 // Method day15/SensorScan.getRadius:()J
      85: lcmp
      86: ifgt          93
      89: iconst_1
      90: goto          94
      93: iconst_0
      94: ifeq          41
      97: aload         6
      99: aload         9
     101: invokeinterface #116,  2          // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     106: pop
     107: goto          41
     110: aload         6
     112: checkcast     #99                 // class java/util/List
     115: nop
     116: checkcast     #31                 // class java/lang/Iterable
     119: astore_3
     120: invokestatic  #120                // Method kotlin/collections/CollectionsKt.emptyList:()Ljava/util/List;
     123: astore        4
     125: iconst_0
     126: istore        5
     128: aload         4
     130: astore        6
     132: aload_3
     133: invokeinterface #47,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     138: astore        7
     140: aload         7
     142: invokeinterface #53,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     147: ifeq          189
     150: aload         7
     152: invokeinterface #57,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     157: astore        8
     159: aload         6
     161: aload         8
     163: checkcast     #59                 // class day15/SensorScan
     166: astore        9
     168: astore        10
     170: iconst_0
     171: istore        11
     173: aload         9
     175: lload_1
     176: invokevirtual #124                // Method day15/SensorScan.lineScanAtHeight:(J)Lday15/LineScan;
     179: aload         10
     181: invokevirtual #130                // Method day15/LineScan.union:(Ljava/util/List;)Ljava/util/List;
     184: astore        6
     186: goto          140
     189: aload         6
     191: areturn

  public static final java.util.List<day15.SensorScan> parseInputFile();
    Code:
       0: new           #145                // class java/io/File
       3: dup
       4: ldc           #147                // String src/main/kotlin/day15/input.txt
       6: invokespecial #150                // Method java/io/File."<init>":(Ljava/lang/String;)V
       9: aconst_null
      10: iconst_1
      11: aconst_null
      12: invokestatic  #156                // Method kotlin/io/FilesKt.readLines$default:(Ljava/io/File;Ljava/nio/charset/Charset;ILjava/lang/Object;)Ljava/util/List;
      15: checkcast     #31                 // class java/lang/Iterable
      18: astore_0
      19: invokestatic  #120                // Method kotlin/collections/CollectionsKt.emptyList:()Ljava/util/List;
      22: astore_1
      23: iconst_0
      24: istore_2
      25: aload_1
      26: astore_3
      27: aload_0
      28: invokeinterface #47,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      33: astore        4
      35: aload         4
      37: invokeinterface #53,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      42: ifeq          194
      45: aload         4
      47: invokeinterface #57,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      52: astore        5
      54: aload_3
      55: aload         5
      57: checkcast     #158                // class java/lang/String
      60: astore        6
      62: astore        7
      64: iconst_0
      65: istore        8
      67: new           #160                // class kotlin/text/Regex
      70: dup
      71: ldc           #162                // String Sensor at x=(-?[0-9]+), y=(-?[0-9]+): closest beacon is at x=(-?[0-9]+), y=(-?[0-9]+)
      73: invokespecial #163                // Method kotlin/text/Regex."<init>":(Ljava/lang/String;)V
      76: aload         6
      78: checkcast     #165                // class java/lang/CharSequence
      81: iconst_0
      82: iconst_2
      83: aconst_null
      84: invokestatic  #169                // Method kotlin/text/Regex.find$default:(Lkotlin/text/Regex;Ljava/lang/CharSequence;IILjava/lang/Object;)Lkotlin/text/MatchResult;
      87: dup
      88: invokestatic  #173                // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
      91: astore        9
      93: iconst_0
      94: istore        10
      96: aload         7
      98: checkcast     #109                // class java/util/Collection
     101: new           #59                 // class day15/SensorScan
     104: dup
     105: aload         9
     107: invokeinterface #178,  1          // InterfaceMethod kotlin/text/MatchResult.getGroupValues:()Ljava/util/List;
     112: iconst_1
     113: invokeinterface #182,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     118: checkcast     #158                // class java/lang/String
     121: invokestatic  #186                // Method java/lang/Long.parseLong:(Ljava/lang/String;)J
     124: aload         9
     126: invokeinterface #178,  1          // InterfaceMethod kotlin/text/MatchResult.getGroupValues:()Ljava/util/List;
     131: iconst_2
     132: invokeinterface #182,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     137: checkcast     #158                // class java/lang/String
     140: invokestatic  #186                // Method java/lang/Long.parseLong:(Ljava/lang/String;)J
     143: aload         9
     145: invokeinterface #178,  1          // InterfaceMethod kotlin/text/MatchResult.getGroupValues:()Ljava/util/List;
     150: iconst_3
     151: invokeinterface #182,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     156: checkcast     #158                // class java/lang/String
     159: invokestatic  #186                // Method java/lang/Long.parseLong:(Ljava/lang/String;)J
     162: aload         9
     164: invokeinterface #178,  1          // InterfaceMethod kotlin/text/MatchResult.getGroupValues:()Ljava/util/List;
     169: iconst_4
     170: invokeinterface #182,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     175: checkcast     #158                // class java/lang/String
     178: invokestatic  #186                // Method java/lang/Long.parseLong:(Ljava/lang/String;)J
     181: invokespecial #189                // Method day15/SensorScan."<init>":(JJJJ)V
     184: invokestatic  #193                // Method kotlin/collections/CollectionsKt.plus:(Ljava/util/Collection;Ljava/lang/Object;)Ljava/util/List;
     187: nop
     188: nop
     189: nop
     190: astore_3
     191: goto          35
     194: aload_3
     195: areturn

  public static final int findBeaconFreePoints(long);
    Code:
       0: invokestatic  #201                // Method parseInputFile:()Ljava/util/List;
       3: astore_2
       4: iconst_0
       5: istore_3
       6: aload_2
       7: invokestatic  #203                // Method findXBoundaries:(Ljava/util/List;)Lkotlin/Pair;
      10: astore        4
      12: new           #205                // class kotlin/ranges/LongRange
      15: dup
      16: aload         4
      18: invokevirtual #66                 // Method kotlin/Pair.getFirst:()Ljava/lang/Object;
      21: checkcast     #207                // class java/lang/Number
      24: invokevirtual #210                // Method java/lang/Number.longValue:()J
      27: aload         4
      29: invokevirtual #69                 // Method kotlin/Pair.getSecond:()Ljava/lang/Object;
      32: checkcast     #207                // class java/lang/Number
      35: invokevirtual #210                // Method java/lang/Number.longValue:()J
      38: invokespecial #213                // Method kotlin/ranges/LongRange."<init>":(JJ)V
      41: checkcast     #31                 // class java/lang/Iterable
      44: astore        5
      46: iconst_0
      47: istore        6
      49: iconst_0
      50: istore        7
      52: iload         6
      54: istore        8
      56: aload         5
      58: invokeinterface #47,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      63: astore        9
      65: aload         9
      67: invokeinterface #53,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      72: ifeq          300
      75: aload         9
      77: checkcast     #215                // class kotlin/collections/LongIterator
      80: invokevirtual #218                // Method kotlin/collections/LongIterator.nextLong:()J
      83: lstore        10
      85: iload         8
      87: lload         10
      89: lstore        12
      91: istore        14
      93: iconst_0
      94: istore        15
      96: aload_2
      97: checkcast     #31                 // class java/lang/Iterable
     100: astore        16
     102: iconst_0
     103: istore        17
     105: aload         16
     107: instanceof    #109                // class java/util/Collection
     110: ifeq          130
     113: aload         16
     115: checkcast     #109                // class java/util/Collection
     118: invokeinterface #221,  1          // InterfaceMethod java/util/Collection.isEmpty:()Z
     123: ifeq          130
     126: iconst_0
     127: goto          184
     130: aload         16
     132: invokeinterface #47,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     137: astore        18
     139: aload         18
     141: invokeinterface #53,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     146: ifeq          183
     149: aload         18
     151: invokeinterface #57,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     156: astore        19
     158: aload         19
     160: checkcast     #59                 // class day15/SensorScan
     163: astore        20
     165: iconst_0
     166: istore        21
     168: aload         20
     170: lload         12
     172: lload_0
     173: invokevirtual #225                // Method day15/SensorScan.contains:(JJ)Z
     176: ifeq          139
     179: iconst_1
     180: goto          184
     183: iconst_0
     184: ifeq          293
     187: aload_2
     188: checkcast     #31                 // class java/lang/Iterable
     191: astore        16
     193: iconst_0
     194: istore        17
     196: aload         16
     198: instanceof    #109                // class java/util/Collection
     201: ifeq          221
     204: aload         16
     206: checkcast     #109                // class java/util/Collection
     209: invokeinterface #221,  1          // InterfaceMethod java/util/Collection.isEmpty:()Z
     214: ifeq          221
     217: iconst_1
     218: goto          283
     221: aload         16
     223: invokeinterface #47,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     228: astore        18
     230: aload         18
     232: invokeinterface #53,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     237: ifeq          282
     240: aload         18
     242: invokeinterface #57,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     247: astore        19
     249: aload         19
     251: checkcast     #59                 // class day15/SensorScan
     254: astore        20
     256: iconst_0
     257: istore        21
     259: aload         20
     261: lload         12
     263: lload_0
     264: invokevirtual #228                // Method day15/SensorScan.hasBeacon:(JJ)Z
     267: ifne          274
     270: iconst_1
     271: goto          275
     274: iconst_0
     275: ifne          230
     278: iconst_0
     279: goto          283
     282: iconst_1
     283: ifeq          293
     286: iload         14
     288: iconst_1
     289: iadd
     290: goto          295
     293: iload         14
     295: istore        8
     297: goto          65
     300: iload         8
     302: nop
     303: nop
     304: ireturn

  public static final java.util.List<java.lang.Long> findBeacon(long);
    Code:
       0: invokestatic  #201                // Method parseInputFile:()Ljava/util/List;
       3: astore_2
       4: iconst_0
       5: istore_3
       6: new           #205                // class kotlin/ranges/LongRange
       9: dup
      10: lconst_0
      11: lload_0
      12: invokespecial #213                // Method kotlin/ranges/LongRange."<init>":(JJ)V
      15: checkcast     #31                 // class java/lang/Iterable
      18: astore        4
      20: invokestatic  #120                // Method kotlin/collections/CollectionsKt.emptyList:()Ljava/util/List;
      23: astore        5
      25: iconst_0
      26: istore        6
      28: aload         5
      30: astore        7
      32: aload         4
      34: invokeinterface #47,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      39: astore        8
      41: aload         8
      43: invokeinterface #53,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      48: ifeq          439
      51: aload         8
      53: checkcast     #215                // class kotlin/collections/LongIterator
      56: invokevirtual #218                // Method kotlin/collections/LongIterator.nextLong:()J
      59: lstore        9
      61: aload         7
      63: lload         9
      65: lstore        11
      67: astore        13
      69: iconst_0
      70: istore        14
      72: aload_2
      73: lload         11
      75: invokestatic  #244                // Method uniqueExcludedPosition:(Ljava/util/List;J)Ljava/util/List;
      78: astore        15
      80: aload         15
      82: invokeinterface #248,  1          // InterfaceMethod java/util/List.size:()I
      87: iconst_1
      88: if_icmpne     128
      91: aload         15
      93: invokestatic  #39                 // Method kotlin/collections/CollectionsKt.first:(Ljava/util/List;)Ljava/lang/Object;
      96: checkcast     #126                // class day15/LineScan
      99: invokevirtual #251                // Method day15/LineScan.getStart:()J
     102: lconst_0
     103: lcmp
     104: ifgt          128
     107: aload         15
     109: invokestatic  #39                 // Method kotlin/collections/CollectionsKt.first:(Ljava/util/List;)Ljava/lang/Object;
     112: checkcast     #126                // class day15/LineScan
     115: invokevirtual #254                // Method day15/LineScan.getEnd:()J
     118: lload_0
     119: lcmp
     120: iflt          128
     123: aload         13
     125: goto          434
     128: aload         15
     130: invokeinterface #248,  1          // InterfaceMethod java/util/List.size:()I
     135: iconst_1
     136: if_icmpne     193
     139: aload         15
     141: invokestatic  #39                 // Method kotlin/collections/CollectionsKt.first:(Ljava/util/List;)Ljava/lang/Object;
     144: checkcast     #126                // class day15/LineScan
     147: invokevirtual #251                // Method day15/LineScan.getStart:()J
     150: lconst_0
     151: lcmp
     152: ifle          193
     155: aload         13
     157: checkcast     #109                // class java/util/Collection
     160: lload         11
     162: aload         15
     164: invokestatic  #39                 // Method kotlin/collections/CollectionsKt.first:(Ljava/util/List;)Ljava/lang/Object;
     167: checkcast     #126                // class day15/LineScan
     170: invokevirtual #251                // Method day15/LineScan.getStart:()J
     173: ldc2_w        #255                // long 4000000l
     176: lmul
     177: ladd
     178: invokestatic  #78                 // Method java/lang/Long.valueOf:(J)Ljava/lang/Long;
     181: invokestatic  #260                // Method kotlin/collections/CollectionsKt.listOf:(Ljava/lang/Object;)Ljava/util/List;
     184: checkcast     #31                 // class java/lang/Iterable
     187: invokestatic  #263                // Method kotlin/collections/CollectionsKt.plus:(Ljava/util/Collection;Ljava/lang/Iterable;)Ljava/util/List;
     190: goto          434
     193: aload         15
     195: invokeinterface #248,  1          // InterfaceMethod java/util/List.size:()I
     200: iconst_1
     201: if_icmpne     258
     204: aload         15
     206: invokestatic  #39                 // Method kotlin/collections/CollectionsKt.first:(Ljava/util/List;)Ljava/lang/Object;
     209: checkcast     #126                // class day15/LineScan
     212: invokevirtual #254                // Method day15/LineScan.getEnd:()J
     215: lload_0
     216: lcmp
     217: ifge          258
     220: aload         13
     222: checkcast     #109                // class java/util/Collection
     225: lload         11
     227: aload         15
     229: invokestatic  #39                 // Method kotlin/collections/CollectionsKt.first:(Ljava/util/List;)Ljava/lang/Object;
     232: checkcast     #126                // class day15/LineScan
     235: invokevirtual #254                // Method day15/LineScan.getEnd:()J
     238: ldc2_w        #255                // long 4000000l
     241: lmul
     242: ladd
     243: invokestatic  #78                 // Method java/lang/Long.valueOf:(J)Ljava/lang/Long;
     246: invokestatic  #260                // Method kotlin/collections/CollectionsKt.listOf:(Ljava/lang/Object;)Ljava/util/List;
     249: checkcast     #31                 // class java/lang/Iterable
     252: invokestatic  #263                // Method kotlin/collections/CollectionsKt.plus:(Ljava/util/Collection;Ljava/lang/Iterable;)Ljava/util/List;
     255: goto          434
     258: aload         13
     260: checkcast     #109                // class java/util/Collection
     263: lload         11
     265: aload         15
     267: checkcast     #31                 // class java/lang/Iterable
     270: astore        16
     272: lstore        17
     274: astore        19
     276: iconst_0
     277: istore        20
     279: aload         16
     281: invokeinterface #47,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     286: astore        21
     288: aload         21
     290: invokeinterface #53,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     295: ifne          306
     298: new           #265                // class java/util/NoSuchElementException
     301: dup
     302: invokespecial #266                // Method java/util/NoSuchElementException."<init>":()V
     305: athrow
     306: aload         21
     308: invokeinterface #57,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     313: astore        22
     315: aload         21
     317: invokeinterface #53,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     322: ifne          330
     325: aload         22
     327: goto          401
     330: aload         22
     332: checkcast     #126                // class day15/LineScan
     335: astore        23
     337: iconst_0
     338: istore        25
     340: aload         23
     342: invokevirtual #254                // Method day15/LineScan.getEnd:()J
     345: lstore        23
     347: aload         21
     349: invokeinterface #57,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     354: astore        25
     356: aload         25
     358: checkcast     #126                // class day15/LineScan
     361: astore        26
     363: iconst_0
     364: istore        28
     366: aload         26
     368: invokevirtual #254                // Method day15/LineScan.getEnd:()J
     371: lstore        26
     373: lload         23
     375: lload         26
     377: lcmp
     378: ifle          389
     381: aload         25
     383: astore        22
     385: lload         26
     387: lstore        23
     389: aload         21
     391: invokeinterface #53,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     396: ifne          347
     399: aload         22
     401: astore        29
     403: aload         19
     405: lload         17
     407: aload         29
     409: checkcast     #126                // class day15/LineScan
     412: invokevirtual #254                // Method day15/LineScan.getEnd:()J
     415: lconst_1
     416: ladd
     417: ldc2_w        #255                // long 4000000l
     420: lmul
     421: ladd
     422: invokestatic  #78                 // Method java/lang/Long.valueOf:(J)Ljava/lang/Long;
     425: invokestatic  #260                // Method kotlin/collections/CollectionsKt.listOf:(Ljava/lang/Object;)Ljava/util/List;
     428: checkcast     #31                 // class java/lang/Iterable
     431: invokestatic  #263                // Method kotlin/collections/CollectionsKt.plus:(Ljava/util/Collection;Ljava/lang/Iterable;)Ljava/util/List;
     434: astore        7
     436: goto          41
     439: aload         7
     441: nop
     442: nop
     443: areturn

  public static final void main();
    Code:
       0: iconst_0
       1: istore_0
       2: invokestatic  #286                // Method java/lang/System.currentTimeMillis:()J
       5: lstore_1
       6: iconst_0
       7: istore_3
       8: new           #288                // class java/lang/StringBuilder
      11: dup
      12: invokespecial #289                // Method java/lang/StringBuilder."<init>":()V
      15: ldc_w         #291                // String In the row where y=2000000, the  positions cannot contain a beacon are:
      18: invokevirtual #295                // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      21: ldc2_w        #296                // long 10l
      24: invokestatic  #299                // Method findBeaconFreePoints:(J)I
      27: invokevirtual #302                // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
      30: invokevirtual #306                // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
      33: getstatic     #310                // Field java/lang/System.out:Ljava/io/PrintStream;
      36: swap
      37: invokevirtual #315                // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
      40: ldc2_w        #255                // long 4000000l
      43: invokestatic  #317                // Method findBeacon:(J)Ljava/util/List;
      46: astore        4
      48: aload         4
      50: invokeinterface #248,  1          // InterfaceMethod java/util/List.size:()I
      55: iconst_1
      56: if_icmple     72
      59: ldc_w         #319                // String I did not find any beacon
      62: getstatic     #310                // Field java/lang/System.out:Ljava/io/PrintStream;
      65: swap
      66: invokevirtual #315                // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
      69: goto          109
      72: new           #288                // class java/lang/StringBuilder
      75: dup
      76: invokespecial #289                // Method java/lang/StringBuilder."<init>":()V
      79: ldc_w         #321                // String The beacon tuning frequency is:
      82: invokevirtual #295                // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      85: aload         4
      87: invokestatic  #39                 // Method kotlin/collections/CollectionsKt.first:(Ljava/util/List;)Ljava/lang/Object;
      90: checkcast     #207                // class java/lang/Number
      93: invokevirtual #210                // Method java/lang/Number.longValue:()J
      96: invokevirtual #324                // Method java/lang/StringBuilder.append:(J)Ljava/lang/StringBuilder;
      99: invokevirtual #306                // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
     102: getstatic     #310                // Field java/lang/System.out:Ljava/io/PrintStream;
     105: swap
     106: invokevirtual #315                // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
     109: nop
     110: nop
     111: invokestatic  #286                // Method java/lang/System.currentTimeMillis:()J
     114: lload_1
     115: lsub
     116: lstore_1
     117: iconst_0
     118: istore_3
     119: new           #288                // class java/lang/StringBuilder
     122: dup
     123: invokespecial #289                // Method java/lang/StringBuilder."<init>":()V
     126: ldc_w         #326                // String The whole process took:
     129: invokevirtual #295                // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     132: lload_1
     133: invokevirtual #324                // Method java/lang/StringBuilder.append:(J)Ljava/lang/StringBuilder;
     136: ldc_w         #328                // String  milliseconds
     139: invokevirtual #295                // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     142: invokevirtual #306                // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
     145: getstatic     #310                // Field java/lang/System.out:Ljava/io/PrintStream;
     148: swap
     149: invokevirtual #315                // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
     152: nop
     153: nop
     154: return

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


package day13

import java.io.File

abstract class Node : Comparable<Node>

class IntNode(
    val value: Int
): Node() {
    override fun compareTo(other: Node): Int =
        if (other is IntNode) this.value.compareTo(other.value)
        else ListNode(listOf(this)).compareTo(other)
}

class ListNode(
    val children: List<Node>
): Node() {
    override fun compareTo(other: Node): Int =
        if(other is IntNode) this.compareTo(ListNode(listOf(other)))
        else {
            if (this.children.isEmpty() && (other as ListNode).children.isEmpty()) 0
            else if (this.children.isEmpty()) -1
            else if ((other as ListNode).children.isEmpty()) 1
            else if (this.children.first().compareTo(other.children.first()) == 0)
                ListNode(this.children.drop(1)).compareTo(ListNode(other.children.drop(1)))
            else this.children.first().compareTo(other.children.first())
        }
}

//fun compareTrees(tree1: Node, tree2: Node): Int =
//    if(tree1 is IntNode && tree2 is IntNode) tree1.value.compareTo(tree2.value)
//    else if (tree1 is ListNode && tree2 is IntNode) compareTrees(tree1, ListNode(listOf(tree2)))
//    else if (tree1 is IntNode && tree2 is ListNode) compareTrees(ListNode(listOf(tree1)), tree2)
//    else {
//        val l1 = tree1 as ListNode
//        val l2 = tree2 as ListNode
//        if (l1.children.isEmpty() && l2.children.isEmpty()) 0
//        else if (l1.children.isEmpty()) -1
//        else if (l2.children.isEmpty()) 1
//        else if (compareTrees(l1.children.first(), l2.children.first()) == 0)
//            compareTrees(ListNode(l1.children.drop(1)), ListNode(l2.children.drop(1)))
//        else compareTrees(l1.children.first(), l2.children.first())
//    }

tailrec fun parseListContent(rawContent: String, innerLists: List<Char> = emptyList(), parsed: List<String> = emptyList()): List<String> =
    if (rawContent.isEmpty()) parsed
    else if(rawContent.startsWith("[")) parseListContent(
        rawContent.substring(1, rawContent.length),
        innerLists + '[',
        if (innerLists.isEmpty())
            parsed + listOf("[")
        else
            parsed.dropLast(1) + listOf(parsed.last() + '[')
    )
    else if(innerLists.isEmpty() && rawContent.startsWith(",")) parseListContent(
        rawContent.substring(1, rawContent.length),
        innerLists,
        parsed)
    else if(innerLists.isEmpty() && rawContent.matches(Regex("^[0-9]+.*"))) parseListContent(
        rawContent.replace(Regex("^[0-9]+"), ""),
        innerLists,
        parsed + listOf(Regex("^[0-9]+").find(rawContent)!!.value))
    else if(rawContent.startsWith("]")) parseListContent(
        rawContent.substring(1, rawContent.length),
        innerLists.dropLast(1),
        parsed.dropLast(1) + listOf(parsed.last() + "]"))
    else parseListContent(
        rawContent.substring(1, rawContent.length),
        innerLists,
        parsed.dropLast(1) + listOf(parsed.last() + rawContent[0]))

fun parseTree(rawString: String): Node =
    if(rawString.first() == '[' && rawString.last() == ']') {
        val rawParams = parseListContent(rawString.substring(1, rawString.length-1))
        val nodes = rawParams.map { parseTree(it) }
        ListNode(nodes)
    } else if (rawString.matches(Regex("^[0-9]+$"))) IntNode(rawString.toInt())
    else throw Exception("Invalid state")

fun parseInputFile() =
    File("src/main/kotlin/day13/input.txt")
        .readText()
        .split(Regex("\r?\n"))
        .filter{ it.isNotBlank() }
        .fold(emptyList<Node>()) { acc, it ->
            acc + parseTree(it)
        }

fun findSumOfIndicesInRightOrder() =
    parseInputFile()
        .chunked(2)
        .foldIndexed(0) { index, acc, it ->
            if(it[0] < it[1]) acc + index + 1
            else acc
        }

fun findDecoderKey() =
    parseInputFile().let{
        val dividerStart = ListNode(listOf(ListNode(listOf(IntNode(2)))))
        val dividerEnd = ListNode(listOf(ListNode(listOf(IntNode(6)))))
        val sortedList = (it + listOf(dividerStart, dividerEnd)).sorted()
        sortedList.foldIndexed(Pair(0, 0)) { index, acc, node ->
            Pair(
                index.takeIf { node.compareTo(dividerStart) == 0 } ?: acc.first,
                index.takeIf { node.compareTo(dividerEnd) == 0 } ?: acc.second
            )
        }.let { p -> (p.first+1) * (p.second+1) }
    }

fun main() {
    println("The sum of the indices of the ordered pairs is ${findSumOfIndicesInRightOrder()}")
    println("The decoder key for the distress signal is ${findDecoderKey()}")
}

Compiled from "Solution.kt"
public final class day13.SolutionKt {
  public static final java.util.List<java.lang.String> parseListContent(java.lang.String, java.util.List<java.lang.Character>, java.util.List<java.lang.String>);
    Code:
       0: aload_0
       1: ldc           #10                 // String rawContent
       3: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_1
       7: ldc           #18                 // String innerLists
       9: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
      12: aload_2
      13: ldc           #20                 // String parsed
      15: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
      18: aload_0
      19: checkcast     #22                 // class java/lang/CharSequence
      22: invokeinterface #26,  1           // InterfaceMethod java/lang/CharSequence.length:()I
      27: ifne          34
      30: iconst_1
      31: goto          35
      34: iconst_0
      35: ifeq          42
      38: aload_2
      39: goto          505
      42: aload_0
      43: ldc           #28                 // String [
      45: iconst_0
      46: iconst_2
      47: aconst_null
      48: invokestatic  #34                 // Method kotlin/text/StringsKt.startsWith$default:(Ljava/lang/String;Ljava/lang/String;ZILjava/lang/Object;)Z
      51: ifeq          166
      54: aload_0
      55: iconst_1
      56: aload_0
      57: invokevirtual #37                 // Method java/lang/String.length:()I
      60: invokevirtual #41                 // Method java/lang/String.substring:(II)Ljava/lang/String;
      63: dup
      64: ldc           #43                 // String substring(...)
      66: invokestatic  #46                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
      69: astore_3
      70: aload_1
      71: checkcast     #48                 // class java/util/Collection
      74: bipush        91
      76: invokestatic  #54                 // Method java/lang/Character.valueOf:(C)Ljava/lang/Character;
      79: invokestatic  #60                 // Method kotlin/collections/CollectionsKt.plus:(Ljava/util/Collection;Ljava/lang/Object;)Ljava/util/List;
      82: astore        4
      84: aload_1
      85: invokeinterface #66,  1           // InterfaceMethod java/util/List.isEmpty:()Z
      90: ifeq          111
      93: aload_2
      94: checkcast     #48                 // class java/util/Collection
      97: ldc           #28                 // String [
      99: invokestatic  #70                 // Method kotlin/collections/CollectionsKt.listOf:(Ljava/lang/Object;)Ljava/util/List;
     102: checkcast     #72                 // class java/lang/Iterable
     105: invokestatic  #75                 // Method kotlin/collections/CollectionsKt.plus:(Ljava/util/Collection;Ljava/lang/Iterable;)Ljava/util/List;
     108: goto          153
     111: aload_2
     112: iconst_1
     113: invokestatic  #79                 // Method kotlin/collections/CollectionsKt.dropLast:(Ljava/util/List;I)Ljava/util/List;
     116: checkcast     #48                 // class java/util/Collection
     119: new           #81                 // class java/lang/StringBuilder
     122: dup
     123: invokespecial #85                 // Method java/lang/StringBuilder."<init>":()V
     126: aload_2
     127: invokestatic  #89                 // Method kotlin/collections/CollectionsKt.last:(Ljava/util/List;)Ljava/lang/Object;
     130: checkcast     #36                 // class java/lang/String
     133: invokevirtual #93                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     136: bipush        91
     138: invokevirtual #96                 // Method java/lang/StringBuilder.append:(C)Ljava/lang/StringBuilder;
     141: invokevirtual #100                // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
     144: invokestatic  #70                 // Method kotlin/collections/CollectionsKt.listOf:(Ljava/lang/Object;)Ljava/util/List;
     147: checkcast     #72                 // class java/lang/Iterable
     150: invokestatic  #75                 // Method kotlin/collections/CollectionsKt.plus:(Ljava/util/Collection;Ljava/lang/Iterable;)Ljava/util/List;
     153: astore        5
     155: aload_3
     156: astore_0
     157: aload         4
     159: astore_1
     160: aload         5
     162: astore_2
     163: goto          18
     166: aload_1
     167: invokeinterface #66,  1           // InterfaceMethod java/util/List.isEmpty:()Z
     172: ifeq          220
     175: aload_0
     176: ldc           #102                // String ,
     178: iconst_0
     179: iconst_2
     180: aconst_null
     181: invokestatic  #34                 // Method kotlin/text/StringsKt.startsWith$default:(Ljava/lang/String;Ljava/lang/String;ZILjava/lang/Object;)Z
     184: ifeq          220
     187: aload_0
     188: iconst_1
     189: aload_0
     190: invokevirtual #37                 // Method java/lang/String.length:()I
     193: invokevirtual #41                 // Method java/lang/String.substring:(II)Ljava/lang/String;
     196: dup
     197: ldc           #43                 // String substring(...)
     199: invokestatic  #46                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
     202: astore_3
     203: aload_1
     204: astore        4
     206: aload_2
     207: astore        5
     209: aload_3
     210: astore_0
     211: aload         4
     213: astore_1
     214: aload         5
     216: astore_2
     217: goto          18
     220: aload_1
     221: invokeinterface #66,  1           // InterfaceMethod java/util/List.isEmpty:()Z
     226: ifeq          338
     229: aload_0
     230: checkcast     #22                 // class java/lang/CharSequence
     233: astore_3
     234: new           #104                // class kotlin/text/Regex
     237: dup
     238: ldc           #106                // String ^[0-9]+.*
     240: invokespecial #109                // Method kotlin/text/Regex."<init>":(Ljava/lang/String;)V
     243: aload_3
     244: invokevirtual #113                // Method kotlin/text/Regex.matches:(Ljava/lang/CharSequence;)Z
     247: ifeq          338
     250: aload_0
     251: checkcast     #22                 // class java/lang/CharSequence
     254: astore        4
     256: new           #104                // class kotlin/text/Regex
     259: dup
     260: ldc           #115                // String ^[0-9]+
     262: invokespecial #109                // Method kotlin/text/Regex."<init>":(Ljava/lang/String;)V
     265: astore        5
     267: ldc           #117                // String
     269: astore        6
     271: aload         5
     273: aload         4
     275: aload         6
     277: invokevirtual #121                // Method kotlin/text/Regex.replace:(Ljava/lang/CharSequence;Ljava/lang/String;)Ljava/lang/String;
     280: astore_3
     281: aload_1
     282: astore        4
     284: aload_2
     285: checkcast     #48                 // class java/util/Collection
     288: new           #104                // class kotlin/text/Regex
     291: dup
     292: ldc           #115                // String ^[0-9]+
     294: invokespecial #109                // Method kotlin/text/Regex."<init>":(Ljava/lang/String;)V
     297: aload_0
     298: checkcast     #22                 // class java/lang/CharSequence
     301: iconst_0
     302: iconst_2
     303: aconst_null
     304: invokestatic  #125                // Method kotlin/text/Regex.find$default:(Lkotlin/text/Regex;Ljava/lang/CharSequence;IILjava/lang/Object;)Lkotlin/text/MatchResult;
     307: dup
     308: invokestatic  #129                // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
     311: invokeinterface #134,  1          // InterfaceMethod kotlin/text/MatchResult.getValue:()Ljava/lang/String;
     316: invokestatic  #70                 // Method kotlin/collections/CollectionsKt.listOf:(Ljava/lang/Object;)Ljava/util/List;
     319: checkcast     #72                 // class java/lang/Iterable
     322: invokestatic  #75                 // Method kotlin/collections/CollectionsKt.plus:(Ljava/util/Collection;Ljava/lang/Iterable;)Ljava/util/List;
     325: astore        5
     327: aload_3
     328: astore_0
     329: aload         4
     331: astore_1
     332: aload         5
     334: astore_2
     335: goto          18
     338: aload_0
     339: ldc           #136                // String ]
     341: iconst_0
     342: iconst_2
     343: aconst_null
     344: invokestatic  #34                 // Method kotlin/text/StringsKt.startsWith$default:(Ljava/lang/String;Ljava/lang/String;ZILjava/lang/Object;)Z
     347: ifeq          428
     350: aload_0
     351: iconst_1
     352: aload_0
     353: invokevirtual #37                 // Method java/lang/String.length:()I
     356: invokevirtual #41                 // Method java/lang/String.substring:(II)Ljava/lang/String;
     359: dup
     360: ldc           #43                 // String substring(...)
     362: invokestatic  #46                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
     365: astore_3
     366: aload_1
     367: iconst_1
     368: invokestatic  #79                 // Method kotlin/collections/CollectionsKt.dropLast:(Ljava/util/List;I)Ljava/util/List;
     371: astore        4
     373: aload_2
     374: iconst_1
     375: invokestatic  #79                 // Method kotlin/collections/CollectionsKt.dropLast:(Ljava/util/List;I)Ljava/util/List;
     378: checkcast     #48                 // class java/util/Collection
     381: new           #81                 // class java/lang/StringBuilder
     384: dup
     385: invokespecial #85                 // Method java/lang/StringBuilder."<init>":()V
     388: aload_2
     389: invokestatic  #89                 // Method kotlin/collections/CollectionsKt.last:(Ljava/util/List;)Ljava/lang/Object;
     392: checkcast     #36                 // class java/lang/String
     395: invokevirtual #93                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     398: bipush        93
     400: invokevirtual #96                 // Method java/lang/StringBuilder.append:(C)Ljava/lang/StringBuilder;
     403: invokevirtual #100                // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
     406: invokestatic  #70                 // Method kotlin/collections/CollectionsKt.listOf:(Ljava/lang/Object;)Ljava/util/List;
     409: checkcast     #72                 // class java/lang/Iterable
     412: invokestatic  #75                 // Method kotlin/collections/CollectionsKt.plus:(Ljava/util/Collection;Ljava/lang/Iterable;)Ljava/util/List;
     415: astore        5
     417: aload_3
     418: astore_0
     419: aload         4
     421: astore_1
     422: aload         5
     424: astore_2
     425: goto          18
     428: aload_0
     429: iconst_1
     430: aload_0
     431: invokevirtual #37                 // Method java/lang/String.length:()I
     434: invokevirtual #41                 // Method java/lang/String.substring:(II)Ljava/lang/String;
     437: dup
     438: ldc           #43                 // String substring(...)
     440: invokestatic  #46                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
     443: astore_3
     444: aload_1
     445: astore        4
     447: aload_2
     448: iconst_1
     449: invokestatic  #79                 // Method kotlin/collections/CollectionsKt.dropLast:(Ljava/util/List;I)Ljava/util/List;
     452: checkcast     #48                 // class java/util/Collection
     455: new           #81                 // class java/lang/StringBuilder
     458: dup
     459: invokespecial #85                 // Method java/lang/StringBuilder."<init>":()V
     462: aload_2
     463: invokestatic  #89                 // Method kotlin/collections/CollectionsKt.last:(Ljava/util/List;)Ljava/lang/Object;
     466: checkcast     #36                 // class java/lang/String
     469: invokevirtual #93                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     472: aload_0
     473: iconst_0
     474: invokevirtual #140                // Method java/lang/String.charAt:(I)C
     477: invokevirtual #96                 // Method java/lang/StringBuilder.append:(C)Ljava/lang/StringBuilder;
     480: invokevirtual #100                // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
     483: invokestatic  #70                 // Method kotlin/collections/CollectionsKt.listOf:(Ljava/lang/Object;)Ljava/util/List;
     486: checkcast     #72                 // class java/lang/Iterable
     489: invokestatic  #75                 // Method kotlin/collections/CollectionsKt.plus:(Ljava/util/Collection;Ljava/lang/Iterable;)Ljava/util/List;
     492: astore        5
     494: aload_3
     495: astore_0
     496: aload         4
     498: astore_1
     499: aload         5
     501: astore_2
     502: goto          18
     505: areturn

  public static java.util.List parseListContent$default(java.lang.String, java.util.List, java.util.List, int, java.lang.Object);
    Code:
       0: iload_3
       1: iconst_2
       2: iand
       3: ifeq          10
       6: invokestatic  #148                // Method kotlin/collections/CollectionsKt.emptyList:()Ljava/util/List;
       9: astore_1
      10: iload_3
      11: iconst_4
      12: iand
      13: ifeq          20
      16: invokestatic  #148                // Method kotlin/collections/CollectionsKt.emptyList:()Ljava/util/List;
      19: astore_2
      20: aload_0
      21: aload_1
      22: aload_2
      23: invokestatic  #150                // Method parseListContent:(Ljava/lang/String;Ljava/util/List;Ljava/util/List;)Ljava/util/List;
      26: areturn

  public static final day13.Node parseTree(java.lang.String);
    Code:
       0: aload_0
       1: ldc           #154                // String rawString
       3: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: checkcast     #22                 // class java/lang/CharSequence
      10: invokestatic  #158                // Method kotlin/text/StringsKt.first:(Ljava/lang/CharSequence;)C
      13: bipush        91
      15: if_icmpne     168
      18: aload_0
      19: checkcast     #22                 // class java/lang/CharSequence
      22: invokestatic  #160                // Method kotlin/text/StringsKt.last:(Ljava/lang/CharSequence;)C
      25: bipush        93
      27: if_icmpne     168
      30: aload_0
      31: iconst_1
      32: aload_0
      33: invokevirtual #37                 // Method java/lang/String.length:()I
      36: iconst_1
      37: isub
      38: invokevirtual #41                 // Method java/lang/String.substring:(II)Ljava/lang/String;
      41: dup
      42: ldc           #43                 // String substring(...)
      44: invokestatic  #46                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
      47: aconst_null
      48: aconst_null
      49: bipush        6
      51: aconst_null
      52: invokestatic  #162                // Method parseListContent$default:(Ljava/lang/String;Ljava/util/List;Ljava/util/List;ILjava/lang/Object;)Ljava/util/List;
      55: astore_1
      56: aload_1
      57: checkcast     #72                 // class java/lang/Iterable
      60: astore_3
      61: iconst_0
      62: istore        4
      64: aload_3
      65: astore        5
      67: new           #164                // class java/util/ArrayList
      70: dup
      71: aload_3
      72: bipush        10
      74: invokestatic  #168                // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
      77: invokespecial #171                // Method java/util/ArrayList."<init>":(I)V
      80: checkcast     #48                 // class java/util/Collection
      83: astore        6
      85: iconst_0
      86: istore        7
      88: aload         5
      90: invokeinterface #175,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      95: astore        8
      97: aload         8
      99: invokeinterface #180,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
     104: ifeq          147
     107: aload         8
     109: invokeinterface #184,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     114: astore        9
     116: aload         6
     118: aload         9
     120: checkcast     #36                 // class java/lang/String
     123: astore        10
     125: astore        12
     127: iconst_0
     128: istore        11
     130: aload         10
     132: invokestatic  #186                // Method parseTree:(Ljava/lang/String;)Lday13/Node;
     135: aload         12
     137: swap
     138: invokeinterface #190,  2          // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     143: pop
     144: goto          97
     147: aload         6
     149: checkcast     #62                 // class java/util/List
     152: nop
     153: astore_2
     154: new           #192                // class day13/ListNode
     157: dup
     158: aload_2
     159: invokespecial #195                // Method day13/ListNode."<init>":(Ljava/util/List;)V
     162: checkcast     #197                // class day13/Node
     165: goto          216
     168: aload_0
     169: checkcast     #22                 // class java/lang/CharSequence
     172: astore_1
     173: new           #104                // class kotlin/text/Regex
     176: dup
     177: ldc           #199                // String ^[0-9]+$
     179: invokespecial #109                // Method kotlin/text/Regex."<init>":(Ljava/lang/String;)V
     182: aload_1
     183: invokevirtual #113                // Method kotlin/text/Regex.matches:(Ljava/lang/CharSequence;)Z
     186: ifeq          206
     189: new           #201                // class day13/IntNode
     192: dup
     193: aload_0
     194: invokestatic  #207                // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
     197: invokespecial #208                // Method day13/IntNode."<init>":(I)V
     200: checkcast     #197                // class day13/Node
     203: goto          216
     206: new           #210                // class java/lang/Exception
     209: dup
     210: ldc           #212                // String Invalid state
     212: invokespecial #213                // Method java/lang/Exception."<init>":(Ljava/lang/String;)V
     215: athrow
     216: areturn

  public static final java.util.List<day13.Node> parseInputFile();
    Code:
       0: new           #231                // class java/io/File
       3: dup
       4: ldc           #233                // String src/main/kotlin/day13/input.txt
       6: invokespecial #234                // Method java/io/File."<init>":(Ljava/lang/String;)V
       9: aconst_null
      10: iconst_1
      11: aconst_null
      12: invokestatic  #240                // Method kotlin/io/FilesKt.readText$default:(Ljava/io/File;Ljava/nio/charset/Charset;ILjava/lang/Object;)Ljava/lang/String;
      15: checkcast     #22                 // class java/lang/CharSequence
      18: astore_0
      19: new           #104                // class kotlin/text/Regex
      22: dup
      23: ldc           #242                // String \r?\n
      25: invokespecial #109                // Method kotlin/text/Regex."<init>":(Ljava/lang/String;)V
      28: astore_1
      29: iconst_0
      30: istore_2
      31: aload_1
      32: aload_0
      33: iload_2
      34: invokevirtual #246                // Method kotlin/text/Regex.split:(Ljava/lang/CharSequence;I)Ljava/util/List;
      37: checkcast     #72                 // class java/lang/Iterable
      40: astore_0
      41: nop
      42: iconst_0
      43: istore_1
      44: aload_0
      45: astore_2
      46: new           #164                // class java/util/ArrayList
      49: dup
      50: invokespecial #247                // Method java/util/ArrayList."<init>":()V
      53: checkcast     #48                 // class java/util/Collection
      56: astore_3
      57: iconst_0
      58: istore        4
      60: aload_2
      61: invokeinterface #175,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      66: astore        5
      68: aload         5
      70: invokeinterface #180,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
      75: ifeq          129
      78: aload         5
      80: invokeinterface #184,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      85: astore        6
      87: aload         6
      89: checkcast     #36                 // class java/lang/String
      92: astore        7
      94: iconst_0
      95: istore        8
      97: aload         7
      99: checkcast     #22                 // class java/lang/CharSequence
     102: invokestatic  #250                // Method kotlin/text/StringsKt.isBlank:(Ljava/lang/CharSequence;)Z
     105: ifne          112
     108: iconst_1
     109: goto          113
     112: iconst_0
     113: nop
     114: ifeq          68
     117: aload_3
     118: aload         6
     120: invokeinterface #190,  2          // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     125: pop
     126: goto          68
     129: aload_3
     130: checkcast     #62                 // class java/util/List
     133: nop
     134: checkcast     #72                 // class java/lang/Iterable
     137: astore_0
     138: invokestatic  #148                // Method kotlin/collections/CollectionsKt.emptyList:()Ljava/util/List;
     141: astore_1
     142: iconst_0
     143: istore_2
     144: aload_1
     145: astore_3
     146: aload_0
     147: invokeinterface #175,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     152: astore        4
     154: aload         4
     156: invokeinterface #180,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
     161: ifeq          203
     164: aload         4
     166: invokeinterface #184,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     171: astore        5
     173: aload_3
     174: aload         5
     176: checkcast     #36                 // class java/lang/String
     179: astore        6
     181: astore        7
     183: iconst_0
     184: istore        8
     186: aload         7
     188: checkcast     #48                 // class java/util/Collection
     191: aload         6
     193: invokestatic  #186                // Method parseTree:(Ljava/lang/String;)Lday13/Node;
     196: invokestatic  #60                 // Method kotlin/collections/CollectionsKt.plus:(Ljava/util/Collection;Ljava/lang/Object;)Ljava/util/List;
     199: astore_3
     200: goto          154
     203: aload_3
     204: areturn

  public static final int findSumOfIndicesInRightOrder();
    Code:
       0: invokestatic  #266                // Method parseInputFile:()Ljava/util/List;
       3: checkcast     #72                 // class java/lang/Iterable
       6: iconst_2
       7: invokestatic  #270                // Method kotlin/collections/CollectionsKt.chunked:(Ljava/lang/Iterable;I)Ljava/util/List;
      10: checkcast     #72                 // class java/lang/Iterable
      13: astore_0
      14: iconst_0
      15: istore_1
      16: iconst_0
      17: istore_2
      18: iconst_0
      19: istore_3
      20: iload_1
      21: istore        4
      23: aload_0
      24: invokeinterface #175,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      29: astore        5
      31: aload         5
      33: invokeinterface #180,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
      38: ifeq          124
      41: aload         5
      43: invokeinterface #184,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      48: astore        6
      50: iload_3
      51: iinc          3, 1
      54: istore        7
      56: iload         7
      58: ifge          64
      61: invokestatic  #273                // Method kotlin/collections/CollectionsKt.throwIndexOverflow:()V
      64: iload         7
      66: iload         4
      68: aload         6
      70: checkcast     #62                 // class java/util/List
      73: astore        8
      75: istore        9
      77: istore        10
      79: iconst_0
      80: istore        11
      82: aload         8
      84: iconst_0
      85: invokeinterface #277,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      90: checkcast     #197                // class day13/Node
      93: aload         8
      95: iconst_1
      96: invokeinterface #277,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     101: invokevirtual #281                // Method day13/Node.compareTo:(Ljava/lang/Object;)I
     104: ifge          117
     107: iload         9
     109: iload         10
     111: iadd
     112: iconst_1
     113: iadd
     114: goto          119
     117: iload         9
     119: istore        4
     121: goto          31
     124: iload         4
     126: ireturn

  public static final int findDecoderKey();
    Code:
       0: invokestatic  #266                // Method parseInputFile:()Ljava/util/List;
       3: astore_0
       4: iconst_0
       5: istore_1
       6: new           #192                // class day13/ListNode
       9: dup
      10: new           #192                // class day13/ListNode
      13: dup
      14: new           #201                // class day13/IntNode
      17: dup
      18: iconst_2
      19: invokespecial #208                // Method day13/IntNode."<init>":(I)V
      22: invokestatic  #70                 // Method kotlin/collections/CollectionsKt.listOf:(Ljava/lang/Object;)Ljava/util/List;
      25: invokespecial #195                // Method day13/ListNode."<init>":(Ljava/util/List;)V
      28: invokestatic  #70                 // Method kotlin/collections/CollectionsKt.listOf:(Ljava/lang/Object;)Ljava/util/List;
      31: invokespecial #195                // Method day13/ListNode."<init>":(Ljava/util/List;)V
      34: astore_2
      35: new           #192                // class day13/ListNode
      38: dup
      39: new           #192                // class day13/ListNode
      42: dup
      43: new           #201                // class day13/IntNode
      46: dup
      47: bipush        6
      49: invokespecial #208                // Method day13/IntNode."<init>":(I)V
      52: invokestatic  #70                 // Method kotlin/collections/CollectionsKt.listOf:(Ljava/lang/Object;)Ljava/util/List;
      55: invokespecial #195                // Method day13/ListNode."<init>":(Ljava/util/List;)V
      58: invokestatic  #70                 // Method kotlin/collections/CollectionsKt.listOf:(Ljava/lang/Object;)Ljava/util/List;
      61: invokespecial #195                // Method day13/ListNode."<init>":(Ljava/util/List;)V
      64: astore_3
      65: aload_0
      66: checkcast     #48                 // class java/util/Collection
      69: iconst_2
      70: anewarray     #192                // class day13/ListNode
      73: astore        4
      75: aload         4
      77: iconst_0
      78: aload_2
      79: aastore
      80: aload         4
      82: iconst_1
      83: aload_3
      84: aastore
      85: aload         4
      87: invokestatic  #290                // Method kotlin/collections/CollectionsKt.listOf:([Ljava/lang/Object;)Ljava/util/List;
      90: checkcast     #72                 // class java/lang/Iterable
      93: invokestatic  #75                 // Method kotlin/collections/CollectionsKt.plus:(Ljava/util/Collection;Ljava/lang/Iterable;)Ljava/util/List;
      96: checkcast     #72                 // class java/lang/Iterable
      99: invokestatic  #294                // Method kotlin/collections/CollectionsKt.sorted:(Ljava/lang/Iterable;)Ljava/util/List;
     102: astore        5
     104: aload         5
     106: checkcast     #72                 // class java/lang/Iterable
     109: astore        4
     111: new           #296                // class kotlin/Pair
     114: dup
     115: iconst_0
     116: invokestatic  #299                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     119: iconst_0
     120: invokestatic  #299                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     123: invokespecial #302                // Method kotlin/Pair."<init>":(Ljava/lang/Object;Ljava/lang/Object;)V
     126: astore        6
     128: iconst_0
     129: istore        7
     131: iconst_0
     132: istore        8
     134: aload         6
     136: astore        9
     138: aload         4
     140: invokeinterface #175,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     145: astore        10
     147: aload         10
     149: invokeinterface #180,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
     154: ifeq          364
     157: aload         10
     159: invokeinterface #184,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     164: astore        11
     166: iload         8
     168: iinc          8, 1
     171: istore        12
     173: iload         12
     175: ifge          181
     178: invokestatic  #273                // Method kotlin/collections/CollectionsKt.throwIndexOverflow:()V
     181: iload         12
     183: aload         9
     185: aload         11
     187: checkcast     #197                // class day13/Node
     190: astore        13
     192: astore        14
     194: istore        15
     196: iconst_0
     197: istore        16
     199: iload         15
     201: invokestatic  #299                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     204: astore        17
     206: aload         17
     208: checkcast     #304                // class java/lang/Number
     211: invokevirtual #307                // Method java/lang/Number.intValue:()I
     214: istore        18
     216: iconst_0
     217: istore        19
     219: aload         13
     221: aload_2
     222: invokevirtual #281                // Method day13/Node.compareTo:(Ljava/lang/Object;)I
     225: ifne          232
     228: iconst_1
     229: goto          233
     232: iconst_0
     233: ifeq          241
     236: aload         17
     238: goto          242
     241: aconst_null
     242: dup
     243: ifnull        252
     246: invokevirtual #308                // Method java/lang/Integer.intValue:()I
     249: goto          264
     252: pop
     253: aload         14
     255: invokevirtual #311                // Method kotlin/Pair.getFirst:()Ljava/lang/Object;
     258: checkcast     #304                // class java/lang/Number
     261: invokevirtual #307                // Method java/lang/Number.intValue:()I
     264: invokestatic  #299                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     267: iload         15
     269: invokestatic  #299                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     272: astore        17
     274: aload         17
     276: checkcast     #304                // class java/lang/Number
     279: invokevirtual #307                // Method java/lang/Number.intValue:()I
     282: istore        18
     284: astore        20
     286: iconst_0
     287: istore        19
     289: aload         13
     291: aload_3
     292: invokevirtual #281                // Method day13/Node.compareTo:(Ljava/lang/Object;)I
     295: ifne          302
     298: iconst_1
     299: goto          303
     302: iconst_0
     303: istore        21
     305: aload         20
     307: iload         21
     309: ifeq          317
     312: aload         17
     314: goto          318
     317: aconst_null
     318: dup
     319: ifnull        328
     322: invokevirtual #308                // Method java/lang/Integer.intValue:()I
     325: goto          340
     328: pop
     329: aload         14
     331: invokevirtual #314                // Method kotlin/Pair.getSecond:()Ljava/lang/Object;
     334: checkcast     #304                // class java/lang/Number
     337: invokevirtual #307                // Method java/lang/Number.intValue:()I
     340: invokestatic  #299                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     343: astore        22
     345: astore        23
     347: new           #296                // class kotlin/Pair
     350: dup
     351: aload         23
     353: aload         22
     355: invokespecial #302                // Method kotlin/Pair."<init>":(Ljava/lang/Object;Ljava/lang/Object;)V
     358: nop
     359: astore        9
     361: goto          147
     364: aload         9
     366: astore        6
     368: iconst_0
     369: istore        7
     371: aload         6
     373: invokevirtual #311                // Method kotlin/Pair.getFirst:()Ljava/lang/Object;
     376: checkcast     #304                // class java/lang/Number
     379: invokevirtual #307                // Method java/lang/Number.intValue:()I
     382: iconst_1
     383: iadd
     384: aload         6
     386: invokevirtual #314                // Method kotlin/Pair.getSecond:()Ljava/lang/Object;
     389: checkcast     #304                // class java/lang/Number
     392: invokevirtual #307                // Method java/lang/Number.intValue:()I
     395: iconst_1
     396: iadd
     397: imul
     398: nop
     399: nop
     400: nop
     401: ireturn

  public static final void main();
    Code:
       0: new           #81                 // class java/lang/StringBuilder
       3: dup
       4: invokespecial #85                 // Method java/lang/StringBuilder."<init>":()V
       7: ldc_w         #330                // String The sum of the indices of the ordered pairs is
      10: invokevirtual #93                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      13: invokestatic  #332                // Method findSumOfIndicesInRightOrder:()I
      16: invokevirtual #335                // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
      19: invokevirtual #100                // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
      22: getstatic     #341                // Field java/lang/System.out:Ljava/io/PrintStream;
      25: swap
      26: invokevirtual #346                // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
      29: new           #81                 // class java/lang/StringBuilder
      32: dup
      33: invokespecial #85                 // Method java/lang/StringBuilder."<init>":()V
      36: ldc_w         #348                // String The decoder key for the distress signal is
      39: invokevirtual #93                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      42: invokestatic  #350                // Method findDecoderKey:()I
      45: invokevirtual #335                // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
      48: invokevirtual #100                // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
      51: getstatic     #341                // Field java/lang/System.out:Ljava/io/PrintStream;
      54: swap
      55: invokevirtual #346                // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
      58: return

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


package day03

import java.io.File

fun getItemsPriorities() =
    ('a'..'z').foldIndexed(mapOf<Char, Int>()) { index, acc, c ->
        acc + (c to index+1)
    } + ('A' .. 'Z').foldIndexed(mapOf()) { index, acc, c ->
        acc + (c to index + 27)
    }

fun getItemsFromFile() =
    File("src/main/kotlin/day03/input.txt")
        .readLines()
        .fold(listOf<Pair<List<Char>, List<Char>>>()) { acc, line ->
            acc + Pair(
                line.subSequence(0, line.length/2).toList(),
                line.subSequence(line.length/2, line.length).toList()
            )
        }

fun findCommonItemAndSumPriorities() =
    getItemsFromFile()
        .fold(0) { acc, items ->
            (items.first.toSet().intersect(items.second.toSet())
                .firstOrNull()
                ?.let { getItemsPriorities()[it] } ?: 0) + acc
        }

fun findBadgesAndSumPriorities() =
    getItemsFromFile()
        .chunked(3)
        .fold(0) { acc, group ->
            (group.fold(emptySet<Char>()) { intersected, backpack ->
                intersected.ifEmpty {
                    backpack.first.toSet() + backpack.second.toSet()
                }.intersect(
                    backpack.first.toSet() + backpack.second.toSet()
                )
            }.firstOrNull()
                ?.let { getItemsPriorities()[it] } ?: 0) + acc
        }

fun main() {
    println("The sum of the priorities of the common items in the compartments is: ${findCommonItemAndSumPriorities()}")
    println("The sum of the priorities of the group badges is: ${findBadgesAndSumPriorities()}")
}

Compiled from "Solution.kt"
public final class day03.SolutionKt {
  public static final java.util.Map<java.lang.Character, java.lang.Integer> getItemsPriorities();
    Code:
       0: new           #10                 // class kotlin/ranges/CharRange
       3: dup
       4: bipush        97
       6: bipush        122
       8: invokespecial #14                 // Method kotlin/ranges/CharRange."<init>":(CC)V
      11: checkcast     #16                 // class java/lang/Iterable
      14: astore_0
      15: invokestatic  #21                 // Method kotlin/collections/MapsKt.emptyMap:()Ljava/util/Map;
      18: astore_1
      19: nop
      20: iconst_0
      21: istore_2
      22: iconst_0
      23: istore_3
      24: aload_1
      25: astore        4
      27: aload_0
      28: invokeinterface #25,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      33: astore        5
      35: aload         5
      37: invokeinterface #31,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      42: ifeq          109
      45: aload         5
      47: checkcast     #33                 // class kotlin/collections/CharIterator
      50: invokevirtual #37                 // Method kotlin/collections/CharIterator.nextChar:()C
      53: istore        6
      55: iload_3
      56: iinc          3, 1
      59: istore        7
      61: iload         7
      63: ifge          69
      66: invokestatic  #43                 // Method kotlin/collections/CollectionsKt.throwIndexOverflow:()V
      69: iload         7
      71: aload         4
      73: iload         6
      75: istore        8
      77: astore        9
      79: istore        10
      81: iconst_0
      82: istore        11
      84: aload         9
      86: iload         8
      88: invokestatic  #49                 // Method java/lang/Character.valueOf:(C)Ljava/lang/Character;
      91: iload         10
      93: iconst_1
      94: iadd
      95: invokestatic  #54                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      98: invokestatic  #60                 // Method kotlin/TuplesKt.to:(Ljava/lang/Object;Ljava/lang/Object;)Lkotlin/Pair;
     101: invokestatic  #64                 // Method kotlin/collections/MapsKt.plus:(Ljava/util/Map;Lkotlin/Pair;)Ljava/util/Map;
     104: astore        4
     106: goto          35
     109: aload         4
     111: new           #10                 // class kotlin/ranges/CharRange
     114: dup
     115: bipush        65
     117: bipush        90
     119: invokespecial #14                 // Method kotlin/ranges/CharRange."<init>":(CC)V
     122: checkcast     #16                 // class java/lang/Iterable
     125: astore_0
     126: invokestatic  #21                 // Method kotlin/collections/MapsKt.emptyMap:()Ljava/util/Map;
     129: astore_1
     130: astore        12
     132: iconst_0
     133: istore_2
     134: iconst_0
     135: istore_3
     136: aload_1
     137: astore        4
     139: aload_0
     140: invokeinterface #25,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     145: astore        5
     147: aload         5
     149: invokeinterface #31,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     154: ifeq          222
     157: aload         5
     159: checkcast     #33                 // class kotlin/collections/CharIterator
     162: invokevirtual #37                 // Method kotlin/collections/CharIterator.nextChar:()C
     165: istore        6
     167: iload_3
     168: iinc          3, 1
     171: istore        7
     173: iload         7
     175: ifge          181
     178: invokestatic  #43                 // Method kotlin/collections/CollectionsKt.throwIndexOverflow:()V
     181: iload         7
     183: aload         4
     185: iload         6
     187: istore        8
     189: astore        9
     191: istore        10
     193: iconst_0
     194: istore        11
     196: aload         9
     198: iload         8
     200: invokestatic  #49                 // Method java/lang/Character.valueOf:(C)Ljava/lang/Character;
     203: iload         10
     205: bipush        27
     207: iadd
     208: invokestatic  #54                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     211: invokestatic  #60                 // Method kotlin/TuplesKt.to:(Ljava/lang/Object;Ljava/lang/Object;)Lkotlin/Pair;
     214: invokestatic  #64                 // Method kotlin/collections/MapsKt.plus:(Ljava/util/Map;Lkotlin/Pair;)Ljava/util/Map;
     217: astore        4
     219: goto          147
     222: aload         4
     224: aload         12
     226: swap
     227: invokestatic  #67                 // Method kotlin/collections/MapsKt.plus:(Ljava/util/Map;Ljava/util/Map;)Ljava/util/Map;
     230: areturn

  public static final java.util.List<kotlin.Pair<java.util.List<java.lang.Character>, java.util.List<java.lang.Character>>> getItemsFromFile();
    Code:
       0: new           #90                 // class java/io/File
       3: dup
       4: ldc           #92                 // String src/main/kotlin/day03/input.txt
       6: invokespecial #95                 // Method java/io/File."<init>":(Ljava/lang/String;)V
       9: aconst_null
      10: iconst_1
      11: aconst_null
      12: invokestatic  #101                // Method kotlin/io/FilesKt.readLines$default:(Ljava/io/File;Ljava/nio/charset/Charset;ILjava/lang/Object;)Ljava/util/List;
      15: checkcast     #16                 // class java/lang/Iterable
      18: astore_0
      19: invokestatic  #104                // Method kotlin/collections/CollectionsKt.emptyList:()Ljava/util/List;
      22: astore_1
      23: nop
      24: iconst_0
      25: istore_2
      26: aload_1
      27: astore_3
      28: aload_0
      29: invokeinterface #25,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      34: astore        4
      36: aload         4
      38: invokeinterface #31,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      43: ifeq          124
      46: aload         4
      48: invokeinterface #108,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      53: astore        5
      55: aload_3
      56: aload         5
      58: checkcast     #110                // class java/lang/String
      61: astore        6
      63: astore        7
      65: iconst_0
      66: istore        8
      68: aload         7
      70: checkcast     #112                // class java/util/Collection
      73: new           #114                // class kotlin/Pair
      76: dup
      77: aload         6
      79: iconst_0
      80: aload         6
      82: invokevirtual #118                // Method java/lang/String.length:()I
      85: iconst_2
      86: idiv
      87: invokevirtual #122                // Method java/lang/String.subSequence:(II)Ljava/lang/CharSequence;
      90: invokestatic  #128                // Method kotlin/text/StringsKt.toList:(Ljava/lang/CharSequence;)Ljava/util/List;
      93: aload         6
      95: aload         6
      97: invokevirtual #118                // Method java/lang/String.length:()I
     100: iconst_2
     101: idiv
     102: aload         6
     104: invokevirtual #118                // Method java/lang/String.length:()I
     107: invokevirtual #122                // Method java/lang/String.subSequence:(II)Ljava/lang/CharSequence;
     110: invokestatic  #128                // Method kotlin/text/StringsKt.toList:(Ljava/lang/CharSequence;)Ljava/util/List;
     113: invokespecial #131                // Method kotlin/Pair."<init>":(Ljava/lang/Object;Ljava/lang/Object;)V
     116: invokestatic  #134                // Method kotlin/collections/CollectionsKt.plus:(Ljava/util/Collection;Ljava/lang/Object;)Ljava/util/List;
     119: nop
     120: astore_3
     121: goto          36
     124: aload_3
     125: areturn

  public static final int findCommonItemAndSumPriorities();
    Code:
       0: invokestatic  #145                // Method getItemsFromFile:()Ljava/util/List;
       3: checkcast     #16                 // class java/lang/Iterable
       6: astore_0
       7: iconst_0
       8: istore_1
       9: iconst_0
      10: istore_2
      11: iload_1
      12: istore_3
      13: aload_0
      14: invokeinterface #25,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      19: astore        4
      21: aload         4
      23: invokeinterface #31,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      28: ifeq          151
      31: aload         4
      33: invokeinterface #108,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      38: astore        5
      40: iload_3
      41: aload         5
      43: checkcast     #114                // class kotlin/Pair
      46: astore        6
      48: istore        7
      50: iconst_0
      51: istore        8
      53: aload         6
      55: invokevirtual #148                // Method kotlin/Pair.getFirst:()Ljava/lang/Object;
      58: checkcast     #16                 // class java/lang/Iterable
      61: invokestatic  #152                // Method kotlin/collections/CollectionsKt.toSet:(Ljava/lang/Iterable;)Ljava/util/Set;
      64: checkcast     #16                 // class java/lang/Iterable
      67: aload         6
      69: invokevirtual #155                // Method kotlin/Pair.getSecond:()Ljava/lang/Object;
      72: checkcast     #16                 // class java/lang/Iterable
      75: invokestatic  #152                // Method kotlin/collections/CollectionsKt.toSet:(Ljava/lang/Iterable;)Ljava/util/Set;
      78: checkcast     #16                 // class java/lang/Iterable
      81: invokestatic  #159                // Method kotlin/collections/CollectionsKt.intersect:(Ljava/lang/Iterable;Ljava/lang/Iterable;)Ljava/util/Set;
      84: checkcast     #16                 // class java/lang/Iterable
      87: invokestatic  #163                // Method kotlin/collections/CollectionsKt.firstOrNull:(Ljava/lang/Iterable;)Ljava/lang/Object;
      90: checkcast     #45                 // class java/lang/Character
      93: astore        9
      95: aload         9
      97: ifnull        142
     100: aload         9
     102: invokevirtual #166                // Method java/lang/Character.charValue:()C
     105: istore        10
     107: iconst_0
     108: istore        11
     110: invokestatic  #168                // Method getItemsPriorities:()Ljava/util/Map;
     113: iload         10
     115: invokestatic  #49                 // Method java/lang/Character.valueOf:(C)Ljava/lang/Character;
     118: invokeinterface #172,  2          // InterfaceMethod java/util/Map.get:(Ljava/lang/Object;)Ljava/lang/Object;
     123: checkcast     #51                 // class java/lang/Integer
     126: nop
     127: astore        12
     129: aload         12
     131: ifnull        142
     134: aload         12
     136: invokevirtual #175                // Method java/lang/Integer.intValue:()I
     139: goto          143
     142: iconst_0
     143: iload         7
     145: iadd
     146: nop
     147: istore_3
     148: goto          21
     151: iload_3
     152: ireturn

  public static final int findBadgesAndSumPriorities();
    Code:
       0: invokestatic  #145                // Method getItemsFromFile:()Ljava/util/List;
       3: checkcast     #16                 // class java/lang/Iterable
       6: iconst_3
       7: invokestatic  #185                // Method kotlin/collections/CollectionsKt.chunked:(Ljava/lang/Iterable;I)Ljava/util/List;
      10: checkcast     #16                 // class java/lang/Iterable
      13: astore_0
      14: iconst_0
      15: istore_1
      16: iconst_0
      17: istore_2
      18: iload_1
      19: istore_3
      20: aload_0
      21: invokeinterface #25,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      26: astore        4
      28: aload         4
      30: invokeinterface #31,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      35: ifeq          286
      38: aload         4
      40: invokeinterface #108,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      45: astore        5
      47: iload_3
      48: aload         5
      50: checkcast     #142                // class java/util/List
      53: astore        6
      55: istore        7
      57: iconst_0
      58: istore        8
      60: aload         6
      62: checkcast     #16                 // class java/lang/Iterable
      65: astore        9
      67: invokestatic  #191                // Method kotlin/collections/SetsKt.emptySet:()Ljava/util/Set;
      70: astore        10
      72: iconst_0
      73: istore        11
      75: aload         10
      77: astore        12
      79: aload         9
      81: invokeinterface #25,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      86: astore        13
      88: aload         13
      90: invokeinterface #31,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      95: ifeq          217
      98: aload         13
     100: invokeinterface #108,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     105: astore        14
     107: aload         12
     109: aload         14
     111: checkcast     #114                // class kotlin/Pair
     114: astore        15
     116: astore        16
     118: iconst_0
     119: istore        17
     121: aload         16
     123: checkcast     #112                // class java/util/Collection
     126: astore        18
     128: aload         18
     130: invokeinterface #194,  1          // InterfaceMethod java/util/Collection.isEmpty:()Z
     135: ifeq          172
     138: iconst_0
     139: istore        19
     141: aload         15
     143: invokevirtual #148                // Method kotlin/Pair.getFirst:()Ljava/lang/Object;
     146: checkcast     #16                 // class java/lang/Iterable
     149: invokestatic  #152                // Method kotlin/collections/CollectionsKt.toSet:(Ljava/lang/Iterable;)Ljava/util/Set;
     152: aload         15
     154: invokevirtual #155                // Method kotlin/Pair.getSecond:()Ljava/lang/Object;
     157: checkcast     #16                 // class java/lang/Iterable
     160: invokestatic  #152                // Method kotlin/collections/CollectionsKt.toSet:(Ljava/lang/Iterable;)Ljava/util/Set;
     163: checkcast     #16                 // class java/lang/Iterable
     166: invokestatic  #197                // Method kotlin/collections/SetsKt.plus:(Ljava/util/Set;Ljava/lang/Iterable;)Ljava/util/Set;
     169: goto          174
     172: aload         18
     174: checkcast     #16                 // class java/lang/Iterable
     177: aload         15
     179: invokevirtual #148                // Method kotlin/Pair.getFirst:()Ljava/lang/Object;
     182: checkcast     #16                 // class java/lang/Iterable
     185: invokestatic  #152                // Method kotlin/collections/CollectionsKt.toSet:(Ljava/lang/Iterable;)Ljava/util/Set;
     188: aload         15
     190: invokevirtual #155                // Method kotlin/Pair.getSecond:()Ljava/lang/Object;
     193: checkcast     #16                 // class java/lang/Iterable
     196: invokestatic  #152                // Method kotlin/collections/CollectionsKt.toSet:(Ljava/lang/Iterable;)Ljava/util/Set;
     199: checkcast     #16                 // class java/lang/Iterable
     202: invokestatic  #197                // Method kotlin/collections/SetsKt.plus:(Ljava/util/Set;Ljava/lang/Iterable;)Ljava/util/Set;
     205: checkcast     #16                 // class java/lang/Iterable
     208: invokestatic  #159                // Method kotlin/collections/CollectionsKt.intersect:(Ljava/lang/Iterable;Ljava/lang/Iterable;)Ljava/util/Set;
     211: nop
     212: astore        12
     214: goto          88
     217: aload         12
     219: checkcast     #16                 // class java/lang/Iterable
     222: invokestatic  #163                // Method kotlin/collections/CollectionsKt.firstOrNull:(Ljava/lang/Iterable;)Ljava/lang/Object;
     225: checkcast     #45                 // class java/lang/Character
     228: astore        20
     230: aload         20
     232: ifnull        277
     235: aload         20
     237: invokevirtual #166                // Method java/lang/Character.charValue:()C
     240: istore        11
     242: iconst_0
     243: istore        12
     245: invokestatic  #168                // Method getItemsPriorities:()Ljava/util/Map;
     248: iload         11
     250: invokestatic  #49                 // Method java/lang/Character.valueOf:(C)Ljava/lang/Character;
     253: invokeinterface #172,  2          // InterfaceMethod java/util/Map.get:(Ljava/lang/Object;)Ljava/lang/Object;
     258: checkcast     #51                 // class java/lang/Integer
     261: nop
     262: astore        9
     264: aload         9
     266: ifnull        277
     269: aload         9
     271: invokevirtual #175                // Method java/lang/Integer.intValue:()I
     274: goto          278
     277: iconst_0
     278: iload         7
     280: iadd
     281: nop
     282: istore_3
     283: goto          28
     286: iload_3
     287: ireturn

  public static final void main();
    Code:
       0: new           #210                // class java/lang/StringBuilder
       3: dup
       4: invokespecial #212                // Method java/lang/StringBuilder."<init>":()V
       7: ldc           #214                // String The sum of the priorities of the common items in the compartments is:
       9: invokevirtual #218                // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      12: invokestatic  #220                // Method findCommonItemAndSumPriorities:()I
      15: invokevirtual #223                // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
      18: invokevirtual #227                // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
      21: getstatic     #233                // Field java/lang/System.out:Ljava/io/PrintStream;
      24: swap
      25: invokevirtual #239                // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
      28: new           #210                // class java/lang/StringBuilder
      31: dup
      32: invokespecial #212                // Method java/lang/StringBuilder."<init>":()V
      35: ldc           #241                // String The sum of the priorities of the group badges is:
      37: invokevirtual #218                // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      40: invokestatic  #243                // Method findBadgesAndSumPriorities:()I
      43: invokevirtual #223                // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
      46: invokevirtual #227                // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
      49: getstatic     #233                // Field java/lang/System.out:Ljava/io/PrintStream;
      52: swap
      53: invokevirtual #239                // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
      56: return

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