package day14

import java.io.File

fun main() {
    val data = parse("src/day14/Day14.txt")

    println("🎄 Day 14 🎄")

    println()

    println("[Part 1]")
    println("Answer: ${part1(data)}")

    println()

    println("[Part 2]")
    println("Answer: ${part2(data)}")
}

private fun parse(path: String): List<List<Char>> =
    File(path)
        .readLines()
        .map(String::toList)

private fun part1(data: List<List<Char>>): Int {
    val h = data.size
    val w = data[0].size

    var counter = 0
    for (col in 0..<w) {
        var top = 0
        for (row in 0..<h) {
            when (data[row][col]) {
                'O' -> counter += h - top++
                '#' -> top = row + 1
            }
        }
    }

    return counter
}

private fun part2(data: List<List<Char>>): Int {
    val h = data.size
    val w = data[0].size

    val cycles = mutableMapOf<String, Int>()
    val loads = mutableListOf<Int>()

    val grid = data.map { it.toMutableList() }
    for (i in 1..1_000_000_000) {
        // North
        for (col in 0..<w) {
            var dst = 0
            for (row in 0..<h) {
                when (grid[row][col]) {
                    '#' -> dst = row + 1
                    'O' -> {
                        grid[row][col] = '.'
                        grid[dst][col] = 'O'
                        dst += 1
                    }
                }
            }
        }

        // West
        for (row in 0..<h) {
            var dst = 0
            for (col in 0..<w) {
                when (grid[row][col]) {
                    '#' -> dst = col + 1
                    'O' -> {
                        grid[row][col] = '.'
                        grid[row][dst] = 'O'
                        dst += 1
                    }
                }
            }
        }

        // South
        for (col in w - 1 downTo 0) {
            var dst = h - 1
            for (row in h - 1 downTo 0) {
                when (grid[row][col]) {
                    '#' -> dst = row - 1
                    'O' -> {
                        grid[row][col] = '.'
                        grid[dst][col] = 'O'
                        dst -= 1
                    }
                }
            }
        }

        // East
        for (row in h - 1 downTo 0) {
            var dst = w - 1
            for (col in w - 1 downTo 0) {
                when (grid[row][col]) {
                    '#' -> dst = col - 1
                    'O' -> {
                        grid[row][col] = '.'
                        grid[row][dst] = 'O'
                        dst -= 1
                    }
                }
            }
        }

        val cycle = grid.flatten().toString()
        cycles[cycle]?.let { start ->
            val remainder = (1_000_000_000 - start) % (i - start)
            val loop = cycles.values.filter { it >= start }
            return loads[loop[remainder] - 1]
        }

        cycles[cycle] = i
        loads += grid.withIndex().sumOf { (y, row) ->
            row.count { it == 'O' } * (h - y)
        }
    }

    error("Could not detect a cycle.")
}

Compiled from "Day14.kt"
public final class day14.Day14Kt {
  public static final void main();
    Code:
       0: ldc           #8                  // String src/day14/Day14.txt
       2: invokestatic  #12                 // Method parse:(Ljava/lang/String;)Ljava/util/List;
       5: astore_0
       6: ldc           #14                 // String 🎄 Day 14 🎄
       8: getstatic     #20                 // Field java/lang/System.out:Ljava/io/PrintStream;
      11: swap
      12: invokevirtual #26                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
      15: getstatic     #20                 // Field java/lang/System.out:Ljava/io/PrintStream;
      18: invokevirtual #28                 // Method java/io/PrintStream.println:()V
      21: ldc           #30                 // String [Part 1]
      23: getstatic     #20                 // Field java/lang/System.out:Ljava/io/PrintStream;
      26: swap
      27: invokevirtual #26                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
      30: new           #32                 // class java/lang/StringBuilder
      33: dup
      34: invokespecial #35                 // Method java/lang/StringBuilder."<init>":()V
      37: ldc           #37                 // String Answer:
      39: invokevirtual #41                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      42: aload_0
      43: invokestatic  #45                 // Method part1:(Ljava/util/List;)I
      46: invokevirtual #48                 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
      49: invokevirtual #52                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
      52: getstatic     #20                 // Field java/lang/System.out:Ljava/io/PrintStream;
      55: swap
      56: invokevirtual #26                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
      59: getstatic     #20                 // Field java/lang/System.out:Ljava/io/PrintStream;
      62: invokevirtual #28                 // Method java/io/PrintStream.println:()V
      65: ldc           #54                 // String [Part 2]
      67: getstatic     #20                 // Field java/lang/System.out:Ljava/io/PrintStream;
      70: swap
      71: invokevirtual #26                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
      74: new           #32                 // class java/lang/StringBuilder
      77: dup
      78: invokespecial #35                 // Method java/lang/StringBuilder."<init>":()V
      81: ldc           #37                 // String Answer:
      83: invokevirtual #41                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      86: aload_0
      87: invokestatic  #57                 // Method part2:(Ljava/util/List;)I
      90: invokevirtual #48                 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
      93: invokevirtual #52                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
      96: getstatic     #20                 // Field java/lang/System.out:Ljava/io/PrintStream;
      99: swap
     100: invokevirtual #26                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
     103: return

  private static final java.util.List<java.util.List<java.lang.Character>> parse(java.lang.String);
    Code:
       0: new           #62                 // class java/io/File
       3: dup
       4: aload_0
       5: invokespecial #65                 // Method java/io/File."<init>":(Ljava/lang/String;)V
       8: aconst_null
       9: iconst_1
      10: aconst_null
      11: invokestatic  #71                 // Method kotlin/io/FilesKt.readLines$default:(Ljava/io/File;Ljava/nio/charset/Charset;ILjava/lang/Object;)Ljava/util/List;
      14: checkcast     #73                 // class java/lang/Iterable
      17: astore_1
      18: nop
      19: iconst_0
      20: istore_2
      21: aload_1
      22: astore_3
      23: new           #75                 // class java/util/ArrayList
      26: dup
      27: aload_1
      28: bipush        10
      30: invokestatic  #81                 // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
      33: invokespecial #84                 // Method java/util/ArrayList."<init>":(I)V
      36: checkcast     #86                 // class java/util/Collection
      39: astore        4
      41: iconst_0
      42: istore        5
      44: aload_3
      45: invokeinterface #90,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      50: astore        6
      52: aload         6
      54: invokeinterface #96,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      59: ifeq          105
      62: aload         6
      64: invokeinterface #100,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      69: astore        7
      71: aload         4
      73: aload         7
      75: checkcast     #102                // class java/lang/String
      78: astore        8
      80: astore        10
      82: iconst_0
      83: istore        9
      85: aload         8
      87: checkcast     #104                // class java/lang/CharSequence
      90: invokestatic  #110                // Method kotlin/text/StringsKt.toList:(Ljava/lang/CharSequence;)Ljava/util/List;
      93: aload         10
      95: swap
      96: invokeinterface #114,  2          // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     101: pop
     102: goto          52
     105: aload         4
     107: checkcast     #116                // class java/util/List
     110: nop
     111: areturn

  private static final int part1(java.util.List<? extends java.util.List<java.lang.Character>>);
    Code:
       0: aload_0
       1: invokeinterface #135,  1          // InterfaceMethod java/util/List.size:()I
       6: istore_1
       7: aload_0
       8: iconst_0
       9: invokeinterface #139,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      14: checkcast     #116                // class java/util/List
      17: invokeinterface #135,  1          // InterfaceMethod java/util/List.size:()I
      22: istore_2
      23: iconst_0
      24: istore_3
      25: iconst_0
      26: istore        4
      28: iload         4
      30: iload_2
      31: if_icmpge     127
      34: iconst_0
      35: istore        5
      37: iconst_0
      38: istore        6
      40: iload         6
      42: iload_1
      43: if_icmpge     121
      46: aload_0
      47: iload         6
      49: invokeinterface #139,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      54: checkcast     #116                // class java/util/List
      57: iload         4
      59: invokeinterface #139,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      64: checkcast     #141                // class java/lang/Character
      67: invokevirtual #145                // Method java/lang/Character.charValue:()C
      70: lookupswitch  { // 2
                    35: 109
                    79: 96
               default: 115
          }
      96: iload_3
      97: iload_1
      98: iload         5
     100: iinc          5, 1
     103: isub
     104: iadd
     105: istore_3
     106: goto          115
     109: iload         6
     111: iconst_1
     112: iadd
     113: istore        5
     115: iinc          6, 1
     118: goto          40
     121: iinc          4, 1
     124: goto          28
     127: iload_3
     128: ireturn

  private static final int part2(java.util.List<? extends java.util.List<java.lang.Character>>);
    Code:
       0: aload_0
       1: invokeinterface #135,  1          // InterfaceMethod java/util/List.size:()I
       6: istore_1
       7: aload_0
       8: iconst_0
       9: invokeinterface #139,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      14: checkcast     #116                // class java/util/List
      17: invokeinterface #135,  1          // InterfaceMethod java/util/List.size:()I
      22: istore_2
      23: new           #153                // class java/util/LinkedHashMap
      26: dup
      27: invokespecial #154                // Method java/util/LinkedHashMap."<init>":()V
      30: checkcast     #156                // class java/util/Map
      33: astore_3
      34: new           #75                 // class java/util/ArrayList
      37: dup
      38: invokespecial #157                // Method java/util/ArrayList."<init>":()V
      41: checkcast     #116                // class java/util/List
      44: astore        4
      46: aload_0
      47: checkcast     #73                 // class java/lang/Iterable
      50: astore        6
      52: iconst_0
      53: istore        7
      55: aload         6
      57: astore        8
      59: new           #75                 // class java/util/ArrayList
      62: dup
      63: aload         6
      65: bipush        10
      67: invokestatic  #81                 // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
      70: invokespecial #84                 // Method java/util/ArrayList."<init>":(I)V
      73: checkcast     #86                 // class java/util/Collection
      76: astore        9
      78: iconst_0
      79: istore        10
      81: aload         8
      83: invokeinterface #90,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      88: astore        11
      90: aload         11
      92: invokeinterface #96,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      97: ifeq          143
     100: aload         11
     102: invokeinterface #100,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     107: astore        12
     109: aload         9
     111: aload         12
     113: checkcast     #116                // class java/util/List
     116: astore        13
     118: astore        24
     120: iconst_0
     121: istore        14
     123: aload         13
     125: checkcast     #86                 // class java/util/Collection
     128: invokestatic  #161                // Method kotlin/collections/CollectionsKt.toMutableList:(Ljava/util/Collection;)Ljava/util/List;
     131: aload         24
     133: swap
     134: invokeinterface #114,  2          // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     139: pop
     140: goto          90
     143: aload         9
     145: checkcast     #116                // class java/util/List
     148: nop
     149: astore        5
     151: iconst_1
     152: istore        6
     154: iload         6
     156: ldc           #162                // int 1000000001
     158: if_icmpge     1203
     161: iconst_0
     162: istore        7
     164: iload         7
     166: iload_2
     167: if_icmpge     306
     170: iconst_0
     171: istore        8
     173: iconst_0
     174: istore        9
     176: iload         9
     178: iload_1
     179: if_icmpge     300
     182: aload         5
     184: iload         9
     186: invokeinterface #139,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     191: checkcast     #116                // class java/util/List
     194: iload         7
     196: invokeinterface #139,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     201: checkcast     #141                // class java/lang/Character
     204: invokevirtual #145                // Method java/lang/Character.charValue:()C
     207: lookupswitch  { // 2
                    35: 232
                    79: 241
               default: 294
          }
     232: iload         9
     234: iconst_1
     235: iadd
     236: istore        8
     238: goto          294
     241: aload         5
     243: iload         9
     245: invokeinterface #139,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     250: checkcast     #116                // class java/util/List
     253: iload         7
     255: bipush        46
     257: invokestatic  #166                // Method java/lang/Character.valueOf:(C)Ljava/lang/Character;
     260: invokeinterface #170,  3          // InterfaceMethod java/util/List.set:(ILjava/lang/Object;)Ljava/lang/Object;
     265: pop
     266: aload         5
     268: iload         8
     270: invokeinterface #139,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     275: checkcast     #116                // class java/util/List
     278: iload         7
     280: bipush        79
     282: invokestatic  #166                // Method java/lang/Character.valueOf:(C)Ljava/lang/Character;
     285: invokeinterface #170,  3          // InterfaceMethod java/util/List.set:(ILjava/lang/Object;)Ljava/lang/Object;
     290: pop
     291: iinc          8, 1
     294: iinc          9, 1
     297: goto          176
     300: iinc          7, 1
     303: goto          164
     306: iconst_0
     307: istore        7
     309: iload         7
     311: iload_1
     312: if_icmpge     454
     315: iconst_0
     316: istore        8
     318: iconst_0
     319: istore        9
     321: iload         9
     323: iload_2
     324: if_icmpge     448
     327: aload         5
     329: iload         7
     331: invokeinterface #139,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     336: checkcast     #116                // class java/util/List
     339: iload         9
     341: invokeinterface #139,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     346: checkcast     #141                // class java/lang/Character
     349: invokevirtual #145                // Method java/lang/Character.charValue:()C
     352: lookupswitch  { // 2
                    35: 380
                    79: 389
               default: 442
          }
     380: iload         9
     382: iconst_1
     383: iadd
     384: istore        8
     386: goto          442
     389: aload         5
     391: iload         7
     393: invokeinterface #139,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     398: checkcast     #116                // class java/util/List
     401: iload         9
     403: bipush        46
     405: invokestatic  #166                // Method java/lang/Character.valueOf:(C)Ljava/lang/Character;
     408: invokeinterface #170,  3          // InterfaceMethod java/util/List.set:(ILjava/lang/Object;)Ljava/lang/Object;
     413: pop
     414: aload         5
     416: iload         7
     418: invokeinterface #139,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     423: checkcast     #116                // class java/util/List
     426: iload         8
     428: bipush        79
     430: invokestatic  #166                // Method java/lang/Character.valueOf:(C)Ljava/lang/Character;
     433: invokeinterface #170,  3          // InterfaceMethod java/util/List.set:(ILjava/lang/Object;)Ljava/lang/Object;
     438: pop
     439: iinc          8, 1
     442: iinc          9, 1
     445: goto          321
     448: iinc          7, 1
     451: goto          309
     454: iload_2
     455: iconst_1
     456: isub
     457: istore        7
     459: iconst_m1
     460: iload         7
     462: if_icmpge     606
     465: iload_1
     466: iconst_1
     467: isub
     468: istore        8
     470: iload_1
     471: iconst_1
     472: isub
     473: istore        9
     475: iconst_m1
     476: iload         9
     478: if_icmpge     600
     481: aload         5
     483: iload         9
     485: invokeinterface #139,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     490: checkcast     #116                // class java/util/List
     493: iload         7
     495: invokeinterface #139,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     500: checkcast     #141                // class java/lang/Character
     503: invokevirtual #145                // Method java/lang/Character.charValue:()C
     506: lookupswitch  { // 2
                    35: 532
                    79: 541
               default: 594
          }
     532: iload         9
     534: iconst_1
     535: isub
     536: istore        8
     538: goto          594
     541: aload         5
     543: iload         9
     545: invokeinterface #139,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     550: checkcast     #116                // class java/util/List
     553: iload         7
     555: bipush        46
     557: invokestatic  #166                // Method java/lang/Character.valueOf:(C)Ljava/lang/Character;
     560: invokeinterface #170,  3          // InterfaceMethod java/util/List.set:(ILjava/lang/Object;)Ljava/lang/Object;
     565: pop
     566: aload         5
     568: iload         8
     570: invokeinterface #139,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     575: checkcast     #116                // class java/util/List
     578: iload         7
     580: bipush        79
     582: invokestatic  #166                // Method java/lang/Character.valueOf:(C)Ljava/lang/Character;
     585: invokeinterface #170,  3          // InterfaceMethod java/util/List.set:(ILjava/lang/Object;)Ljava/lang/Object;
     590: pop
     591: iinc          8, -1
     594: iinc          9, -1
     597: goto          475
     600: iinc          7, -1
     603: goto          459
     606: iload_1
     607: iconst_1
     608: isub
     609: istore        7
     611: iconst_m1
     612: iload         7
     614: if_icmpge     758
     617: iload_2
     618: iconst_1
     619: isub
     620: istore        8
     622: iload_2
     623: iconst_1
     624: isub
     625: istore        9
     627: iconst_m1
     628: iload         9
     630: if_icmpge     752
     633: aload         5
     635: iload         7
     637: invokeinterface #139,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     642: checkcast     #116                // class java/util/List
     645: iload         9
     647: invokeinterface #139,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     652: checkcast     #141                // class java/lang/Character
     655: invokevirtual #145                // Method java/lang/Character.charValue:()C
     658: lookupswitch  { // 2
                    35: 684
                    79: 693
               default: 746
          }
     684: iload         9
     686: iconst_1
     687: isub
     688: istore        8
     690: goto          746
     693: aload         5
     695: iload         7
     697: invokeinterface #139,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     702: checkcast     #116                // class java/util/List
     705: iload         9
     707: bipush        46
     709: invokestatic  #166                // Method java/lang/Character.valueOf:(C)Ljava/lang/Character;
     712: invokeinterface #170,  3          // InterfaceMethod java/util/List.set:(ILjava/lang/Object;)Ljava/lang/Object;
     717: pop
     718: aload         5
     720: iload         7
     722: invokeinterface #139,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     727: checkcast     #116                // class java/util/List
     730: iload         8
     732: bipush        79
     734: invokestatic  #166                // Method java/lang/Character.valueOf:(C)Ljava/lang/Character;
     737: invokeinterface #170,  3          // InterfaceMethod java/util/List.set:(ILjava/lang/Object;)Ljava/lang/Object;
     742: pop
     743: iinc          8, -1
     746: iinc          9, -1
     749: goto          627
     752: iinc          7, -1
     755: goto          611
     758: aload         5
     760: checkcast     #73                 // class java/lang/Iterable
     763: invokestatic  #174                // Method kotlin/collections/CollectionsKt.flatten:(Ljava/lang/Iterable;)Ljava/util/List;
     766: invokevirtual #175                // Method java/lang/Object.toString:()Ljava/lang/String;
     769: astore        7
     771: aload_3
     772: aload         7
     774: invokeinterface #178,  2          // InterfaceMethod java/util/Map.get:(Ljava/lang/Object;)Ljava/lang/Object;
     779: checkcast     #180                // class java/lang/Integer
     782: astore        8
     784: aload         8
     786: ifnull        956
     789: aload         8
     791: checkcast     #182                // class java/lang/Number
     794: invokevirtual #185                // Method java/lang/Number.intValue:()I
     797: istore        10
     799: iconst_0
     800: istore        11
     802: ldc           #186                // int 1000000000
     804: iload         10
     806: isub
     807: iload         6
     809: iload         10
     811: isub
     812: irem
     813: istore        12
     815: aload_3
     816: invokeinterface #190,  1          // InterfaceMethod java/util/Map.values:()Ljava/util/Collection;
     821: checkcast     #73                 // class java/lang/Iterable
     824: astore        13
     826: iconst_0
     827: istore        14
     829: aload         13
     831: astore        15
     833: new           #75                 // class java/util/ArrayList
     836: dup
     837: invokespecial #157                // Method java/util/ArrayList."<init>":()V
     840: checkcast     #86                 // class java/util/Collection
     843: astore        16
     845: iconst_0
     846: istore        17
     848: aload         15
     850: invokeinterface #90,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     855: astore        18
     857: aload         18
     859: invokeinterface #96,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     864: ifeq          917
     867: aload         18
     869: invokeinterface #100,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     874: astore        19
     876: aload         19
     878: checkcast     #182                // class java/lang/Number
     881: invokevirtual #185                // Method java/lang/Number.intValue:()I
     884: istore        20
     886: iconst_0
     887: istore        21
     889: iload         20
     891: iload         10
     893: if_icmplt     900
     896: iconst_1
     897: goto          901
     900: iconst_0
     901: ifeq          857
     904: aload         16
     906: aload         19
     908: invokeinterface #114,  2          // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     913: pop
     914: goto          857
     917: aload         16
     919: checkcast     #116                // class java/util/List
     922: nop
     923: astore        22
     925: aload         4
     927: aload         22
     929: iload         12
     931: invokeinterface #139,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     936: checkcast     #182                // class java/lang/Number
     939: invokevirtual #185                // Method java/lang/Number.intValue:()I
     942: iconst_1
     943: isub
     944: invokeinterface #139,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     949: checkcast     #182                // class java/lang/Number
     952: invokevirtual #185                // Method java/lang/Number.intValue:()I
     955: ireturn
     956: iload         6
     958: invokestatic  #193                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     961: astore        9
     963: aload_3
     964: aload         7
     966: aload         9
     968: invokeinterface #197,  3          // InterfaceMethod java/util/Map.put:(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
     973: pop
     974: aload         4
     976: checkcast     #86                 // class java/util/Collection
     979: astore        8
     981: aload         5
     983: checkcast     #73                 // class java/lang/Iterable
     986: invokestatic  #201                // Method kotlin/collections/CollectionsKt.withIndex:(Ljava/lang/Iterable;)Ljava/lang/Iterable;
     989: astore        9
     991: iconst_0
     992: istore        10
     994: aload         9
     996: invokeinterface #90,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
    1001: astore        11
    1003: aload         11
    1005: invokeinterface #96,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
    1010: ifeq          1180
    1013: aload         11
    1015: invokeinterface #100,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
    1020: astore        12
    1022: iload         10
    1024: aload         12
    1026: checkcast     #203                // class kotlin/collections/IndexedValue
    1029: astore        13
    1031: istore        24
    1033: iconst_0
    1034: istore        14
    1036: aload         13
    1038: invokevirtual #206                // Method kotlin/collections/IndexedValue.component1:()I
    1041: istore        15
    1043: aload         13
    1045: invokevirtual #209                // Method kotlin/collections/IndexedValue.component2:()Ljava/lang/Object;
    1048: checkcast     #116                // class java/util/List
    1051: astore        16
    1053: aload         16
    1055: checkcast     #73                 // class java/lang/Iterable
    1058: astore        17
    1060: iconst_0
    1061: istore        18
    1063: aload         17
    1065: instanceof    #86                 // class java/util/Collection
    1068: ifeq          1088
    1071: aload         17
    1073: checkcast     #86                 // class java/util/Collection
    1076: invokeinterface #212,  1          // InterfaceMethod java/util/Collection.isEmpty:()Z
    1081: ifeq          1088
    1084: iconst_0
    1085: goto          1163
    1088: iconst_0
    1089: istore        19
    1091: aload         17
    1093: invokeinterface #90,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
    1098: astore        20
    1100: aload         20
    1102: invokeinterface #96,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
    1107: ifeq          1161
    1110: aload         20
    1112: invokeinterface #100,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
    1117: astore        21
    1119: aload         21
    1121: checkcast     #141                // class java/lang/Character
    1124: invokevirtual #145                // Method java/lang/Character.charValue:()C
    1127: istore        22
    1129: iconst_0
    1130: istore        23
    1132: iload         22
    1134: bipush        79
    1136: if_icmpne     1143
    1139: iconst_1
    1140: goto          1144
    1143: iconst_0
    1144: ifeq          1100
    1147: iinc          19, 1
    1150: iload         19
    1152: ifge          1100
    1155: invokestatic  #215                // Method kotlin/collections/CollectionsKt.throwCountOverflow:()V
    1158: goto          1100
    1161: iload         19
    1163: iload_1
    1164: iload         15
    1166: isub
    1167: imul
    1168: istore        25
    1170: iload         24
    1172: iload         25
    1174: iadd
    1175: istore        10
    1177: goto          1003
    1180: iload         10
    1182: invokestatic  #193                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
    1185: astore        9
    1187: aload         8
    1189: aload         9
    1191: invokeinterface #114,  2          // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
    1196: pop
    1197: iinc          6, 1
    1200: goto          154
    1203: new           #217                // class java/lang/IllegalStateException
    1206: dup
    1207: ldc           #219                // String Could not detect a cycle.
    1209: invokevirtual #175                // Method java/lang/Object.toString:()Ljava/lang/String;
    1212: invokespecial #220                // Method java/lang/IllegalStateException."<init>":(Ljava/lang/String;)V
    1215: athrow

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


package day13

import java.io.File
import kotlin.math.min

fun main() {
    val data = parse("src/day13/Day13.txt")

    println("🎄 Day 13 🎄")

    println()

    println("[Part 1]")
    println("Answer: ${part1(data)}")

    println()

    println("[Part 2]")
    println("Answer: ${part2(data)}")
}

private typealias Pattern = List<List<Char>>

private fun parse(path: String): List<Pattern> =
    File(path)
        .readText()
        .split("\n\n", "\r\n\r\n")
        .map { it.lines().map(String::toList) }

private fun Pattern.row(index: Int): List<Char> =
    this[index]

private fun Pattern.col(index: Int): List<Char> =
    this.indices.map { this[it][index] }

private fun verticalDifference(pattern: Pattern, y: Int): Int {
    val distance = min(y, pattern.size - y)
    return (0..<distance).sumOf { dy ->
        val lineTop = pattern.row(y - dy - 1)
        val lineBottom = pattern.row(y + dy)
        lineTop.zip(lineBottom).count { (a, b) -> a != b }
    }
}

private fun horizontalDifference(pattern: Pattern, x: Int): Int {
    val distance = min(x, pattern[0].size - x)
    return (0..<distance).sumOf { dx ->
        val lineLeft = pattern.col(x - dx - 1)
        val lineRight = pattern.col(x + dx)
        lineLeft.zip(lineRight).count { (a, b) -> a != b }
    }
}

private fun solve(data: List<Pattern>, tolerance: Int): Int {
    return data.sumOf { pattern ->
        val height = pattern.size
        for (y in 1..<height) {
            if (verticalDifference(pattern, y) == tolerance) {
                return@sumOf 100 * y
            }
        }

        val width = pattern[0].size
        for (x in 1..<width) {
            if (horizontalDifference(pattern, x) == tolerance) {
                return@sumOf x
            }
        }

        error("Reflection line could not be found.")
    }
}

private fun part1(data: List<Pattern>): Int =
    solve(data, tolerance = 0)

private fun part2(data: List<Pattern>): Int =
    solve(data, tolerance = 1)

Compiled from "Day13.kt"
public final class day13.Day13Kt {
  public static final void main();
    Code:
       0: ldc           #8                  // String src/day13/Day13.txt
       2: invokestatic  #12                 // Method parse:(Ljava/lang/String;)Ljava/util/List;
       5: astore_0
       6: ldc           #14                 // String 🎄 Day 13 🎄
       8: getstatic     #20                 // Field java/lang/System.out:Ljava/io/PrintStream;
      11: swap
      12: invokevirtual #26                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
      15: getstatic     #20                 // Field java/lang/System.out:Ljava/io/PrintStream;
      18: invokevirtual #28                 // Method java/io/PrintStream.println:()V
      21: ldc           #30                 // String [Part 1]
      23: getstatic     #20                 // Field java/lang/System.out:Ljava/io/PrintStream;
      26: swap
      27: invokevirtual #26                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
      30: new           #32                 // class java/lang/StringBuilder
      33: dup
      34: invokespecial #35                 // Method java/lang/StringBuilder."<init>":()V
      37: ldc           #37                 // String Answer:
      39: invokevirtual #41                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      42: aload_0
      43: invokestatic  #45                 // Method part1:(Ljava/util/List;)I
      46: invokevirtual #48                 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
      49: invokevirtual #52                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
      52: getstatic     #20                 // Field java/lang/System.out:Ljava/io/PrintStream;
      55: swap
      56: invokevirtual #26                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
      59: getstatic     #20                 // Field java/lang/System.out:Ljava/io/PrintStream;
      62: invokevirtual #28                 // Method java/io/PrintStream.println:()V
      65: ldc           #54                 // String [Part 2]
      67: getstatic     #20                 // Field java/lang/System.out:Ljava/io/PrintStream;
      70: swap
      71: invokevirtual #26                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
      74: new           #32                 // class java/lang/StringBuilder
      77: dup
      78: invokespecial #35                 // Method java/lang/StringBuilder."<init>":()V
      81: ldc           #37                 // String Answer:
      83: invokevirtual #41                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      86: aload_0
      87: invokestatic  #57                 // Method part2:(Ljava/util/List;)I
      90: invokevirtual #48                 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
      93: invokevirtual #52                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
      96: getstatic     #20                 // Field java/lang/System.out:Ljava/io/PrintStream;
      99: swap
     100: invokevirtual #26                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
     103: return

  private static final java.util.List<java.util.List<java.util.List<java.lang.Character>>> parse(java.lang.String);
    Code:
       0: new           #62                 // class java/io/File
       3: dup
       4: aload_0
       5: invokespecial #65                 // Method java/io/File."<init>":(Ljava/lang/String;)V
       8: aconst_null
       9: iconst_1
      10: aconst_null
      11: invokestatic  #71                 // Method kotlin/io/FilesKt.readText$default:(Ljava/io/File;Ljava/nio/charset/Charset;ILjava/lang/Object;)Ljava/lang/String;
      14: checkcast     #73                 // class java/lang/CharSequence
      17: iconst_2
      18: anewarray     #75                 // class java/lang/String
      21: astore_1
      22: aload_1
      23: iconst_0
      24: ldc           #77                 // String \n\n
      26: aastore
      27: aload_1
      28: iconst_1
      29: ldc           #79                 // String \r\n\r\n
      31: aastore
      32: aload_1
      33: iconst_0
      34: iconst_0
      35: bipush        6
      37: aconst_null
      38: invokestatic  #85                 // Method kotlin/text/StringsKt.split$default:(Ljava/lang/CharSequence;[Ljava/lang/String;ZIILjava/lang/Object;)Ljava/util/List;
      41: checkcast     #87                 // class java/lang/Iterable
      44: astore_1
      45: nop
      46: iconst_0
      47: istore_2
      48: aload_1
      49: astore_3
      50: new           #89                 // class java/util/ArrayList
      53: dup
      54: aload_1
      55: bipush        10
      57: invokestatic  #95                 // 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          235
      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     #75                 // class java/lang/String
     105: astore        8
     107: astore        20
     109: iconst_0
     110: istore        9
     112: aload         8
     114: checkcast     #73                 // class java/lang/CharSequence
     117: invokestatic  #118                // Method kotlin/text/StringsKt.lines:(Ljava/lang/CharSequence;)Ljava/util/List;
     120: checkcast     #87                 // class java/lang/Iterable
     123: astore        10
     125: iconst_0
     126: istore        11
     128: aload         10
     130: astore        12
     132: new           #89                 // class java/util/ArrayList
     135: dup
     136: aload         10
     138: bipush        10
     140: invokestatic  #95                 // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
     143: invokespecial #98                 // Method java/util/ArrayList."<init>":(I)V
     146: checkcast     #100                // class java/util/Collection
     149: astore        13
     151: iconst_0
     152: istore        14
     154: aload         12
     156: invokeinterface #104,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     161: astore        15
     163: aload         15
     165: invokeinterface #110,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
     170: ifeq          216
     173: aload         15
     175: invokeinterface #114,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     180: astore        16
     182: aload         13
     184: aload         16
     186: checkcast     #75                 // class java/lang/String
     189: astore        17
     191: astore        18
     193: iconst_0
     194: istore        19
     196: aload         17
     198: checkcast     #73                 // class java/lang/CharSequence
     201: invokestatic  #121                // Method kotlin/text/StringsKt.toList:(Ljava/lang/CharSequence;)Ljava/util/List;
     204: aload         18
     206: swap
     207: invokeinterface #125,  2          // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     212: pop
     213: goto          163
     216: aload         13
     218: checkcast     #127                // class java/util/List
     221: nop
     222: nop
     223: aload         20
     225: swap
     226: invokeinterface #125,  2          // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     231: pop
     232: goto          79
     235: aload         4
     237: checkcast     #127                // class java/util/List
     240: nop
     241: areturn

  private static final java.util.List<java.lang.Character> row(java.util.List<? extends java.util.List<java.lang.Character>>, int);
    Code:
       0: aload_0
       1: iload_1
       2: invokeinterface #150,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
       7: checkcast     #127                // class java/util/List
      10: areturn

  private static final java.util.List<java.lang.Character> col(java.util.List<? extends java.util.List<java.lang.Character>>, int);
    Code:
       0: aload_0
       1: checkcast     #100                // class java/util/Collection
       4: invokestatic  #157                // Method kotlin/collections/CollectionsKt.getIndices:(Ljava/util/Collection;)Lkotlin/ranges/IntRange;
       7: checkcast     #87                 // class java/lang/Iterable
      10: astore_2
      11: iconst_0
      12: istore_3
      13: aload_2
      14: astore        4
      16: new           #89                 // class java/util/ArrayList
      19: dup
      20: aload_2
      21: bipush        10
      23: invokestatic  #95                 // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
      26: invokespecial #98                 // Method java/util/ArrayList."<init>":(I)V
      29: checkcast     #100                // class java/util/Collection
      32: astore        5
      34: iconst_0
      35: istore        6
      37: aload         4
      39: invokeinterface #104,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      44: astore        7
      46: aload         7
      48: invokeinterface #110,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
      53: ifeq          115
      56: aload         7
      58: checkcast     #159                // class kotlin/collections/IntIterator
      61: invokevirtual #163                // Method kotlin/collections/IntIterator.nextInt:()I
      64: istore        8
      66: aload         5
      68: iload         8
      70: istore        9
      72: astore        11
      74: iconst_0
      75: istore        10
      77: aload_0
      78: iload         9
      80: invokeinterface #150,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      85: checkcast     #127                // class java/util/List
      88: iload_1
      89: invokeinterface #150,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      94: checkcast     #165                // class java/lang/Character
      97: invokevirtual #169                // Method java/lang/Character.charValue:()C
     100: invokestatic  #173                // Method java/lang/Character.valueOf:(C)Ljava/lang/Character;
     103: aload         11
     105: swap
     106: invokeinterface #125,  2          // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     111: pop
     112: goto          46
     115: aload         5
     117: checkcast     #127                // class java/util/List
     120: nop
     121: areturn

  private static final int verticalDifference(java.util.List<? extends java.util.List<java.lang.Character>>, int);
    Code:
       0: iload_1
       1: aload_0
       2: invokeinterface #181,  1          // InterfaceMethod java/util/List.size:()I
       7: iload_1
       8: isub
       9: invokestatic  #187                // Method java/lang/Math.min:(II)I
      12: istore_2
      13: iconst_0
      14: iload_2
      15: invokestatic  #193                // Method kotlin/ranges/RangesKt.until:(II)Lkotlin/ranges/IntRange;
      18: checkcast     #87                 // class java/lang/Iterable
      21: astore_3
      22: iconst_0
      23: istore        4
      25: aload_3
      26: invokeinterface #104,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      31: astore        5
      33: aload         5
      35: invokeinterface #110,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
      40: ifeq          243
      43: aload         5
      45: checkcast     #159                // class kotlin/collections/IntIterator
      48: invokevirtual #163                // Method kotlin/collections/IntIterator.nextInt:()I
      51: istore        6
      53: iload         4
      55: iload         6
      57: istore        7
      59: istore        20
      61: iconst_0
      62: istore        8
      64: aload_0
      65: iload_1
      66: iload         7
      68: isub
      69: iconst_1
      70: isub
      71: invokestatic  #195                // Method row:(Ljava/util/List;I)Ljava/util/List;
      74: astore        9
      76: aload_0
      77: iload_1
      78: iload         7
      80: iadd
      81: invokestatic  #195                // Method row:(Ljava/util/List;I)Ljava/util/List;
      84: astore        10
      86: aload         9
      88: checkcast     #87                 // class java/lang/Iterable
      91: aload         10
      93: checkcast     #87                 // class java/lang/Iterable
      96: invokestatic  #199                // Method kotlin/collections/CollectionsKt.zip:(Ljava/lang/Iterable;Ljava/lang/Iterable;)Ljava/util/List;
      99: checkcast     #87                 // class java/lang/Iterable
     102: astore        11
     104: iconst_0
     105: istore        12
     107: aload         11
     109: instanceof    #100                // class java/util/Collection
     112: ifeq          132
     115: aload         11
     117: checkcast     #100                // class java/util/Collection
     120: invokeinterface #202,  1          // InterfaceMethod java/util/Collection.isEmpty:()Z
     125: ifeq          132
     128: iconst_0
     129: goto          230
     132: iconst_0
     133: istore        13
     135: aload         11
     137: invokeinterface #104,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     142: astore        14
     144: aload         14
     146: invokeinterface #110,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
     151: ifeq          228
     154: aload         14
     156: invokeinterface #114,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     161: astore        15
     163: aload         15
     165: checkcast     #204                // class kotlin/Pair
     168: astore        16
     170: iconst_0
     171: istore        17
     173: aload         16
     175: invokevirtual #207                // Method kotlin/Pair.component1:()Ljava/lang/Object;
     178: checkcast     #165                // class java/lang/Character
     181: invokevirtual #169                // Method java/lang/Character.charValue:()C
     184: istore        18
     186: aload         16
     188: invokevirtual #210                // Method kotlin/Pair.component2:()Ljava/lang/Object;
     191: checkcast     #165                // class java/lang/Character
     194: invokevirtual #169                // Method java/lang/Character.charValue:()C
     197: istore        19
     199: iload         18
     201: iload         19
     203: if_icmpeq     210
     206: iconst_1
     207: goto          211
     210: iconst_0
     211: ifeq          144
     214: iinc          13, 1
     217: iload         13
     219: ifge          144
     222: invokestatic  #213                // Method kotlin/collections/CollectionsKt.throwCountOverflow:()V
     225: goto          144
     228: iload         13
     230: nop
     231: istore        21
     233: iload         20
     235: iload         21
     237: iadd
     238: istore        4
     240: goto          33
     243: iload         4
     245: ireturn

  private static final int horizontalDifference(java.util.List<? extends java.util.List<java.lang.Character>>, int);
    Code:
       0: iload_1
       1: aload_0
       2: iconst_0
       3: invokeinterface #150,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
       8: checkcast     #127                // class java/util/List
      11: invokeinterface #181,  1          // InterfaceMethod java/util/List.size:()I
      16: iload_1
      17: isub
      18: invokestatic  #187                // Method java/lang/Math.min:(II)I
      21: istore_2
      22: iconst_0
      23: iload_2
      24: invokestatic  #193                // Method kotlin/ranges/RangesKt.until:(II)Lkotlin/ranges/IntRange;
      27: checkcast     #87                 // class java/lang/Iterable
      30: astore_3
      31: iconst_0
      32: istore        4
      34: aload_3
      35: invokeinterface #104,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      40: astore        5
      42: aload         5
      44: invokeinterface #110,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
      49: ifeq          252
      52: aload         5
      54: checkcast     #159                // class kotlin/collections/IntIterator
      57: invokevirtual #163                // Method kotlin/collections/IntIterator.nextInt:()I
      60: istore        6
      62: iload         4
      64: iload         6
      66: istore        7
      68: istore        20
      70: iconst_0
      71: istore        8
      73: aload_0
      74: iload_1
      75: iload         7
      77: isub
      78: iconst_1
      79: isub
      80: invokestatic  #231                // Method col:(Ljava/util/List;I)Ljava/util/List;
      83: astore        9
      85: aload_0
      86: iload_1
      87: iload         7
      89: iadd
      90: invokestatic  #231                // Method col:(Ljava/util/List;I)Ljava/util/List;
      93: astore        10
      95: aload         9
      97: checkcast     #87                 // class java/lang/Iterable
     100: aload         10
     102: checkcast     #87                 // class java/lang/Iterable
     105: invokestatic  #199                // Method kotlin/collections/CollectionsKt.zip:(Ljava/lang/Iterable;Ljava/lang/Iterable;)Ljava/util/List;
     108: checkcast     #87                 // class java/lang/Iterable
     111: astore        11
     113: iconst_0
     114: istore        12
     116: aload         11
     118: instanceof    #100                // class java/util/Collection
     121: ifeq          141
     124: aload         11
     126: checkcast     #100                // class java/util/Collection
     129: invokeinterface #202,  1          // InterfaceMethod java/util/Collection.isEmpty:()Z
     134: ifeq          141
     137: iconst_0
     138: goto          239
     141: iconst_0
     142: istore        13
     144: aload         11
     146: invokeinterface #104,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     151: astore        14
     153: aload         14
     155: invokeinterface #110,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
     160: ifeq          237
     163: aload         14
     165: invokeinterface #114,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     170: astore        15
     172: aload         15
     174: checkcast     #204                // class kotlin/Pair
     177: astore        16
     179: iconst_0
     180: istore        17
     182: aload         16
     184: invokevirtual #207                // Method kotlin/Pair.component1:()Ljava/lang/Object;
     187: checkcast     #165                // class java/lang/Character
     190: invokevirtual #169                // Method java/lang/Character.charValue:()C
     193: istore        18
     195: aload         16
     197: invokevirtual #210                // Method kotlin/Pair.component2:()Ljava/lang/Object;
     200: checkcast     #165                // class java/lang/Character
     203: invokevirtual #169                // Method java/lang/Character.charValue:()C
     206: istore        19
     208: iload         18
     210: iload         19
     212: if_icmpeq     219
     215: iconst_1
     216: goto          220
     219: iconst_0
     220: ifeq          153
     223: iinc          13, 1
     226: iload         13
     228: ifge          153
     231: invokestatic  #213                // Method kotlin/collections/CollectionsKt.throwCountOverflow:()V
     234: goto          153
     237: iload         13
     239: nop
     240: istore        21
     242: iload         20
     244: iload         21
     246: iadd
     247: istore        4
     249: goto          42
     252: iload         4
     254: ireturn

  private static final int solve(java.util.List<? extends java.util.List<? extends java.util.List<java.lang.Character>>>, int);
    Code:
       0: aload_0
       1: checkcast     #87                 // class java/lang/Iterable
       4: astore_2
       5: iconst_0
       6: istore_3
       7: aload_2
       8: invokeinterface #104,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      13: astore        4
      15: aload         4
      17: invokeinterface #110,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
      22: ifeq          165
      25: aload         4
      27: invokeinterface #114,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      32: astore        5
      34: iload_3
      35: aload         5
      37: checkcast     #127                // class java/util/List
      40: astore        6
      42: istore        11
      44: iconst_0
      45: istore        7
      47: aload         6
      49: invokeinterface #181,  1          // InterfaceMethod java/util/List.size:()I
      54: istore        8
      56: iconst_1
      57: istore        9
      59: iload         9
      61: iload         8
      63: if_icmpge     91
      66: aload         6
      68: iload         9
      70: invokestatic  #241                // Method verticalDifference:(Ljava/util/List;I)I
      73: iload_1
      74: if_icmpne     85
      77: bipush        100
      79: iload         9
      81: imul
      82: goto          154
      85: iinc          9, 1
      88: goto          59
      91: aload         6
      93: iconst_0
      94: invokeinterface #150,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      99: checkcast     #127                // class java/util/List
     102: invokeinterface #181,  1          // InterfaceMethod java/util/List.size:()I
     107: istore        9
     109: iconst_1
     110: istore        10
     112: iload         10
     114: iload         9
     116: if_icmpge     141
     119: aload         6
     121: iload         10
     123: invokestatic  #243                // Method horizontalDifference:(Ljava/util/List;I)I
     126: iload_1
     127: if_icmpne     135
     130: iload         10
     132: goto          154
     135: iinc          10, 1
     138: goto          112
     141: new           #245                // class java/lang/IllegalStateException
     144: dup
     145: ldc           #247                // String Reflection line could not be found.
     147: invokevirtual #248                // Method java/lang/Object.toString:()Ljava/lang/String;
     150: invokespecial #249                // Method java/lang/IllegalStateException."<init>":(Ljava/lang/String;)V
     153: athrow
     154: istore        12
     156: iload         11
     158: iload         12
     160: iadd
     161: istore_3
     162: goto          15
     165: iload_3
     166: ireturn

  private static final int part1(java.util.List<? extends java.util.List<? extends java.util.List<java.lang.Character>>>);
    Code:
       0: aload_0
       1: iconst_0
       2: invokestatic  #256                // Method solve:(Ljava/util/List;I)I
       5: ireturn

  private static final int part2(java.util.List<? extends java.util.List<? extends java.util.List<java.lang.Character>>>);
    Code:
       0: aload_0
       1: iconst_1
       2: invokestatic  #256                // Method solve:(Ljava/util/List;I)I
       5: ireturn

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


package day22

import java.io.File
import kotlin.math.max

fun main() {
    val data = parse("src/day22/Day22.txt")
    val (supports, standsOn) = simulate(data)

    println("🎄 Day 22 🎄")

    println()

    println("[Part 1]")
    println("Answer: ${part1(data.size, supports, standsOn)}")

    println()

    println("[Part 2]")
    println("Answer: ${part2(data.size, supports, standsOn)}")
}

private data class Brick(
    val x0: Int, val x1: Int,
    val y0: Int, val y1: Int,
    val z0: Int, val z1: Int,
)

private fun String.toBrick(): Brick {
    val (lhs, rhs) = this.split('~')

    val (x0, y0, z0) = lhs.split(',')
    val (x1, y1, z1) = rhs.split(',')

    return Brick(
        x0 = x0.toInt(), x1 = x1.toInt(),
        y0 = y0.toInt(), y1 = y1.toInt(),
        z0 = z0.toInt(), z1 = z1.toInt(),
    )
}

private fun parse(path: String): List<Brick> =
    File(path)
        .readLines()
        .map(String::toBrick)
        .sortedBy { it.z0 }

private fun overlap(a: Brick, b: Brick): Boolean =
    (a.x1 >= b.x0 && b.x1 >= a.x0) && (a.y1 >= b.y0 && b.y1 >= a.y0)

private fun simulate(data: List<Brick>): Pair<List<Set<Int>>, List<Set<Int>>> {
    val bricks = data.toMutableList()
    for ((i, upper) in bricks.withIndex()) {
        var z = 1
        for (lower in bricks.subList(0, i)) {
            if (overlap(upper, lower)) {
                z = max(z, lower.z1 + 1)
            }
        }

        bricks[i] = upper.copy(
            z0 = z,
            z1 = z + (upper.z1 - upper.z0),
        )
    }

    bricks.sortBy { it.z0 }

    val supports = bricks.map { mutableSetOf<Int>() }
    val standsOn = bricks.map { mutableSetOf<Int>() }

    for ((i, upper) in bricks.withIndex()) {
        for ((j, lower) in bricks.subList(0, i).withIndex()) {
            if (upper.z0 == lower.z1 + 1) {
                if (overlap(upper, lower)) {
                    supports[j] += i
                    standsOn[i] += j
                }
            }
        }
    }

    return Pair(supports, standsOn)
}

private fun part1(n: Int, supports: List<Set<Int>>, standsOn: List<Set<Int>>): Int =
    (0..<n).count { i -> supports[i].all { j -> standsOn[j].size > 1 } }

private fun part2(n: Int, supports: List<Set<Int>>, standsOn: List<Set<Int>>): Int {
    var count = 0
    for (i in 0..<n) {
        val queue = ArrayDeque<Int>()
            .apply { addAll(supports[i].filter { j -> standsOn[j].size == 1 }) }

        val falling = mutableSetOf(i)
        falling += queue

        while (queue.isNotEmpty()) {
            val j = queue.removeFirst()
            for (k in supports[j]) {
                if (k !in falling && falling.containsAll(standsOn[k])) {
                    queue += k
                    falling += k
                }
            }
        }

        count += falling.size - 1
    }

    return count
}

Compiled from "Day22.kt"
public final class day22.Day22Kt {
  public static final void main();
    Code:
       0: ldc           #8                  // String src/day22/Day22.txt
       2: invokestatic  #12                 // Method parse:(Ljava/lang/String;)Ljava/util/List;
       5: astore_0
       6: aload_0
       7: invokestatic  #16                 // Method simulate:(Ljava/util/List;)Lkotlin/Pair;
      10: astore_1
      11: aload_1
      12: invokevirtual #22                 // Method kotlin/Pair.component1:()Ljava/lang/Object;
      15: checkcast     #24                 // class java/util/List
      18: astore_2
      19: aload_1
      20: invokevirtual #27                 // Method kotlin/Pair.component2:()Ljava/lang/Object;
      23: checkcast     #24                 // class java/util/List
      26: astore_3
      27: ldc           #29                 // String 🎄 Day 22 🎄
      29: getstatic     #35                 // Field java/lang/System.out:Ljava/io/PrintStream;
      32: swap
      33: invokevirtual #41                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
      36: getstatic     #35                 // Field java/lang/System.out:Ljava/io/PrintStream;
      39: invokevirtual #43                 // Method java/io/PrintStream.println:()V
      42: ldc           #45                 // String [Part 1]
      44: getstatic     #35                 // Field java/lang/System.out:Ljava/io/PrintStream;
      47: swap
      48: invokevirtual #41                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
      51: new           #47                 // class java/lang/StringBuilder
      54: dup
      55: invokespecial #50                 // Method java/lang/StringBuilder."<init>":()V
      58: ldc           #52                 // String Answer:
      60: invokevirtual #56                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      63: aload_0
      64: invokeinterface #60,  1           // InterfaceMethod java/util/List.size:()I
      69: aload_2
      70: aload_3
      71: invokestatic  #64                 // Method part1:(ILjava/util/List;Ljava/util/List;)I
      74: invokevirtual #67                 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
      77: invokevirtual #71                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
      80: getstatic     #35                 // Field java/lang/System.out:Ljava/io/PrintStream;
      83: swap
      84: invokevirtual #41                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
      87: getstatic     #35                 // Field java/lang/System.out:Ljava/io/PrintStream;
      90: invokevirtual #43                 // Method java/io/PrintStream.println:()V
      93: ldc           #73                 // String [Part 2]
      95: getstatic     #35                 // Field java/lang/System.out:Ljava/io/PrintStream;
      98: swap
      99: invokevirtual #41                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
     102: new           #47                 // class java/lang/StringBuilder
     105: dup
     106: invokespecial #50                 // Method java/lang/StringBuilder."<init>":()V
     109: ldc           #52                 // String Answer:
     111: invokevirtual #56                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     114: aload_0
     115: invokeinterface #60,  1           // InterfaceMethod java/util/List.size:()I
     120: aload_2
     121: aload_3
     122: invokestatic  #76                 // Method part2:(ILjava/util/List;Ljava/util/List;)I
     125: invokevirtual #67                 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
     128: invokevirtual #71                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
     131: getstatic     #35                 // Field java/lang/System.out:Ljava/io/PrintStream;
     134: swap
     135: invokevirtual #41                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
     138: return

  private static final day22.Brick toBrick(java.lang.String);
    Code:
       0: aload_0
       1: checkcast     #84                 // class java/lang/CharSequence
       4: iconst_1
       5: newarray       char
       7: astore_2
       8: aload_2
       9: iconst_0
      10: bipush        126
      12: castore
      13: aload_2
      14: iconst_0
      15: iconst_0
      16: bipush        6
      18: aconst_null
      19: invokestatic  #90                 // Method kotlin/text/StringsKt.split$default:(Ljava/lang/CharSequence;[CZIILjava/lang/Object;)Ljava/util/List;
      22: astore_1
      23: aload_1
      24: iconst_0
      25: invokeinterface #94,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      30: checkcast     #96                 // class java/lang/String
      33: astore_2
      34: aload_1
      35: iconst_1
      36: invokeinterface #94,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      41: checkcast     #96                 // class java/lang/String
      44: astore_3
      45: aload_2
      46: checkcast     #84                 // class java/lang/CharSequence
      49: iconst_1
      50: newarray       char
      52: astore        5
      54: aload         5
      56: iconst_0
      57: bipush        44
      59: castore
      60: aload         5
      62: iconst_0
      63: iconst_0
      64: bipush        6
      66: aconst_null
      67: invokestatic  #90                 // Method kotlin/text/StringsKt.split$default:(Ljava/lang/CharSequence;[CZIILjava/lang/Object;)Ljava/util/List;
      70: astore        4
      72: aload         4
      74: iconst_0
      75: invokeinterface #94,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      80: checkcast     #96                 // class java/lang/String
      83: astore        5
      85: aload         4
      87: iconst_1
      88: invokeinterface #94,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      93: checkcast     #96                 // class java/lang/String
      96: astore        6
      98: aload         4
     100: iconst_2
     101: invokeinterface #94,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     106: checkcast     #96                 // class java/lang/String
     109: astore        7
     111: aload_3
     112: checkcast     #84                 // class java/lang/CharSequence
     115: iconst_1
     116: newarray       char
     118: astore        9
     120: aload         9
     122: iconst_0
     123: bipush        44
     125: castore
     126: aload         9
     128: iconst_0
     129: iconst_0
     130: bipush        6
     132: aconst_null
     133: invokestatic  #90                 // Method kotlin/text/StringsKt.split$default:(Ljava/lang/CharSequence;[CZIILjava/lang/Object;)Ljava/util/List;
     136: astore        8
     138: aload         8
     140: iconst_0
     141: invokeinterface #94,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     146: checkcast     #96                 // class java/lang/String
     149: astore        9
     151: aload         8
     153: iconst_1
     154: invokeinterface #94,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     159: checkcast     #96                 // class java/lang/String
     162: astore        10
     164: aload         8
     166: iconst_2
     167: invokeinterface #94,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     172: checkcast     #96                 // class java/lang/String
     175: astore        11
     177: new           #98                 // class day22/Brick
     180: dup
     181: aload         5
     183: invokestatic  #104                // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
     186: aload         9
     188: invokestatic  #104                // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
     191: aload         6
     193: invokestatic  #104                // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
     196: aload         10
     198: invokestatic  #104                // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
     201: aload         7
     203: invokestatic  #104                // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
     206: aload         11
     208: invokestatic  #104                // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
     211: invokespecial #107                // Method day22/Brick."<init>":(IIIIII)V
     214: areturn

  private static final java.util.List<day22.Brick> parse(java.lang.String);
    Code:
       0: new           #120                // class java/io/File
       3: dup
       4: aload_0
       5: invokespecial #123                // Method java/io/File."<init>":(Ljava/lang/String;)V
       8: aconst_null
       9: iconst_1
      10: aconst_null
      11: invokestatic  #129                // Method kotlin/io/FilesKt.readLines$default:(Ljava/io/File;Ljava/nio/charset/Charset;ILjava/lang/Object;)Ljava/util/List;
      14: checkcast     #131                // class java/lang/Iterable
      17: astore_1
      18: nop
      19: iconst_0
      20: istore_2
      21: aload_1
      22: astore_3
      23: new           #133                // class java/util/ArrayList
      26: dup
      27: aload_1
      28: bipush        10
      30: invokestatic  #139                // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
      33: invokespecial #142                // Method java/util/ArrayList."<init>":(I)V
      36: checkcast     #144                // class java/util/Collection
      39: astore        4
      41: iconst_0
      42: istore        5
      44: aload_3
      45: invokeinterface #148,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      50: astore        6
      52: aload         6
      54: invokeinterface #154,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
      59: ifeq          102
      62: aload         6
      64: invokeinterface #157,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      69: astore        7
      71: aload         4
      73: aload         7
      75: checkcast     #96                 // class java/lang/String
      78: astore        8
      80: astore        10
      82: iconst_0
      83: istore        9
      85: aload         8
      87: invokestatic  #159                // Method toBrick:(Ljava/lang/String;)Lday22/Brick;
      90: aload         10
      92: swap
      93: invokeinterface #163,  2          // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
      98: pop
      99: goto          52
     102: aload         4
     104: checkcast     #24                 // class java/util/List
     107: nop
     108: checkcast     #131                // class java/lang/Iterable
     111: astore_1
     112: nop
     113: iconst_0
     114: istore_2
     115: aload_1
     116: new           #165                // class day22/Day22Kt$parse$$inlined$sortedBy$1
     119: dup
     120: invokespecial #166                // Method day22/Day22Kt$parse$$inlined$sortedBy$1."<init>":()V
     123: checkcast     #168                // class java/util/Comparator
     126: invokestatic  #172                // Method kotlin/collections/CollectionsKt.sortedWith:(Ljava/lang/Iterable;Ljava/util/Comparator;)Ljava/util/List;
     129: areturn

  private static final boolean overlap(day22.Brick, day22.Brick);
    Code:
       0: aload_0
       1: invokevirtual #192                // Method day22/Brick.getX1:()I
       4: aload_1
       5: invokevirtual #195                // Method day22/Brick.getX0:()I
       8: if_icmplt     48
      11: aload_1
      12: invokevirtual #192                // Method day22/Brick.getX1:()I
      15: aload_0
      16: invokevirtual #195                // Method day22/Brick.getX0:()I
      19: if_icmplt     48
      22: aload_0
      23: invokevirtual #198                // Method day22/Brick.getY1:()I
      26: aload_1
      27: invokevirtual #201                // Method day22/Brick.getY0:()I
      30: if_icmplt     48
      33: aload_1
      34: invokevirtual #198                // Method day22/Brick.getY1:()I
      37: aload_0
      38: invokevirtual #201                // Method day22/Brick.getY0:()I
      41: if_icmplt     48
      44: iconst_1
      45: goto          49
      48: iconst_0
      49: ireturn

  private static final kotlin.Pair<java.util.List<java.util.Set<java.lang.Integer>>, java.util.List<java.util.Set<java.lang.Integer>>> simulate(java.util.List<day22.Brick>);
    Code:
       0: aload_0
       1: checkcast     #144                // class java/util/Collection
       4: invokestatic  #209                // Method kotlin/collections/CollectionsKt.toMutableList:(Ljava/util/Collection;)Ljava/util/List;
       7: astore_1
       8: aload_1
       9: checkcast     #131                // class java/lang/Iterable
      12: invokeinterface #148,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      17: astore_2
      18: iconst_0
      19: istore_3
      20: aload_2
      21: invokeinterface #154,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
      26: ifeq          155
      29: iload_3
      30: istore        4
      32: iload_3
      33: iconst_1
      34: iadd
      35: istore_3
      36: aload_2
      37: invokeinterface #157,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      42: checkcast     #98                 // class day22/Brick
      45: astore        5
      47: iconst_1
      48: istore        6
      50: aload_1
      51: iconst_0
      52: iload         4
      54: invokeinterface #213,  3          // InterfaceMethod java/util/List.subList:(II)Ljava/util/List;
      59: invokeinterface #214,  1          // InterfaceMethod java/util/List.iterator:()Ljava/util/Iterator;
      64: astore        7
      66: aload         7
      68: invokeinterface #154,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
      73: ifeq          115
      76: aload         7
      78: invokeinterface #157,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      83: checkcast     #98                 // class day22/Brick
      86: astore        8
      88: aload         5
      90: aload         8
      92: invokestatic  #216                // Method overlap:(Lday22/Brick;Lday22/Brick;)Z
      95: ifeq          66
      98: iload         6
     100: aload         8
     102: invokevirtual #219                // Method day22/Brick.getZ1:()I
     105: iconst_1
     106: iadd
     107: invokestatic  #225                // Method java/lang/Math.max:(II)I
     110: istore        6
     112: goto          66
     115: aload_1
     116: iload         4
     118: aload         5
     120: iconst_0
     121: iconst_0
     122: iconst_0
     123: iconst_0
     124: iload         6
     126: iload         6
     128: aload         5
     130: invokevirtual #219                // Method day22/Brick.getZ1:()I
     133: aload         5
     135: invokevirtual #228                // Method day22/Brick.getZ0:()I
     138: isub
     139: iadd
     140: bipush        15
     142: aconst_null
     143: invokestatic  #232                // Method day22/Brick.copy$default:(Lday22/Brick;IIIIIIILjava/lang/Object;)Lday22/Brick;
     146: invokeinterface #236,  3          // InterfaceMethod java/util/List.set:(ILjava/lang/Object;)Ljava/lang/Object;
     151: pop
     152: goto          20
     155: aload_1
     156: astore_2
     157: iconst_0
     158: istore_3
     159: aload_2
     160: invokeinterface #60,  1           // InterfaceMethod java/util/List.size:()I
     165: iconst_1
     166: if_icmple     183
     169: aload_2
     170: new           #238                // class day22/Day22Kt$simulate$$inlined$sortBy$1
     173: dup
     174: invokespecial #239                // Method day22/Day22Kt$simulate$$inlined$sortBy$1."<init>":()V
     177: checkcast     #168                // class java/util/Comparator
     180: invokestatic  #243                // Method kotlin/collections/CollectionsKt.sortWith:(Ljava/util/List;Ljava/util/Comparator;)V
     183: nop
     184: aload_1
     185: checkcast     #131                // class java/lang/Iterable
     188: astore_3
     189: iconst_0
     190: istore        4
     192: aload_3
     193: astore        5
     195: new           #133                // class java/util/ArrayList
     198: dup
     199: aload_3
     200: bipush        10
     202: invokestatic  #139                // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
     205: invokespecial #142                // Method java/util/ArrayList."<init>":(I)V
     208: checkcast     #144                // class java/util/Collection
     211: astore        6
     213: iconst_0
     214: istore        7
     216: aload         5
     218: invokeinterface #148,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     223: astore        8
     225: aload         8
     227: invokeinterface #154,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
     232: ifeq          281
     235: aload         8
     237: invokeinterface #157,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     242: astore        9
     244: aload         6
     246: aload         9
     248: checkcast     #98                 // class day22/Brick
     251: astore        10
     253: astore        14
     255: iconst_0
     256: istore        11
     258: new           #245                // class java/util/LinkedHashSet
     261: dup
     262: invokespecial #246                // Method java/util/LinkedHashSet."<init>":()V
     265: checkcast     #248                // class java/util/Set
     268: nop
     269: aload         14
     271: swap
     272: invokeinterface #163,  2          // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     277: pop
     278: goto          225
     281: aload         6
     283: checkcast     #24                 // class java/util/List
     286: nop
     287: astore_2
     288: aload_1
     289: checkcast     #131                // class java/lang/Iterable
     292: astore        4
     294: iconst_0
     295: istore        5
     297: aload         4
     299: astore        6
     301: new           #133                // class java/util/ArrayList
     304: dup
     305: aload         4
     307: bipush        10
     309: invokestatic  #139                // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
     312: invokespecial #142                // Method java/util/ArrayList."<init>":(I)V
     315: checkcast     #144                // class java/util/Collection
     318: astore        7
     320: iconst_0
     321: istore        8
     323: aload         6
     325: invokeinterface #148,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     330: astore        9
     332: aload         9
     334: invokeinterface #154,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
     339: ifeq          388
     342: aload         9
     344: invokeinterface #157,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     349: astore        10
     351: aload         7
     353: aload         10
     355: checkcast     #98                 // class day22/Brick
     358: astore        11
     360: astore        14
     362: iconst_0
     363: istore        12
     365: new           #245                // class java/util/LinkedHashSet
     368: dup
     369: invokespecial #246                // Method java/util/LinkedHashSet."<init>":()V
     372: checkcast     #248                // class java/util/Set
     375: nop
     376: aload         14
     378: swap
     379: invokeinterface #163,  2          // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     384: pop
     385: goto          332
     388: aload         7
     390: checkcast     #24                 // class java/util/List
     393: nop
     394: astore_3
     395: aload_1
     396: checkcast     #131                // class java/lang/Iterable
     399: invokeinterface #148,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     404: astore        4
     406: iconst_0
     407: istore        5
     409: aload         4
     411: invokeinterface #154,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
     416: ifeq          575
     419: iload         5
     421: istore        6
     423: iload         5
     425: iconst_1
     426: iadd
     427: istore        5
     429: aload         4
     431: invokeinterface #157,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     436: checkcast     #98                 // class day22/Brick
     439: astore        7
     441: aload_1
     442: iconst_0
     443: iload         6
     445: invokeinterface #213,  3          // InterfaceMethod java/util/List.subList:(II)Ljava/util/List;
     450: checkcast     #131                // class java/lang/Iterable
     453: invokeinterface #148,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     458: astore        8
     460: iconst_0
     461: istore        9
     463: aload         8
     465: invokeinterface #154,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
     470: ifeq          409
     473: iload         9
     475: istore        10
     477: iload         9
     479: iconst_1
     480: iadd
     481: istore        9
     483: aload         8
     485: invokeinterface #157,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     490: checkcast     #98                 // class day22/Brick
     493: astore        11
     495: aload         7
     497: invokevirtual #228                // Method day22/Brick.getZ0:()I
     500: aload         11
     502: invokevirtual #219                // Method day22/Brick.getZ1:()I
     505: iconst_1
     506: iadd
     507: if_icmpne     463
     510: aload         7
     512: aload         11
     514: invokestatic  #216                // Method overlap:(Lday22/Brick;Lday22/Brick;)Z
     517: ifeq          463
     520: iload         6
     522: invokestatic  #252                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     525: astore        13
     527: aload_2
     528: iload         10
     530: invokeinterface #94,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     535: checkcast     #144                // class java/util/Collection
     538: aload         13
     540: invokeinterface #163,  2          // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     545: pop
     546: iload         10
     548: invokestatic  #252                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     551: astore        13
     553: aload_3
     554: iload         6
     556: invokeinterface #94,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     561: checkcast     #144                // class java/util/Collection
     564: aload         13
     566: invokeinterface #163,  2          // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     571: pop
     572: goto          463
     575: new           #18                 // class kotlin/Pair
     578: dup
     579: aload_2
     580: aload_3
     581: invokespecial #255                // Method kotlin/Pair."<init>":(Ljava/lang/Object;Ljava/lang/Object;)V
     584: areturn

  private static final int part1(int, java.util.List<? extends java.util.Set<java.lang.Integer>>, java.util.List<? extends java.util.Set<java.lang.Integer>>);
    Code:
       0: iconst_0
       1: iload_0
       2: invokestatic  #273                // Method kotlin/ranges/RangesKt.until:(II)Lkotlin/ranges/IntRange;
       5: checkcast     #131                // class java/lang/Iterable
       8: astore_3
       9: iconst_0
      10: istore        4
      12: aload_3
      13: instanceof    #144                // class java/util/Collection
      16: ifeq          35
      19: aload_3
      20: checkcast     #144                // class java/util/Collection
      23: invokeinterface #276,  1          // InterfaceMethod java/util/Collection.isEmpty:()Z
      28: ifeq          35
      31: iconst_0
      32: goto          208
      35: iconst_0
      36: istore        5
      38: aload_3
      39: invokeinterface #148,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      44: astore        6
      46: aload         6
      48: invokeinterface #154,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
      53: ifeq          206
      56: aload         6
      58: checkcast     #278                // class kotlin/collections/IntIterator
      61: invokevirtual #281                // Method kotlin/collections/IntIterator.nextInt:()I
      64: istore        7
      66: iload         7
      68: istore        8
      70: iconst_0
      71: istore        9
      73: aload_1
      74: iload         8
      76: invokeinterface #94,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      81: checkcast     #131                // class java/lang/Iterable
      84: astore        10
      86: iconst_0
      87: istore        11
      89: aload         10
      91: instanceof    #144                // class java/util/Collection
      94: ifeq          114
      97: aload         10
      99: checkcast     #144                // class java/util/Collection
     102: invokeinterface #276,  1          // InterfaceMethod java/util/Collection.isEmpty:()Z
     107: ifeq          114
     110: iconst_1
     111: goto          188
     114: aload         10
     116: invokeinterface #148,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     121: astore        12
     123: aload         12
     125: invokeinterface #154,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
     130: ifeq          187
     133: aload         12
     135: invokeinterface #157,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     140: astore        13
     142: aload         13
     144: checkcast     #283                // class java/lang/Number
     147: invokevirtual #286                // Method java/lang/Number.intValue:()I
     150: istore        14
     152: iconst_0
     153: istore        15
     155: aload_2
     156: iload         14
     158: invokeinterface #94,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     163: checkcast     #248                // class java/util/Set
     166: invokeinterface #287,  1          // InterfaceMethod java/util/Set.size:()I
     171: iconst_1
     172: if_icmple     179
     175: iconst_1
     176: goto          180
     179: iconst_0
     180: ifne          123
     183: iconst_0
     184: goto          188
     187: iconst_1
     188: nop
     189: ifeq          46
     192: iinc          5, 1
     195: iload         5
     197: ifge          46
     200: invokestatic  #290                // Method kotlin/collections/CollectionsKt.throwCountOverflow:()V
     203: goto          46
     206: iload         5
     208: ireturn

  private static final int part2(int, java.util.List<? extends java.util.Set<java.lang.Integer>>, java.util.List<? extends java.util.Set<java.lang.Integer>>);
    Code:
       0: iconst_0
       1: istore_3
       2: iconst_0
       3: istore        4
       5: iload         4
       7: iload_0
       8: if_icmpge     378
      11: new           #301                // class kotlin/collections/ArrayDeque
      14: dup
      15: invokespecial #302                // Method kotlin/collections/ArrayDeque."<init>":()V
      18: astore        6
      20: aload         6
      22: astore        7
      24: iconst_0
      25: istore        8
      27: aload         7
      29: aload_1
      30: iload         4
      32: invokeinterface #94,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      37: checkcast     #131                // class java/lang/Iterable
      40: astore        9
      42: astore        10
      44: iconst_0
      45: istore        11
      47: aload         9
      49: astore        12
      51: new           #133                // class java/util/ArrayList
      54: dup
      55: invokespecial #303                // Method java/util/ArrayList."<init>":()V
      58: checkcast     #144                // class java/util/Collection
      61: astore        13
      63: iconst_0
      64: istore        14
      66: aload         12
      68: invokeinterface #148,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      73: astore        15
      75: aload         15
      77: invokeinterface #154,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
      82: ifeq          148
      85: aload         15
      87: invokeinterface #157,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      92: astore        16
      94: aload         16
      96: checkcast     #283                // class java/lang/Number
      99: invokevirtual #286                // Method java/lang/Number.intValue:()I
     102: istore        17
     104: iconst_0
     105: istore        18
     107: aload_2
     108: iload         17
     110: invokeinterface #94,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     115: checkcast     #248                // class java/util/Set
     118: invokeinterface #287,  1          // InterfaceMethod java/util/Set.size:()I
     123: iconst_1
     124: if_icmpne     131
     127: iconst_1
     128: goto          132
     131: iconst_0
     132: ifeq          75
     135: aload         13
     137: aload         16
     139: invokeinterface #163,  2          // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     144: pop
     145: goto          75
     148: aload         13
     150: checkcast     #24                 // class java/util/List
     153: nop
     154: aload         10
     156: swap
     157: checkcast     #144                // class java/util/Collection
     160: invokevirtual #307                // Method kotlin/collections/ArrayDeque.addAll:(Ljava/util/Collection;)Z
     163: pop
     164: aload         6
     166: astore        5
     168: iconst_1
     169: anewarray     #100                // class java/lang/Integer
     172: astore        7
     174: aload         7
     176: iconst_0
     177: iload         4
     179: invokestatic  #252                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     182: aastore
     183: aload         7
     185: invokestatic  #313                // Method kotlin/collections/SetsKt.mutableSetOf:([Ljava/lang/Object;)Ljava/util/Set;
     188: astore        6
     190: aload         6
     192: checkcast     #144                // class java/util/Collection
     195: aload         5
     197: checkcast     #131                // class java/lang/Iterable
     200: invokestatic  #316                // Method kotlin/collections/CollectionsKt.addAll:(Ljava/util/Collection;Ljava/lang/Iterable;)Z
     203: pop
     204: aload         5
     206: checkcast     #144                // class java/util/Collection
     209: invokeinterface #276,  1          // InterfaceMethod java/util/Collection.isEmpty:()Z
     214: ifne          221
     217: iconst_1
     218: goto          222
     221: iconst_0
     222: ifeq          360
     225: aload         5
     227: invokevirtual #319                // Method kotlin/collections/ArrayDeque.removeFirst:()Ljava/lang/Object;
     230: checkcast     #283                // class java/lang/Number
     233: invokevirtual #286                // Method java/lang/Number.intValue:()I
     236: istore        7
     238: aload_1
     239: iload         7
     241: invokeinterface #94,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     246: checkcast     #248                // class java/util/Set
     249: invokeinterface #320,  1          // InterfaceMethod java/util/Set.iterator:()Ljava/util/Iterator;
     254: astore        8
     256: aload         8
     258: invokeinterface #154,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
     263: ifeq          204
     266: aload         8
     268: invokeinterface #157,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     273: checkcast     #283                // class java/lang/Number
     276: invokevirtual #286                // Method java/lang/Number.intValue:()I
     279: istore        9
     281: aload         6
     283: iload         9
     285: invokestatic  #252                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     288: invokeinterface #323,  2          // InterfaceMethod java/util/Set.contains:(Ljava/lang/Object;)Z
     293: ifne          256
     296: aload         6
     298: aload_2
     299: iload         9
     301: invokeinterface #94,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     306: checkcast     #144                // class java/util/Collection
     309: invokeinterface #326,  2          // InterfaceMethod java/util/Set.containsAll:(Ljava/util/Collection;)Z
     314: ifeq          256
     317: iload         9
     319: invokestatic  #252                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     322: astore        11
     324: aload         5
     326: checkcast     #144                // class java/util/Collection
     329: aload         11
     331: invokeinterface #163,  2          // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     336: pop
     337: iload         9
     339: invokestatic  #252                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     342: astore        11
     344: aload         6
     346: checkcast     #144                // class java/util/Collection
     349: aload         11
     351: invokeinterface #163,  2          // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     356: pop
     357: goto          256
     360: iload_3
     361: aload         6
     363: invokeinterface #287,  1          // InterfaceMethod java/util/Set.size:()I
     368: iconst_1
     369: isub
     370: iadd
     371: istore_3
     372: iinc          4, 1
     375: goto          5
     378: iload_3
     379: ireturn

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

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

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

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

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