package day18

import java.io.File
import java.lang.IllegalStateException

interface Element

class RegularNumber(val value: Int) : Element
object Open : Element
object Close : Element

fun main() {
    val elements = File("src/day18/input.txt").readLines().map { line ->
        line.toList().map { ch ->
            when (ch) {
                '[' -> Open
                ']' -> Close
                ',' -> null
                else -> RegularNumber(ch.digitToInt())
            }
        }.filterNotNull()
    }

    println(elements.reduce { x, y -> x + y }.magnitude())

    println(sequence {
        for ((i, x) in elements.withIndex()) {
            for ((j, y) in elements.withIndex()) {
                if (i != j) yield(x + y)
            }
        }
    }.maxOf { it.magnitude() })
}

private operator fun List<Element>.plus(other: List<Element>): List<Element> {
    val result = ArrayList<Element>(this.size + other.size + 2)
    result.add(Open)
    result.addAll(this)
    result.addAll(other)
    result.add(Close)

    outer@ while (true) {
        var depth = 0
        for (i in 0 until result.size - 4) {
            if (depth > 3 && result[i] is Open && result[i + 3] is Close) { // Explode
                val left = result[i + 1]
                val right = result[i + 2]
                if (left is RegularNumber && right is RegularNumber) {
                    result[i] = RegularNumber(0)
                    result.subList(i + 1, i + 4).clear()
                    for (j in i - 1 downTo 0) {
                        val number = result[j] as? RegularNumber ?: continue
                        result[j] = RegularNumber(number.value + left.value)
                        break
                    }
                    for (j in i + 1 until result.size) {
                        val number = result[j] as? RegularNumber ?: continue
                        result[j] = RegularNumber(number.value + right.value)
                        break
                    }
                    continue@outer
                }
            }
            depth += when (result[i]) {
                Open -> 1
                Close -> -1
                else -> 0
            }
        }
        for ((i, element) in result.withIndex()) {
            if (element is RegularNumber && element.value > 9) { // Split
                result.removeAt(i)
                result.addAll(
                    i,
                    listOf(Open, RegularNumber(element.value / 2), RegularNumber((element.value + 1) / 2), Close)
                )
                continue@outer
            }
        }
        break@outer
    }
    return result
}

fun List<Element>.magnitude() = recurse(0).first

fun List<Element>.recurse(index: Int): Pair<Int, Int> {
    return when (val element = this[index]) {
        Open -> {
            val (left, leftEnd) = recurse(index + 1)
            val (right, rightEnd) = recurse(leftEnd)
            Pair(3 * left + 2 * right, rightEnd + 1)
        }
        is RegularNumber -> Pair(element.value, index + 1)
        else -> throw IllegalStateException()
    }
}

Compiled from "result.kt"
public final class day18.ResultKt {
  public static final void main();
    Code:
       0: new           #8                  // class java/io/File
       3: dup
       4: ldc           #10                 // String src/day18/input.txt
       6: invokespecial #14                 // Method java/io/File."<init>":(Ljava/lang/String;)V
       9: aconst_null
      10: iconst_1
      11: aconst_null
      12: invokestatic  #20                 // Method kotlin/io/FilesKt.readLines$default:(Ljava/io/File;Ljava/nio/charset/Charset;ILjava/lang/Object;)Ljava/util/List;
      15: checkcast     #22                 // class java/lang/Iterable
      18: astore_1
      19: iconst_0
      20: istore_2
      21: aload_1
      22: astore_3
      23: new           #24                 // class java/util/ArrayList
      26: dup
      27: aload_1
      28: bipush        10
      30: invokestatic  #30                 // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
      33: invokespecial #33                 // Method java/util/ArrayList."<init>":(I)V
      36: checkcast     #35                 // class java/util/Collection
      39: astore        4
      41: iconst_0
      42: istore        5
      44: aload_3
      45: invokeinterface #39,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      50: astore        6
      52: aload         6
      54: invokeinterface #45,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      59: ifeq          282
      62: aload         6
      64: invokeinterface #49,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      69: astore        7
      71: aload         4
      73: aload         7
      75: checkcast     #51                 // class java/lang/String
      78: astore        8
      80: astore        20
      82: iconst_0
      83: istore        9
      85: aload         8
      87: checkcast     #53                 // class java/lang/CharSequence
      90: invokestatic  #59                 // Method kotlin/text/StringsKt.toList:(Ljava/lang/CharSequence;)Ljava/util/List;
      93: checkcast     #22                 // class java/lang/Iterable
      96: astore        10
      98: iconst_0
      99: istore        11
     101: aload         10
     103: astore        12
     105: new           #24                 // class java/util/ArrayList
     108: dup
     109: aload         10
     111: bipush        10
     113: invokestatic  #30                 // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
     116: invokespecial #33                 // Method java/util/ArrayList."<init>":(I)V
     119: checkcast     #35                 // class java/util/Collection
     122: astore        13
     124: iconst_0
     125: istore        14
     127: aload         12
     129: invokeinterface #39,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     134: astore        15
     136: aload         15
     138: invokeinterface #45,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     143: ifeq          258
     146: aload         15
     148: invokeinterface #49,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     153: astore        16
     155: aload         13
     157: aload         16
     159: checkcast     #61                 // class java/lang/Character
     162: invokevirtual #65                 // Method java/lang/Character.charValue:()C
     165: istore        17
     167: astore        18
     169: iconst_0
     170: istore        19
     172: iload         17
     174: lookupswitch  { // 3
                    44: 226
                    91: 208
                    93: 217
               default: 230
          }
     208: getstatic     #71                 // Field day18/Open.INSTANCE:Lday18/Open;
     211: checkcast     #73                 // class day18/Element
     214: goto          245
     217: getstatic     #78                 // Field day18/Close.INSTANCE:Lday18/Close;
     220: checkcast     #73                 // class day18/Element
     223: goto          245
     226: aconst_null
     227: goto          245
     230: new           #80                 // class day18/RegularNumber
     233: dup
     234: iload         17
     236: invokestatic  #86                 // Method kotlin/text/CharsKt.digitToInt:(C)I
     239: invokespecial #87                 // Method day18/RegularNumber."<init>":(I)V
     242: checkcast     #73                 // class day18/Element
     245: nop
     246: aload         18
     248: swap
     249: invokeinterface #91,  2           // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     254: pop
     255: goto          136
     258: aload         13
     260: checkcast     #93                 // class java/util/List
     263: nop
     264: checkcast     #22                 // class java/lang/Iterable
     267: invokestatic  #97                 // Method kotlin/collections/CollectionsKt.filterNotNull:(Ljava/lang/Iterable;)Ljava/util/List;
     270: aload         20
     272: swap
     273: invokeinterface #91,  2           // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     278: pop
     279: goto          52
     282: aload         4
     284: checkcast     #93                 // class java/util/List
     287: nop
     288: astore_0
     289: aload_0
     290: checkcast     #22                 // class java/lang/Iterable
     293: astore_1
     294: iconst_0
     295: istore_2
     296: aload_1
     297: invokeinterface #39,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     302: astore_3
     303: aload_3
     304: invokeinterface #45,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     309: ifne          322
     312: new           #99                 // class java/lang/UnsupportedOperationException
     315: dup
     316: ldc           #101                // String Empty collection can\'t be reduced.
     318: invokespecial #102                // Method java/lang/UnsupportedOperationException."<init>":(Ljava/lang/String;)V
     321: athrow
     322: aload_3
     323: invokeinterface #49,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     328: astore        4
     330: aload_3
     331: invokeinterface #45,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     336: ifeq          372
     339: aload         4
     341: aload_3
     342: invokeinterface #49,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     347: checkcast     #93                 // class java/util/List
     350: astore        5
     352: checkcast     #93                 // class java/util/List
     355: astore        6
     357: iconst_0
     358: istore        7
     360: aload         6
     362: aload         5
     364: invokestatic  #106                // Method plus:(Ljava/util/List;Ljava/util/List;)Ljava/util/List;
     367: astore        4
     369: goto          330
     372: aload         4
     374: checkcast     #93                 // class java/util/List
     377: invokestatic  #110                // Method magnitude:(Ljava/util/List;)I
     380: istore_1
     381: getstatic     #116                // Field java/lang/System.out:Ljava/io/PrintStream;
     384: iload_1
     385: invokevirtual #121                // Method java/io/PrintStream.println:(I)V
     388: new           #123                // class day18/ResultKt$main$2
     391: dup
     392: aload_0
     393: aconst_null
     394: invokespecial #126                // Method day18/ResultKt$main$2."<init>":(Ljava/util/List;Lkotlin/coroutines/Continuation;)V
     397: checkcast     #128                // class kotlin/jvm/functions/Function2
     400: invokestatic  #134                // Method kotlin/sequences/SequencesKt.sequence:(Lkotlin/jvm/functions/Function2;)Lkotlin/sequences/Sequence;
     403: invokeinterface #137,  1          // InterfaceMethod kotlin/sequences/Sequence.iterator:()Ljava/util/Iterator;
     408: astore_2
     409: aload_2
     410: invokeinterface #45,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     415: ifne          426
     418: new           #139                // class java/util/NoSuchElementException
     421: dup
     422: invokespecial #141                // Method java/util/NoSuchElementException."<init>":()V
     425: athrow
     426: aload_2
     427: invokeinterface #49,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     432: checkcast     #93                 // class java/util/List
     435: astore_3
     436: iconst_0
     437: istore        4
     439: aload_3
     440: invokestatic  #110                // Method magnitude:(Ljava/util/List;)I
     443: istore_3
     444: aload_2
     445: invokeinterface #45,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     450: ifeq          486
     453: aload_2
     454: invokeinterface #49,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     459: checkcast     #93                 // class java/util/List
     462: astore        4
     464: iconst_0
     465: istore        5
     467: aload         4
     469: invokestatic  #110                // Method magnitude:(Ljava/util/List;)I
     472: istore        4
     474: iload_3
     475: iload         4
     477: if_icmpge     444
     480: iload         4
     482: istore_3
     483: goto          444
     486: iload_3
     487: istore_1
     488: getstatic     #116                // Field java/lang/System.out:Ljava/io/PrintStream;
     491: iload_1
     492: invokevirtual #121                // Method java/io/PrintStream.println:(I)V
     495: return

  private static final java.util.List<day18.Element> plus(java.util.List<? extends day18.Element>, java.util.List<? extends day18.Element>);
    Code:
       0: aload_0
       1: ldc           #172                // String <this>
       3: invokestatic  #178                // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: new           #24                 // class java/util/ArrayList
       9: dup
      10: aload_0
      11: invokeinterface #182,  1          // InterfaceMethod java/util/List.size:()I
      16: aload_1
      17: invokeinterface #182,  1          // InterfaceMethod java/util/List.size:()I
      22: iadd
      23: iconst_2
      24: iadd
      25: invokespecial #33                 // Method java/util/ArrayList."<init>":(I)V
      28: astore_2
      29: aload_2
      30: getstatic     #71                 // Field day18/Open.INSTANCE:Lday18/Open;
      33: invokevirtual #183                // Method java/util/ArrayList.add:(Ljava/lang/Object;)Z
      36: pop
      37: aload_2
      38: aload_0
      39: checkcast     #35                 // class java/util/Collection
      42: invokevirtual #187                // Method java/util/ArrayList.addAll:(Ljava/util/Collection;)Z
      45: pop
      46: aload_2
      47: aload_1
      48: checkcast     #35                 // class java/util/Collection
      51: invokevirtual #187                // Method java/util/ArrayList.addAll:(Ljava/util/Collection;)Z
      54: pop
      55: aload_2
      56: getstatic     #78                 // Field day18/Close.INSTANCE:Lday18/Close;
      59: invokevirtual #183                // Method java/util/ArrayList.add:(Ljava/lang/Object;)Z
      62: pop
      63: nop
      64: iconst_0
      65: istore_3
      66: iconst_0
      67: istore        4
      69: aload_2
      70: invokevirtual #188                // Method java/util/ArrayList.size:()I
      73: iconst_4
      74: isub
      75: istore        5
      77: iload         4
      79: iload         5
      81: if_icmpge     430
      84: iload_3
      85: iconst_3
      86: if_icmple     379
      89: aload_2
      90: iload         4
      92: invokevirtual #192                // Method java/util/ArrayList.get:(I)Ljava/lang/Object;
      95: instanceof    #67                 // class day18/Open
      98: ifeq          379
     101: aload_2
     102: iload         4
     104: iconst_3
     105: iadd
     106: invokevirtual #192                // Method java/util/ArrayList.get:(I)Ljava/lang/Object;
     109: instanceof    #75                 // class day18/Close
     112: ifeq          379
     115: aload_2
     116: iload         4
     118: iconst_1
     119: iadd
     120: invokevirtual #192                // Method java/util/ArrayList.get:(I)Ljava/lang/Object;
     123: dup
     124: ldc           #194                // String get(...)
     126: invokestatic  #197                // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
     129: checkcast     #73                 // class day18/Element
     132: astore        6
     134: aload_2
     135: iload         4
     137: iconst_2
     138: iadd
     139: invokevirtual #192                // Method java/util/ArrayList.get:(I)Ljava/lang/Object;
     142: dup
     143: ldc           #194                // String get(...)
     145: invokestatic  #197                // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
     148: checkcast     #73                 // class day18/Element
     151: astore        7
     153: aload         6
     155: instanceof    #80                 // class day18/RegularNumber
     158: ifeq          379
     161: aload         7
     163: instanceof    #80                 // class day18/RegularNumber
     166: ifeq          379
     169: aload_2
     170: iload         4
     172: new           #80                 // class day18/RegularNumber
     175: dup
     176: iconst_0
     177: invokespecial #87                 // Method day18/RegularNumber."<init>":(I)V
     180: invokevirtual #201                // Method java/util/ArrayList.set:(ILjava/lang/Object;)Ljava/lang/Object;
     183: pop
     184: aload_2
     185: iload         4
     187: iconst_1
     188: iadd
     189: iload         4
     191: iconst_4
     192: iadd
     193: invokevirtual #205                // Method java/util/ArrayList.subList:(II)Ljava/util/List;
     196: invokeinterface #208,  1          // InterfaceMethod java/util/List.clear:()V
     201: iload         4
     203: iconst_1
     204: isub
     205: istore        8
     207: iconst_m1
     208: iload         8
     210: if_icmpge     285
     213: aload_2
     214: iload         8
     216: invokevirtual #192                // Method java/util/ArrayList.get:(I)Ljava/lang/Object;
     219: astore        11
     221: aload         11
     223: instanceof    #80                 // class day18/RegularNumber
     226: ifeq          237
     229: aload         11
     231: checkcast     #80                 // class day18/RegularNumber
     234: goto          238
     237: aconst_null
     238: dup
     239: ifnonnull     246
     242: pop
     243: goto          279
     246: astore        9
     248: aload_2
     249: iload         8
     251: new           #80                 // class day18/RegularNumber
     254: dup
     255: aload         9
     257: invokevirtual #211                // Method day18/RegularNumber.getValue:()I
     260: aload         6
     262: checkcast     #80                 // class day18/RegularNumber
     265: invokevirtual #211                // Method day18/RegularNumber.getValue:()I
     268: iadd
     269: invokespecial #87                 // Method day18/RegularNumber."<init>":(I)V
     272: invokevirtual #201                // Method java/util/ArrayList.set:(ILjava/lang/Object;)Ljava/lang/Object;
     275: pop
     276: goto          285
     279: iinc          8, -1
     282: goto          207
     285: iload         4
     287: iconst_1
     288: iadd
     289: istore        8
     291: aload_2
     292: invokevirtual #188                // Method java/util/ArrayList.size:()I
     295: istore        9
     297: iload         8
     299: iload         9
     301: if_icmpge     376
     304: aload_2
     305: iload         8
     307: invokevirtual #192                // Method java/util/ArrayList.get:(I)Ljava/lang/Object;
     310: astore        12
     312: aload         12
     314: instanceof    #80                 // class day18/RegularNumber
     317: ifeq          328
     320: aload         12
     322: checkcast     #80                 // class day18/RegularNumber
     325: goto          329
     328: aconst_null
     329: dup
     330: ifnonnull     337
     333: pop
     334: goto          370
     337: astore        10
     339: aload_2
     340: iload         8
     342: new           #80                 // class day18/RegularNumber
     345: dup
     346: aload         10
     348: invokevirtual #211                // Method day18/RegularNumber.getValue:()I
     351: aload         7
     353: checkcast     #80                 // class day18/RegularNumber
     356: invokevirtual #211                // Method day18/RegularNumber.getValue:()I
     359: iadd
     360: invokespecial #87                 // Method day18/RegularNumber."<init>":(I)V
     363: invokevirtual #201                // Method java/util/ArrayList.set:(ILjava/lang/Object;)Ljava/lang/Object;
     366: pop
     367: goto          376
     370: iinc          8, 1
     373: goto          297
     376: goto          63
     379: iload_3
     380: aload_2
     381: iload         4
     383: invokevirtual #192                // Method java/util/ArrayList.get:(I)Ljava/lang/Object;
     386: checkcast     #73                 // class day18/Element
     389: astore        6
     391: aload         6
     393: getstatic     #71                 // Field day18/Open.INSTANCE:Lday18/Open;
     396: invokestatic  #215                // Method kotlin/jvm/internal/Intrinsics.areEqual:(Ljava/lang/Object;Ljava/lang/Object;)Z
     399: ifeq          406
     402: iconst_1
     403: goto          422
     406: aload         6
     408: getstatic     #78                 // Field day18/Close.INSTANCE:Lday18/Close;
     411: invokestatic  #215                // Method kotlin/jvm/internal/Intrinsics.areEqual:(Ljava/lang/Object;Ljava/lang/Object;)Z
     414: ifeq          421
     417: iconst_m1
     418: goto          422
     421: iconst_0
     422: iadd
     423: istore_3
     424: iinc          4, 1
     427: goto          77
     430: aload_2
     431: checkcast     #22                 // class java/lang/Iterable
     434: invokeinterface #39,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     439: astore        4
     441: iconst_0
     442: istore        5
     444: aload         4
     446: invokeinterface #45,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     451: ifeq          586
     454: iload         5
     456: istore        6
     458: iload         5
     460: iconst_1
     461: iadd
     462: istore        5
     464: aload         4
     466: invokeinterface #49,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     471: checkcast     #73                 // class day18/Element
     474: astore        7
     476: aload         7
     478: instanceof    #80                 // class day18/RegularNumber
     481: ifeq          444
     484: aload         7
     486: checkcast     #80                 // class day18/RegularNumber
     489: invokevirtual #211                // Method day18/RegularNumber.getValue:()I
     492: bipush        9
     494: if_icmple     444
     497: aload_2
     498: iload         6
     500: invokevirtual #218                // Method java/util/ArrayList.remove:(I)Ljava/lang/Object;
     503: pop
     504: aload_2
     505: iload         6
     507: iconst_4
     508: anewarray     #73                 // class day18/Element
     511: astore        8
     513: aload         8
     515: iconst_0
     516: getstatic     #71                 // Field day18/Open.INSTANCE:Lday18/Open;
     519: aastore
     520: aload         8
     522: iconst_1
     523: new           #80                 // class day18/RegularNumber
     526: dup
     527: aload         7
     529: checkcast     #80                 // class day18/RegularNumber
     532: invokevirtual #211                // Method day18/RegularNumber.getValue:()I
     535: iconst_2
     536: idiv
     537: invokespecial #87                 // Method day18/RegularNumber."<init>":(I)V
     540: aastore
     541: aload         8
     543: iconst_2
     544: new           #80                 // class day18/RegularNumber
     547: dup
     548: aload         7
     550: checkcast     #80                 // class day18/RegularNumber
     553: invokevirtual #211                // Method day18/RegularNumber.getValue:()I
     556: iconst_1
     557: iadd
     558: iconst_2
     559: idiv
     560: invokespecial #87                 // Method day18/RegularNumber."<init>":(I)V
     563: aastore
     564: aload         8
     566: iconst_3
     567: getstatic     #78                 // Field day18/Close.INSTANCE:Lday18/Close;
     570: aastore
     571: aload         8
     573: invokestatic  #222                // Method kotlin/collections/CollectionsKt.listOf:([Ljava/lang/Object;)Ljava/util/List;
     576: checkcast     #35                 // class java/util/Collection
     579: invokevirtual #225                // Method java/util/ArrayList.addAll:(ILjava/util/Collection;)Z
     582: pop
     583: goto          63
     586: nop
     587: aload_2
     588: checkcast     #93                 // class java/util/List
     591: areturn

  public static final int magnitude(java.util.List<? extends day18.Element>);
    Code:
       0: aload_0
       1: ldc           #172                // String <this>
       3: invokestatic  #178                // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: iconst_0
       8: invokestatic  #244                // Method recurse:(Ljava/util/List;I)Lkotlin/Pair;
      11: invokevirtual #249                // Method kotlin/Pair.getFirst:()Ljava/lang/Object;
      14: checkcast     #251                // class java/lang/Number
      17: invokevirtual #254                // Method java/lang/Number.intValue:()I
      20: ireturn

  public static final kotlin.Pair<java.lang.Integer, java.lang.Integer> recurse(java.util.List<? extends day18.Element>, int);
    Code:
       0: aload_0
       1: ldc           #172                // String <this>
       3: invokestatic  #178                // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: iload_1
       8: invokeinterface #257,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      13: checkcast     #73                 // class day18/Element
      16: astore_2
      17: aload_2
      18: getstatic     #71                 // Field day18/Open.INSTANCE:Lday18/Open;
      21: invokestatic  #215                // Method kotlin/jvm/internal/Intrinsics.areEqual:(Ljava/lang/Object;Ljava/lang/Object;)Z
      24: ifeq          122
      27: aload_0
      28: iload_1
      29: iconst_1
      30: iadd
      31: invokestatic  #244                // Method recurse:(Ljava/util/List;I)Lkotlin/Pair;
      34: astore_3
      35: aload_3
      36: invokevirtual #260                // Method kotlin/Pair.component1:()Ljava/lang/Object;
      39: checkcast     #251                // class java/lang/Number
      42: invokevirtual #254                // Method java/lang/Number.intValue:()I
      45: istore        4
      47: aload_3
      48: invokevirtual #263                // Method kotlin/Pair.component2:()Ljava/lang/Object;
      51: checkcast     #251                // class java/lang/Number
      54: invokevirtual #254                // Method java/lang/Number.intValue:()I
      57: istore        5
      59: aload_0
      60: iload         5
      62: invokestatic  #244                // Method recurse:(Ljava/util/List;I)Lkotlin/Pair;
      65: astore        6
      67: aload         6
      69: invokevirtual #260                // Method kotlin/Pair.component1:()Ljava/lang/Object;
      72: checkcast     #251                // class java/lang/Number
      75: invokevirtual #254                // Method java/lang/Number.intValue:()I
      78: istore        7
      80: aload         6
      82: invokevirtual #263                // Method kotlin/Pair.component2:()Ljava/lang/Object;
      85: checkcast     #251                // class java/lang/Number
      88: invokevirtual #254                // Method java/lang/Number.intValue:()I
      91: istore        8
      93: new           #246                // class kotlin/Pair
      96: dup
      97: iconst_3
      98: iload         4
     100: imul
     101: iconst_2
     102: iload         7
     104: imul
     105: iadd
     106: invokestatic  #269                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     109: iload         8
     111: iconst_1
     112: iadd
     113: invokestatic  #269                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     116: invokespecial #272                // Method kotlin/Pair."<init>":(Ljava/lang/Object;Ljava/lang/Object;)V
     119: goto          163
     122: aload_2
     123: instanceof    #80                 // class day18/RegularNumber
     126: ifeq          155
     129: new           #246                // class kotlin/Pair
     132: dup
     133: aload_2
     134: checkcast     #80                 // class day18/RegularNumber
     137: invokevirtual #211                // Method day18/RegularNumber.getValue:()I
     140: invokestatic  #269                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     143: iload_1
     144: iconst_1
     145: iadd
     146: invokestatic  #269                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     149: invokespecial #272                // Method kotlin/Pair."<init>":(Ljava/lang/Object;Ljava/lang/Object;)V
     152: goto          163
     155: new           #274                // class java/lang/IllegalStateException
     158: dup
     159: invokespecial #275                // Method java/lang/IllegalStateException."<init>":()V
     162: athrow
     163: areturn

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

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

Compiled from "result.kt"
final class day18.ResultKt$main$2 extends kotlin.coroutines.jvm.internal.RestrictedSuspendLambda implements kotlin.jvm.functions.Function2<kotlin.sequences.SequenceScope<? super java.util.List<? extends day18.Element>>, kotlin.coroutines.Continuation<? super kotlin.Unit>, java.lang.Object> {
  java.lang.Object L$1;

  java.lang.Object L$2;

  java.lang.Object L$3;

  int I$0;

  int I$1;

  int I$2;

  int label;

  private java.lang.Object L$0;

  final java.util.List<java.util.List<day18.Element>> $elements;

  day18.ResultKt$main$2(java.util.List<? extends java.util.List<? extends day18.Element>>, kotlin.coroutines.Continuation<? super day18.ResultKt$main$2>);
    Code:
       0: aload_0
       1: aload_1
       2: putfield      #14                 // Field $elements:Ljava/util/List;
       5: aload_0
       6: iconst_2
       7: aload_2
       8: invokespecial #17                 // Method kotlin/coroutines/jvm/internal/RestrictedSuspendLambda."<init>":(ILkotlin/coroutines/Continuation;)V
      11: return

  public final java.lang.Object invokeSuspend(java.lang.Object);
    Code:
       0: invokestatic  #52                 // Method kotlin/coroutines/intrinsics/IntrinsicsKt.getCOROUTINE_SUSPENDED:()Ljava/lang/Object;
       3: astore        11
       5: aload_0
       6: getfield      #55                 // Field label:I
       9: tableswitch   { // 0 to 1
                     0: 32
                     1: 215
               default: 280
          }
      32: aload_1
      33: invokestatic  #61                 // Method kotlin/ResultKt.throwOnFailure:(Ljava/lang/Object;)V
      36: aload_0
      37: getfield      #63                 // Field L$0:Ljava/lang/Object;
      40: checkcast     #65                 // class kotlin/sequences/SequenceScope
      43: astore_2
      44: aload_0
      45: getfield      #14                 // Field $elements:Ljava/util/List;
      48: checkcast     #67                 // class java/lang/Iterable
      51: invokeinterface #71,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      56: astore_3
      57: iconst_0
      58: istore        4
      60: aload_3
      61: invokeinterface #77,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      66: ifeq          276
      69: iload         4
      71: istore        5
      73: iload         4
      75: iconst_1
      76: iadd
      77: istore        4
      79: aload_3
      80: invokeinterface #80,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      85: checkcast     #82                 // class java/util/List
      88: astore        6
      90: aload_0
      91: getfield      #14                 // Field $elements:Ljava/util/List;
      94: checkcast     #67                 // class java/lang/Iterable
      97: invokeinterface #71,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     102: astore        7
     104: iconst_0
     105: istore        8
     107: aload         7
     109: invokeinterface #77,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     114: ifeq          60
     117: iload         8
     119: istore        9
     121: iload         8
     123: iconst_1
     124: iadd
     125: istore        8
     127: aload         7
     129: invokeinterface #80,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     134: checkcast     #82                 // class java/util/List
     137: astore        10
     139: iload         5
     141: iload         9
     143: if_icmpeq     107
     146: aload_2
     147: aload         6
     149: aload         10
     151: invokestatic  #88                 // Method day18/ResultKt.access$plus:(Ljava/util/List;Ljava/util/List;)Ljava/util/List;
     154: aload_0
     155: checkcast     #90                 // class kotlin/coroutines/Continuation
     158: aload_0
     159: aload_2
     160: putfield      #63                 // Field L$0:Ljava/lang/Object;
     163: aload_0
     164: aload_3
     165: putfield      #92                 // Field L$1:Ljava/lang/Object;
     168: aload_0
     169: aload         6
     171: putfield      #94                 // Field L$2:Ljava/lang/Object;
     174: aload_0
     175: aload         7
     177: putfield      #96                 // Field L$3:Ljava/lang/Object;
     180: aload_0
     181: iload         4
     183: putfield      #98                 // Field I$0:I
     186: aload_0
     187: iload         5
     189: putfield      #100                // Field I$1:I
     192: aload_0
     193: iload         8
     195: putfield      #102                // Field I$2:I
     198: aload_0
     199: iconst_1
     200: putfield      #55                 // Field label:I
     203: invokevirtual #106                // Method kotlin/sequences/SequenceScope.yield:(Ljava/lang/Object;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
     206: dup
     207: aload         11
     209: if_acmpne     272
     212: aload         11
     214: areturn
     215: aload_0
     216: getfield      #102                // Field I$2:I
     219: istore        8
     221: aload_0
     222: getfield      #100                // Field I$1:I
     225: istore        5
     227: aload_0
     228: getfield      #98                 // Field I$0:I
     231: istore        4
     233: aload_0
     234: getfield      #96                 // Field L$3:Ljava/lang/Object;
     237: checkcast     #73                 // class java/util/Iterator
     240: astore        7
     242: aload_0
     243: getfield      #94                 // Field L$2:Ljava/lang/Object;
     246: checkcast     #82                 // class java/util/List
     249: astore        6
     251: aload_0
     252: getfield      #92                 // Field L$1:Ljava/lang/Object;
     255: checkcast     #73                 // class java/util/Iterator
     258: astore_3
     259: aload_0
     260: getfield      #63                 // Field L$0:Ljava/lang/Object;
     263: checkcast     #65                 // class kotlin/sequences/SequenceScope
     266: astore_2
     267: aload_1
     268: invokestatic  #61                 // Method kotlin/ResultKt.throwOnFailure:(Ljava/lang/Object;)V
     271: aload_1
     272: pop
     273: goto          107
     276: getstatic     #112                // Field kotlin/Unit.INSTANCE:Lkotlin/Unit;
     279: areturn
     280: new           #114                // class java/lang/IllegalStateException
     283: dup
     284: ldc           #116                // String call to \'resume\' before \'invoke\' with coroutine
     286: invokespecial #119                // Method java/lang/IllegalStateException."<init>":(Ljava/lang/String;)V
     289: athrow

  public final kotlin.coroutines.Continuation<kotlin.Unit> create(java.lang.Object, kotlin.coroutines.Continuation<?>);
    Code:
       0: new           #2                  // class day18/ResultKt$main$2
       3: dup
       4: aload_0
       5: getfield      #14                 // Field $elements:Ljava/util/List;
       8: aload_2
       9: invokespecial #130                // Method "<init>":(Ljava/util/List;Lkotlin/coroutines/Continuation;)V
      12: astore_3
      13: aload_3
      14: aload_1
      15: putfield      #63                 // Field L$0:Ljava/lang/Object;
      18: aload_3
      19: checkcast     #90                 // class kotlin/coroutines/Continuation
      22: areturn

  public final java.lang.Object invoke(kotlin.sequences.SequenceScope<? super java.util.List<? extends day18.Element>>, kotlin.coroutines.Continuation<? super kotlin.Unit>);
    Code:
       0: aload_0
       1: aload_1
       2: aload_2
       3: invokevirtual #136                // Method create:(Ljava/lang/Object;Lkotlin/coroutines/Continuation;)Lkotlin/coroutines/Continuation;
       6: checkcast     #2                  // class day18/ResultKt$main$2
       9: getstatic     #112                // Field kotlin/Unit.INSTANCE:Lkotlin/Unit;
      12: invokevirtual #138                // Method invokeSuspend:(Ljava/lang/Object;)Ljava/lang/Object;
      15: areturn

  public java.lang.Object invoke(java.lang.Object, java.lang.Object);
    Code:
       0: aload_0
       1: aload_1
       2: checkcast     #65                 // class kotlin/sequences/SequenceScope
       5: aload_2
       6: checkcast     #90                 // class kotlin/coroutines/Continuation
       9: invokevirtual #143                // Method invoke:(Lkotlin/sequences/SequenceScope;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
      12: areturn
}


package day20

import java.io.File

fun Array<Array<Boolean>>.count() = sumOf { row -> row.count { it } }

fun Array<Array<Boolean>>.enhance(algo: List<Boolean>, infinity: Boolean) =
    (-1..size).map { y ->
        (-1..this[0].size).map { x ->
            algo[index(x, y, infinity)]
        }.toTypedArray()
    }.toTypedArray()

fun Array<Array<Boolean>>.index(x: Int, y: Int, infinity: Boolean) =
    (-1..1).map { dy -> (-1..1).map { dx -> if (get(x + dx, y + dy, infinity)) 1 else 0 } }
        .flatten().joinToString("").toInt(2)

fun Array<Array<Boolean>>.get(x: Int, y: Int, infinity: Boolean) =
    if (y < 0 || y >= size || x < 0 || x >= this[0].size) infinity else this[y][x]

fun main() {
    val lines = File("src/day20/input.txt").readLines()
    val algo = lines[0].toList().map { it == '#' }

    var pixels = lines.drop(2)
        .map { line -> line.toList().map { (it == '#') }.toTypedArray() }.toTypedArray()

    repeat(2) {
        pixels = pixels.enhance(algo, algo[0] && (it % 2 != 0))
    }
    println(pixels.count())

    repeat(48) {
        pixels = pixels.enhance(algo, algo[0] && (it % 2 != 0))
    }
    println(pixels.count())
}

Compiled from "result.kt"
public final class day20.ResultKt {
  public static final int count(java.lang.Boolean[][]);
    Code:
       0: aload_0
       1: ldc           #9                  // String <this>
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: checkcast     #17                 // class "[Ljava/lang/Object;"
      10: astore_1
      11: iconst_0
      12: istore_2
      13: iconst_0
      14: istore_3
      15: aload_1
      16: arraylength
      17: istore        4
      19: iload_3
      20: iload         4
      22: if_icmpge     116
      25: aload_1
      26: iload_3
      27: aaload
      28: astore        5
      30: iload_2
      31: aload         5
      33: checkcast     #19                 // class "[Ljava/lang/Boolean;"
      36: astore        6
      38: istore        16
      40: iconst_0
      41: istore        7
      43: aload         6
      45: astore        8
      47: iconst_0
      48: istore        9
      50: iconst_0
      51: istore        10
      53: iconst_0
      54: istore        11
      56: aload         8
      58: arraylength
      59: istore        12
      61: iload         11
      63: iload         12
      65: if_icmpge     99
      68: aload         8
      70: iload         11
      72: aaload
      73: astore        13
      75: aload         13
      77: invokevirtual #25                 // Method java/lang/Boolean.booleanValue:()Z
      80: istore        14
      82: iconst_0
      83: istore        15
      85: iload         14
      87: ifeq          93
      90: iinc          10, 1
      93: iinc          11, 1
      96: goto          61
      99: iload         10
     101: nop
     102: istore        17
     104: iload         16
     106: iload         17
     108: iadd
     109: istore_2
     110: iinc          3, 1
     113: goto          19
     116: iload_2
     117: ireturn

  public static final java.lang.Boolean[][] enhance(java.lang.Boolean[][], java.util.List<java.lang.Boolean>, boolean);
    Code:
       0: aload_0
       1: ldc           #9                  // String <this>
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_1
       7: ldc           #44                 // String algo
       9: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
      12: new           #46                 // class kotlin/ranges/IntRange
      15: dup
      16: iconst_m1
      17: aload_0
      18: checkcast     #17                 // class "[Ljava/lang/Object;"
      21: arraylength
      22: invokespecial #50                 // Method kotlin/ranges/IntRange."<init>":(II)V
      25: checkcast     #52                 // class java/lang/Iterable
      28: astore_3
      29: iconst_0
      30: istore        4
      32: aload_3
      33: astore        5
      35: new           #54                 // class java/util/ArrayList
      38: dup
      39: aload_3
      40: bipush        10
      42: invokestatic  #60                 // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
      45: invokespecial #63                 // Method java/util/ArrayList."<init>":(I)V
      48: checkcast     #65                 // class java/util/Collection
      51: astore        6
      53: iconst_0
      54: istore        7
      56: aload         5
      58: invokeinterface #69,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      63: astore        8
      65: aload         8
      67: invokeinterface #74,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      72: ifeq          264
      75: aload         8
      77: checkcast     #76                 // class kotlin/collections/IntIterator
      80: invokevirtual #80                 // Method kotlin/collections/IntIterator.nextInt:()I
      83: istore        9
      85: aload         6
      87: iload         9
      89: istore        10
      91: astore        22
      93: iconst_0
      94: istore        11
      96: new           #46                 // class kotlin/ranges/IntRange
      99: dup
     100: iconst_m1
     101: aload_0
     102: iconst_0
     103: aaload
     104: arraylength
     105: invokespecial #50                 // Method kotlin/ranges/IntRange."<init>":(II)V
     108: checkcast     #52                 // class java/lang/Iterable
     111: astore        12
     113: iconst_0
     114: istore        13
     116: aload         12
     118: astore        14
     120: new           #54                 // class java/util/ArrayList
     123: dup
     124: aload         12
     126: bipush        10
     128: invokestatic  #60                 // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
     131: invokespecial #63                 // Method java/util/ArrayList."<init>":(I)V
     134: checkcast     #65                 // class java/util/Collection
     137: astore        15
     139: iconst_0
     140: istore        16
     142: aload         14
     144: invokeinterface #69,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     149: astore        17
     151: aload         17
     153: invokeinterface #74,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     158: ifeq          218
     161: aload         17
     163: checkcast     #76                 // class kotlin/collections/IntIterator
     166: invokevirtual #80                 // Method kotlin/collections/IntIterator.nextInt:()I
     169: istore        18
     171: aload         15
     173: iload         18
     175: istore        19
     177: astore        20
     179: iconst_0
     180: istore        21
     182: aload_1
     183: aload_0
     184: iload         19
     186: iload         10
     188: iload_2
     189: invokestatic  #84                 // Method index:([[Ljava/lang/Boolean;IIZ)I
     192: invokeinterface #90,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     197: checkcast     #21                 // class java/lang/Boolean
     200: invokevirtual #25                 // Method java/lang/Boolean.booleanValue:()Z
     203: invokestatic  #94                 // Method java/lang/Boolean.valueOf:(Z)Ljava/lang/Boolean;
     206: aload         20
     208: swap
     209: invokeinterface #98,  2           // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     214: pop
     215: goto          151
     218: aload         15
     220: checkcast     #86                 // class java/util/List
     223: nop
     224: checkcast     #65                 // class java/util/Collection
     227: astore        12
     229: nop
     230: iconst_0
     231: istore        13
     233: aload         12
     235: astore        14
     237: aload         14
     239: iconst_0
     240: anewarray     #21                 // class java/lang/Boolean
     243: invokeinterface #102,  2          // InterfaceMethod java/util/Collection.toArray:([Ljava/lang/Object;)[Ljava/lang/Object;
     248: checkcast     #19                 // class "[Ljava/lang/Boolean;"
     251: nop
     252: aload         22
     254: swap
     255: invokeinterface #98,  2           // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     260: pop
     261: goto          65
     264: aload         6
     266: checkcast     #86                 // class java/util/List
     269: nop
     270: checkcast     #65                 // class java/util/Collection
     273: astore_3
     274: nop
     275: iconst_0
     276: istore        4
     278: aload_3
     279: astore        5
     281: aload         5
     283: iconst_0
     284: anewarray     #19                 // class "[Ljava/lang/Boolean;"
     287: invokeinterface #102,  2          // InterfaceMethod java/util/Collection.toArray:([Ljava/lang/Object;)[Ljava/lang/Object;
     292: checkcast     #39                 // class "[[Ljava/lang/Boolean;"
     295: areturn

  public static final int index(java.lang.Boolean[][], int, int, boolean);
    Code:
       0: aload_0
       1: ldc           #9                  // String <this>
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: new           #46                 // class kotlin/ranges/IntRange
       9: dup
      10: iconst_m1
      11: iconst_1
      12: invokespecial #50                 // Method kotlin/ranges/IntRange."<init>":(II)V
      15: checkcast     #52                 // class java/lang/Iterable
      18: astore        4
      20: iconst_0
      21: istore        5
      23: aload         4
      25: astore        6
      27: new           #54                 // class java/util/ArrayList
      30: dup
      31: aload         4
      33: bipush        10
      35: invokestatic  #60                 // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
      38: invokespecial #63                 // Method java/util/ArrayList."<init>":(I)V
      41: checkcast     #65                 // class java/util/Collection
      44: astore        7
      46: iconst_0
      47: istore        8
      49: aload         6
      51: invokeinterface #69,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      56: astore        9
      58: aload         9
      60: invokeinterface #74,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      65: ifeq          227
      68: aload         9
      70: checkcast     #76                 // class kotlin/collections/IntIterator
      73: invokevirtual #80                 // Method kotlin/collections/IntIterator.nextInt:()I
      76: istore        10
      78: aload         7
      80: iload         10
      82: istore        11
      84: astore        23
      86: iconst_0
      87: istore        12
      89: new           #46                 // class kotlin/ranges/IntRange
      92: dup
      93: iconst_m1
      94: iconst_1
      95: invokespecial #50                 // Method kotlin/ranges/IntRange."<init>":(II)V
      98: checkcast     #52                 // class java/lang/Iterable
     101: astore        13
     103: iconst_0
     104: istore        14
     106: aload         13
     108: astore        15
     110: new           #54                 // class java/util/ArrayList
     113: dup
     114: aload         13
     116: bipush        10
     118: invokestatic  #60                 // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
     121: invokespecial #63                 // Method java/util/ArrayList."<init>":(I)V
     124: checkcast     #65                 // class java/util/Collection
     127: astore        16
     129: iconst_0
     130: istore        17
     132: aload         15
     134: invokeinterface #69,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     139: astore        18
     141: aload         18
     143: invokeinterface #74,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     148: ifeq          208
     151: aload         18
     153: checkcast     #76                 // class kotlin/collections/IntIterator
     156: invokevirtual #80                 // Method kotlin/collections/IntIterator.nextInt:()I
     159: istore        19
     161: aload         16
     163: iload         19
     165: istore        20
     167: astore        21
     169: iconst_0
     170: istore        22
     172: aload_0
     173: iload_1
     174: iload         20
     176: iadd
     177: iload_2
     178: iload         11
     180: iadd
     181: iload_3
     182: invokestatic  #123                // Method get:([[Ljava/lang/Boolean;IIZ)Z
     185: ifeq          192
     188: iconst_1
     189: goto          193
     192: iconst_0
     193: invokestatic  #128                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     196: aload         21
     198: swap
     199: invokeinterface #98,  2           // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     204: pop
     205: goto          141
     208: aload         16
     210: checkcast     #86                 // class java/util/List
     213: nop
     214: nop
     215: aload         23
     217: swap
     218: invokeinterface #98,  2           // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     223: pop
     224: goto          58
     227: aload         7
     229: checkcast     #86                 // class java/util/List
     232: nop
     233: checkcast     #52                 // class java/lang/Iterable
     236: invokestatic  #132                // Method kotlin/collections/CollectionsKt.flatten:(Ljava/lang/Iterable;)Ljava/util/List;
     239: checkcast     #52                 // class java/lang/Iterable
     242: ldc           #134                // String
     244: checkcast     #136                // class java/lang/CharSequence
     247: aconst_null
     248: aconst_null
     249: iconst_0
     250: aconst_null
     251: aconst_null
     252: bipush        62
     254: aconst_null
     255: invokestatic  #140                // 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;
     258: astore        4
     260: iconst_2
     261: istore        5
     263: aload         4
     265: iload         5
     267: invokestatic  #146                // Method kotlin/text/CharsKt.checkRadix:(I)I
     270: invokestatic  #150                // Method java/lang/Integer.parseInt:(Ljava/lang/String;I)I
     273: ireturn

  public static final boolean get(java.lang.Boolean[][], int, int, boolean);
    Code:
       0: aload_0
       1: ldc           #9                  // String <this>
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: iload_2
       7: iflt          31
      10: iload_2
      11: aload_0
      12: checkcast     #17                 // class "[Ljava/lang/Object;"
      15: arraylength
      16: if_icmpge     31
      19: iload_1
      20: iflt          31
      23: iload_1
      24: aload_0
      25: iconst_0
      26: aaload
      27: arraylength
      28: if_icmplt     35
      31: iload_3
      32: goto          43
      35: aload_0
      36: iload_2
      37: aaload
      38: iload_1
      39: aaload
      40: invokevirtual #25                 // Method java/lang/Boolean.booleanValue:()Z
      43: ireturn

  public static final void main();
    Code:
       0: new           #160                // class java/io/File
       3: dup
       4: ldc           #162                // String src/day20/input.txt
       6: invokespecial #165                // Method java/io/File."<init>":(Ljava/lang/String;)V
       9: aconst_null
      10: iconst_1
      11: aconst_null
      12: invokestatic  #171                // Method kotlin/io/FilesKt.readLines$default:(Ljava/io/File;Ljava/nio/charset/Charset;ILjava/lang/Object;)Ljava/util/List;
      15: astore_0
      16: aload_0
      17: iconst_0
      18: invokeinterface #90,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      23: checkcast     #136                // class java/lang/CharSequence
      26: invokestatic  #177                // Method kotlin/text/StringsKt.toList:(Ljava/lang/CharSequence;)Ljava/util/List;
      29: checkcast     #52                 // class java/lang/Iterable
      32: astore_2
      33: iconst_0
      34: istore_3
      35: aload_2
      36: astore        4
      38: new           #54                 // class java/util/ArrayList
      41: dup
      42: aload_2
      43: bipush        10
      45: invokestatic  #60                 // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
      48: invokespecial #63                 // Method java/util/ArrayList."<init>":(I)V
      51: checkcast     #65                 // class java/util/Collection
      54: astore        5
      56: iconst_0
      57: istore        6
      59: aload         4
      61: invokeinterface #69,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      66: astore        7
      68: aload         7
      70: invokeinterface #74,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      75: ifeq          131
      78: aload         7
      80: invokeinterface #181,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      85: astore        8
      87: aload         5
      89: aload         8
      91: checkcast     #183                // class java/lang/Character
      94: invokevirtual #187                // Method java/lang/Character.charValue:()C
      97: istore        9
      99: astore        22
     101: iconst_0
     102: istore        10
     104: iload         9
     106: bipush        35
     108: if_icmpne     115
     111: iconst_1
     112: goto          116
     115: iconst_0
     116: invokestatic  #94                 // Method java/lang/Boolean.valueOf:(Z)Ljava/lang/Boolean;
     119: aload         22
     121: swap
     122: invokeinterface #98,  2           // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     127: pop
     128: goto          68
     131: aload         5
     133: checkcast     #86                 // class java/util/List
     136: nop
     137: astore_1
     138: aconst_null
     139: astore_2
     140: aload_0
     141: checkcast     #52                 // class java/lang/Iterable
     144: iconst_2
     145: invokestatic  #191                // Method kotlin/collections/CollectionsKt.drop:(Ljava/lang/Iterable;I)Ljava/util/List;
     148: checkcast     #52                 // class java/lang/Iterable
     151: astore_3
     152: nop
     153: iconst_0
     154: istore        4
     156: aload_3
     157: astore        5
     159: new           #54                 // class java/util/ArrayList
     162: dup
     163: aload_3
     164: bipush        10
     166: invokestatic  #60                 // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
     169: invokespecial #63                 // Method java/util/ArrayList."<init>":(I)V
     172: checkcast     #65                 // class java/util/Collection
     175: astore        6
     177: iconst_0
     178: istore        7
     180: aload         5
     182: invokeinterface #69,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     187: astore        8
     189: aload         8
     191: invokeinterface #74,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     196: ifeq          382
     199: aload         8
     201: invokeinterface #181,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     206: astore        9
     208: aload         6
     210: aload         9
     212: checkcast     #193                // class java/lang/String
     215: astore        10
     217: astore        23
     219: iconst_0
     220: istore        11
     222: aload         10
     224: checkcast     #136                // class java/lang/CharSequence
     227: invokestatic  #177                // Method kotlin/text/StringsKt.toList:(Ljava/lang/CharSequence;)Ljava/util/List;
     230: checkcast     #52                 // class java/lang/Iterable
     233: astore        12
     235: iconst_0
     236: istore        13
     238: aload         12
     240: astore        14
     242: new           #54                 // class java/util/ArrayList
     245: dup
     246: aload         12
     248: bipush        10
     250: invokestatic  #60                 // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
     253: invokespecial #63                 // Method java/util/ArrayList."<init>":(I)V
     256: checkcast     #65                 // class java/util/Collection
     259: astore        15
     261: iconst_0
     262: istore        16
     264: aload         14
     266: invokeinterface #69,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     271: astore        17
     273: aload         17
     275: invokeinterface #74,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     280: ifeq          336
     283: aload         17
     285: invokeinterface #181,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     290: astore        18
     292: aload         15
     294: aload         18
     296: checkcast     #183                // class java/lang/Character
     299: invokevirtual #187                // Method java/lang/Character.charValue:()C
     302: istore        19
     304: astore        20
     306: iconst_0
     307: istore        21
     309: iload         19
     311: bipush        35
     313: if_icmpne     320
     316: iconst_1
     317: goto          321
     320: iconst_0
     321: invokestatic  #94                 // Method java/lang/Boolean.valueOf:(Z)Ljava/lang/Boolean;
     324: aload         20
     326: swap
     327: invokeinterface #98,  2           // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     332: pop
     333: goto          273
     336: aload         15
     338: checkcast     #86                 // class java/util/List
     341: nop
     342: checkcast     #65                 // class java/util/Collection
     345: astore        12
     347: nop
     348: iconst_0
     349: istore        13
     351: aload         12
     353: astore        14
     355: aload         14
     357: iconst_0
     358: anewarray     #21                 // class java/lang/Boolean
     361: invokeinterface #102,  2          // InterfaceMethod java/util/Collection.toArray:([Ljava/lang/Object;)[Ljava/lang/Object;
     366: checkcast     #19                 // class "[Ljava/lang/Boolean;"
     369: nop
     370: aload         23
     372: swap
     373: invokeinterface #98,  2           // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     378: pop
     379: goto          189
     382: aload         6
     384: checkcast     #86                 // class java/util/List
     387: nop
     388: checkcast     #65                 // class java/util/Collection
     391: astore_3
     392: nop
     393: iconst_0
     394: istore        4
     396: aload_3
     397: astore        5
     399: aload         5
     401: iconst_0
     402: anewarray     #19                 // class "[Ljava/lang/Boolean;"
     405: invokeinterface #102,  2          // InterfaceMethod java/util/Collection.toArray:([Ljava/lang/Object;)[Ljava/lang/Object;
     410: astore_2
     411: iconst_2
     412: istore_3
     413: iconst_0
     414: istore        4
     416: iload         4
     418: iload_3
     419: if_icmpge     473
     422: iload         4
     424: istore        5
     426: iconst_0
     427: istore        6
     429: aload_2
     430: checkcast     #39                 // class "[[Ljava/lang/Boolean;"
     433: aload_1
     434: aload_1
     435: iconst_0
     436: invokeinterface #90,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     441: checkcast     #21                 // class java/lang/Boolean
     444: invokevirtual #25                 // Method java/lang/Boolean.booleanValue:()Z
     447: ifeq          461
     450: iload         5
     452: iconst_2
     453: irem
     454: ifeq          461
     457: iconst_1
     458: goto          462
     461: iconst_0
     462: invokestatic  #195                // Method enhance:([[Ljava/lang/Boolean;Ljava/util/List;Z)[[Ljava/lang/Boolean;
     465: astore_2
     466: nop
     467: iinc          4, 1
     470: goto          416
     473: aload_2
     474: checkcast     #39                 // class "[[Ljava/lang/Boolean;"
     477: invokestatic  #197                // Method count:([[Ljava/lang/Boolean;)I
     480: istore_3
     481: getstatic     #203                // Field java/lang/System.out:Ljava/io/PrintStream;
     484: iload_3
     485: invokevirtual #208                // Method java/io/PrintStream.println:(I)V
     488: bipush        48
     490: istore_3
     491: iconst_0
     492: istore        4
     494: iload         4
     496: iload_3
     497: if_icmpge     551
     500: iload         4
     502: istore        5
     504: iconst_0
     505: istore        6
     507: aload_2
     508: checkcast     #39                 // class "[[Ljava/lang/Boolean;"
     511: aload_1
     512: aload_1
     513: iconst_0
     514: invokeinterface #90,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     519: checkcast     #21                 // class java/lang/Boolean
     522: invokevirtual #25                 // Method java/lang/Boolean.booleanValue:()Z
     525: ifeq          539
     528: iload         5
     530: iconst_2
     531: irem
     532: ifeq          539
     535: iconst_1
     536: goto          540
     539: iconst_0
     540: invokestatic  #195                // Method enhance:([[Ljava/lang/Boolean;Ljava/util/List;Z)[[Ljava/lang/Boolean;
     543: astore_2
     544: nop
     545: iinc          4, 1
     548: goto          494
     551: aload_2
     552: checkcast     #39                 // class "[[Ljava/lang/Boolean;"
     555: invokestatic  #197                // Method count:([[Ljava/lang/Boolean;)I
     558: istore_3
     559: getstatic     #203                // Field java/lang/System.out:Ljava/io/PrintStream;
     562: iload_3
     563: invokevirtual #208                // Method java/io/PrintStream.println:(I)V
     566: return

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


package day16

import java.io.File
import java.lang.IllegalStateException
import kotlin.math.max
import kotlin.math.min

data class Packet(val length: Int, val versionSum: Int, val value: Long)
data class Literal(val length: Int, val value: Long)

data class Bits(val input: String) {
    private var index = 0
    private fun getString(length: Int): String {
        val result = input.substring(index, index + length)
        index += length
        return result
    }
    private fun getInt(length: Int) = getString(length).toInt(2)
    private fun getLong(length: Int) = getString(length).toLong(2)
    private fun getLiteral(): Literal {
        val startIndex = index
        var result = ""
        do {
            val hasNext = getInt(1) == 1
            result += getString(4)
        } while (hasNext)
        return Literal(index - startIndex, result.toLong(2))
    }

    fun getPacket(): Packet {
        val version = getInt(3)
        val type = getInt(3)

        if (type == 4) {
            return getLiteral().let { Packet(it.length + 6, version, it.value) }
        } else {
            val lengthType = getInt(1)
            val subPackets = mutableListOf<Packet>()
            if (lengthType == 0) {
                val totalLength = getLong(15)
                val startIndex = index
                while (index < startIndex + totalLength) {
                    subPackets += getPacket()
                }
            } else {
                val numSubPackets = getLong(11)
                for (i in 0 until numSubPackets) {
                    subPackets += getPacket()
                }
            }
            val fn: (Long, Long) -> Long = when (type) {
                0 -> Long::plus
                1 -> Long::times
                2 -> ::min
                3 -> ::max
                5 -> { p1, p2 -> if (p1 > p2) 1 else 0 }
                6 -> { p1, p2 -> if (p1 < p2) 1 else 0 }
                7 -> { p1, p2 -> if (p1 == p2) 1 else 0 }
                else -> throw IllegalStateException()
            }
            return subPackets
                .reduce { p1, p2 ->
                    Packet(
                        p1.length + p2.length,
                        p1.versionSum + p2.versionSum,
                        fn(p1.value, p2.value)
                    )
                }
                .let { it.copy(length = it.length + 6, versionSum = it.versionSum + version) }
        }
    }
}

fun main() {
    val bits = Bits(File("src/day16/input.txt").readText()
        .map { hex -> Integer.parseInt(hex.toString(), 16) }
        .map { int -> Integer.toBinaryString(int) }
        .map { str -> str.padStart(4, '0') }
        .joinToString(""))
    bits.getPacket().let {
        println(it.versionSum)
        println(it.value)
    }
}

Compiled from "result.kt"
public final class day16.ResultKt {
  public static final void main();
    Code:
       0: new           #8                  // class java/io/File
       3: dup
       4: ldc           #10                 // String src/day16/input.txt
       6: invokespecial #14                 // Method java/io/File."<init>":(Ljava/lang/String;)V
       9: aconst_null
      10: iconst_1
      11: aconst_null
      12: invokestatic  #20                 // 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_1
      19: nop
      20: iconst_0
      21: istore_2
      22: aload_1
      23: astore_3
      24: new           #24                 // class java/util/ArrayList
      27: dup
      28: aload_1
      29: invokeinterface #28,  1           // InterfaceMethod java/lang/CharSequence.length:()I
      34: invokespecial #31                 // Method java/util/ArrayList."<init>":(I)V
      37: checkcast     #33                 // class java/util/Collection
      40: astore        4
      42: iconst_0
      43: istore        5
      45: iconst_0
      46: istore        6
      48: iload         6
      50: aload_3
      51: invokeinterface #28,  1           // InterfaceMethod java/lang/CharSequence.length:()I
      56: if_icmpge     108
      59: aload_3
      60: iload         6
      62: invokeinterface #37,  2           // InterfaceMethod java/lang/CharSequence.charAt:(I)C
      67: istore        7
      69: aload         4
      71: iload         7
      73: istore        8
      75: astore        10
      77: iconst_0
      78: istore        9
      80: iload         8
      82: invokestatic  #43                 // Method java/lang/String.valueOf:(C)Ljava/lang/String;
      85: bipush        16
      87: invokestatic  #49                 // Method java/lang/Integer.parseInt:(Ljava/lang/String;I)I
      90: invokestatic  #52                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      93: aload         10
      95: swap
      96: invokeinterface #56,  2           // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     101: pop
     102: iinc          6, 1
     105: goto          48
     108: aload         4
     110: checkcast     #58                 // class java/util/List
     113: nop
     114: checkcast     #60                 // class java/lang/Iterable
     117: astore_1
     118: nop
     119: iconst_0
     120: istore_2
     121: aload_1
     122: astore_3
     123: new           #24                 // class java/util/ArrayList
     126: dup
     127: aload_1
     128: bipush        10
     130: invokestatic  #66                 // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
     133: invokespecial #31                 // Method java/util/ArrayList."<init>":(I)V
     136: checkcast     #33                 // class java/util/Collection
     139: astore        4
     141: iconst_0
     142: istore        5
     144: aload_3
     145: invokeinterface #70,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     150: astore        6
     152: aload         6
     154: invokeinterface #76,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     159: ifeq          205
     162: aload         6
     164: invokeinterface #80,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     169: astore        7
     171: aload         4
     173: aload         7
     175: checkcast     #82                 // class java/lang/Number
     178: invokevirtual #85                 // Method java/lang/Number.intValue:()I
     181: istore        8
     183: astore        10
     185: iconst_0
     186: istore        9
     188: iload         8
     190: invokestatic  #89                 // Method java/lang/Integer.toBinaryString:(I)Ljava/lang/String;
     193: aload         10
     195: swap
     196: invokeinterface #56,  2           // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     201: pop
     202: goto          152
     205: aload         4
     207: checkcast     #58                 // class java/util/List
     210: nop
     211: checkcast     #60                 // class java/lang/Iterable
     214: astore_1
     215: nop
     216: iconst_0
     217: istore_2
     218: aload_1
     219: astore_3
     220: new           #24                 // class java/util/ArrayList
     223: dup
     224: aload_1
     225: bipush        10
     227: invokestatic  #66                 // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
     230: invokespecial #31                 // Method java/util/ArrayList."<init>":(I)V
     233: checkcast     #33                 // class java/util/Collection
     236: astore        4
     238: iconst_0
     239: istore        5
     241: aload_3
     242: invokeinterface #70,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     247: astore        6
     249: aload         6
     251: invokeinterface #76,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     256: ifeq          307
     259: aload         6
     261: invokeinterface #80,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     266: astore        7
     268: aload         4
     270: aload         7
     272: checkcast     #39                 // class java/lang/String
     275: astore        8
     277: astore        10
     279: iconst_0
     280: istore        9
     282: aload         8
     284: invokestatic  #95                 // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
     287: aload         8
     289: iconst_4
     290: bipush        48
     292: invokestatic  #101                // Method kotlin/text/StringsKt.padStart:(Ljava/lang/String;IC)Ljava/lang/String;
     295: aload         10
     297: swap
     298: invokeinterface #56,  2           // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     303: pop
     304: goto          249
     307: aload         4
     309: checkcast     #58                 // class java/util/List
     312: nop
     313: checkcast     #60                 // class java/lang/Iterable
     316: ldc           #103                // String
     318: checkcast     #22                 // class java/lang/CharSequence
     321: aconst_null
     322: aconst_null
     323: iconst_0
     324: aconst_null
     325: aconst_null
     326: bipush        62
     328: aconst_null
     329: invokestatic  #107                // 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;
     332: astore        11
     334: new           #109                // class day16/Bits
     337: dup
     338: aload         11
     340: invokespecial #110                // Method day16/Bits."<init>":(Ljava/lang/String;)V
     343: astore_0
     344: aload_0
     345: invokevirtual #114                // Method day16/Bits.getPacket:()Lday16/Packet;
     348: astore_2
     349: iconst_0
     350: istore_3
     351: aload_2
     352: invokevirtual #119                // Method day16/Packet.getVersionSum:()I
     355: istore        4
     357: getstatic     #125                // Field java/lang/System.out:Ljava/io/PrintStream;
     360: iload         4
     362: invokevirtual #130                // Method java/io/PrintStream.println:(I)V
     365: aload_2
     366: invokevirtual #134                // Method day16/Packet.getValue:()J
     369: lstore        5
     371: getstatic     #125                // Field java/lang/System.out:Ljava/io/PrintStream;
     374: lload         5
     376: invokevirtual #137                // Method java/io/PrintStream.println:(J)V
     379: nop
     380: nop
     381: return

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