package day16

import java.nio.file.Files
import java.nio.file.Paths

fun main() {
  fun part1(input: Input): Int {
    fun dfs(packet: Packet): Int {
      var sum = packet.version
      if (packet is Operation) {
        for (subPacket in packet.subPackets) {
          sum += dfs(subPacket)
        }
      }
      return sum
    }

    Parser(input).use { parser ->
      return dfs(parser.parsePacket())
    }
  }

  fun part2(input: Input): Long {
    fun evaluate(packet: Packet): Long = when(packet) {
      is Literal -> packet.value
      is Operation -> when(packet.typeId) {
        0 -> packet.subPackets.sumOf { evaluate(it) }
        1 -> packet.subPackets.fold(1L) { acc, subPacket -> acc * evaluate(subPacket) }
        2 -> packet.subPackets.minOf { evaluate(it) }
        3 -> packet.subPackets.maxOf { evaluate(it) }
        5, 6, 7 -> {
          check(packet.subPackets.size == 2)
          val (lhs, rhs) = packet.subPackets.map { evaluate(it) }
          when (packet.typeId) {
            5 -> if (lhs > rhs) 1 else 0
            6 -> if (lhs < rhs) 1 else 0
            7 -> if (lhs == rhs) 1 else 0
            else -> error("unexpected packet type ID: ${packet.typeId}")
          }
        }
        else -> error("unexpected packet type ID: ${packet.typeId}")
      }
    }

    Parser(input).use { parser ->
      return evaluate(parser.parsePacket())
    }
  }

  check(part1("D2FE28".toBinaryString()) == 6)
  check(part1("38006F45291200".toBinaryString()) == 9)
  check(part1("EE00D40C823060".toBinaryString()) == 14)
  check(part1("8A004A801A8002F478".toBinaryString()) == 16)
  check(part1("620080001611562C8802118E34".toBinaryString()) == 12)
  check(part1("C0015000016115A2E0802F182340".toBinaryString()) == 23)
  check(part1("A0016C880162017C3686B18A3D4780".toBinaryString()) == 31)
  check(part2("C200B40A82".toBinaryString()) == 3L)
  check(part2("04005AC33890".toBinaryString()) == 54L)
  check(part2("880086C3E88112".toBinaryString()) == 7L)
  check(part2("CE00C43D881120".toBinaryString()) == 9L)
  check(part2("D8005AC2A8F0".toBinaryString()) == 1L)
  check(part2("F600BC2D8F".toBinaryString()) == 0L)
  check(part2("9C005AC2F8F0".toBinaryString()) == 0L)
  check(part2("9C0141080250320F1802104A08".toBinaryString()) == 1L)

  println(part1(readInput("input.txt")))
  println(part2(readInput("input.txt")))
}

private sealed interface Packet {
  val version: Int
  val typeId: Int
}
private data class Literal(
  override val version: Int,
  override val typeId: Int,
  val value: Long
) : Packet
private data class Operation(
  override val version: Int,
  override val typeId: Int,
  val subPackets: List<Packet>
) : Packet

private class Parser(val string: String): AutoCloseable {
  private var ptr = 0

  fun parsePacket(): Packet {
    val version = parseNum(3)
    val typeId = parseNum(3)
    return if (typeId == 4) {
      Literal(version, typeId, parseLiteralValue())
    } else {
      Operation(version, typeId, parseSubPackets())
    }
  }

  private fun parseSubPackets(): List<Packet> {
    val lengthType = parseNum(1)
    val length = parseNum(if (lengthType == 0) 15 else 11)
    val subPacketsStart = ptr
    val subPackets = mutableListOf<Packet>()
    fun parseNext(): Boolean = when (lengthType) {
      0 -> ptr - subPacketsStart < length
      1 -> subPackets.size < length
      else -> error("unexpected length type: $lengthType")
    }
    while (parseNext()) {
      subPackets.add(parsePacket())
    }
    when (lengthType) {
      0 -> check(ptr - subPacketsStart == length)
      1 -> check(subPackets.size == length)
    }
    return subPackets
  }

  private fun parseLiteralValue(): Long {
    var value = 0L
    do {
      val last = parseNum(1) == 0
      value = (value shl 4) + parseNum(4)
    } while (!last)
    return value
  }

  private fun parseNum(len: Int): Int {
    return string.substring(ptr, ptr + len).toInt(2).also {
      ptr += len
    }
  }

  override fun close() {
    while (ptr < string.length) {
      check(string[ptr++] == '0')
    }
  }
}

private fun String.toBinaryString() =
  map {
    it.toString()
      .toInt(16)
      .toString(2)
      .padStart(4, '0')
  }.joinToString("") { it }

private fun readInput(s: String): Input {
  return Files.newBufferedReader(Paths.get("src/day16/$s")).readLines().first().toBinaryString()
}

private typealias Input = String

Compiled from "Solution.kt"
public final class day16.SolutionKt {
  public static final void main();
    Code:
       0: ldc           #8                  // String D2FE28
       2: invokestatic  #12                 // Method toBinaryString:(Ljava/lang/String;)Ljava/lang/String;
       5: invokestatic  #16                 // Method main$part1:(Ljava/lang/String;)I
       8: bipush        6
      10: if_icmpne     17
      13: iconst_1
      14: goto          18
      17: iconst_0
      18: ifne          31
      21: new           #18                 // class java/lang/IllegalStateException
      24: dup
      25: ldc           #20                 // String Check failed.
      27: invokespecial #24                 // Method java/lang/IllegalStateException."<init>":(Ljava/lang/String;)V
      30: athrow
      31: ldc           #26                 // String 38006F45291200
      33: invokestatic  #12                 // Method toBinaryString:(Ljava/lang/String;)Ljava/lang/String;
      36: invokestatic  #16                 // Method main$part1:(Ljava/lang/String;)I
      39: bipush        9
      41: if_icmpne     48
      44: iconst_1
      45: goto          49
      48: iconst_0
      49: ifne          62
      52: new           #18                 // class java/lang/IllegalStateException
      55: dup
      56: ldc           #20                 // String Check failed.
      58: invokespecial #24                 // Method java/lang/IllegalStateException."<init>":(Ljava/lang/String;)V
      61: athrow
      62: ldc           #28                 // String EE00D40C823060
      64: invokestatic  #12                 // Method toBinaryString:(Ljava/lang/String;)Ljava/lang/String;
      67: invokestatic  #16                 // Method main$part1:(Ljava/lang/String;)I
      70: bipush        14
      72: if_icmpne     79
      75: iconst_1
      76: goto          80
      79: iconst_0
      80: ifne          93
      83: new           #18                 // class java/lang/IllegalStateException
      86: dup
      87: ldc           #20                 // String Check failed.
      89: invokespecial #24                 // Method java/lang/IllegalStateException."<init>":(Ljava/lang/String;)V
      92: athrow
      93: ldc           #30                 // String 8A004A801A8002F478
      95: invokestatic  #12                 // Method toBinaryString:(Ljava/lang/String;)Ljava/lang/String;
      98: invokestatic  #16                 // Method main$part1:(Ljava/lang/String;)I
     101: bipush        16
     103: if_icmpne     110
     106: iconst_1
     107: goto          111
     110: iconst_0
     111: ifne          124
     114: new           #18                 // class java/lang/IllegalStateException
     117: dup
     118: ldc           #20                 // String Check failed.
     120: invokespecial #24                 // Method java/lang/IllegalStateException."<init>":(Ljava/lang/String;)V
     123: athrow
     124: ldc           #32                 // String 620080001611562C8802118E34
     126: invokestatic  #12                 // Method toBinaryString:(Ljava/lang/String;)Ljava/lang/String;
     129: invokestatic  #16                 // Method main$part1:(Ljava/lang/String;)I
     132: bipush        12
     134: if_icmpne     141
     137: iconst_1
     138: goto          142
     141: iconst_0
     142: ifne          155
     145: new           #18                 // class java/lang/IllegalStateException
     148: dup
     149: ldc           #20                 // String Check failed.
     151: invokespecial #24                 // Method java/lang/IllegalStateException."<init>":(Ljava/lang/String;)V
     154: athrow
     155: ldc           #34                 // String C0015000016115A2E0802F182340
     157: invokestatic  #12                 // Method toBinaryString:(Ljava/lang/String;)Ljava/lang/String;
     160: invokestatic  #16                 // Method main$part1:(Ljava/lang/String;)I
     163: bipush        23
     165: if_icmpne     172
     168: iconst_1
     169: goto          173
     172: iconst_0
     173: ifne          186
     176: new           #18                 // class java/lang/IllegalStateException
     179: dup
     180: ldc           #20                 // String Check failed.
     182: invokespecial #24                 // Method java/lang/IllegalStateException."<init>":(Ljava/lang/String;)V
     185: athrow
     186: ldc           #36                 // String A0016C880162017C3686B18A3D4780
     188: invokestatic  #12                 // Method toBinaryString:(Ljava/lang/String;)Ljava/lang/String;
     191: invokestatic  #16                 // Method main$part1:(Ljava/lang/String;)I
     194: bipush        31
     196: if_icmpne     203
     199: iconst_1
     200: goto          204
     203: iconst_0
     204: ifne          217
     207: new           #18                 // class java/lang/IllegalStateException
     210: dup
     211: ldc           #20                 // String Check failed.
     213: invokespecial #24                 // Method java/lang/IllegalStateException."<init>":(Ljava/lang/String;)V
     216: athrow
     217: ldc           #38                 // String C200B40A82
     219: invokestatic  #12                 // Method toBinaryString:(Ljava/lang/String;)Ljava/lang/String;
     222: invokestatic  #42                 // Method main$part2:(Ljava/lang/String;)J
     225: ldc2_w        #43                 // long 3l
     228: lcmp
     229: ifne          236
     232: iconst_1
     233: goto          237
     236: iconst_0
     237: ifne          250
     240: new           #18                 // class java/lang/IllegalStateException
     243: dup
     244: ldc           #20                 // String Check failed.
     246: invokespecial #24                 // Method java/lang/IllegalStateException."<init>":(Ljava/lang/String;)V
     249: athrow
     250: ldc           #46                 // String 04005AC33890
     252: invokestatic  #12                 // Method toBinaryString:(Ljava/lang/String;)Ljava/lang/String;
     255: invokestatic  #42                 // Method main$part2:(Ljava/lang/String;)J
     258: ldc2_w        #47                 // long 54l
     261: lcmp
     262: ifne          269
     265: iconst_1
     266: goto          270
     269: iconst_0
     270: ifne          283
     273: new           #18                 // class java/lang/IllegalStateException
     276: dup
     277: ldc           #20                 // String Check failed.
     279: invokespecial #24                 // Method java/lang/IllegalStateException."<init>":(Ljava/lang/String;)V
     282: athrow
     283: ldc           #50                 // String 880086C3E88112
     285: invokestatic  #12                 // Method toBinaryString:(Ljava/lang/String;)Ljava/lang/String;
     288: invokestatic  #42                 // Method main$part2:(Ljava/lang/String;)J
     291: ldc2_w        #51                 // long 7l
     294: lcmp
     295: ifne          302
     298: iconst_1
     299: goto          303
     302: iconst_0
     303: ifne          316
     306: new           #18                 // class java/lang/IllegalStateException
     309: dup
     310: ldc           #20                 // String Check failed.
     312: invokespecial #24                 // Method java/lang/IllegalStateException."<init>":(Ljava/lang/String;)V
     315: athrow
     316: ldc           #54                 // String CE00C43D881120
     318: invokestatic  #12                 // Method toBinaryString:(Ljava/lang/String;)Ljava/lang/String;
     321: invokestatic  #42                 // Method main$part2:(Ljava/lang/String;)J
     324: ldc2_w        #55                 // long 9l
     327: lcmp
     328: ifne          335
     331: iconst_1
     332: goto          336
     335: iconst_0
     336: ifne          349
     339: new           #18                 // class java/lang/IllegalStateException
     342: dup
     343: ldc           #20                 // String Check failed.
     345: invokespecial #24                 // Method java/lang/IllegalStateException."<init>":(Ljava/lang/String;)V
     348: athrow
     349: ldc           #58                 // String D8005AC2A8F0
     351: invokestatic  #12                 // Method toBinaryString:(Ljava/lang/String;)Ljava/lang/String;
     354: invokestatic  #42                 // Method main$part2:(Ljava/lang/String;)J
     357: lconst_1
     358: lcmp
     359: ifne          366
     362: iconst_1
     363: goto          367
     366: iconst_0
     367: ifne          380
     370: new           #18                 // class java/lang/IllegalStateException
     373: dup
     374: ldc           #20                 // String Check failed.
     376: invokespecial #24                 // Method java/lang/IllegalStateException."<init>":(Ljava/lang/String;)V
     379: athrow
     380: ldc           #60                 // String F600BC2D8F
     382: invokestatic  #12                 // Method toBinaryString:(Ljava/lang/String;)Ljava/lang/String;
     385: invokestatic  #42                 // Method main$part2:(Ljava/lang/String;)J
     388: lconst_0
     389: lcmp
     390: ifne          397
     393: iconst_1
     394: goto          398
     397: iconst_0
     398: ifne          411
     401: new           #18                 // class java/lang/IllegalStateException
     404: dup
     405: ldc           #20                 // String Check failed.
     407: invokespecial #24                 // Method java/lang/IllegalStateException."<init>":(Ljava/lang/String;)V
     410: athrow
     411: ldc           #62                 // String 9C005AC2F8F0
     413: invokestatic  #12                 // Method toBinaryString:(Ljava/lang/String;)Ljava/lang/String;
     416: invokestatic  #42                 // Method main$part2:(Ljava/lang/String;)J
     419: lconst_0
     420: lcmp
     421: ifne          428
     424: iconst_1
     425: goto          429
     428: iconst_0
     429: ifne          442
     432: new           #18                 // class java/lang/IllegalStateException
     435: dup
     436: ldc           #20                 // String Check failed.
     438: invokespecial #24                 // Method java/lang/IllegalStateException."<init>":(Ljava/lang/String;)V
     441: athrow
     442: ldc           #64                 // String 9C0141080250320F1802104A08
     444: invokestatic  #12                 // Method toBinaryString:(Ljava/lang/String;)Ljava/lang/String;
     447: invokestatic  #42                 // Method main$part2:(Ljava/lang/String;)J
     450: lconst_1
     451: lcmp
     452: ifne          459
     455: iconst_1
     456: goto          460
     459: iconst_0
     460: ifne          473
     463: new           #18                 // class java/lang/IllegalStateException
     466: dup
     467: ldc           #20                 // String Check failed.
     469: invokespecial #24                 // Method java/lang/IllegalStateException."<init>":(Ljava/lang/String;)V
     472: athrow
     473: ldc           #66                 // String input.txt
     475: invokestatic  #69                 // Method readInput:(Ljava/lang/String;)Ljava/lang/String;
     478: invokestatic  #16                 // Method main$part1:(Ljava/lang/String;)I
     481: istore_0
     482: getstatic     #75                 // Field java/lang/System.out:Ljava/io/PrintStream;
     485: iload_0
     486: invokevirtual #81                 // Method java/io/PrintStream.println:(I)V
     489: ldc           #66                 // String input.txt
     491: invokestatic  #69                 // Method readInput:(Ljava/lang/String;)Ljava/lang/String;
     494: invokestatic  #42                 // Method main$part2:(Ljava/lang/String;)J
     497: lstore_0
     498: getstatic     #75                 // Field java/lang/System.out:Ljava/io/PrintStream;
     501: lload_0
     502: invokevirtual #84                 // Method java/io/PrintStream.println:(J)V
     505: return

  private static final java.lang.String toBinaryString(java.lang.String);
    Code:
       0: aload_0
       1: checkcast     #86                 // class java/lang/CharSequence
       4: astore_1
       5: iconst_0
       6: istore_2
       7: aload_1
       8: astore_3
       9: new           #88                 // class java/util/ArrayList
      12: dup
      13: aload_1
      14: invokeinterface #92,  1           // InterfaceMethod java/lang/CharSequence.length:()I
      19: invokespecial #94                 // Method java/util/ArrayList."<init>":(I)V
      22: checkcast     #96                 // class java/util/Collection
      25: astore        4
      27: iconst_0
      28: istore        5
      30: iconst_0
      31: istore        6
      33: iload         6
      35: aload_3
      36: invokeinterface #92,  1           // InterfaceMethod java/lang/CharSequence.length:()I
      41: if_icmpge     112
      44: aload_3
      45: iload         6
      47: invokeinterface #100,  2          // InterfaceMethod java/lang/CharSequence.charAt:(I)C
      52: istore        7
      54: aload         4
      56: iload         7
      58: istore        8
      60: astore        10
      62: iconst_0
      63: istore        9
      65: iload         8
      67: invokestatic  #106                // Method java/lang/String.valueOf:(C)Ljava/lang/String;
      70: bipush        16
      72: invokestatic  #112                // Method kotlin/text/CharsKt.checkRadix:(I)I
      75: invokestatic  #118                // Method java/lang/Integer.parseInt:(Ljava/lang/String;I)I
      78: iconst_2
      79: invokestatic  #112                // Method kotlin/text/CharsKt.checkRadix:(I)I
      82: invokestatic  #122                // Method java/lang/Integer.toString:(II)Ljava/lang/String;
      85: dup
      86: ldc           #124                // String toString(...)
      88: invokestatic  #130                // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
      91: iconst_4
      92: bipush        48
      94: invokestatic  #136                // Method kotlin/text/StringsKt.padStart:(Ljava/lang/String;IC)Ljava/lang/String;
      97: aload         10
      99: swap
     100: invokeinterface #140,  2          // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     105: pop
     106: iinc          6, 1
     109: goto          33
     112: aload         4
     114: checkcast     #142                // class java/util/List
     117: nop
     118: checkcast     #144                // class java/lang/Iterable
     121: ldc           #146                // String
     123: checkcast     #86                 // class java/lang/CharSequence
     126: aconst_null
     127: aconst_null
     128: iconst_0
     129: aconst_null
     130: invokedynamic #165,  0            // InvokeDynamic #0:invoke:()Lkotlin/jvm/functions/Function1;
     135: bipush        30
     137: aconst_null
     138: invokestatic  #171                // Method kotlin/collections/CollectionsKt.joinToString$default:(Ljava/lang/Iterable;Ljava/lang/CharSequence;Ljava/lang/CharSequence;Ljava/lang/CharSequence;ILjava/lang/CharSequence;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Ljava/lang/String;
     141: areturn

  private static final java.lang.String readInput(java.lang.String);
    Code:
       0: new           #187                // class java/lang/StringBuilder
       3: dup
       4: invokespecial #189                // Method java/lang/StringBuilder."<init>":()V
       7: ldc           #191                // String src/day16/
       9: invokevirtual #195                // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      12: aload_0
      13: invokevirtual #195                // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      16: invokevirtual #198                // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
      19: iconst_0
      20: anewarray     #102                // class java/lang/String
      23: invokestatic  #204                // Method java/nio/file/Paths.get:(Ljava/lang/String;[Ljava/lang/String;)Ljava/nio/file/Path;
      26: invokestatic  #210                // Method java/nio/file/Files.newBufferedReader:(Ljava/nio/file/Path;)Ljava/io/BufferedReader;
      29: dup
      30: ldc           #212                // String newBufferedReader(...)
      32: invokestatic  #130                // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
      35: checkcast     #214                // class java/io/Reader
      38: invokestatic  #220                // Method kotlin/io/TextStreamsKt.readLines:(Ljava/io/Reader;)Ljava/util/List;
      41: invokestatic  #224                // Method kotlin/collections/CollectionsKt.first:(Ljava/util/List;)Ljava/lang/Object;
      44: checkcast     #102                // class java/lang/String
      47: invokestatic  #12                 // Method toBinaryString:(Ljava/lang/String;)Ljava/lang/String;
      50: areturn

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

  private static final int main$part1$dfs(day16.Packet);
    Code:
       0: aload_0
       1: invokeinterface #237,  1          // InterfaceMethod day16/Packet.getVersion:()I
       6: istore_1
       7: aload_0
       8: instanceof    #239                // class day16/Operation
      11: ifeq          56
      14: aload_0
      15: checkcast     #239                // class day16/Operation
      18: invokevirtual #243                // Method day16/Operation.getSubPackets:()Ljava/util/List;
      21: invokeinterface #247,  1          // InterfaceMethod java/util/List.iterator:()Ljava/util/Iterator;
      26: astore_2
      27: aload_2
      28: invokeinterface #253,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
      33: ifeq          56
      36: aload_2
      37: invokeinterface #257,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      42: checkcast     #234                // class day16/Packet
      45: astore_3
      46: iload_1
      47: aload_3
      48: invokestatic  #259                // Method main$part1$dfs:(Lday16/Packet;)I
      51: iadd
      52: istore_1
      53: goto          27
      56: iload_1
      57: ireturn

  private static final int main$part1(java.lang.String);
    Code:
       0: new           #267                // class day16/Parser
       3: dup
       4: aload_0
       5: invokespecial #268                // Method day16/Parser."<init>":(Ljava/lang/String;)V
       8: checkcast     #270                // class java/lang/AutoCloseable
      11: astore_1
      12: aconst_null
      13: astore_2
      14: nop
      15: aload_1
      16: checkcast     #267                // class day16/Parser
      19: astore_3
      20: iconst_0
      21: istore        4
      23: aload_3
      24: invokevirtual #274                // Method day16/Parser.parsePacket:()Lday16/Packet;
      27: invokestatic  #259                // Method main$part1$dfs:(Lday16/Packet;)I
      30: istore        5
      32: aload_1
      33: aload_2
      34: invokestatic  #280                // Method kotlin/jdk7/AutoCloseableKt.closeFinally:(Ljava/lang/AutoCloseable;Ljava/lang/Throwable;)V
      37: iload         5
      39: ireturn
      40: astore        4
      42: aload         4
      44: astore_2
      45: aload         4
      47: athrow
      48: astore        4
      50: aload_1
      51: aload_2
      52: invokestatic  #280                // Method kotlin/jdk7/AutoCloseableKt.closeFinally:(Ljava/lang/AutoCloseable;Ljava/lang/Throwable;)V
      55: aload         4
      57: athrow
    Exception table:
       from    to  target type
          14    32    40   Class java/lang/Throwable
          14    32    48   any
          40    48    48   any
          48    50    48   any

  private static final long main$part2$evaluate(day16.Packet);
    Code:
       0: aload_0
       1: astore_1
       2: aload_1
       3: instanceof    #288                // class day16/Literal
       6: ifeq          19
       9: aload_0
      10: checkcast     #288                // class day16/Literal
      13: invokevirtual #292                // Method day16/Literal.getValue:()J
      16: goto          771
      19: aload_1
      20: instanceof    #239                // class day16/Operation
      23: ifeq          763
      26: aload_0
      27: checkcast     #239                // class day16/Operation
      30: invokevirtual #295                // Method day16/Operation.getTypeId:()I
      33: tableswitch   { // 0 to 7
                     0: 80
                     1: 153
                     2: 231
                     3: 335
                     4: 726
                     5: 439
                     6: 439
                     7: 439
               default: 726
          }
      80: aload_0
      81: checkcast     #239                // class day16/Operation
      84: invokevirtual #243                // Method day16/Operation.getSubPackets:()Ljava/util/List;
      87: checkcast     #144                // class java/lang/Iterable
      90: astore_2
      91: lconst_0
      92: lstore_3
      93: aload_2
      94: invokeinterface #296,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      99: astore        7
     101: aload         7
     103: invokeinterface #253,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
     108: ifeq          149
     111: aload         7
     113: invokeinterface #257,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     118: astore        9
     120: lload_3
     121: aload         9
     123: checkcast     #234                // class day16/Packet
     126: astore        10
     128: lstore        15
     130: iconst_0
     131: istore        11
     133: aload         10
     135: invokestatic  #298                // Method main$part2$evaluate:(Lday16/Packet;)J
     138: lstore        17
     140: lload         15
     142: lload         17
     144: ladd
     145: lstore_3
     146: goto          101
     149: lload_3
     150: goto          771
     153: aload_0
     154: checkcast     #239                // class day16/Operation
     157: invokevirtual #243                // Method day16/Operation.getSubPackets:()Ljava/util/List;
     160: checkcast     #144                // class java/lang/Iterable
     163: astore_2
     164: lconst_1
     165: lstore_3
     166: iconst_0
     167: istore        5
     169: lload_3
     170: lstore        7
     172: aload_2
     173: invokeinterface #296,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     178: astore        9
     180: aload         9
     182: invokeinterface #253,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
     187: ifeq          226
     190: aload         9
     192: invokeinterface #257,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     197: astore        10
     199: lload         7
     201: aload         10
     203: checkcast     #234                // class day16/Packet
     206: astore        11
     208: lstore        12
     210: iconst_0
     211: istore        14
     213: lload         12
     215: aload         11
     217: invokestatic  #298                // Method main$part2$evaluate:(Lday16/Packet;)J
     220: lmul
     221: lstore        7
     223: goto          180
     226: lload         7
     228: goto          771
     231: aload_0
     232: checkcast     #239                // class day16/Operation
     235: invokevirtual #243                // Method day16/Operation.getSubPackets:()Ljava/util/List;
     238: checkcast     #144                // class java/lang/Iterable
     241: invokeinterface #296,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     246: astore_3
     247: aload_3
     248: invokeinterface #253,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
     253: ifne          264
     256: new           #300                // class java/util/NoSuchElementException
     259: dup
     260: invokespecial #301                // Method java/util/NoSuchElementException."<init>":()V
     263: athrow
     264: aload_3
     265: invokeinterface #257,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     270: checkcast     #234                // class day16/Packet
     273: astore        5
     275: iconst_0
     276: istore        7
     278: aload         5
     280: invokestatic  #298                // Method main$part2$evaluate:(Lday16/Packet;)J
     283: lstore        5
     285: aload_3
     286: invokeinterface #253,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
     291: ifeq          330
     294: aload_3
     295: invokeinterface #257,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     300: checkcast     #234                // class day16/Packet
     303: astore        7
     305: iconst_0
     306: istore        9
     308: aload         7
     310: invokestatic  #298                // Method main$part2$evaluate:(Lday16/Packet;)J
     313: lstore        7
     315: lload         5
     317: lload         7
     319: lcmp
     320: ifle          285
     323: lload         7
     325: lstore        5
     327: goto          285
     330: lload         5
     332: goto          771
     335: aload_0
     336: checkcast     #239                // class day16/Operation
     339: invokevirtual #243                // Method day16/Operation.getSubPackets:()Ljava/util/List;
     342: checkcast     #144                // class java/lang/Iterable
     345: invokeinterface #296,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     350: astore_3
     351: aload_3
     352: invokeinterface #253,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
     357: ifne          368
     360: new           #300                // class java/util/NoSuchElementException
     363: dup
     364: invokespecial #301                // Method java/util/NoSuchElementException."<init>":()V
     367: athrow
     368: aload_3
     369: invokeinterface #257,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     374: checkcast     #234                // class day16/Packet
     377: astore        5
     379: iconst_0
     380: istore        7
     382: aload         5
     384: invokestatic  #298                // Method main$part2$evaluate:(Lday16/Packet;)J
     387: lstore        5
     389: aload_3
     390: invokeinterface #253,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
     395: ifeq          434
     398: aload_3
     399: invokeinterface #257,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     404: checkcast     #234                // class day16/Packet
     407: astore        7
     409: iconst_0
     410: istore        9
     412: aload         7
     414: invokestatic  #298                // Method main$part2$evaluate:(Lday16/Packet;)J
     417: lstore        7
     419: lload         5
     421: lload         7
     423: lcmp
     424: ifge          389
     427: lload         7
     429: lstore        5
     431: goto          389
     434: lload         5
     436: goto          771
     439: aload_0
     440: checkcast     #239                // class day16/Operation
     443: invokevirtual #243                // Method day16/Operation.getSubPackets:()Ljava/util/List;
     446: invokeinterface #304,  1          // InterfaceMethod java/util/List.size:()I
     451: iconst_2
     452: if_icmpne     459
     455: iconst_1
     456: goto          460
     459: iconst_0
     460: ifne          473
     463: new           #18                 // class java/lang/IllegalStateException
     466: dup
     467: ldc           #20                 // String Check failed.
     469: invokespecial #24                 // Method java/lang/IllegalStateException."<init>":(Ljava/lang/String;)V
     472: athrow
     473: aload_0
     474: checkcast     #239                // class day16/Operation
     477: invokevirtual #243                // Method day16/Operation.getSubPackets:()Ljava/util/List;
     480: checkcast     #144                // class java/lang/Iterable
     483: astore_3
     484: iconst_0
     485: istore        5
     487: aload_3
     488: astore        7
     490: new           #88                 // class java/util/ArrayList
     493: dup
     494: aload_3
     495: bipush        10
     497: invokestatic  #308                // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
     500: invokespecial #94                 // Method java/util/ArrayList."<init>":(I)V
     503: checkcast     #96                 // class java/util/Collection
     506: astore        9
     508: iconst_0
     509: istore        10
     511: aload         7
     513: invokeinterface #296,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     518: astore        11
     520: aload         11
     522: invokeinterface #253,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
     527: ifeq          573
     530: aload         11
     532: invokeinterface #257,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     537: astore        12
     539: aload         9
     541: aload         12
     543: checkcast     #234                // class day16/Packet
     546: astore        13
     548: astore        15
     550: iconst_0
     551: istore        14
     553: aload         13
     555: invokestatic  #298                // Method main$part2$evaluate:(Lday16/Packet;)J
     558: invokestatic  #313                // Method java/lang/Long.valueOf:(J)Ljava/lang/Long;
     561: aload         15
     563: swap
     564: invokeinterface #140,  2          // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     569: pop
     570: goto          520
     573: aload         9
     575: checkcast     #142                // class java/util/List
     578: nop
     579: astore_2
     580: aload_2
     581: iconst_0
     582: invokeinterface #316,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     587: checkcast     #318                // class java/lang/Number
     590: invokevirtual #321                // Method java/lang/Number.longValue:()J
     593: lstore_3
     594: aload_2
     595: iconst_1
     596: invokeinterface #316,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     601: checkcast     #318                // class java/lang/Number
     604: invokevirtual #321                // Method java/lang/Number.longValue:()J
     607: lstore        7
     609: aload_0
     610: checkcast     #239                // class day16/Operation
     613: invokevirtual #295                // Method day16/Operation.getTypeId:()I
     616: tableswitch   { // 5 to 7
                     5: 644
                     6: 659
                     7: 674
               default: 689
          }
     644: lload_3
     645: lload         7
     647: lcmp
     648: ifle          655
     651: lconst_1
     652: goto          771
     655: lconst_0
     656: goto          771
     659: lload_3
     660: lload         7
     662: lcmp
     663: ifge          670
     666: lconst_1
     667: goto          771
     670: lconst_0
     671: goto          771
     674: lload_3
     675: lload         7
     677: lcmp
     678: ifne          685
     681: lconst_1
     682: goto          771
     685: lconst_0
     686: goto          771
     689: new           #18                 // class java/lang/IllegalStateException
     692: dup
     693: new           #187                // class java/lang/StringBuilder
     696: dup
     697: invokespecial #189                // Method java/lang/StringBuilder."<init>":()V
     700: ldc_w         #323                // String unexpected packet type ID:
     703: invokevirtual #195                // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     706: aload_0
     707: checkcast     #239                // class day16/Operation
     710: invokevirtual #295                // Method day16/Operation.getTypeId:()I
     713: invokevirtual #326                // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
     716: invokevirtual #198                // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
     719: invokevirtual #327                // Method java/lang/Object.toString:()Ljava/lang/String;
     722: invokespecial #24                 // Method java/lang/IllegalStateException."<init>":(Ljava/lang/String;)V
     725: athrow
     726: new           #18                 // class java/lang/IllegalStateException
     729: dup
     730: new           #187                // class java/lang/StringBuilder
     733: dup
     734: invokespecial #189                // Method java/lang/StringBuilder."<init>":()V
     737: ldc_w         #323                // String unexpected packet type ID:
     740: invokevirtual #195                // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     743: aload_0
     744: checkcast     #239                // class day16/Operation
     747: invokevirtual #295                // Method day16/Operation.getTypeId:()I
     750: invokevirtual #326                // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
     753: invokevirtual #198                // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
     756: invokevirtual #327                // Method java/lang/Object.toString:()Ljava/lang/String;
     759: invokespecial #24                 // Method java/lang/IllegalStateException."<init>":(Ljava/lang/String;)V
     762: athrow
     763: new           #329                // class kotlin/NoWhenBranchMatchedException
     766: dup
     767: invokespecial #330                // Method kotlin/NoWhenBranchMatchedException."<init>":()V
     770: athrow
     771: lreturn

  private static final long main$part2(java.lang.String);
    Code:
       0: new           #267                // class day16/Parser
       3: dup
       4: aload_0
       5: invokespecial #268                // Method day16/Parser."<init>":(Ljava/lang/String;)V
       8: checkcast     #270                // class java/lang/AutoCloseable
      11: astore_1
      12: aconst_null
      13: astore_2
      14: nop
      15: aload_1
      16: checkcast     #267                // class day16/Parser
      19: astore_3
      20: iconst_0
      21: istore        4
      23: aload_3
      24: invokevirtual #274                // Method day16/Parser.parsePacket:()Lday16/Packet;
      27: invokestatic  #298                // Method main$part2$evaluate:(Lday16/Packet;)J
      30: lstore        5
      32: aload_1
      33: aload_2
      34: invokestatic  #280                // Method kotlin/jdk7/AutoCloseableKt.closeFinally:(Ljava/lang/AutoCloseable;Ljava/lang/Throwable;)V
      37: lload         5
      39: lreturn
      40: astore        4
      42: aload         4
      44: astore_2
      45: aload         4
      47: athrow
      48: astore        4
      50: aload_1
      51: aload_2
      52: invokestatic  #280                // Method kotlin/jdk7/AutoCloseableKt.closeFinally:(Ljava/lang/AutoCloseable;Ljava/lang/Throwable;)V
      55: aload         4
      57: athrow
    Exception table:
       from    to  target type
          14    32    40   Class java/lang/Throwable
          14    32    48   any
          40    48    48   any
          48    50    48   any

  private static final java.lang.CharSequence toBinaryString$lambda$8(java.lang.String);
    Code:
       0: aload_0
       1: ldc_w         #348                // String it
       4: invokestatic  #351                // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       7: aload_0
       8: checkcast     #86                 // class java/lang/CharSequence
      11: areturn
}


package day11

import java.nio.file.Files
import java.nio.file.Paths

fun main() {
  fun iterate(octopuses: Input): Int {
    var flashes = 0
    val queue = mutableListOf<Pair<Int, Int>>()
    val flashed = Array(octopuses.size) { BooleanArray(octopuses[it].size) }
    for ((i, row) in octopuses.withIndex()) {
      for (j in row.indices) {
        row[j]++
        if (row[j] > 9) {
          flashed[i][j] = true
          queue.add(i to j)
        }
      }
    }
    var head = 0
    while (head < queue.size) {
      val (i, j) = queue[head++]
      octopuses[i][j] = 0
      flashes++
      for (di in -1..1) {
        for (dj in -1..1) {
          val ii = i + di
          val jj = j + dj
          if (ii !in octopuses.indices || jj !in octopuses[ii].indices) continue
          if (!flashed[ii][jj]) {
            octopuses[ii][jj]++
            if (octopuses[ii][jj] > 9) {
              flashed[ii][jj] = true
              queue.add(ii to jj)
            }
          }
        }
      }
    }
    return flashes
  }

  fun part1(octopuses: Input): Int {
    var flashes = 0
    repeat(100) {
      flashes += iterate(octopuses)
    }
    return flashes
  }

  fun part2(octopuses: Input): Int {
    repeat(100500) {
      val flashes = iterate(octopuses)
      if (flashes == 100) return it + 1
    }
    error("POLUNDRA")
  }

  check(part1(readInput("test-input.txt")) == 1656)
  check(part2(readInput("test-input.txt")) == 195)

  println(part1(readInput("input.txt")))
  println(part2(readInput("input.txt")))
}

private fun readInput(s: String): Input {
  return Files.newBufferedReader(Paths.get("src/day11/$s"))
    .readLines()
    .map {
      it
        .map { it.code - '0'.code }
        .toMutableList()
    }
}

private typealias Input = List<MutableList<Int>>

Compiled from "Solution.kt"
public final class day11.SolutionKt {
  public static final void main();
    Code:
       0: ldc           #8                  // String test-input.txt
       2: invokestatic  #12                 // Method readInput:(Ljava/lang/String;)Ljava/util/List;
       5: invokestatic  #16                 // Method main$part1:(Ljava/util/List;)I
       8: sipush        1656
      11: if_icmpne     18
      14: iconst_1
      15: goto          19
      18: iconst_0
      19: ifne          32
      22: new           #18                 // class java/lang/IllegalStateException
      25: dup
      26: ldc           #20                 // String Check failed.
      28: invokespecial #24                 // Method java/lang/IllegalStateException."<init>":(Ljava/lang/String;)V
      31: athrow
      32: ldc           #8                  // String test-input.txt
      34: invokestatic  #12                 // Method readInput:(Ljava/lang/String;)Ljava/util/List;
      37: invokestatic  #27                 // Method main$part2:(Ljava/util/List;)I
      40: sipush        195
      43: if_icmpne     50
      46: iconst_1
      47: goto          51
      50: iconst_0
      51: ifne          64
      54: new           #18                 // class java/lang/IllegalStateException
      57: dup
      58: ldc           #20                 // String Check failed.
      60: invokespecial #24                 // Method java/lang/IllegalStateException."<init>":(Ljava/lang/String;)V
      63: athrow
      64: ldc           #29                 // String input.txt
      66: invokestatic  #12                 // Method readInput:(Ljava/lang/String;)Ljava/util/List;
      69: invokestatic  #16                 // Method main$part1:(Ljava/util/List;)I
      72: istore_0
      73: getstatic     #35                 // Field java/lang/System.out:Ljava/io/PrintStream;
      76: iload_0
      77: invokevirtual #41                 // Method java/io/PrintStream.println:(I)V
      80: ldc           #29                 // String input.txt
      82: invokestatic  #12                 // Method readInput:(Ljava/lang/String;)Ljava/util/List;
      85: invokestatic  #27                 // Method main$part2:(Ljava/util/List;)I
      88: istore_0
      89: getstatic     #35                 // Field java/lang/System.out:Ljava/io/PrintStream;
      92: iload_0
      93: invokevirtual #41                 // Method java/io/PrintStream.println:(I)V
      96: return

  private static final java.util.List<java.util.List<java.lang.Integer>> readInput(java.lang.String);
    Code:
       0: new           #44                 // class java/lang/StringBuilder
       3: dup
       4: invokespecial #46                 // Method java/lang/StringBuilder."<init>":()V
       7: ldc           #48                 // String src/day11/
       9: invokevirtual #52                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      12: aload_0
      13: invokevirtual #52                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      16: invokevirtual #56                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
      19: iconst_0
      20: anewarray     #58                 // class java/lang/String
      23: invokestatic  #64                 // Method java/nio/file/Paths.get:(Ljava/lang/String;[Ljava/lang/String;)Ljava/nio/file/Path;
      26: invokestatic  #70                 // Method java/nio/file/Files.newBufferedReader:(Ljava/nio/file/Path;)Ljava/io/BufferedReader;
      29: dup
      30: ldc           #72                 // String newBufferedReader(...)
      32: invokestatic  #78                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
      35: checkcast     #80                 // class java/io/Reader
      38: invokestatic  #86                 // Method kotlin/io/TextStreamsKt.readLines:(Ljava/io/Reader;)Ljava/util/List;
      41: checkcast     #88                 // class java/lang/Iterable
      44: astore_1
      45: nop
      46: iconst_0
      47: istore_2
      48: aload_1
      49: astore_3
      50: new           #90                 // class java/util/ArrayList
      53: dup
      54: aload_1
      55: bipush        10
      57: invokestatic  #96                 // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
      60: invokespecial #98                 // Method java/util/ArrayList."<init>":(I)V
      63: checkcast     #100                // class java/util/Collection
      66: astore        4
      68: iconst_0
      69: istore        5
      71: aload_3
      72: invokeinterface #104,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      77: astore        6
      79: aload         6
      81: invokeinterface #110,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
      86: ifeq          233
      89: aload         6
      91: invokeinterface #114,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      96: astore        7
      98: aload         4
     100: aload         7
     102: checkcast     #58                 // class java/lang/String
     105: astore        8
     107: astore        20
     109: iconst_0
     110: istore        9
     112: aload         8
     114: checkcast     #116                // class java/lang/CharSequence
     117: astore        10
     119: nop
     120: iconst_0
     121: istore        11
     123: aload         10
     125: astore        12
     127: new           #90                 // class java/util/ArrayList
     130: dup
     131: aload         10
     133: invokeinterface #120,  1          // InterfaceMethod java/lang/CharSequence.length:()I
     138: invokespecial #98                 // Method java/util/ArrayList."<init>":(I)V
     141: checkcast     #100                // class java/util/Collection
     144: astore        13
     146: iconst_0
     147: istore        14
     149: iconst_0
     150: istore        15
     152: iload         15
     154: aload         12
     156: invokeinterface #120,  1          // InterfaceMethod java/lang/CharSequence.length:()I
     161: if_icmpge     209
     164: aload         12
     166: iload         15
     168: invokeinterface #124,  2          // InterfaceMethod java/lang/CharSequence.charAt:(I)C
     173: istore        16
     175: aload         13
     177: iload         16
     179: istore        17
     181: astore        18
     183: iconst_0
     184: istore        19
     186: iload         17
     188: bipush        48
     190: isub
     191: invokestatic  #130                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     194: aload         18
     196: swap
     197: invokeinterface #134,  2          // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     202: pop
     203: iinc          15, 1
     206: goto          152
     209: aload         13
     211: checkcast     #136                // class java/util/List
     214: nop
     215: checkcast     #100                // class java/util/Collection
     218: invokestatic  #140                // Method kotlin/collections/CollectionsKt.toMutableList:(Ljava/util/Collection;)Ljava/util/List;
     221: aload         20
     223: swap
     224: invokeinterface #134,  2          // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     229: pop
     230: goto          79
     233: aload         4
     235: checkcast     #136                // class java/util/List
     238: nop
     239: areturn

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

  private static final int main$iterate(java.util.List<? extends java.util.List<java.lang.Integer>>);
    Code:
       0: iconst_0
       1: istore_1
       2: new           #90                 // class java/util/ArrayList
       5: dup
       6: invokespecial #165                // Method java/util/ArrayList."<init>":()V
       9: checkcast     #136                // class java/util/List
      12: astore_2
      13: iconst_0
      14: istore        4
      16: aload_0
      17: invokeinterface #168,  1          // InterfaceMethod java/util/List.size:()I
      22: istore        5
      24: iload         5
      26: anewarray     #170                // class "[Z"
      29: astore        6
      31: iload         4
      33: iload         5
      35: if_icmpge     71
      38: iload         4
      40: istore        7
      42: aload         6
      44: iload         7
      46: aload_0
      47: iload         7
      49: invokeinterface #173,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      54: checkcast     #136                // class java/util/List
      57: invokeinterface #168,  1          // InterfaceMethod java/util/List.size:()I
      62: newarray       boolean
      64: aastore
      65: iinc          4, 1
      68: goto          31
      71: aload         6
      73: astore_3
      74: aload_0
      75: checkcast     #88                 // class java/lang/Iterable
      78: invokeinterface #104,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      83: astore        4
      85: iconst_0
      86: istore        5
      88: aload         4
      90: invokeinterface #110,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
      95: ifeq          234
      98: iload         5
     100: istore        6
     102: iload         5
     104: iconst_1
     105: iadd
     106: istore        5
     108: aload         4
     110: invokeinterface #114,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     115: checkcast     #136                // class java/util/List
     118: astore        7
     120: iconst_0
     121: istore        8
     123: aload         7
     125: checkcast     #100                // class java/util/Collection
     128: invokeinterface #174,  1          // InterfaceMethod java/util/Collection.size:()I
     133: istore        9
     135: iload         8
     137: iload         9
     139: if_icmpge     88
     142: iload         8
     144: istore        10
     146: aload         7
     148: iload         10
     150: invokeinterface #173,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     155: checkcast     #176                // class java/lang/Number
     158: invokevirtual #179                // Method java/lang/Number.intValue:()I
     161: istore        11
     163: aload         7
     165: iload         10
     167: iload         11
     169: iconst_1
     170: iadd
     171: invokestatic  #130                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     174: invokeinterface #183,  3          // InterfaceMethod java/util/List.set:(ILjava/lang/Object;)Ljava/lang/Object;
     179: pop
     180: aload         7
     182: iload         8
     184: invokeinterface #173,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     189: checkcast     #176                // class java/lang/Number
     192: invokevirtual #179                // Method java/lang/Number.intValue:()I
     195: bipush        9
     197: if_icmple     228
     200: aload_3
     201: iload         6
     203: aaload
     204: iload         8
     206: iconst_1
     207: bastore
     208: aload_2
     209: iload         6
     211: invokestatic  #130                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     214: iload         8
     216: invokestatic  #130                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     219: invokestatic  #189                // Method kotlin/TuplesKt.to:(Ljava/lang/Object;Ljava/lang/Object;)Lkotlin/Pair;
     222: invokeinterface #190,  2          // InterfaceMethod java/util/List.add:(Ljava/lang/Object;)Z
     227: pop
     228: iinc          8, 1
     231: goto          135
     234: iconst_0
     235: istore        4
     237: iload         4
     239: aload_2
     240: invokeinterface #168,  1          // InterfaceMethod java/util/List.size:()I
     245: if_icmpge     548
     248: aload_2
     249: iload         4
     251: iinc          4, 1
     254: invokeinterface #173,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     259: checkcast     #192                // class kotlin/Pair
     262: astore        5
     264: aload         5
     266: invokevirtual #195                // Method kotlin/Pair.component1:()Ljava/lang/Object;
     269: checkcast     #176                // class java/lang/Number
     272: invokevirtual #179                // Method java/lang/Number.intValue:()I
     275: istore        6
     277: aload         5
     279: invokevirtual #198                // Method kotlin/Pair.component2:()Ljava/lang/Object;
     282: checkcast     #176                // class java/lang/Number
     285: invokevirtual #179                // Method java/lang/Number.intValue:()I
     288: istore        7
     290: aload_0
     291: iload         6
     293: invokeinterface #173,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     298: checkcast     #136                // class java/util/List
     301: iload         7
     303: iconst_0
     304: invokestatic  #130                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     307: invokeinterface #183,  3          // InterfaceMethod java/util/List.set:(ILjava/lang/Object;)Ljava/lang/Object;
     312: pop
     313: iinc          1, 1
     316: iconst_m1
     317: istore        8
     319: iload         8
     321: iconst_2
     322: if_icmpge     237
     325: iconst_m1
     326: istore        9
     328: iload         9
     330: iconst_2
     331: if_icmpge     542
     334: iload         6
     336: iload         8
     338: iadd
     339: istore        10
     341: iload         7
     343: iload         9
     345: iadd
     346: istore        11
     348: iconst_0
     349: iload         10
     351: if_icmpgt     376
     354: iload         10
     356: aload_0
     357: checkcast     #100                // class java/util/Collection
     360: invokeinterface #174,  1          // InterfaceMethod java/util/Collection.size:()I
     365: if_icmpge     372
     368: iconst_1
     369: goto          377
     372: iconst_0
     373: goto          377
     376: iconst_0
     377: ifeq          536
     380: iconst_0
     381: iload         11
     383: if_icmpgt     415
     386: iload         11
     388: aload_0
     389: iload         10
     391: invokeinterface #173,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     396: checkcast     #100                // class java/util/Collection
     399: invokeinterface #174,  1          // InterfaceMethod java/util/Collection.size:()I
     404: if_icmpge     411
     407: iconst_1
     408: goto          416
     411: iconst_0
     412: goto          416
     415: iconst_0
     416: ifne          422
     419: goto          536
     422: aload_3
     423: iload         10
     425: aaload
     426: iload         11
     428: baload
     429: ifne          536
     432: aload_0
     433: iload         10
     435: invokeinterface #173,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     440: checkcast     #136                // class java/util/List
     443: astore        12
     445: aload         12
     447: iload         11
     449: invokeinterface #173,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     454: checkcast     #176                // class java/lang/Number
     457: invokevirtual #179                // Method java/lang/Number.intValue:()I
     460: istore        13
     462: aload         12
     464: iload         11
     466: iload         13
     468: iconst_1
     469: iadd
     470: invokestatic  #130                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     473: invokeinterface #183,  3          // InterfaceMethod java/util/List.set:(ILjava/lang/Object;)Ljava/lang/Object;
     478: pop
     479: aload_0
     480: iload         10
     482: invokeinterface #173,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     487: checkcast     #136                // class java/util/List
     490: iload         11
     492: invokeinterface #173,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     497: checkcast     #176                // class java/lang/Number
     500: invokevirtual #179                // Method java/lang/Number.intValue:()I
     503: bipush        9
     505: if_icmple     536
     508: aload_3
     509: iload         10
     511: aaload
     512: iload         11
     514: iconst_1
     515: bastore
     516: aload_2
     517: iload         10
     519: invokestatic  #130                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     522: iload         11
     524: invokestatic  #130                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     527: invokestatic  #189                // Method kotlin/TuplesKt.to:(Ljava/lang/Object;Ljava/lang/Object;)Lkotlin/Pair;
     530: invokeinterface #190,  2          // InterfaceMethod java/util/List.add:(Ljava/lang/Object;)Z
     535: pop
     536: iinc          9, 1
     539: goto          328
     542: iinc          8, 1
     545: goto          319
     548: iload_1
     549: ireturn

  private static final int main$part1(java.util.List<? extends java.util.List<java.lang.Integer>>);
    Code:
       0: iconst_0
       1: istore_1
       2: bipush        100
       4: istore_2
       5: iconst_0
       6: istore_3
       7: iload_3
       8: iload_2
       9: if_icmpge     32
      12: iload_3
      13: istore        4
      15: iconst_0
      16: istore        5
      18: iload_1
      19: aload_0
      20: invokestatic  #215                // Method main$iterate:(Ljava/util/List;)I
      23: iadd
      24: istore_1
      25: nop
      26: iinc          3, 1
      29: goto          7
      32: iload_1
      33: ireturn

  private static final int main$part2(java.util.List<? extends java.util.List<java.lang.Integer>>);
    Code:
       0: ldc           #217                // int 100500
       2: istore_1
       3: iconst_0
       4: istore_2
       5: iload_2
       6: iload_1
       7: if_icmpge     39
      10: iload_2
      11: istore_3
      12: iconst_0
      13: istore        4
      15: aload_0
      16: invokestatic  #215                // Method main$iterate:(Ljava/util/List;)I
      19: istore        5
      21: iload         5
      23: bipush        100
      25: if_icmpne     32
      28: iload_3
      29: iconst_1
      30: iadd
      31: ireturn
      32: nop
      33: iinc          2, 1
      36: goto          5
      39: new           #18                 // class java/lang/IllegalStateException
      42: dup
      43: ldc           #219                // String POLUNDRA
      45: invokevirtual #220                // Method java/lang/Object.toString:()Ljava/lang/String;
      48: invokespecial #24                 // Method java/lang/IllegalStateException."<init>":(Ljava/lang/String;)V
      51: athrow
}


package day10

import java.nio.file.Files
import java.nio.file.Paths

fun main() {
  val matching = mapOf(
    '(' to ')',
    '[' to ']',
    '{' to '}',
    '<' to '>',
  )

  fun part1(lines: Input): Int {
    val points = mapOf(
      ')' to 3,
      ']' to 57,
      '}' to 1197,
      '>' to 25137,
    )
    return lines.sumOf {
      val stack = mutableListOf<Char>()
      for (elem in it) {
        if (matching.containsKey(elem)) {
          stack.add(elem)
        } else {
          if (matching[stack.last()] != elem) {
            return@sumOf points[elem]!!
          } else {
            stack.removeAt(stack.lastIndex)
          }
        }
      }
      0
    }
  }

  fun part2(lines: Input): Long {
    val points = mapOf(
      ')' to 1,
      ']' to 2,
      '}' to 3,
      '>' to 4,
    )
    return lines.map { line ->
      val stack = mutableListOf<Char>()
      for (elem in line) {
        if (matching.containsKey(elem)) {
          stack.add(elem)
        } else {
          if (matching[stack.last()] != elem) {
            return@map 0
          } else {
            stack.removeAt(stack.lastIndex)
          }
        }
      }
      stack.reversed().map { matching[it] }.fold(0L) { acc, c -> acc * 5 + points[c]!! }
    }.filter { it != 0L }.sorted().let {
      it[it.size / 2]
    }
  }

  check(part1(readInput("test-input.txt")) == 26397)
  check(part2(readInput("test-input.txt")) == 288957L)

  println(part1(readInput("input.txt")))
  println(part2(readInput("input.txt")))
}

private fun readInput(s: String): Input {
  return Files.newBufferedReader(Paths.get("src/day10/$s")).readLines()
}

private typealias Input = List<String>

Compiled from "Solution.kt"
public final class day10.SolutionKt {
  public static final void main();
    Code:
       0: iconst_4
       1: anewarray     #8                  // class kotlin/Pair
       4: astore_1
       5: aload_1
       6: iconst_0
       7: bipush        40
       9: invokestatic  #14                 // Method java/lang/Character.valueOf:(C)Ljava/lang/Character;
      12: bipush        41
      14: invokestatic  #14                 // Method java/lang/Character.valueOf:(C)Ljava/lang/Character;
      17: invokestatic  #20                 // Method kotlin/TuplesKt.to:(Ljava/lang/Object;Ljava/lang/Object;)Lkotlin/Pair;
      20: aastore
      21: aload_1
      22: iconst_1
      23: bipush        91
      25: invokestatic  #14                 // Method java/lang/Character.valueOf:(C)Ljava/lang/Character;
      28: bipush        93
      30: invokestatic  #14                 // Method java/lang/Character.valueOf:(C)Ljava/lang/Character;
      33: invokestatic  #20                 // Method kotlin/TuplesKt.to:(Ljava/lang/Object;Ljava/lang/Object;)Lkotlin/Pair;
      36: aastore
      37: aload_1
      38: iconst_2
      39: bipush        123
      41: invokestatic  #14                 // Method java/lang/Character.valueOf:(C)Ljava/lang/Character;
      44: bipush        125
      46: invokestatic  #14                 // Method java/lang/Character.valueOf:(C)Ljava/lang/Character;
      49: invokestatic  #20                 // Method kotlin/TuplesKt.to:(Ljava/lang/Object;Ljava/lang/Object;)Lkotlin/Pair;
      52: aastore
      53: aload_1
      54: iconst_3
      55: bipush        60
      57: invokestatic  #14                 // Method java/lang/Character.valueOf:(C)Ljava/lang/Character;
      60: bipush        62
      62: invokestatic  #14                 // Method java/lang/Character.valueOf:(C)Ljava/lang/Character;
      65: invokestatic  #20                 // Method kotlin/TuplesKt.to:(Ljava/lang/Object;Ljava/lang/Object;)Lkotlin/Pair;
      68: aastore
      69: aload_1
      70: invokestatic  #26                 // Method kotlin/collections/MapsKt.mapOf:([Lkotlin/Pair;)Ljava/util/Map;
      73: astore_0
      74: aload_0
      75: ldc           #28                 // String test-input.txt
      77: invokestatic  #32                 // Method readInput:(Ljava/lang/String;)Ljava/util/List;
      80: invokestatic  #36                 // Method main$part1:(Ljava/util/Map;Ljava/util/List;)I
      83: sipush        26397
      86: if_icmpne     93
      89: iconst_1
      90: goto          94
      93: iconst_0
      94: ifne          107
      97: new           #38                 // class java/lang/IllegalStateException
     100: dup
     101: ldc           #40                 // String Check failed.
     103: invokespecial #44                 // Method java/lang/IllegalStateException."<init>":(Ljava/lang/String;)V
     106: athrow
     107: aload_0
     108: ldc           #28                 // String test-input.txt
     110: invokestatic  #32                 // Method readInput:(Ljava/lang/String;)Ljava/util/List;
     113: invokestatic  #48                 // Method main$part2:(Ljava/util/Map;Ljava/util/List;)J
     116: ldc2_w        #49                 // long 288957l
     119: lcmp
     120: ifne          127
     123: iconst_1
     124: goto          128
     127: iconst_0
     128: ifne          141
     131: new           #38                 // class java/lang/IllegalStateException
     134: dup
     135: ldc           #40                 // String Check failed.
     137: invokespecial #44                 // Method java/lang/IllegalStateException."<init>":(Ljava/lang/String;)V
     140: athrow
     141: aload_0
     142: ldc           #52                 // String input.txt
     144: invokestatic  #32                 // Method readInput:(Ljava/lang/String;)Ljava/util/List;
     147: invokestatic  #36                 // Method main$part1:(Ljava/util/Map;Ljava/util/List;)I
     150: istore_1
     151: getstatic     #58                 // Field java/lang/System.out:Ljava/io/PrintStream;
     154: iload_1
     155: invokevirtual #64                 // Method java/io/PrintStream.println:(I)V
     158: aload_0
     159: ldc           #52                 // String input.txt
     161: invokestatic  #32                 // Method readInput:(Ljava/lang/String;)Ljava/util/List;
     164: invokestatic  #48                 // Method main$part2:(Ljava/util/Map;Ljava/util/List;)J
     167: lstore_1
     168: getstatic     #58                 // Field java/lang/System.out:Ljava/io/PrintStream;
     171: lload_1
     172: invokevirtual #67                 // Method java/io/PrintStream.println:(J)V
     175: return

  private static final java.util.List<java.lang.String> readInput(java.lang.String);
    Code:
       0: new           #76                 // class java/lang/StringBuilder
       3: dup
       4: invokespecial #78                 // Method java/lang/StringBuilder."<init>":()V
       7: ldc           #80                 // String src/day10/
       9: invokevirtual #84                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      12: aload_0
      13: invokevirtual #84                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      16: invokevirtual #88                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
      19: iconst_0
      20: anewarray     #90                 // class java/lang/String
      23: invokestatic  #96                 // Method java/nio/file/Paths.get:(Ljava/lang/String;[Ljava/lang/String;)Ljava/nio/file/Path;
      26: invokestatic  #102                // Method java/nio/file/Files.newBufferedReader:(Ljava/nio/file/Path;)Ljava/io/BufferedReader;
      29: dup
      30: ldc           #104                // String newBufferedReader(...)
      32: invokestatic  #110                // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
      35: checkcast     #112                // class java/io/Reader
      38: invokestatic  #118                // Method kotlin/io/TextStreamsKt.readLines:(Ljava/io/Reader;)Ljava/util/List;
      41: areturn

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

  private static final int main$part1(java.util.Map<java.lang.Character, java.lang.Character>, java.util.List<java.lang.String>);
    Code:
       0: iconst_4
       1: anewarray     #8                  // class kotlin/Pair
       4: astore_3
       5: aload_3
       6: iconst_0
       7: bipush        41
       9: invokestatic  #14                 // Method java/lang/Character.valueOf:(C)Ljava/lang/Character;
      12: iconst_3
      13: invokestatic  #131                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      16: invokestatic  #20                 // Method kotlin/TuplesKt.to:(Ljava/lang/Object;Ljava/lang/Object;)Lkotlin/Pair;
      19: aastore
      20: aload_3
      21: iconst_1
      22: bipush        93
      24: invokestatic  #14                 // Method java/lang/Character.valueOf:(C)Ljava/lang/Character;
      27: bipush        57
      29: invokestatic  #131                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      32: invokestatic  #20                 // Method kotlin/TuplesKt.to:(Ljava/lang/Object;Ljava/lang/Object;)Lkotlin/Pair;
      35: aastore
      36: aload_3
      37: iconst_2
      38: bipush        125
      40: invokestatic  #14                 // Method java/lang/Character.valueOf:(C)Ljava/lang/Character;
      43: sipush        1197
      46: invokestatic  #131                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      49: invokestatic  #20                 // Method kotlin/TuplesKt.to:(Ljava/lang/Object;Ljava/lang/Object;)Lkotlin/Pair;
      52: aastore
      53: aload_3
      54: iconst_3
      55: bipush        62
      57: invokestatic  #14                 // Method java/lang/Character.valueOf:(C)Ljava/lang/Character;
      60: sipush        25137
      63: invokestatic  #131                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      66: invokestatic  #20                 // Method kotlin/TuplesKt.to:(Ljava/lang/Object;Ljava/lang/Object;)Lkotlin/Pair;
      69: aastore
      70: aload_3
      71: invokestatic  #26                 // Method kotlin/collections/MapsKt.mapOf:([Lkotlin/Pair;)Ljava/util/Map;
      74: astore_2
      75: aload_1
      76: checkcast     #133                // class java/lang/Iterable
      79: astore_3
      80: iconst_0
      81: istore        4
      83: aload_3
      84: invokeinterface #137,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      89: astore        5
      91: aload         5
      93: invokeinterface #143,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
      98: ifeq          288
     101: aload         5
     103: invokeinterface #147,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     108: astore        6
     110: iload         4
     112: aload         6
     114: checkcast     #90                 // class java/lang/String
     117: astore        7
     119: istore        14
     121: iconst_0
     122: istore        8
     124: new           #149                // class java/util/ArrayList
     127: dup
     128: invokespecial #150                // Method java/util/ArrayList."<init>":()V
     131: checkcast     #152                // class java/util/List
     134: astore        9
     136: iconst_0
     137: istore        10
     139: aload         7
     141: invokevirtual #156                // Method java/lang/String.length:()I
     144: istore        11
     146: iload         10
     148: iload         11
     150: if_icmpge     275
     153: aload         7
     155: iload         10
     157: invokevirtual #160                // Method java/lang/String.charAt:(I)C
     160: istore        12
     162: aload_0
     163: iload         12
     165: invokestatic  #14                 // Method java/lang/Character.valueOf:(C)Ljava/lang/Character;
     168: invokeinterface #164,  2          // InterfaceMethod java/util/Map.containsKey:(Ljava/lang/Object;)Z
     173: ifeq          192
     176: aload         9
     178: iload         12
     180: invokestatic  #14                 // Method java/lang/Character.valueOf:(C)Ljava/lang/Character;
     183: invokeinterface #167,  2          // InterfaceMethod java/util/List.add:(Ljava/lang/Object;)Z
     188: pop
     189: goto          269
     192: aload_0
     193: aload         9
     195: invokestatic  #173                // Method kotlin/collections/CollectionsKt.last:(Ljava/util/List;)Ljava/lang/Object;
     198: invokeinterface #176,  2          // InterfaceMethod java/util/Map.get:(Ljava/lang/Object;)Ljava/lang/Object;
     203: checkcast     #10                 // class java/lang/Character
     206: iload         12
     208: istore        13
     210: dup
     211: ifnonnull     218
     214: pop
     215: goto          226
     218: invokevirtual #180                // Method java/lang/Character.charValue:()C
     221: iload         13
     223: if_icmpeq     250
     226: aload_2
     227: iload         12
     229: invokestatic  #14                 // Method java/lang/Character.valueOf:(C)Ljava/lang/Character;
     232: invokeinterface #176,  2          // InterfaceMethod java/util/Map.get:(Ljava/lang/Object;)Ljava/lang/Object;
     237: dup
     238: invokestatic  #184                // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
     241: checkcast     #186                // class java/lang/Number
     244: invokevirtual #189                // Method java/lang/Number.intValue:()I
     247: goto          276
     250: aload         9
     252: aload         9
     254: invokestatic  #193                // Method kotlin/collections/CollectionsKt.getLastIndex:(Ljava/util/List;)I
     257: invokeinterface #197,  2          // InterfaceMethod java/util/List.remove:(I)Ljava/lang/Object;
     262: checkcast     #10                 // class java/lang/Character
     265: invokevirtual #180                // Method java/lang/Character.charValue:()C
     268: pop
     269: iinc          10, 1
     272: goto          146
     275: iconst_0
     276: istore        15
     278: iload         14
     280: iload         15
     282: iadd
     283: istore        4
     285: goto          91
     288: iload         4
     290: ireturn

  private static final long main$part2(java.util.Map<java.lang.Character, java.lang.Character>, java.util.List<java.lang.String>);
    Code:
       0: iconst_4
       1: anewarray     #8                  // class kotlin/Pair
       4: astore_3
       5: aload_3
       6: iconst_0
       7: bipush        41
       9: invokestatic  #14                 // Method java/lang/Character.valueOf:(C)Ljava/lang/Character;
      12: iconst_1
      13: invokestatic  #131                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      16: invokestatic  #20                 // Method kotlin/TuplesKt.to:(Ljava/lang/Object;Ljava/lang/Object;)Lkotlin/Pair;
      19: aastore
      20: aload_3
      21: iconst_1
      22: bipush        93
      24: invokestatic  #14                 // Method java/lang/Character.valueOf:(C)Ljava/lang/Character;
      27: iconst_2
      28: invokestatic  #131                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      31: invokestatic  #20                 // Method kotlin/TuplesKt.to:(Ljava/lang/Object;Ljava/lang/Object;)Lkotlin/Pair;
      34: aastore
      35: aload_3
      36: iconst_2
      37: bipush        125
      39: invokestatic  #14                 // Method java/lang/Character.valueOf:(C)Ljava/lang/Character;
      42: iconst_3
      43: invokestatic  #131                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      46: invokestatic  #20                 // Method kotlin/TuplesKt.to:(Ljava/lang/Object;Ljava/lang/Object;)Lkotlin/Pair;
      49: aastore
      50: aload_3
      51: iconst_3
      52: bipush        62
      54: invokestatic  #14                 // Method java/lang/Character.valueOf:(C)Ljava/lang/Character;
      57: iconst_4
      58: invokestatic  #131                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      61: invokestatic  #20                 // Method kotlin/TuplesKt.to:(Ljava/lang/Object;Ljava/lang/Object;)Lkotlin/Pair;
      64: aastore
      65: aload_3
      66: invokestatic  #26                 // Method kotlin/collections/MapsKt.mapOf:([Lkotlin/Pair;)Ljava/util/Map;
      69: astore_2
      70: aload_1
      71: checkcast     #133                // class java/lang/Iterable
      74: astore_3
      75: iconst_0
      76: istore        4
      78: aload_3
      79: astore        5
      81: new           #149                // class java/util/ArrayList
      84: dup
      85: aload_3
      86: bipush        10
      88: invokestatic  #211                // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
      91: invokespecial #213                // Method java/util/ArrayList."<init>":(I)V
      94: checkcast     #215                // class java/util/Collection
      97: astore        6
      99: iconst_0
     100: istore        7
     102: aload         5
     104: invokeinterface #137,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     109: astore        8
     111: aload         8
     113: invokeinterface #143,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
     118: ifeq          498
     121: aload         8
     123: invokeinterface #147,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     128: astore        9
     130: aload         6
     132: aload         9
     134: checkcast     #90                 // class java/lang/String
     137: astore        10
     139: astore        28
     141: iconst_0
     142: istore        11
     144: new           #149                // class java/util/ArrayList
     147: dup
     148: invokespecial #150                // Method java/util/ArrayList."<init>":()V
     151: checkcast     #152                // class java/util/List
     154: astore        12
     156: iconst_0
     157: istore        13
     159: aload         10
     161: invokevirtual #156                // Method java/lang/String.length:()I
     164: istore        14
     166: iload         13
     168: iload         14
     170: if_icmpge     275
     173: aload         10
     175: iload         13
     177: invokevirtual #160                // Method java/lang/String.charAt:(I)C
     180: istore        16
     182: aload_0
     183: iload         16
     185: invokestatic  #14                 // Method java/lang/Character.valueOf:(C)Ljava/lang/Character;
     188: invokeinterface #164,  2          // InterfaceMethod java/util/Map.containsKey:(Ljava/lang/Object;)Z
     193: ifeq          212
     196: aload         12
     198: iload         16
     200: invokestatic  #14                 // Method java/lang/Character.valueOf:(C)Ljava/lang/Character;
     203: invokeinterface #167,  2          // InterfaceMethod java/util/List.add:(Ljava/lang/Object;)Z
     208: pop
     209: goto          269
     212: aload_0
     213: aload         12
     215: invokestatic  #173                // Method kotlin/collections/CollectionsKt.last:(Ljava/util/List;)Ljava/lang/Object;
     218: invokeinterface #176,  2          // InterfaceMethod java/util/Map.get:(Ljava/lang/Object;)Ljava/lang/Object;
     223: checkcast     #10                 // class java/lang/Character
     226: iload         16
     228: istore        17
     230: dup
     231: ifnonnull     238
     234: pop
     235: goto          246
     238: invokevirtual #180                // Method java/lang/Character.charValue:()C
     241: iload         17
     243: if_icmpeq     250
     246: lconst_0
     247: goto          483
     250: aload         12
     252: aload         12
     254: invokestatic  #193                // Method kotlin/collections/CollectionsKt.getLastIndex:(Ljava/util/List;)I
     257: invokeinterface #197,  2          // InterfaceMethod java/util/List.remove:(I)Ljava/lang/Object;
     262: checkcast     #10                 // class java/lang/Character
     265: invokevirtual #180                // Method java/lang/Character.charValue:()C
     268: pop
     269: iinc          13, 1
     272: goto          166
     275: aload         12
     277: checkcast     #133                // class java/lang/Iterable
     280: invokestatic  #219                // Method kotlin/collections/CollectionsKt.reversed:(Ljava/lang/Iterable;)Ljava/util/List;
     283: checkcast     #133                // class java/lang/Iterable
     286: astore        13
     288: iconst_0
     289: istore        14
     291: aload         13
     293: astore        16
     295: new           #149                // class java/util/ArrayList
     298: dup
     299: aload         13
     301: bipush        10
     303: invokestatic  #211                // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
     306: invokespecial #213                // Method java/util/ArrayList."<init>":(I)V
     309: checkcast     #215                // class java/util/Collection
     312: astore        17
     314: iconst_0
     315: istore        19
     317: aload         16
     319: invokeinterface #137,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     324: astore        20
     326: aload         20
     328: invokeinterface #143,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
     333: ifeq          388
     336: aload         20
     338: invokeinterface #147,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     343: astore        21
     345: aload         17
     347: aload         21
     349: checkcast     #10                 // class java/lang/Character
     352: invokevirtual #180                // Method java/lang/Character.charValue:()C
     355: istore        22
     357: astore        23
     359: iconst_0
     360: istore        24
     362: aload_0
     363: iload         22
     365: invokestatic  #14                 // Method java/lang/Character.valueOf:(C)Ljava/lang/Character;
     368: invokeinterface #176,  2          // InterfaceMethod java/util/Map.get:(Ljava/lang/Object;)Ljava/lang/Object;
     373: checkcast     #10                 // class java/lang/Character
     376: aload         23
     378: swap
     379: invokeinterface #220,  2          // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     384: pop
     385: goto          326
     388: aload         17
     390: checkcast     #152                // class java/util/List
     393: nop
     394: checkcast     #133                // class java/lang/Iterable
     397: astore        13
     399: lconst_0
     400: lstore        14
     402: iconst_0
     403: istore        16
     405: lload         14
     407: lstore        17
     409: aload         13
     411: invokeinterface #137,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     416: astore        19
     418: aload         19
     420: invokeinterface #143,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
     425: ifeq          480
     428: aload         19
     430: invokeinterface #147,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     435: astore        20
     437: lload         17
     439: aload         20
     441: checkcast     #10                 // class java/lang/Character
     444: astore        21
     446: lstore        25
     448: iconst_0
     449: istore        27
     451: lload         25
     453: iconst_5
     454: i2l
     455: lmul
     456: aload_2
     457: aload         21
     459: invokeinterface #176,  2          // InterfaceMethod java/util/Map.get:(Ljava/lang/Object;)Ljava/lang/Object;
     464: dup
     465: invokestatic  #184                // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
     468: checkcast     #186                // class java/lang/Number
     471: invokevirtual #224                // Method java/lang/Number.longValue:()J
     474: ladd
     475: lstore        17
     477: goto          418
     480: lload         17
     482: nop
     483: invokestatic  #229                // Method java/lang/Long.valueOf:(J)Ljava/lang/Long;
     486: aload         28
     488: swap
     489: invokeinterface #220,  2          // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     494: pop
     495: goto          111
     498: aload         6
     500: checkcast     #152                // class java/util/List
     503: nop
     504: checkcast     #133                // class java/lang/Iterable
     507: astore_3
     508: nop
     509: iconst_0
     510: istore        4
     512: aload_3
     513: astore        5
     515: new           #149                // class java/util/ArrayList
     518: dup
     519: invokespecial #150                // Method java/util/ArrayList."<init>":()V
     522: checkcast     #215                // class java/util/Collection
     525: astore        6
     527: iconst_0
     528: istore        7
     530: aload         5
     532: invokeinterface #137,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     537: astore        8
     539: aload         8
     541: invokeinterface #143,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
     546: ifeq          599
     549: aload         8
     551: invokeinterface #147,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     556: astore        9
     558: aload         9
     560: checkcast     #186                // class java/lang/Number
     563: invokevirtual #224                // Method java/lang/Number.longValue:()J
     566: lstore        10
     568: iconst_0
     569: istore        12
     571: lload         10
     573: lconst_0
     574: lcmp
     575: ifeq          582
     578: iconst_1
     579: goto          583
     582: iconst_0
     583: ifeq          539
     586: aload         6
     588: aload         9
     590: invokeinterface #220,  2          // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     595: pop
     596: goto          539
     599: aload         6
     601: checkcast     #152                // class java/util/List
     604: nop
     605: checkcast     #133                // class java/lang/Iterable
     608: invokestatic  #232                // Method kotlin/collections/CollectionsKt.sorted:(Ljava/lang/Iterable;)Ljava/util/List;
     611: astore        4
     613: iconst_0
     614: istore        5
     616: aload         4
     618: aload         4
     620: invokeinterface #235,  1          // InterfaceMethod java/util/List.size:()I
     625: iconst_2
     626: idiv
     627: invokeinterface #237,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     632: checkcast     #186                // class java/lang/Number
     635: invokevirtual #224                // Method java/lang/Number.longValue:()J
     638: nop
     639: lreturn
}