package com.luckystar.advent2020

fun main() {
    val file = object {}.javaClass.getResource("/2020/input_7.txt").readText()
    var rules = file.split("\r\n")

    rules = rules.filter { rule -> !rule.contains("no other bags") }

    val regex = """^(\w+ \w+) bags contain (.*)\.$""".toRegex()
    val rightRegex = """^(\d) (\w+ \w+) bags?$""".toRegex()
    val flatRules = rules.map { rule ->
        val (left, right) = regex.find(rule)!!.destructured
        right.split(", ").map { g ->
            val (num, bag) = rightRegex.find(g)!!.destructured
            Triple(left, num.toInt(), bag)
        }
    }.flatten()

    // part 1
    val mapRight = flatRules.groupBy { r -> r.third }
    var deps = mapRight.getValue("shiny gold");
    val uniqueBags = mutableSetOf<String>()
    while (deps.isNotEmpty()) {
        val lefts = deps.map { d -> d.first }
        uniqueBags.addAll(lefts)
        deps = lefts.map { l -> mapRight.get(l) }.filterNotNull().flatten()
    }
    println(uniqueBags.size)

    // part 2
    val mapLeft = flatRules.groupBy { r -> r.first }
    var deps2 = mapLeft.getValue("shiny gold").map { t -> Triple(t.first, t.second, t.third) };
    var numberOfBags = 0
    while (deps2.isNotEmpty()) {
        numberOfBags += deps2
                .map { q ->
                    val first = mapLeft.get(q.third)
                    Pair(first, q.second) }
                .filter { p -> p.first == null }
                .map { p -> p.second }
                .sum()
        deps2 = deps2
                .map { q -> Pair(mapLeft.get(q.third), q.second) }
                .filter { p -> p.first != null }
                .flatMap { p ->
                    numberOfBags += p.second
                    p.first!!.map { t -> Triple(t.first, t.second * p.second, t.third) }
                }
    }
    println(numberOfBags)

}

data class Quadruple<T1, T2, T3, T4>(val first: T1, val second: T2, val third: T3, val fourth: T4)

Compiled from "Advent7.kt"
public final class com.luckystar.advent2020.Advent7Kt$main$file$1 {
  com.luckystar.advent2020.Advent7Kt$main$file$1();
    Code:
       0: aload_0
       1: invokespecial #8                  // Method java/lang/Object."<init>":()V
       4: return
}

Compiled from "Advent7.kt"
public final class com.luckystar.advent2020.Advent7Kt {
  public static final void main();
    Code:
       0: new           #8                  // class com/luckystar/advent2020/Advent7Kt$main$file$1
       3: dup
       4: invokespecial #11                 // Method com/luckystar/advent2020/Advent7Kt$main$file$1."<init>":()V
       7: invokevirtual #15                 // Method java/lang/Object.getClass:()Ljava/lang/Class;
      10: ldc           #17                 // String /2020/input_7.txt
      12: invokevirtual #23                 // Method java/lang/Class.getResource:(Ljava/lang/String;)Ljava/net/URL;
      15: dup
      16: ldc           #25                 // String getResource(...)
      18: invokestatic  #31                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
      21: astore_1
      22: getstatic     #37                 // Field kotlin/text/Charsets.UTF_8:Ljava/nio/charset/Charset;
      25: astore_2
      26: aload_1
      27: invokestatic  #43                 // Method kotlin/io/TextStreamsKt.readBytes:(Ljava/net/URL;)[B
      30: astore_3
      31: new           #45                 // class java/lang/String
      34: dup
      35: aload_3
      36: aload_2
      37: invokespecial #48                 // Method java/lang/String."<init>":([BLjava/nio/charset/Charset;)V
      40: astore_0
      41: aload_0
      42: checkcast     #50                 // class java/lang/CharSequence
      45: iconst_1
      46: anewarray     #45                 // class java/lang/String
      49: astore_2
      50: aload_2
      51: iconst_0
      52: ldc           #52                 // String \r\n
      54: aastore
      55: aload_2
      56: iconst_0
      57: iconst_0
      58: bipush        6
      60: aconst_null
      61: invokestatic  #58                 // Method kotlin/text/StringsKt.split$default:(Ljava/lang/CharSequence;[Ljava/lang/String;ZIILjava/lang/Object;)Ljava/util/List;
      64: astore_1
      65: aload_1
      66: checkcast     #60                 // class java/lang/Iterable
      69: astore_2
      70: iconst_0
      71: istore_3
      72: aload_2
      73: astore        4
      75: new           #62                 // class java/util/ArrayList
      78: dup
      79: invokespecial #63                 // Method java/util/ArrayList."<init>":()V
      82: checkcast     #65                 // class java/util/Collection
      85: astore        5
      87: iconst_0
      88: istore        6
      90: aload         4
      92: invokeinterface #69,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      97: astore        7
      99: aload         7
     101: invokeinterface #75,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     106: ifeq          168
     109: aload         7
     111: invokeinterface #79,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     116: astore        8
     118: aload         8
     120: checkcast     #45                 // class java/lang/String
     123: astore        9
     125: iconst_0
     126: istore        10
     128: aload         9
     130: checkcast     #50                 // class java/lang/CharSequence
     133: ldc           #81                 // String no other bags
     135: checkcast     #50                 // class java/lang/CharSequence
     138: iconst_0
     139: iconst_2
     140: aconst_null
     141: invokestatic  #85                 // Method kotlin/text/StringsKt.contains$default:(Ljava/lang/CharSequence;Ljava/lang/CharSequence;ZILjava/lang/Object;)Z
     144: ifne          151
     147: iconst_1
     148: goto          152
     151: iconst_0
     152: ifeq          99
     155: aload         5
     157: aload         8
     159: invokeinterface #89,  2           // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     164: pop
     165: goto          99
     168: aload         5
     170: checkcast     #91                 // class java/util/List
     173: nop
     174: astore_1
     175: new           #93                 // class kotlin/text/Regex
     178: dup
     179: ldc           #95                 // String ^(\\w+ \\w+) bags contain (.*)\\.$
     181: invokespecial #98                 // Method kotlin/text/Regex."<init>":(Ljava/lang/String;)V
     184: astore_2
     185: new           #93                 // class kotlin/text/Regex
     188: dup
     189: ldc           #100                // String ^(\\d) (\\w+ \\w+) bags?$
     191: invokespecial #98                 // Method kotlin/text/Regex."<init>":(Ljava/lang/String;)V
     194: astore_3
     195: aload_1
     196: checkcast     #60                 // class java/lang/Iterable
     199: astore        5
     201: iconst_0
     202: istore        6
     204: aload         5
     206: astore        7
     208: new           #62                 // class java/util/ArrayList
     211: dup
     212: aload         5
     214: bipush        10
     216: invokestatic  #106                // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
     219: invokespecial #109                // Method java/util/ArrayList."<init>":(I)V
     222: checkcast     #65                 // class java/util/Collection
     225: astore        8
     227: iconst_0
     228: istore        9
     230: aload         7
     232: invokeinterface #69,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     237: astore        10
     239: aload         10
     241: invokeinterface #75,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     246: ifeq          555
     249: aload         10
     251: invokeinterface #79,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     256: astore        11
     258: aload         8
     260: aload         11
     262: checkcast     #45                 // class java/lang/String
     265: astore        12
     267: astore        30
     269: iconst_0
     270: istore        13
     272: aload_2
     273: aload         12
     275: checkcast     #50                 // class java/lang/CharSequence
     278: iconst_0
     279: iconst_2
     280: aconst_null
     281: invokestatic  #113                // Method kotlin/text/Regex.find$default:(Lkotlin/text/Regex;Ljava/lang/CharSequence;IILjava/lang/Object;)Lkotlin/text/MatchResult;
     284: dup
     285: invokestatic  #117                // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
     288: invokeinterface #123,  1          // InterfaceMethod kotlin/text/MatchResult.getDestructured:()Lkotlin/text/MatchResult$Destructured;
     293: astore        14
     295: aload         14
     297: invokevirtual #129                // Method kotlin/text/MatchResult$Destructured.getMatch:()Lkotlin/text/MatchResult;
     300: invokeinterface #133,  1          // InterfaceMethod kotlin/text/MatchResult.getGroupValues:()Ljava/util/List;
     305: iconst_1
     306: invokeinterface #137,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     311: checkcast     #45                 // class java/lang/String
     314: astore        15
     316: aload         14
     318: invokevirtual #129                // Method kotlin/text/MatchResult$Destructured.getMatch:()Lkotlin/text/MatchResult;
     321: invokeinterface #133,  1          // InterfaceMethod kotlin/text/MatchResult.getGroupValues:()Ljava/util/List;
     326: iconst_2
     327: invokeinterface #137,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     332: checkcast     #45                 // class java/lang/String
     335: astore        16
     337: aload         16
     339: checkcast     #50                 // class java/lang/CharSequence
     342: iconst_1
     343: anewarray     #45                 // class java/lang/String
     346: astore        17
     348: aload         17
     350: iconst_0
     351: ldc           #139                // String ,
     353: aastore
     354: aload         17
     356: iconst_0
     357: iconst_0
     358: bipush        6
     360: aconst_null
     361: invokestatic  #58                 // Method kotlin/text/StringsKt.split$default:(Ljava/lang/CharSequence;[Ljava/lang/String;ZIILjava/lang/Object;)Ljava/util/List;
     364: checkcast     #60                 // class java/lang/Iterable
     367: astore        17
     369: iconst_0
     370: istore        18
     372: aload         17
     374: astore        19
     376: new           #62                 // class java/util/ArrayList
     379: dup
     380: aload         17
     382: bipush        10
     384: invokestatic  #106                // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
     387: invokespecial #109                // Method java/util/ArrayList."<init>":(I)V
     390: checkcast     #65                 // class java/util/Collection
     393: astore        20
     395: iconst_0
     396: istore        21
     398: aload         19
     400: invokeinterface #69,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     405: astore        22
     407: aload         22
     409: invokeinterface #75,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     414: ifeq          536
     417: aload         22
     419: invokeinterface #79,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     424: astore        23
     426: aload         20
     428: aload         23
     430: checkcast     #45                 // class java/lang/String
     433: astore        24
     435: astore        25
     437: iconst_0
     438: istore        26
     440: aload_3
     441: aload         24
     443: checkcast     #50                 // class java/lang/CharSequence
     446: iconst_0
     447: iconst_2
     448: aconst_null
     449: invokestatic  #113                // Method kotlin/text/Regex.find$default:(Lkotlin/text/Regex;Ljava/lang/CharSequence;IILjava/lang/Object;)Lkotlin/text/MatchResult;
     452: dup
     453: invokestatic  #117                // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
     456: invokeinterface #123,  1          // InterfaceMethod kotlin/text/MatchResult.getDestructured:()Lkotlin/text/MatchResult$Destructured;
     461: astore        27
     463: aload         27
     465: invokevirtual #129                // Method kotlin/text/MatchResult$Destructured.getMatch:()Lkotlin/text/MatchResult;
     468: invokeinterface #133,  1          // InterfaceMethod kotlin/text/MatchResult.getGroupValues:()Ljava/util/List;
     473: iconst_1
     474: invokeinterface #137,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     479: checkcast     #45                 // class java/lang/String
     482: astore        28
     484: aload         27
     486: invokevirtual #129                // Method kotlin/text/MatchResult$Destructured.getMatch:()Lkotlin/text/MatchResult;
     489: invokeinterface #133,  1          // InterfaceMethod kotlin/text/MatchResult.getGroupValues:()Ljava/util/List;
     494: iconst_2
     495: invokeinterface #137,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     500: checkcast     #45                 // class java/lang/String
     503: astore        29
     505: new           #141                // class kotlin/Triple
     508: dup
     509: aload         15
     511: aload         28
     513: invokestatic  #147                // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
     516: invokestatic  #151                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     519: aload         29
     521: invokespecial #154                // Method kotlin/Triple."<init>":(Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)V
     524: aload         25
     526: swap
     527: invokeinterface #89,  2           // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     532: pop
     533: goto          407
     536: aload         20
     538: checkcast     #91                 // class java/util/List
     541: nop
     542: nop
     543: aload         30
     545: swap
     546: invokeinterface #89,  2           // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     551: pop
     552: goto          239
     555: aload         8
     557: checkcast     #91                 // class java/util/List
     560: nop
     561: checkcast     #60                 // class java/lang/Iterable
     564: invokestatic  #158                // Method kotlin/collections/CollectionsKt.flatten:(Ljava/lang/Iterable;)Ljava/util/List;
     567: astore        4
     569: aload         4
     571: checkcast     #60                 // class java/lang/Iterable
     574: astore        6
     576: iconst_0
     577: istore        7
     579: aload         6
     581: astore        8
     583: new           #160                // class java/util/LinkedHashMap
     586: dup
     587: invokespecial #161                // Method java/util/LinkedHashMap."<init>":()V
     590: checkcast     #163                // class java/util/Map
     593: astore        9
     595: iconst_0
     596: istore        10
     598: aload         8
     600: invokeinterface #69,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     605: astore        11
     607: aload         11
     609: invokeinterface #75,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     614: ifeq          722
     617: aload         11
     619: invokeinterface #79,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     624: astore        12
     626: aload         12
     628: checkcast     #141                // class kotlin/Triple
     631: astore        13
     633: iconst_0
     634: istore        14
     636: aload         13
     638: invokevirtual #166                // Method kotlin/Triple.getThird:()Ljava/lang/Object;
     641: checkcast     #45                 // class java/lang/String
     644: astore        15
     646: aload         9
     648: astore        16
     650: iconst_0
     651: istore        17
     653: aload         16
     655: aload         15
     657: invokeinterface #169,  2          // InterfaceMethod java/util/Map.get:(Ljava/lang/Object;)Ljava/lang/Object;
     662: astore        18
     664: aload         18
     666: ifnonnull     701
     669: iconst_0
     670: istore        19
     672: new           #62                 // class java/util/ArrayList
     675: dup
     676: invokespecial #63                 // Method java/util/ArrayList."<init>":()V
     679: checkcast     #91                 // class java/util/List
     682: astore        19
     684: aload         16
     686: aload         15
     688: aload         19
     690: invokeinterface #173,  3          // InterfaceMethod java/util/Map.put:(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
     695: pop
     696: aload         19
     698: goto          703
     701: aload         18
     703: nop
     704: checkcast     #91                 // class java/util/List
     707: astore        13
     709: aload         13
     711: aload         12
     713: invokeinterface #174,  2          // InterfaceMethod java/util/List.add:(Ljava/lang/Object;)Z
     718: pop
     719: goto          607
     722: aload         9
     724: nop
     725: astore        5
     727: aload         5
     729: ldc           #176                // String shiny gold
     731: invokestatic  #182                // Method kotlin/collections/MapsKt.getValue:(Ljava/util/Map;Ljava/lang/Object;)Ljava/lang/Object;
     734: checkcast     #91                 // class java/util/List
     737: astore        6
     739: new           #184                // class java/util/LinkedHashSet
     742: dup
     743: invokespecial #185                // Method java/util/LinkedHashSet."<init>":()V
     746: checkcast     #187                // class java/util/Set
     749: astore        7
     751: aload         6
     753: checkcast     #65                 // class java/util/Collection
     756: invokeinterface #190,  1          // InterfaceMethod java/util/Collection.isEmpty:()Z
     761: ifne          768
     764: iconst_1
     765: goto          769
     768: iconst_0
     769: ifeq          1016
     772: aload         6
     774: checkcast     #60                 // class java/lang/Iterable
     777: astore        9
     779: iconst_0
     780: istore        10
     782: aload         9
     784: astore        11
     786: new           #62                 // class java/util/ArrayList
     789: dup
     790: aload         9
     792: bipush        10
     794: invokestatic  #106                // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
     797: invokespecial #109                // Method java/util/ArrayList."<init>":(I)V
     800: checkcast     #65                 // class java/util/Collection
     803: astore        12
     805: iconst_0
     806: istore        13
     808: aload         11
     810: invokeinterface #69,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     815: astore        14
     817: aload         14
     819: invokeinterface #75,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     824: ifeq          870
     827: aload         14
     829: invokeinterface #79,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     834: astore        15
     836: aload         12
     838: aload         15
     840: checkcast     #141                // class kotlin/Triple
     843: astore        16
     845: astore        30
     847: iconst_0
     848: istore        17
     850: aload         16
     852: invokevirtual #193                // Method kotlin/Triple.getFirst:()Ljava/lang/Object;
     855: checkcast     #45                 // class java/lang/String
     858: aload         30
     860: swap
     861: invokeinterface #89,  2           // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     866: pop
     867: goto          817
     870: aload         12
     872: checkcast     #91                 // class java/util/List
     875: nop
     876: astore        8
     878: aload         7
     880: aload         8
     882: checkcast     #65                 // class java/util/Collection
     885: invokeinterface #197,  2          // InterfaceMethod java/util/Set.addAll:(Ljava/util/Collection;)Z
     890: pop
     891: aload         8
     893: checkcast     #60                 // class java/lang/Iterable
     896: astore        9
     898: iconst_0
     899: istore        10
     901: aload         9
     903: astore        11
     905: new           #62                 // class java/util/ArrayList
     908: dup
     909: aload         9
     911: bipush        10
     913: invokestatic  #106                // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
     916: invokespecial #109                // Method java/util/ArrayList."<init>":(I)V
     919: checkcast     #65                 // class java/util/Collection
     922: astore        12
     924: iconst_0
     925: istore        13
     927: aload         11
     929: invokeinterface #69,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     934: astore        14
     936: aload         14
     938: invokeinterface #75,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     943: ifeq          993
     946: aload         14
     948: invokeinterface #79,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     953: astore        15
     955: aload         12
     957: aload         15
     959: checkcast     #45                 // class java/lang/String
     962: astore        16
     964: astore        30
     966: iconst_0
     967: istore        17
     969: aload         5
     971: aload         16
     973: invokeinterface #169,  2          // InterfaceMethod java/util/Map.get:(Ljava/lang/Object;)Ljava/lang/Object;
     978: checkcast     #91                 // class java/util/List
     981: aload         30
     983: swap
     984: invokeinterface #89,  2           // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     989: pop
     990: goto          936
     993: aload         12
     995: checkcast     #91                 // class java/util/List
     998: nop
     999: checkcast     #60                 // class java/lang/Iterable
    1002: invokestatic  #200                // Method kotlin/collections/CollectionsKt.filterNotNull:(Ljava/lang/Iterable;)Ljava/util/List;
    1005: checkcast     #60                 // class java/lang/Iterable
    1008: invokestatic  #158                // Method kotlin/collections/CollectionsKt.flatten:(Ljava/lang/Iterable;)Ljava/util/List;
    1011: astore        6
    1013: goto          751
    1016: aload         7
    1018: invokeinterface #204,  1          // InterfaceMethod java/util/Set.size:()I
    1023: istore        8
    1025: getstatic     #210                // Field java/lang/System.out:Ljava/io/PrintStream;
    1028: iload         8
    1030: invokevirtual #215                // Method java/io/PrintStream.println:(I)V
    1033: aload         4
    1035: checkcast     #60                 // class java/lang/Iterable
    1038: astore        9
    1040: iconst_0
    1041: istore        10
    1043: aload         9
    1045: astore        11
    1047: new           #160                // class java/util/LinkedHashMap
    1050: dup
    1051: invokespecial #161                // Method java/util/LinkedHashMap."<init>":()V
    1054: checkcast     #163                // class java/util/Map
    1057: astore        12
    1059: iconst_0
    1060: istore        13
    1062: aload         11
    1064: invokeinterface #69,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
    1069: astore        14
    1071: aload         14
    1073: invokeinterface #75,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
    1078: ifeq          1186
    1081: aload         14
    1083: invokeinterface #79,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
    1088: astore        15
    1090: aload         15
    1092: checkcast     #141                // class kotlin/Triple
    1095: astore        16
    1097: iconst_0
    1098: istore        17
    1100: aload         16
    1102: invokevirtual #193                // Method kotlin/Triple.getFirst:()Ljava/lang/Object;
    1105: checkcast     #45                 // class java/lang/String
    1108: astore        18
    1110: aload         12
    1112: astore        19
    1114: iconst_0
    1115: istore        20
    1117: aload         19
    1119: aload         18
    1121: invokeinterface #169,  2          // InterfaceMethod java/util/Map.get:(Ljava/lang/Object;)Ljava/lang/Object;
    1126: astore        21
    1128: aload         21
    1130: ifnonnull     1165
    1133: iconst_0
    1134: istore        22
    1136: new           #62                 // class java/util/ArrayList
    1139: dup
    1140: invokespecial #63                 // Method java/util/ArrayList."<init>":()V
    1143: checkcast     #91                 // class java/util/List
    1146: astore        22
    1148: aload         19
    1150: aload         18
    1152: aload         22
    1154: invokeinterface #173,  3          // InterfaceMethod java/util/Map.put:(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
    1159: pop
    1160: aload         22
    1162: goto          1167
    1165: aload         21
    1167: nop
    1168: checkcast     #91                 // class java/util/List
    1171: astore        16
    1173: aload         16
    1175: aload         15
    1177: invokeinterface #174,  2          // InterfaceMethod java/util/List.add:(Ljava/lang/Object;)Z
    1182: pop
    1183: goto          1071
    1186: aload         12
    1188: nop
    1189: astore        8
    1191: aload         8
    1193: ldc           #176                // String shiny gold
    1195: invokestatic  #182                // Method kotlin/collections/MapsKt.getValue:(Ljava/util/Map;Ljava/lang/Object;)Ljava/lang/Object;
    1198: checkcast     #60                 // class java/lang/Iterable
    1201: astore        10
    1203: iconst_0
    1204: istore        11
    1206: aload         10
    1208: astore        12
    1210: new           #62                 // class java/util/ArrayList
    1213: dup
    1214: aload         10
    1216: bipush        10
    1218: invokestatic  #106                // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
    1221: invokespecial #109                // Method java/util/ArrayList."<init>":(I)V
    1224: checkcast     #65                 // class java/util/Collection
    1227: astore        13
    1229: iconst_0
    1230: istore        14
    1232: aload         12
    1234: invokeinterface #69,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
    1239: astore        15
    1241: aload         15
    1243: invokeinterface #75,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
    1248: ifeq          1308
    1251: aload         15
    1253: invokeinterface #79,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
    1258: astore        16
    1260: aload         13
    1262: aload         16
    1264: checkcast     #141                // class kotlin/Triple
    1267: astore        17
    1269: astore        30
    1271: iconst_0
    1272: istore        18
    1274: new           #141                // class kotlin/Triple
    1277: dup
    1278: aload         17
    1280: invokevirtual #193                // Method kotlin/Triple.getFirst:()Ljava/lang/Object;
    1283: aload         17
    1285: invokevirtual #218                // Method kotlin/Triple.getSecond:()Ljava/lang/Object;
    1288: aload         17
    1290: invokevirtual #166                // Method kotlin/Triple.getThird:()Ljava/lang/Object;
    1293: invokespecial #154                // Method kotlin/Triple."<init>":(Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)V
    1296: aload         30
    1298: swap
    1299: invokeinterface #89,  2           // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
    1304: pop
    1305: goto          1241
    1308: aload         13
    1310: checkcast     #91                 // class java/util/List
    1313: nop
    1314: astore        9
    1316: iconst_0
    1317: istore        10
    1319: aload         9
    1321: checkcast     #65                 // class java/util/Collection
    1324: invokeinterface #190,  1          // InterfaceMethod java/util/Collection.isEmpty:()Z
    1329: ifne          1336
    1332: iconst_1
    1333: goto          1337
    1336: iconst_0
    1337: ifeq          2188
    1340: iload         10
    1342: aload         9
    1344: checkcast     #60                 // class java/lang/Iterable
    1347: astore        11
    1349: istore        31
    1351: iconst_0
    1352: istore        12
    1354: aload         11
    1356: astore        13
    1358: new           #62                 // class java/util/ArrayList
    1361: dup
    1362: aload         11
    1364: bipush        10
    1366: invokestatic  #106                // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
    1369: invokespecial #109                // Method java/util/ArrayList."<init>":(I)V
    1372: checkcast     #65                 // class java/util/Collection
    1375: astore        14
    1377: iconst_0
    1378: istore        15
    1380: aload         13
    1382: invokeinterface #69,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
    1387: astore        16
    1389: aload         16
    1391: invokeinterface #75,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
    1396: ifeq          1465
    1399: aload         16
    1401: invokeinterface #79,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
    1406: astore        17
    1408: aload         14
    1410: aload         17
    1412: checkcast     #141                // class kotlin/Triple
    1415: astore        18
    1417: astore        32
    1419: iconst_0
    1420: istore        19
    1422: aload         8
    1424: aload         18
    1426: invokevirtual #166                // Method kotlin/Triple.getThird:()Ljava/lang/Object;
    1429: invokeinterface #169,  2          // InterfaceMethod java/util/Map.get:(Ljava/lang/Object;)Ljava/lang/Object;
    1434: checkcast     #91                 // class java/util/List
    1437: astore        20
    1439: new           #220                // class kotlin/Pair
    1442: dup
    1443: aload         20
    1445: aload         18
    1447: invokevirtual #218                // Method kotlin/Triple.getSecond:()Ljava/lang/Object;
    1450: invokespecial #223                // Method kotlin/Pair."<init>":(Ljava/lang/Object;Ljava/lang/Object;)V
    1453: aload         32
    1455: swap
    1456: invokeinterface #89,  2           // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
    1461: pop
    1462: goto          1389
    1465: aload         14
    1467: checkcast     #91                 // class java/util/List
    1470: nop
    1471: astore        32
    1473: iload         31
    1475: aload         32
    1477: checkcast     #60                 // class java/lang/Iterable
    1480: astore        11
    1482: istore        31
    1484: iconst_0
    1485: istore        12
    1487: aload         11
    1489: astore        13
    1491: new           #62                 // class java/util/ArrayList
    1494: dup
    1495: invokespecial #63                 // Method java/util/ArrayList."<init>":()V
    1498: checkcast     #65                 // class java/util/Collection
    1501: astore        14
    1503: iconst_0
    1504: istore        15
    1506: aload         13
    1508: invokeinterface #69,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
    1513: astore        16
    1515: aload         16
    1517: invokeinterface #75,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
    1522: ifeq          1573
    1525: aload         16
    1527: invokeinterface #79,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
    1532: astore        17
    1534: aload         17
    1536: checkcast     #220                // class kotlin/Pair
    1539: astore        18
    1541: iconst_0
    1542: istore        19
    1544: aload         18
    1546: invokevirtual #224                // Method kotlin/Pair.getFirst:()Ljava/lang/Object;
    1549: ifnonnull     1556
    1552: iconst_1
    1553: goto          1557
    1556: iconst_0
    1557: ifeq          1515
    1560: aload         14
    1562: aload         17
    1564: invokeinterface #89,  2           // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
    1569: pop
    1570: goto          1515
    1573: aload         14
    1575: checkcast     #91                 // class java/util/List
    1578: nop
    1579: astore        32
    1581: iload         31
    1583: aload         32
    1585: checkcast     #60                 // class java/lang/Iterable
    1588: astore        11
    1590: istore        31
    1592: iconst_0
    1593: istore        12
    1595: aload         11
    1597: astore        13
    1599: new           #62                 // class java/util/ArrayList
    1602: dup
    1603: aload         11
    1605: bipush        10
    1607: invokestatic  #106                // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
    1610: invokespecial #109                // Method java/util/ArrayList."<init>":(I)V
    1613: checkcast     #65                 // class java/util/Collection
    1616: astore        14
    1618: iconst_0
    1619: istore        15
    1621: aload         13
    1623: invokeinterface #69,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
    1628: astore        16
    1630: aload         16
    1632: invokeinterface #75,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
    1637: ifeq          1689
    1640: aload         16
    1642: invokeinterface #79,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
    1647: astore        17
    1649: aload         14
    1651: aload         17
    1653: checkcast     #220                // class kotlin/Pair
    1656: astore        18
    1658: astore        32
    1660: iconst_0
    1661: istore        19
    1663: aload         18
    1665: invokevirtual #225                // Method kotlin/Pair.getSecond:()Ljava/lang/Object;
    1668: checkcast     #227                // class java/lang/Number
    1671: invokevirtual #230                // Method java/lang/Number.intValue:()I
    1674: invokestatic  #151                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
    1677: aload         32
    1679: swap
    1680: invokeinterface #89,  2           // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
    1685: pop
    1686: goto          1630
    1689: aload         14
    1691: checkcast     #91                 // class java/util/List
    1694: nop
    1695: astore        32
    1697: iload         31
    1699: aload         32
    1701: checkcast     #60                 // class java/lang/Iterable
    1704: invokestatic  #234                // Method kotlin/collections/CollectionsKt.sumOfInt:(Ljava/lang/Iterable;)I
    1707: iadd
    1708: istore        10
    1710: aload         9
    1712: checkcast     #60                 // class java/lang/Iterable
    1715: astore        11
    1717: nop
    1718: iconst_0
    1719: istore        12
    1721: aload         11
    1723: astore        13
    1725: new           #62                 // class java/util/ArrayList
    1728: dup
    1729: aload         11
    1731: bipush        10
    1733: invokestatic  #106                // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
    1736: invokespecial #109                // Method java/util/ArrayList."<init>":(I)V
    1739: checkcast     #65                 // class java/util/Collection
    1742: astore        14
    1744: iconst_0
    1745: istore        15
    1747: aload         13
    1749: invokeinterface #69,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
    1754: astore        16
    1756: aload         16
    1758: invokeinterface #75,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
    1763: ifeq          1825
    1766: aload         16
    1768: invokeinterface #79,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
    1773: astore        17
    1775: aload         14
    1777: aload         17
    1779: checkcast     #141                // class kotlin/Triple
    1782: astore        18
    1784: astore        30
    1786: iconst_0
    1787: istore        19
    1789: new           #220                // class kotlin/Pair
    1792: dup
    1793: aload         8
    1795: aload         18
    1797: invokevirtual #166                // Method kotlin/Triple.getThird:()Ljava/lang/Object;
    1800: invokeinterface #169,  2          // InterfaceMethod java/util/Map.get:(Ljava/lang/Object;)Ljava/lang/Object;
    1805: aload         18
    1807: invokevirtual #218                // Method kotlin/Triple.getSecond:()Ljava/lang/Object;
    1810: invokespecial #223                // Method kotlin/Pair."<init>":(Ljava/lang/Object;Ljava/lang/Object;)V
    1813: aload         30
    1815: swap
    1816: invokeinterface #89,  2           // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
    1821: pop
    1822: goto          1756
    1825: aload         14
    1827: checkcast     #91                 // class java/util/List
    1830: nop
    1831: checkcast     #60                 // class java/lang/Iterable
    1834: astore        11
    1836: nop
    1837: iconst_0
    1838: istore        12
    1840: aload         11
    1842: astore        13
    1844: new           #62                 // class java/util/ArrayList
    1847: dup
    1848: invokespecial #63                 // Method java/util/ArrayList."<init>":()V
    1851: checkcast     #65                 // class java/util/Collection
    1854: astore        14
    1856: iconst_0
    1857: istore        15
    1859: aload         13
    1861: invokeinterface #69,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
    1866: astore        16
    1868: aload         16
    1870: invokeinterface #75,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
    1875: ifeq          1926
    1878: aload         16
    1880: invokeinterface #79,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
    1885: astore        17
    1887: aload         17
    1889: checkcast     #220                // class kotlin/Pair
    1892: astore        18
    1894: iconst_0
    1895: istore        19
    1897: aload         18
    1899: invokevirtual #224                // Method kotlin/Pair.getFirst:()Ljava/lang/Object;
    1902: ifnull        1909
    1905: iconst_1
    1906: goto          1910
    1909: iconst_0
    1910: ifeq          1868
    1913: aload         14
    1915: aload         17
    1917: invokeinterface #89,  2           // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
    1922: pop
    1923: goto          1868
    1926: aload         14
    1928: checkcast     #91                 // class java/util/List
    1931: nop
    1932: checkcast     #60                 // class java/lang/Iterable
    1935: astore        11
    1937: nop
    1938: iconst_0
    1939: istore        12
    1941: aload         11
    1943: astore        13
    1945: new           #62                 // class java/util/ArrayList
    1948: dup
    1949: invokespecial #63                 // Method java/util/ArrayList."<init>":()V
    1952: checkcast     #65                 // class java/util/Collection
    1955: astore        14
    1957: iconst_0
    1958: istore        15
    1960: aload         13
    1962: invokeinterface #69,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
    1967: astore        16
    1969: aload         16
    1971: invokeinterface #75,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
    1976: ifeq          2177
    1979: aload         16
    1981: invokeinterface #79,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
    1986: astore        17
    1988: aload         17
    1990: checkcast     #220                // class kotlin/Pair
    1993: astore        18
    1995: iconst_0
    1996: istore        19
    1998: iload         10
    2000: aload         18
    2002: invokevirtual #225                // Method kotlin/Pair.getSecond:()Ljava/lang/Object;
    2005: checkcast     #227                // class java/lang/Number
    2008: invokevirtual #230                // Method java/lang/Number.intValue:()I
    2011: iadd
    2012: istore        10
    2014: aload         18
    2016: invokevirtual #224                // Method kotlin/Pair.getFirst:()Ljava/lang/Object;
    2019: dup
    2020: invokestatic  #117                // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
    2023: checkcast     #60                 // class java/lang/Iterable
    2026: astore        20
    2028: iconst_0
    2029: istore        21
    2031: aload         20
    2033: astore        22
    2035: new           #62                 // class java/util/ArrayList
    2038: dup
    2039: aload         20
    2041: bipush        10
    2043: invokestatic  #106                // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
    2046: invokespecial #109                // Method java/util/ArrayList."<init>":(I)V
    2049: checkcast     #65                 // class java/util/Collection
    2052: astore        23
    2054: iconst_0
    2055: istore        24
    2057: aload         22
    2059: invokeinterface #69,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
    2064: astore        25
    2066: aload         25
    2068: invokeinterface #75,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
    2073: ifeq          2154
    2076: aload         25
    2078: invokeinterface #79,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
    2083: astore        26
    2085: aload         23
    2087: aload         26
    2089: checkcast     #141                // class kotlin/Triple
    2092: astore        27
    2094: astore        28
    2096: iconst_0
    2097: istore        29
    2099: new           #141                // class kotlin/Triple
    2102: dup
    2103: aload         27
    2105: invokevirtual #193                // Method kotlin/Triple.getFirst:()Ljava/lang/Object;
    2108: aload         27
    2110: invokevirtual #218                // Method kotlin/Triple.getSecond:()Ljava/lang/Object;
    2113: checkcast     #227                // class java/lang/Number
    2116: invokevirtual #230                // Method java/lang/Number.intValue:()I
    2119: aload         18
    2121: invokevirtual #225                // Method kotlin/Pair.getSecond:()Ljava/lang/Object;
    2124: checkcast     #227                // class java/lang/Number
    2127: invokevirtual #230                // Method java/lang/Number.intValue:()I
    2130: imul
    2131: invokestatic  #151                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
    2134: aload         27
    2136: invokevirtual #166                // Method kotlin/Triple.getThird:()Ljava/lang/Object;
    2139: invokespecial #154                // Method kotlin/Triple."<init>":(Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)V
    2142: aload         28
    2144: swap
    2145: invokeinterface #89,  2           // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
    2150: pop
    2151: goto          2066
    2154: aload         23
    2156: checkcast     #91                 // class java/util/List
    2159: nop
    2160: checkcast     #60                 // class java/lang/Iterable
    2163: nop
    2164: astore        18
    2166: aload         14
    2168: aload         18
    2170: invokestatic  #237                // Method kotlin/collections/CollectionsKt.addAll:(Ljava/util/Collection;Ljava/lang/Iterable;)Z
    2173: pop
    2174: goto          1969
    2177: aload         14
    2179: checkcast     #91                 // class java/util/List
    2182: nop
    2183: astore        9
    2185: goto          1319
    2188: iload         10
    2190: istore        11
    2192: getstatic     #210                // Field java/lang/System.out:Ljava/io/PrintStream;
    2195: iload         11
    2197: invokevirtual #215                // Method java/io/PrintStream.println:(I)V
    2200: return

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


package com.github.pjozsef

import java.util.Random

class WeightedCoin(val trueProbability: Double, val random: Random = Random()) {

    init {
        require(trueProbability in 0.0..1.0) {
            "trueProbability must be between 0.0 and 1.0, but was: $trueProbability"
        }
    }

    fun flip(): Boolean {
        return random.nextDouble() <= trueProbability
    }
}

class WeightedDie<T>(probabilities: Map<T, Number>, val random: Random = Random()) {
    constructor(values: List<T>, probabilities: List<Number>, random: Random = Random()) : this(values.zip(probabilities), random)
    constructor(probabilities: List<Pair<T, Number>>, random: Random = Random()) : this(probabilities.toMap(), random)

    private val n = probabilities.size
    private val alias = IntArray(n)
    private val prob = DoubleArray(n)
    private val values: List<T>

    init {
        val sumOfProbabilities = probabilities.values.map { it.toDouble() }.sum()
        val probList = probabilities.mapValues { it.value.toDouble() / sumOfProbabilities }.toList()
        require(probList.map { it.second }.none { it < 0.0 }) { "Probabilities must not be negative" }
        values = probList.unzip().first

        val small = mutableListOf<Int>()
        val large = mutableListOf<Int>()

        val scaledProbs = probList.zip(0 until n) { (_, percent), index ->
            index to percent
        }.map {
            it.first to it.second * n
        }.onEach { (i, value) ->
            if (value < 1.0) small.add(i) else large.add(i)
        }.map {
            it.second
        }.toTypedArray()

        while (small.isNotEmpty() && large.isNotEmpty()) {
            val l = small.removeAt(0)
            val g = large.removeAt(0)
            prob[l] = scaledProbs[l]
            alias[l] = g
            val pgtemp = scaledProbs[g] + scaledProbs[l] - 1
            scaledProbs[g] = pgtemp
            if (pgtemp < 1) small.add(g) else large.add(g)
        }

        while (large.isNotEmpty()) {
            val g = large.removeAt(0)
            prob[g] = 1.0
        }

        while (small.isNotEmpty()) {
            val l = small.removeAt(0)
            prob[l] = 1.0
        }
    }

    fun roll(): T = random.nextInt(n).let { i ->
        if (flipCoin(prob[i], random)) values[i] else values[alias[i]]
    }
}

fun flipCoin(trueProbability: Double, random: Random = Random()): Boolean {
    return WeightedCoin(trueProbability, random).flip()
}

fun intWeightedDice(probabilities: List<Double>, random: Random = Random()) =
    WeightedDie(probabilities.indices.toList().zip(probabilities).toMap(), random)

Compiled from "WeightedRandom.kt"
public final class com.github.pjozsef.WeightedRandomKt {
  public static final boolean flipCoin(double, java.util.Random);
    Code:
       0: aload_2
       1: ldc           #9                  // String random
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: new           #17                 // class com/github/pjozsef/WeightedCoin
       9: dup
      10: dload_0
      11: aload_2
      12: invokespecial #21                 // Method com/github/pjozsef/WeightedCoin."<init>":(DLjava/util/Random;)V
      15: invokevirtual #25                 // Method com/github/pjozsef/WeightedCoin.flip:()Z
      18: ireturn

  public static boolean flipCoin$default(double, java.util.Random, int, java.lang.Object);
    Code:
       0: iload_3
       1: iconst_2
       2: iand
       3: ifeq          14
       6: new           #32                 // class java/util/Random
       9: dup
      10: invokespecial #35                 // Method java/util/Random."<init>":()V
      13: astore_2
      14: dload_0
      15: aload_2
      16: invokestatic  #37                 // Method flipCoin:(DLjava/util/Random;)Z
      19: ireturn

  public static final com.github.pjozsef.WeightedDie<java.lang.Integer> intWeightedDice(java.util.List<java.lang.Double>, java.util.Random);
    Code:
       0: aload_0
       1: ldc           #42                 // String probabilities
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_1
       7: ldc           #9                  // String random
       9: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
      12: new           #44                 // class com/github/pjozsef/WeightedDie
      15: dup
      16: aload_0
      17: checkcast     #46                 // class java/util/Collection
      20: invokestatic  #52                 // Method kotlin/collections/CollectionsKt.getIndices:(Ljava/util/Collection;)Lkotlin/ranges/IntRange;
      23: checkcast     #54                 // class java/lang/Iterable
      26: invokestatic  #58                 // Method kotlin/collections/CollectionsKt.toList:(Ljava/lang/Iterable;)Ljava/util/List;
      29: checkcast     #54                 // class java/lang/Iterable
      32: aload_0
      33: checkcast     #54                 // class java/lang/Iterable
      36: invokestatic  #62                 // Method kotlin/collections/CollectionsKt.zip:(Ljava/lang/Iterable;Ljava/lang/Iterable;)Ljava/util/List;
      39: checkcast     #54                 // class java/lang/Iterable
      42: invokestatic  #68                 // Method kotlin/collections/MapsKt.toMap:(Ljava/lang/Iterable;)Ljava/util/Map;
      45: aload_1
      46: invokespecial #71                 // Method com/github/pjozsef/WeightedDie."<init>":(Ljava/util/Map;Ljava/util/Random;)V
      49: areturn

  public static com.github.pjozsef.WeightedDie intWeightedDice$default(java.util.List, java.util.Random, int, java.lang.Object);
    Code:
       0: iload_2
       1: iconst_2
       2: iand
       3: ifeq          14
       6: new           #32                 // class java/util/Random
       9: dup
      10: invokespecial #35                 // Method java/util/Random."<init>":()V
      13: astore_1
      14: aload_0
      15: aload_1
      16: invokestatic  #76                 // Method intWeightedDice:(Ljava/util/List;Ljava/util/Random;)Lcom/github/pjozsef/WeightedDie;
      19: areturn
}


package chjaeggi

class Day9(private val input: List<Long>, private val preAmble: Int) {

    fun solvePart1(): Long {
        input.forEachIndexed { index, _ ->
            if (index < (preAmble)) {
                return@forEachIndexed
            } else if (index + 1 >= input.size) {
                return@forEachIndexed
            } else {
                val expected = input[index + 1]
                val subList = input.subList(index - (preAmble - 1), index + 1)
                if (!subList.hasSumInTwo(expected)) {
                    return expected
                }
            }
        }
        return -1
    }

    fun solvePart2(): Long {
        return input.hasSumInRange(solvePart1())
    }
}

fun List<Long>.hasSumInTwo(expectedSum: Long): Boolean {
    for (first in this.indices) {
        for (second in this.indices) {
            if (second == first) {
                continue
            }
            if ((this[first] + this[second]) == expectedSum) {
                return true
            }
        }
    }
    return false
}

fun List<Long>.hasSumInRange(expectedSum: Long): Long {

    var first = 0
    var second = 0
    var result = 0L

    fun reset() {
        first++
        second = first
        result = 0L
    }

    while (first < this.size) {
        if (second >= this.size) {
            reset()
            continue
        }
        result += this[second]
        if (result == expectedSum) {
            return (this.subList(first, second).minOrNull() ?: -1) + (this.subList(first, second).maxOrNull() ?: -1)
        }
        if (result > expectedSum) {
            reset()
            continue
        }
        second++
    }
    return -1
}

Compiled from "Day9.kt"
public final class chjaeggi.Day9Kt {
  public static final boolean hasSumInTwo(java.util.List<java.lang.Long>, long);
    Code:
       0: aload_0
       1: ldc           #10                 // String <this>
       3: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: iconst_0
       7: istore_3
       8: aload_0
       9: checkcast     #18                 // class java/util/Collection
      12: invokeinterface #22,  1           // InterfaceMethod java/util/Collection.size:()I
      17: istore        4
      19: iload_3
      20: iload         4
      22: if_icmpge     102
      25: iconst_0
      26: istore        5
      28: aload_0
      29: checkcast     #18                 // class java/util/Collection
      32: invokeinterface #22,  1           // InterfaceMethod java/util/Collection.size:()I
      37: istore        6
      39: iload         5
      41: iload         6
      43: if_icmpge     96
      46: iload         5
      48: iload_3
      49: if_icmpne     55
      52: goto          90
      55: aload_0
      56: iload_3
      57: invokeinterface #28,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      62: checkcast     #30                 // class java/lang/Number
      65: invokevirtual #34                 // Method java/lang/Number.longValue:()J
      68: aload_0
      69: iload         5
      71: invokeinterface #28,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      76: checkcast     #30                 // class java/lang/Number
      79: invokevirtual #34                 // Method java/lang/Number.longValue:()J
      82: ladd
      83: lload_1
      84: lcmp
      85: ifne          90
      88: iconst_1
      89: ireturn
      90: iinc          5, 1
      93: goto          39
      96: iinc          3, 1
      99: goto          19
     102: iconst_0
     103: ireturn

  public static final long hasSumInRange(java.util.List<java.lang.Long>, long);
    Code:
       0: aload_0
       1: ldc           #10                 // String <this>
       3: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: new           #46                 // class kotlin/jvm/internal/Ref$IntRef
       9: dup
      10: invokespecial #50                 // Method kotlin/jvm/internal/Ref$IntRef."<init>":()V
      13: astore_3
      14: new           #46                 // class kotlin/jvm/internal/Ref$IntRef
      17: dup
      18: invokespecial #50                 // Method kotlin/jvm/internal/Ref$IntRef."<init>":()V
      21: astore        4
      23: new           #52                 // class kotlin/jvm/internal/Ref$LongRef
      26: dup
      27: invokespecial #53                 // Method kotlin/jvm/internal/Ref$LongRef."<init>":()V
      30: astore        5
      32: aload_3
      33: getfield      #56                 // Field kotlin/jvm/internal/Ref$IntRef.element:I
      36: aload_0
      37: invokeinterface #57,  1           // InterfaceMethod java/util/List.size:()I
      42: if_icmpge     226
      45: aload         4
      47: getfield      #56                 // Field kotlin/jvm/internal/Ref$IntRef.element:I
      50: aload_0
      51: invokeinterface #57,  1           // InterfaceMethod java/util/List.size:()I
      56: if_icmplt     70
      59: aload_3
      60: aload         4
      62: aload         5
      64: invokestatic  #61                 // Method hasSumInRange$reset:(Lkotlin/jvm/internal/Ref$IntRef;Lkotlin/jvm/internal/Ref$IntRef;Lkotlin/jvm/internal/Ref$LongRef;)V
      67: goto          32
      70: aload         5
      72: aload         5
      74: getfield      #63                 // Field kotlin/jvm/internal/Ref$LongRef.element:J
      77: aload_0
      78: aload         4
      80: getfield      #56                 // Field kotlin/jvm/internal/Ref$IntRef.element:I
      83: invokeinterface #28,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      88: checkcast     #30                 // class java/lang/Number
      91: invokevirtual #34                 // Method java/lang/Number.longValue:()J
      94: ladd
      95: putfield      #63                 // Field kotlin/jvm/internal/Ref$LongRef.element:J
      98: aload         5
     100: getfield      #63                 // Field kotlin/jvm/internal/Ref$LongRef.element:J
     103: lload_1
     104: lcmp
     105: ifne          186
     108: aload_0
     109: aload_3
     110: getfield      #56                 // Field kotlin/jvm/internal/Ref$IntRef.element:I
     113: aload         4
     115: getfield      #56                 // Field kotlin/jvm/internal/Ref$IntRef.element:I
     118: invokeinterface #67,  3           // InterfaceMethod java/util/List.subList:(II)Ljava/util/List;
     123: checkcast     #69                 // class java/lang/Iterable
     126: invokestatic  #75                 // Method kotlin/collections/CollectionsKt.minOrNull:(Ljava/lang/Iterable;)Ljava/lang/Comparable;
     129: checkcast     #77                 // class java/lang/Long
     132: dup
     133: ifnull        142
     136: invokevirtual #78                 // Method java/lang/Long.longValue:()J
     139: goto          146
     142: pop
     143: ldc2_w        #79                 // long -1l
     146: aload_0
     147: aload_3
     148: getfield      #56                 // Field kotlin/jvm/internal/Ref$IntRef.element:I
     151: aload         4
     153: getfield      #56                 // Field kotlin/jvm/internal/Ref$IntRef.element:I
     156: invokeinterface #67,  3           // InterfaceMethod java/util/List.subList:(II)Ljava/util/List;
     161: checkcast     #69                 // class java/lang/Iterable
     164: invokestatic  #83                 // Method kotlin/collections/CollectionsKt.maxOrNull:(Ljava/lang/Iterable;)Ljava/lang/Comparable;
     167: checkcast     #77                 // class java/lang/Long
     170: dup
     171: ifnull        180
     174: invokevirtual #78                 // Method java/lang/Long.longValue:()J
     177: goto          184
     180: pop
     181: ldc2_w        #79                 // long -1l
     184: ladd
     185: lreturn
     186: aload         5
     188: getfield      #63                 // Field kotlin/jvm/internal/Ref$LongRef.element:J
     191: lload_1
     192: lcmp
     193: ifle          207
     196: aload_3
     197: aload         4
     199: aload         5
     201: invokestatic  #61                 // Method hasSumInRange$reset:(Lkotlin/jvm/internal/Ref$IntRef;Lkotlin/jvm/internal/Ref$IntRef;Lkotlin/jvm/internal/Ref$LongRef;)V
     204: goto          32
     207: aload         4
     209: getfield      #56                 // Field kotlin/jvm/internal/Ref$IntRef.element:I
     212: istore        6
     214: aload         4
     216: iload         6
     218: iconst_1
     219: iadd
     220: putfield      #56                 // Field kotlin/jvm/internal/Ref$IntRef.element:I
     223: goto          32
     226: ldc2_w        #79                 // long -1l
     229: lreturn

  private static final void hasSumInRange$reset(kotlin.jvm.internal.Ref$IntRef, kotlin.jvm.internal.Ref$IntRef, kotlin.jvm.internal.Ref$LongRef);
    Code:
       0: aload_0
       1: getfield      #56                 // Field kotlin/jvm/internal/Ref$IntRef.element:I
       4: istore_3
       5: aload_0
       6: iload_3
       7: iconst_1
       8: iadd
       9: putfield      #56                 // Field kotlin/jvm/internal/Ref$IntRef.element:I
      12: aload_1
      13: aload_0
      14: getfield      #56                 // Field kotlin/jvm/internal/Ref$IntRef.element:I
      17: putfield      #56                 // Field kotlin/jvm/internal/Ref$IntRef.element:I
      20: aload_2
      21: lconst_0
      22: putfield      #63                 // Field kotlin/jvm/internal/Ref$LongRef.element:J
      25: return
}

Compiled from "Day9.kt"
public final class chjaeggi.Day9 {
  private final java.util.List<java.lang.Long> input;

  private final int preAmble;

  public chjaeggi.Day9(java.util.List<java.lang.Long>, int);
    Code:
       0: aload_1
       1: ldc           #10                 // String input
       3: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: invokespecial #19                 // Method java/lang/Object."<init>":()V
      10: aload_0
      11: aload_1
      12: putfield      #22                 // Field input:Ljava/util/List;
      15: aload_0
      16: iload_2
      17: putfield      #26                 // Field preAmble:I
      20: return

  public final long solvePart1();
    Code:
       0: aload_0
       1: getfield      #22                 // Field input:Ljava/util/List;
       4: checkcast     #32                 // class java/lang/Iterable
       7: astore_1
       8: iconst_0
       9: istore_2
      10: iconst_0
      11: istore_3
      12: aload_1
      13: invokeinterface #36,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      18: astore        4
      20: aload         4
      22: invokeinterface #42,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      27: ifeq          163
      30: aload         4
      32: invokeinterface #46,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      37: astore        5
      39: iload_3
      40: iinc          3, 1
      43: istore        6
      45: iload         6
      47: ifge          53
      50: invokestatic  #51                 // Method kotlin/collections/CollectionsKt.throwIndexOverflow:()V
      53: iload         6
      55: aload         5
      57: checkcast     #53                 // class java/lang/Number
      60: invokevirtual #56                 // Method java/lang/Number.longValue:()J
      63: pop2
      64: istore        7
      66: iconst_0
      67: istore        8
      69: iload         7
      71: aload_0
      72: getfield      #26                 // Field preAmble:I
      75: if_icmpge     81
      78: goto          159
      81: iload         7
      83: iconst_1
      84: iadd
      85: aload_0
      86: getfield      #22                 // Field input:Ljava/util/List;
      89: invokeinterface #62,  1           // InterfaceMethod java/util/List.size:()I
      94: if_icmplt     100
      97: goto          159
     100: aload_0
     101: getfield      #22                 // Field input:Ljava/util/List;
     104: iload         7
     106: iconst_1
     107: iadd
     108: invokeinterface #66,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     113: checkcast     #53                 // class java/lang/Number
     116: invokevirtual #56                 // Method java/lang/Number.longValue:()J
     119: lstore        9
     121: aload_0
     122: getfield      #22                 // Field input:Ljava/util/List;
     125: iload         7
     127: aload_0
     128: getfield      #26                 // Field preAmble:I
     131: iconst_1
     132: isub
     133: isub
     134: iload         7
     136: iconst_1
     137: iadd
     138: invokeinterface #70,  3           // InterfaceMethod java/util/List.subList:(II)Ljava/util/List;
     143: astore        11
     145: aload         11
     147: lload         9
     149: invokestatic  #76                 // Method chjaeggi/Day9Kt.hasSumInTwo:(Ljava/util/List;J)Z
     152: ifne          158
     155: lload         9
     157: lreturn
     158: nop
     159: nop
     160: goto          20
     163: nop
     164: ldc2_w        #77                 // long -1l
     167: lreturn

  public final long solvePart2();
    Code:
       0: aload_0
       1: getfield      #22                 // Field input:Ljava/util/List;
       4: aload_0
       5: invokevirtual #91                 // Method solvePart1:()J
       8: invokestatic  #95                 // Method chjaeggi/Day9Kt.hasSumInRange:(Ljava/util/List;J)J
      11: lreturn
}


package chjaeggi

class Day7(private val input: List<String>) {

    private val parentsPerBag: MutableMap<String, List<String>> = mutableMapOf()
    private val childrenPerBag: MutableMap<String, MutableMap<String, Int>> = mutableMapOf()

    init {
        input.forEach {
            val splitted = it.split("contain")
            val key = splitted[0].replace(" bags", "").dropLast(1)
            Regex("[a-z]+ [a-z]+,|[a-z]+ [a-z]+.").findAll(splitted[1])
                .forEach {
                    val value = it.value.dropLast(1)
                    if (parentsPerBag.containsKey(value)) {
                        parentsPerBag[value] = parentsPerBag[value]!! + key
                    } else {
                        parentsPerBag[value] = listOf(key)
                    }
                }
            Regex("[0-9]+ [a-z]+ [a-z]+,|[0-9]+ [a-z]+ [a-z]+.|no other ").findAll(splitted[1])
                .forEach {
                    val valueString = it.value.dropLast(1)
                    if (valueString.contains("no other")) {
                        return@forEach
                    }
                    val amount = valueString.substringBefore(" ").toInt()
                    val value = valueString.dropWhile { !it.isLetter() }
                    if (!childrenPerBag.containsKey(key)) {
                        childrenPerBag[key] = mutableMapOf()
                    }
                    childrenPerBag[key]!![value] = amount
                }
        }
    }

    fun solvePart1(): Int {
        return parentsPerBag["shiny gold"]?.flatMap { getParents(it) }?.toSet()?.count() ?: -1
    }

    fun solvePart2(): Int {
        return getCosts("shiny gold", 1)
    }

    private fun getParents(bag: String): List<String> {
        return when (parentsPerBag.containsKey(bag)) {
            false -> listOf(bag)
            true -> listOf(bag) + parentsPerBag[bag]!!.flatMap { getParents(it) }
        }
    }

    private fun getCosts(bag: String, amount: Int): Int {
        return amount * getSameLevelChildrenCost(bag) +
                (getNextLevelChildren(bag)?.map {
                    amount * getCosts(it.key, it.value)
                }?.sum() ?: 0)
    }

    private fun getNextLevelChildren(bag: String): Map<String, Int>? {
        return childrenPerBag[bag]
    }

    private fun getSameLevelChildrenCost(bag: String): Int {
        return childrenPerBag[bag]?.values?.sum() ?: 0
    }
}

Compiled from "Day7.kt"
public final class chjaeggi.Day7 {
  private final java.util.List<java.lang.String> input;

  private final java.util.Map<java.lang.String, java.util.List<java.lang.String>> parentsPerBag;

  private final java.util.Map<java.lang.String, java.util.Map<java.lang.String, java.lang.Integer>> childrenPerBag;

  public chjaeggi.Day7(java.util.List<java.lang.String>);
    Code:
       0: aload_1
       1: ldc           #10                 // String input
       3: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: invokespecial #19                 // Method java/lang/Object."<init>":()V
      10: aload_0
      11: aload_1
      12: putfield      #22                 // Field input:Ljava/util/List;
      15: aload_0
      16: new           #24                 // class java/util/LinkedHashMap
      19: dup
      20: invokespecial #25                 // Method java/util/LinkedHashMap."<init>":()V
      23: checkcast     #27                 // class java/util/Map
      26: putfield      #31                 // Field parentsPerBag:Ljava/util/Map;
      29: aload_0
      30: new           #24                 // class java/util/LinkedHashMap
      33: dup
      34: invokespecial #25                 // Method java/util/LinkedHashMap."<init>":()V
      37: checkcast     #27                 // class java/util/Map
      40: putfield      #34                 // Field childrenPerBag:Ljava/util/Map;
      43: nop
      44: aload_0
      45: getfield      #22                 // Field input:Ljava/util/List;
      48: checkcast     #36                 // class java/lang/Iterable
      51: astore_2
      52: iconst_0
      53: istore_3
      54: aload_2
      55: invokeinterface #40,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      60: astore        4
      62: aload         4
      64: invokeinterface #46,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      69: ifeq          591
      72: aload         4
      74: invokeinterface #50,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      79: astore        5
      81: aload         5
      83: checkcast     #52                 // class java/lang/String
      86: astore        6
      88: iconst_0
      89: istore        7
      91: aload         6
      93: checkcast     #54                 // class java/lang/CharSequence
      96: iconst_1
      97: anewarray     #52                 // class java/lang/String
     100: astore        8
     102: aload         8
     104: iconst_0
     105: ldc           #56                 // String contain
     107: aastore
     108: aload         8
     110: iconst_0
     111: iconst_0
     112: bipush        6
     114: aconst_null
     115: invokestatic  #62                 // Method kotlin/text/StringsKt.split$default:(Ljava/lang/CharSequence;[Ljava/lang/String;ZIILjava/lang/Object;)Ljava/util/List;
     118: astore        9
     120: aload         9
     122: iconst_0
     123: invokeinterface #68,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     128: checkcast     #52                 // class java/lang/String
     131: ldc           #70                 // String  bags
     133: ldc           #72                 // String
     135: iconst_0
     136: iconst_4
     137: aconst_null
     138: invokestatic  #76                 // Method kotlin/text/StringsKt.replace$default:(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;ZILjava/lang/Object;)Ljava/lang/String;
     141: iconst_1
     142: invokestatic  #80                 // Method kotlin/text/StringsKt.dropLast:(Ljava/lang/String;I)Ljava/lang/String;
     145: astore        8
     147: new           #82                 // class kotlin/text/Regex
     150: dup
     151: ldc           #84                 // String [a-z]+ [a-z]+,|[a-z]+ [a-z]+.
     153: invokespecial #87                 // Method kotlin/text/Regex."<init>":(Ljava/lang/String;)V
     156: aload         9
     158: iconst_1
     159: invokeinterface #68,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     164: checkcast     #54                 // class java/lang/CharSequence
     167: iconst_0
     168: iconst_2
     169: aconst_null
     170: invokestatic  #91                 // Method kotlin/text/Regex.findAll$default:(Lkotlin/text/Regex;Ljava/lang/CharSequence;IILjava/lang/Object;)Lkotlin/sequences/Sequence;
     173: astore        10
     175: nop
     176: iconst_0
     177: istore        11
     179: aload         10
     181: invokeinterface #94,  1           // InterfaceMethod kotlin/sequences/Sequence.iterator:()Ljava/util/Iterator;
     186: astore        12
     188: aload         12
     190: invokeinterface #46,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     195: ifeq          304
     198: aload         12
     200: invokeinterface #50,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     205: astore        13
     207: aload         13
     209: checkcast     #96                 // class kotlin/text/MatchResult
     212: astore        14
     214: iconst_0
     215: istore        15
     217: aload         14
     219: invokeinterface #100,  1          // InterfaceMethod kotlin/text/MatchResult.getValue:()Ljava/lang/String;
     224: iconst_1
     225: invokestatic  #80                 // Method kotlin/text/StringsKt.dropLast:(Ljava/lang/String;I)Ljava/lang/String;
     228: astore        16
     230: aload_0
     231: getfield      #31                 // Field parentsPerBag:Ljava/util/Map;
     234: aload         16
     236: invokeinterface #104,  2          // InterfaceMethod java/util/Map.containsKey:(Ljava/lang/Object;)Z
     241: ifeq          282
     244: aload_0
     245: getfield      #31                 // Field parentsPerBag:Ljava/util/Map;
     248: aload         16
     250: aload_0
     251: getfield      #31                 // Field parentsPerBag:Ljava/util/Map;
     254: aload         16
     256: invokeinterface #107,  2          // InterfaceMethod java/util/Map.get:(Ljava/lang/Object;)Ljava/lang/Object;
     261: dup
     262: invokestatic  #111                // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
     265: checkcast     #113                // class java/util/Collection
     268: aload         8
     270: invokestatic  #119                // Method kotlin/collections/CollectionsKt.plus:(Ljava/util/Collection;Ljava/lang/Object;)Ljava/util/List;
     273: invokeinterface #123,  3          // InterfaceMethod java/util/Map.put:(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
     278: pop
     279: goto          299
     282: aload_0
     283: getfield      #31                 // Field parentsPerBag:Ljava/util/Map;
     286: aload         16
     288: aload         8
     290: invokestatic  #127                // Method kotlin/collections/CollectionsKt.listOf:(Ljava/lang/Object;)Ljava/util/List;
     293: invokeinterface #123,  3          // InterfaceMethod java/util/Map.put:(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
     298: pop
     299: nop
     300: nop
     301: goto          188
     304: nop
     305: new           #82                 // class kotlin/text/Regex
     308: dup
     309: ldc           #129                // String [0-9]+ [a-z]+ [a-z]+,|[0-9]+ [a-z]+ [a-z]+.|no other
     311: invokespecial #87                 // Method kotlin/text/Regex."<init>":(Ljava/lang/String;)V
     314: aload         9
     316: iconst_1
     317: invokeinterface #68,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     322: checkcast     #54                 // class java/lang/CharSequence
     325: iconst_0
     326: iconst_2
     327: aconst_null
     328: invokestatic  #91                 // Method kotlin/text/Regex.findAll$default:(Lkotlin/text/Regex;Ljava/lang/CharSequence;IILjava/lang/Object;)Lkotlin/sequences/Sequence;
     331: astore        10
     333: nop
     334: iconst_0
     335: istore        11
     337: aload         10
     339: invokeinterface #94,  1           // InterfaceMethod kotlin/sequences/Sequence.iterator:()Ljava/util/Iterator;
     344: astore        12
     346: aload         12
     348: invokeinterface #46,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     353: ifeq          585
     356: aload         12
     358: invokeinterface #50,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     363: astore        13
     365: aload         13
     367: checkcast     #96                 // class kotlin/text/MatchResult
     370: astore        14
     372: iconst_0
     373: istore        15
     375: aload         14
     377: invokeinterface #100,  1          // InterfaceMethod kotlin/text/MatchResult.getValue:()Ljava/lang/String;
     382: iconst_1
     383: invokestatic  #80                 // Method kotlin/text/StringsKt.dropLast:(Ljava/lang/String;I)Ljava/lang/String;
     386: astore        16
     388: aload         16
     390: checkcast     #54                 // class java/lang/CharSequence
     393: ldc           #131                // String no other
     395: checkcast     #54                 // class java/lang/CharSequence
     398: iconst_0
     399: iconst_2
     400: aconst_null
     401: invokestatic  #135                // Method kotlin/text/StringsKt.contains$default:(Ljava/lang/CharSequence;Ljava/lang/CharSequence;ZILjava/lang/Object;)Z
     404: ifeq          410
     407: goto          581
     410: aload         16
     412: ldc           #137                // String
     414: aconst_null
     415: iconst_2
     416: aconst_null
     417: invokestatic  #141                // Method kotlin/text/StringsKt.substringBefore$default:(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;ILjava/lang/Object;)Ljava/lang/String;
     420: invokestatic  #147                // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
     423: istore        17
     425: aload         16
     427: astore        18
     429: iconst_0
     430: istore        19
     432: iconst_0
     433: istore        20
     435: aload         18
     437: checkcast     #54                 // class java/lang/CharSequence
     440: invokeinterface #151,  1          // InterfaceMethod java/lang/CharSequence.length:()I
     445: istore        21
     447: iload         20
     449: iload         21
     451: if_icmpge     504
     454: aload         18
     456: iload         20
     458: invokevirtual #155                // Method java/lang/String.charAt:(I)C
     461: istore        22
     463: iconst_0
     464: istore        23
     466: iload         22
     468: invokestatic  #161                // Method java/lang/Character.isLetter:(C)Z
     471: ifne          478
     474: iconst_1
     475: goto          479
     478: iconst_0
     479: ifne          498
     482: aload         18
     484: iload         20
     486: invokevirtual #165                // Method java/lang/String.substring:(I)Ljava/lang/String;
     489: dup
     490: ldc           #167                // String substring(...)
     492: invokestatic  #170                // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
     495: goto          506
     498: iinc          20, 1
     501: goto          447
     504: ldc           #72                 // String
     506: astore        24
     508: aload_0
     509: getfield      #34                 // Field childrenPerBag:Ljava/util/Map;
     512: aload         8
     514: invokeinterface #104,  2          // InterfaceMethod java/util/Map.containsKey:(Ljava/lang/Object;)Z
     519: ifne          545
     522: nop
     523: aload_0
     524: getfield      #34                 // Field childrenPerBag:Ljava/util/Map;
     527: aload         8
     529: new           #24                 // class java/util/LinkedHashMap
     532: dup
     533: invokespecial #25                 // Method java/util/LinkedHashMap."<init>":()V
     536: checkcast     #27                 // class java/util/Map
     539: invokeinterface #123,  3          // InterfaceMethod java/util/Map.put:(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
     544: pop
     545: iload         17
     547: invokestatic  #174                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     550: astore        19
     552: aload_0
     553: getfield      #34                 // Field childrenPerBag:Ljava/util/Map;
     556: aload         8
     558: invokeinterface #107,  2          // InterfaceMethod java/util/Map.get:(Ljava/lang/Object;)Ljava/lang/Object;
     563: dup
     564: invokestatic  #111                // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
     567: checkcast     #27                 // class java/util/Map
     570: aload         24
     572: aload         19
     574: invokeinterface #123,  3          // InterfaceMethod java/util/Map.put:(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
     579: pop
     580: nop
     581: nop
     582: goto          346
     585: nop
     586: nop
     587: nop
     588: goto          62
     591: nop
     592: nop
     593: return

  public final int solvePart1();
    Code:
       0: aload_0
       1: getfield      #31                 // Field parentsPerBag:Ljava/util/Map;
       4: ldc           #202                // String shiny gold
       6: invokeinterface #107,  2          // InterfaceMethod java/util/Map.get:(Ljava/lang/Object;)Ljava/lang/Object;
      11: checkcast     #64                 // class java/util/List
      14: dup
      15: ifnull        127
      18: checkcast     #36                 // class java/lang/Iterable
      21: astore_1
      22: iconst_0
      23: istore_2
      24: aload_1
      25: astore_3
      26: new           #204                // class java/util/ArrayList
      29: dup
      30: invokespecial #205                // Method java/util/ArrayList."<init>":()V
      33: checkcast     #113                // class java/util/Collection
      36: astore        4
      38: iconst_0
      39: istore        5
      41: aload_3
      42: invokeinterface #40,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      47: astore        6
      49: aload         6
      51: invokeinterface #46,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      56: ifeq          100
      59: aload         6
      61: invokeinterface #50,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      66: astore        7
      68: aload         7
      70: checkcast     #52                 // class java/lang/String
      73: astore        8
      75: iconst_0
      76: istore        9
      78: aload_0
      79: aload         8
      81: invokespecial #209                // Method getParents:(Ljava/lang/String;)Ljava/util/List;
      84: checkcast     #36                 // class java/lang/Iterable
      87: astore        8
      89: aload         4
      91: aload         8
      93: invokestatic  #213                // Method kotlin/collections/CollectionsKt.addAll:(Ljava/util/Collection;Ljava/lang/Iterable;)Z
      96: pop
      97: goto          49
     100: aload         4
     102: checkcast     #64                 // class java/util/List
     105: nop
     106: checkcast     #36                 // class java/lang/Iterable
     109: invokestatic  #217                // Method kotlin/collections/CollectionsKt.toSet:(Ljava/lang/Iterable;)Ljava/util/Set;
     112: dup
     113: ifnull        127
     116: checkcast     #113                // class java/util/Collection
     119: invokeinterface #220,  1          // InterfaceMethod java/util/Collection.size:()I
     124: goto          129
     127: pop
     128: iconst_m1
     129: ireturn

  public final int solvePart2();
    Code:
       0: aload_0
       1: ldc           #202                // String shiny gold
       3: iconst_1
       4: invokespecial #234                // Method getCosts:(Ljava/lang/String;I)I
       7: ireturn

  private final java.util.List<java.lang.String> getParents(java.lang.String);
    Code:
       0: aload_0
       1: getfield      #31                 // Field parentsPerBag:Ljava/util/Map;
       4: aload_1
       5: invokeinterface #104,  2          // InterfaceMethod java/util/Map.containsKey:(Ljava/lang/Object;)Z
      10: istore_2
      11: iload_2
      12: ifne          22
      15: aload_1
      16: invokestatic  #127                // Method kotlin/collections/CollectionsKt.listOf:(Ljava/lang/Object;)Ljava/util/List;
      19: goto          161
      22: iload_2
      23: iconst_1
      24: if_icmpne     153
      27: aload_1
      28: invokestatic  #127                // Method kotlin/collections/CollectionsKt.listOf:(Ljava/lang/Object;)Ljava/util/List;
      31: checkcast     #113                // class java/util/Collection
      34: aload_0
      35: getfield      #31                 // Field parentsPerBag:Ljava/util/Map;
      38: aload_1
      39: invokeinterface #107,  2          // InterfaceMethod java/util/Map.get:(Ljava/lang/Object;)Ljava/lang/Object;
      44: dup
      45: invokestatic  #111                // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
      48: checkcast     #36                 // class java/lang/Iterable
      51: astore_3
      52: astore        12
      54: iconst_0
      55: istore        4
      57: aload_3
      58: astore        5
      60: new           #204                // class java/util/ArrayList
      63: dup
      64: invokespecial #205                // Method java/util/ArrayList."<init>":()V
      67: checkcast     #113                // class java/util/Collection
      70: astore        6
      72: iconst_0
      73: istore        7
      75: aload         5
      77: invokeinterface #40,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      82: astore        8
      84: aload         8
      86: invokeinterface #46,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      91: ifeq          135
      94: aload         8
      96: invokeinterface #50,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     101: astore        9
     103: aload         9
     105: checkcast     #52                 // class java/lang/String
     108: astore        10
     110: iconst_0
     111: istore        11
     113: aload_0
     114: aload         10
     116: invokespecial #209                // Method getParents:(Ljava/lang/String;)Ljava/util/List;
     119: checkcast     #36                 // class java/lang/Iterable
     122: astore        10
     124: aload         6
     126: aload         10
     128: invokestatic  #213                // Method kotlin/collections/CollectionsKt.addAll:(Ljava/util/Collection;Ljava/lang/Iterable;)Z
     131: pop
     132: goto          84
     135: aload         6
     137: checkcast     #64                 // class java/util/List
     140: nop
     141: aload         12
     143: swap
     144: checkcast     #36                 // class java/lang/Iterable
     147: invokestatic  #238                // Method kotlin/collections/CollectionsKt.plus:(Ljava/util/Collection;Ljava/lang/Iterable;)Ljava/util/List;
     150: goto          161
     153: new           #240                // class kotlin/NoWhenBranchMatchedException
     156: dup
     157: invokespecial #241                // Method kotlin/NoWhenBranchMatchedException."<init>":()V
     160: athrow
     161: areturn

  private final int getCosts(java.lang.String, int);
    Code:
       0: iload_2
       1: aload_0
       2: aload_1
       3: invokespecial #246                // Method getSameLevelChildrenCost:(Ljava/lang/String;)I
       6: imul
       7: aload_0
       8: aload_1
       9: invokespecial #250                // Method getNextLevelChildren:(Ljava/lang/String;)Ljava/util/Map;
      12: dup
      13: ifnull        158
      16: astore_3
      17: istore        12
      19: iconst_0
      20: istore        4
      22: aload_3
      23: astore        5
      25: new           #204                // class java/util/ArrayList
      28: dup
      29: aload_3
      30: invokeinterface #251,  1          // InterfaceMethod java/util/Map.size:()I
      35: invokespecial #254                // Method java/util/ArrayList."<init>":(I)V
      38: checkcast     #113                // class java/util/Collection
      41: astore        6
      43: iconst_0
      44: istore        7
      46: aload         5
      48: invokeinterface #258,  1          // InterfaceMethod java/util/Map.entrySet:()Ljava/util/Set;
      53: invokeinterface #261,  1          // InterfaceMethod java/util/Set.iterator:()Ljava/util/Iterator;
      58: astore        8
      60: aload         8
      62: invokeinterface #46,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      67: ifeq          137
      70: aload         8
      72: invokeinterface #50,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      77: checkcast     #263                // class java/util/Map$Entry
      80: astore        9
      82: aload         6
      84: aload         9
      86: astore        10
      88: astore        13
      90: iconst_0
      91: istore        11
      93: iload_2
      94: aload_0
      95: aload         10
      97: invokeinterface #266,  1          // InterfaceMethod java/util/Map$Entry.getKey:()Ljava/lang/Object;
     102: checkcast     #52                 // class java/lang/String
     105: aload         10
     107: invokeinterface #268,  1          // InterfaceMethod java/util/Map$Entry.getValue:()Ljava/lang/Object;
     112: checkcast     #270                // class java/lang/Number
     115: invokevirtual #273                // Method java/lang/Number.intValue:()I
     118: invokespecial #234                // Method getCosts:(Ljava/lang/String;I)I
     121: imul
     122: invokestatic  #174                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     125: aload         13
     127: swap
     128: invokeinterface #276,  2          // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     133: pop
     134: goto          60
     137: aload         6
     139: checkcast     #64                 // class java/util/List
     142: nop
     143: astore        13
     145: iload         12
     147: aload         13
     149: checkcast     #36                 // class java/lang/Iterable
     152: invokestatic  #280                // Method kotlin/collections/CollectionsKt.sumOfInt:(Ljava/lang/Iterable;)I
     155: goto          160
     158: pop
     159: iconst_0
     160: iadd
     161: ireturn

  private final java.util.Map<java.lang.String, java.lang.Integer> getNextLevelChildren(java.lang.String);
    Code:
       0: aload_0
       1: getfield      #34                 // Field childrenPerBag:Ljava/util/Map;
       4: aload_1
       5: invokeinterface #107,  2          // InterfaceMethod java/util/Map.get:(Ljava/lang/Object;)Ljava/lang/Object;
      10: checkcast     #27                 // class java/util/Map
      13: areturn

  private final int getSameLevelChildrenCost(java.lang.String);
    Code:
       0: aload_0
       1: getfield      #34                 // Field childrenPerBag:Ljava/util/Map;
       4: aload_1
       5: invokeinterface #107,  2          // InterfaceMethod java/util/Map.get:(Ljava/lang/Object;)Ljava/lang/Object;
      10: checkcast     #27                 // class java/util/Map
      13: dup
      14: ifnull        35
      17: invokeinterface #292,  1          // InterfaceMethod java/util/Map.values:()Ljava/util/Collection;
      22: dup
      23: ifnull        35
      26: checkcast     #36                 // class java/lang/Iterable
      29: invokestatic  #280                // Method kotlin/collections/CollectionsKt.sumOfInt:(Ljava/lang/Iterable;)I
      32: goto          37
      35: pop
      36: iconst_0
      37: ireturn
}