package day19

import java.io.File

fun main() {
    val (workflows, parts) = parse("src/day19/Day19.txt")

    println("🎄 Day 19 🎄")

    println()

    println("[Part 1]")
    println("Answer: ${part1(workflows, parts)}")

    println()

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

private data class Condition(
    val key: Char,
    val cmp: Char,
    val num: Int,
)

private data class Rule(
    val condition: Condition?,
    val transition: String,
)

private typealias Part = Map<Char, Int>
private typealias Workflows = Map<String, List<Rule>>

private fun String.toPart(): Part =
    this.trim('{', '}')
        .split(',')
        .map { it.split('=') }
        .associate { (k, n) -> k.first() to n.toInt() }

private fun String.toRule(): Rule {
    if (!this.contains(':')) {
        return Rule(condition = null, transition = this)
    }

    val (lhs, transition) = this.split(':')
    val condition = Condition(
        key = lhs[0],
        cmp = lhs[1],
        num = lhs.substring(2).toInt(),
    )

    return Rule(condition, transition)
}

private fun String.toWorkflow(): Pair<String, List<Rule>> {
    val (name, rules) = this.trim('}').split('{')
    return Pair(name, rules.split(',').map(String::toRule))
}

private fun parse(path: String): Pair<Workflows, List<Part>> {
    val (workflowsPart, partsPart) = File(path)
        .readText()
        .split("\n\n", "\r\n\r\n")

    val workflows = workflowsPart.lines()
        .map(String::toWorkflow)
        .associate { (name, rules) -> name to rules }

    val parts = partsPart.lines()
        .map(String::toPart)

    return Pair(workflows, parts)
}

private fun count(part: Part, workflows: Workflows): Int {
    var target = "in"
    while (target != "A" && target != "R") {
        val rules = workflows.getValue(target)
        for ((condition, transition) in rules) {
            if (condition == null) {
                target = transition
                break
            }

            val (key, cmp, num) = condition
            val satisfies = if (cmp == '>') {
                part.getValue(key) > num
            } else {
                part.getValue(key) < num
            }

            if (satisfies) {
                target = transition
                break
            }
        }
    }

    return if (target == "A") {
        part.values.sum()
    } else {
        0
    }
}

private fun part1(workflows: Workflows, parts: List<Part>): Int =
    parts.sumOf { count(it, workflows) }

private fun part2(workflows: Workflows): Long {
    val queue = ArrayDeque<Pair<String, Map<Char, IntRange>>>()
        .apply { add("in" to mapOf(
            'x' to 1..4000,
            'm' to 1..4000,
            'a' to 1..4000,
            's' to 1..4000,
        )) }

    var total = 0L
    while (queue.isNotEmpty()) {
        val (target, startingRanges) = queue.removeFirst()
        if (target == "A" || target == "R") {
            if (target == "A") {
                val product = startingRanges.values
                    .map { (it.last - it.first + 1).toLong() }
                    .reduce { acc, it -> acc * it }

                total += product
            }
            continue
        }

        val ranges = startingRanges.toMutableMap()
        val rules = workflows.getValue(target)
        for ((condition, transition) in rules) {
            if (condition == null) {
                queue += transition to ranges
                break
            }

            val (key, cmp, num) = condition
            val range = ranges.getValue(key)

            val (t, f) = if (cmp == '>') {
                IntRange(num + 1, range.last) to IntRange(range.first, num)
            } else {
                IntRange(range.first, num - 1) to IntRange(num, range.last)
            }

            queue += transition to ranges.toMutableMap().apply { set(key, t) }
            ranges[key] = f
        }
    }

    return total
}

Compiled from "Day19.kt"
public final class day19.Day19Kt {
  public static final void main();
    Code:
       0: ldc           #8                  // String src/day19/Day19.txt
       2: invokestatic  #12                 // Method parse:(Ljava/lang/String;)Lkotlin/Pair;
       5: astore_0
       6: aload_0
       7: invokevirtual #18                 // Method kotlin/Pair.component1:()Ljava/lang/Object;
      10: checkcast     #20                 // class java/util/Map
      13: astore_1
      14: aload_0
      15: invokevirtual #23                 // Method kotlin/Pair.component2:()Ljava/lang/Object;
      18: checkcast     #25                 // class java/util/List
      21: astore_2
      22: ldc           #27                 // String 🎄 Day 19 🎄
      24: getstatic     #33                 // Field java/lang/System.out:Ljava/io/PrintStream;
      27: swap
      28: invokevirtual #39                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
      31: getstatic     #33                 // Field java/lang/System.out:Ljava/io/PrintStream;
      34: invokevirtual #41                 // Method java/io/PrintStream.println:()V
      37: ldc           #43                 // String [Part 1]
      39: getstatic     #33                 // Field java/lang/System.out:Ljava/io/PrintStream;
      42: swap
      43: invokevirtual #39                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
      46: new           #45                 // class java/lang/StringBuilder
      49: dup
      50: invokespecial #48                 // Method java/lang/StringBuilder."<init>":()V
      53: ldc           #50                 // String Answer:
      55: invokevirtual #54                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      58: aload_1
      59: aload_2
      60: invokestatic  #58                 // Method part1:(Ljava/util/Map;Ljava/util/List;)I
      63: invokevirtual #61                 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
      66: invokevirtual #65                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
      69: getstatic     #33                 // Field java/lang/System.out:Ljava/io/PrintStream;
      72: swap
      73: invokevirtual #39                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
      76: getstatic     #33                 // Field java/lang/System.out:Ljava/io/PrintStream;
      79: invokevirtual #41                 // Method java/io/PrintStream.println:()V
      82: ldc           #67                 // String [Part 2]
      84: getstatic     #33                 // Field java/lang/System.out:Ljava/io/PrintStream;
      87: swap
      88: invokevirtual #39                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
      91: new           #45                 // class java/lang/StringBuilder
      94: dup
      95: invokespecial #48                 // Method java/lang/StringBuilder."<init>":()V
      98: ldc           #50                 // String Answer:
     100: invokevirtual #54                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     103: aload_1
     104: invokestatic  #71                 // Method part2:(Ljava/util/Map;)J
     107: invokevirtual #74                 // Method java/lang/StringBuilder.append:(J)Ljava/lang/StringBuilder;
     110: invokevirtual #65                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
     113: getstatic     #33                 // Field java/lang/System.out:Ljava/io/PrintStream;
     116: swap
     117: invokevirtual #39                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
     120: return

  private static final java.util.Map<java.lang.Character, java.lang.Integer> toPart(java.lang.String);
    Code:
       0: aload_0
       1: iconst_2
       2: newarray       char
       4: astore_1
       5: aload_1
       6: iconst_0
       7: bipush        123
       9: castore
      10: aload_1
      11: iconst_1
      12: bipush        125
      14: castore
      15: aload_1
      16: invokestatic  #87                 // Method kotlin/text/StringsKt.trim:(Ljava/lang/String;[C)Ljava/lang/String;
      19: checkcast     #89                 // class java/lang/CharSequence
      22: iconst_1
      23: newarray       char
      25: astore_1
      26: aload_1
      27: iconst_0
      28: bipush        44
      30: castore
      31: aload_1
      32: iconst_0
      33: iconst_0
      34: bipush        6
      36: aconst_null
      37: invokestatic  #93                 // Method kotlin/text/StringsKt.split$default:(Ljava/lang/CharSequence;[CZIILjava/lang/Object;)Ljava/util/List;
      40: checkcast     #95                 // class java/lang/Iterable
      43: astore_1
      44: nop
      45: iconst_0
      46: istore_2
      47: aload_1
      48: astore_3
      49: new           #97                 // class java/util/ArrayList
      52: dup
      53: aload_1
      54: bipush        10
      56: invokestatic  #103                // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
      59: invokespecial #106                // Method java/util/ArrayList."<init>":(I)V
      62: checkcast     #108                // class java/util/Collection
      65: astore        4
      67: iconst_0
      68: istore        5
      70: aload_3
      71: invokeinterface #112,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      76: astore        6
      78: aload         6
      80: invokeinterface #118,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
      85: ifeq          149
      88: aload         6
      90: invokeinterface #121,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      95: astore        7
      97: aload         4
      99: aload         7
     101: checkcast     #123                // class java/lang/String
     104: astore        8
     106: astore        14
     108: iconst_0
     109: istore        9
     111: aload         8
     113: checkcast     #89                 // class java/lang/CharSequence
     116: iconst_1
     117: newarray       char
     119: astore        10
     121: aload         10
     123: iconst_0
     124: bipush        61
     126: castore
     127: aload         10
     129: iconst_0
     130: iconst_0
     131: bipush        6
     133: aconst_null
     134: invokestatic  #93                 // Method kotlin/text/StringsKt.split$default:(Ljava/lang/CharSequence;[CZIILjava/lang/Object;)Ljava/util/List;
     137: aload         14
     139: swap
     140: invokeinterface #127,  2          // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     145: pop
     146: goto          78
     149: aload         4
     151: checkcast     #25                 // class java/util/List
     154: nop
     155: checkcast     #95                 // class java/lang/Iterable
     158: astore_1
     159: nop
     160: iconst_0
     161: istore_2
     162: aload_1
     163: bipush        10
     165: invokestatic  #103                // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
     168: invokestatic  #133                // Method kotlin/collections/MapsKt.mapCapacity:(I)I
     171: bipush        16
     173: invokestatic  #139                // Method kotlin/ranges/RangesKt.coerceAtLeast:(II)I
     176: istore_3
     177: aload_1
     178: astore        4
     180: new           #141                // class java/util/LinkedHashMap
     183: dup
     184: iload_3
     185: invokespecial #142                // Method java/util/LinkedHashMap."<init>":(I)V
     188: checkcast     #20                 // class java/util/Map
     191: astore        5
     193: iconst_0
     194: istore        6
     196: aload         4
     198: invokeinterface #112,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     203: astore        7
     205: aload         7
     207: invokeinterface #118,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
     212: ifeq          309
     215: aload         7
     217: invokeinterface #121,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     222: astore        8
     224: aload         5
     226: astore        9
     228: aload         8
     230: checkcast     #25                 // class java/util/List
     233: astore        10
     235: iconst_0
     236: istore        11
     238: aload         10
     240: iconst_0
     241: invokeinterface #146,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     246: checkcast     #123                // class java/lang/String
     249: astore        12
     251: aload         10
     253: iconst_1
     254: invokeinterface #146,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     259: checkcast     #123                // class java/lang/String
     262: astore        13
     264: aload         12
     266: checkcast     #89                 // class java/lang/CharSequence
     269: invokestatic  #150                // Method kotlin/text/StringsKt.first:(Ljava/lang/CharSequence;)C
     272: invokestatic  #156                // Method java/lang/Character.valueOf:(C)Ljava/lang/Character;
     275: aload         13
     277: invokestatic  #162                // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
     280: invokestatic  #165                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     283: invokestatic  #171                // Method kotlin/TuplesKt.to:(Ljava/lang/Object;Ljava/lang/Object;)Lkotlin/Pair;
     286: astore        10
     288: aload         9
     290: aload         10
     292: invokevirtual #174                // Method kotlin/Pair.getFirst:()Ljava/lang/Object;
     295: aload         10
     297: invokevirtual #177                // Method kotlin/Pair.getSecond:()Ljava/lang/Object;
     300: invokeinterface #181,  3          // InterfaceMethod java/util/Map.put:(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
     305: pop
     306: goto          205
     309: aload         5
     311: nop
     312: areturn

  private static final day19.Rule toRule(java.lang.String);
    Code:
       0: aload_0
       1: checkcast     #89                 // class java/lang/CharSequence
       4: bipush        58
       6: iconst_0
       7: iconst_2
       8: aconst_null
       9: invokestatic  #210                // Method kotlin/text/StringsKt.contains$default:(Ljava/lang/CharSequence;CZILjava/lang/Object;)Z
      12: ifne          25
      15: new           #212                // class day19/Rule
      18: dup
      19: aconst_null
      20: aload_0
      21: invokespecial #215                // Method day19/Rule."<init>":(Lday19/Condition;Ljava/lang/String;)V
      24: areturn
      25: aload_0
      26: checkcast     #89                 // class java/lang/CharSequence
      29: iconst_1
      30: newarray       char
      32: astore_2
      33: aload_2
      34: iconst_0
      35: bipush        58
      37: castore
      38: aload_2
      39: iconst_0
      40: iconst_0
      41: bipush        6
      43: aconst_null
      44: invokestatic  #93                 // Method kotlin/text/StringsKt.split$default:(Ljava/lang/CharSequence;[CZIILjava/lang/Object;)Ljava/util/List;
      47: astore_1
      48: aload_1
      49: iconst_0
      50: invokeinterface #146,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      55: checkcast     #123                // class java/lang/String
      58: astore_2
      59: aload_1
      60: iconst_1
      61: invokeinterface #146,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      66: checkcast     #123                // class java/lang/String
      69: astore_3
      70: new           #217                // class day19/Condition
      73: dup
      74: aload_2
      75: iconst_0
      76: invokevirtual #221                // Method java/lang/String.charAt:(I)C
      79: aload_2
      80: iconst_1
      81: invokevirtual #221                // Method java/lang/String.charAt:(I)C
      84: nop
      85: aload_2
      86: iconst_2
      87: invokevirtual #225                // Method java/lang/String.substring:(I)Ljava/lang/String;
      90: dup
      91: ldc           #227                // String substring(...)
      93: invokestatic  #233                // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
      96: invokestatic  #162                // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
      99: invokespecial #236                // Method day19/Condition."<init>":(CCI)V
     102: astore        4
     104: new           #212                // class day19/Rule
     107: dup
     108: aload         4
     110: aload_3
     111: invokespecial #215                // Method day19/Rule."<init>":(Lday19/Condition;Ljava/lang/String;)V
     114: areturn

  private static final kotlin.Pair<java.lang.String, java.util.List<day19.Rule>> toWorkflow(java.lang.String);
    Code:
       0: aload_0
       1: iconst_1
       2: newarray       char
       4: astore_2
       5: aload_2
       6: iconst_0
       7: bipush        125
       9: castore
      10: aload_2
      11: invokestatic  #87                 // Method kotlin/text/StringsKt.trim:(Ljava/lang/String;[C)Ljava/lang/String;
      14: checkcast     #89                 // class java/lang/CharSequence
      17: iconst_1
      18: newarray       char
      20: astore_2
      21: aload_2
      22: iconst_0
      23: bipush        123
      25: castore
      26: aload_2
      27: iconst_0
      28: iconst_0
      29: bipush        6
      31: aconst_null
      32: invokestatic  #93                 // Method kotlin/text/StringsKt.split$default:(Ljava/lang/CharSequence;[CZIILjava/lang/Object;)Ljava/util/List;
      35: astore_1
      36: aload_1
      37: iconst_0
      38: invokeinterface #146,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      43: checkcast     #123                // class java/lang/String
      46: astore_2
      47: aload_1
      48: iconst_1
      49: invokeinterface #146,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      54: checkcast     #123                // class java/lang/String
      57: astore_3
      58: aload_2
      59: aload_3
      60: checkcast     #89                 // class java/lang/CharSequence
      63: iconst_1
      64: newarray       char
      66: astore        4
      68: aload         4
      70: iconst_0
      71: bipush        44
      73: castore
      74: aload         4
      76: iconst_0
      77: iconst_0
      78: bipush        6
      80: aconst_null
      81: invokestatic  #93                 // Method kotlin/text/StringsKt.split$default:(Ljava/lang/CharSequence;[CZIILjava/lang/Object;)Ljava/util/List;
      84: checkcast     #95                 // class java/lang/Iterable
      87: astore        4
      89: astore        13
      91: iconst_0
      92: istore        5
      94: aload         4
      96: astore        6
      98: new           #97                 // class java/util/ArrayList
     101: dup
     102: aload         4
     104: bipush        10
     106: invokestatic  #103                // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
     109: invokespecial #106                // Method java/util/ArrayList."<init>":(I)V
     112: checkcast     #108                // class java/util/Collection
     115: astore        7
     117: iconst_0
     118: istore        8
     120: aload         6
     122: invokeinterface #112,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     127: astore        9
     129: aload         9
     131: invokeinterface #118,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
     136: ifeq          179
     139: aload         9
     141: invokeinterface #121,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     146: astore        10
     148: aload         7
     150: aload         10
     152: checkcast     #123                // class java/lang/String
     155: astore        11
     157: astore        14
     159: iconst_0
     160: istore        12
     162: aload         11
     164: invokestatic  #245                // Method toRule:(Ljava/lang/String;)Lday19/Rule;
     167: aload         14
     169: swap
     170: invokeinterface #127,  2          // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     175: pop
     176: goto          129
     179: aload         7
     181: checkcast     #25                 // class java/util/List
     184: nop
     185: aload         13
     187: swap
     188: astore        15
     190: astore        16
     192: new           #14                 // class kotlin/Pair
     195: dup
     196: aload         16
     198: aload         15
     200: invokespecial #248                // Method kotlin/Pair."<init>":(Ljava/lang/Object;Ljava/lang/Object;)V
     203: areturn

  private static final kotlin.Pair<java.util.Map<java.lang.String, java.util.List<day19.Rule>>, java.util.List<java.util.Map<java.lang.Character, java.lang.Integer>>> parse(java.lang.String);
    Code:
       0: new           #256                // class java/io/File
       3: dup
       4: aload_0
       5: invokespecial #259                // Method java/io/File."<init>":(Ljava/lang/String;)V
       8: aconst_null
       9: iconst_1
      10: aconst_null
      11: invokestatic  #265                // Method kotlin/io/FilesKt.readText$default:(Ljava/io/File;Ljava/nio/charset/Charset;ILjava/lang/Object;)Ljava/lang/String;
      14: checkcast     #89                 // class java/lang/CharSequence
      17: iconst_2
      18: anewarray     #123                // class java/lang/String
      21: astore_2
      22: aload_2
      23: iconst_0
      24: ldc_w         #267                // String \n\n
      27: aastore
      28: aload_2
      29: iconst_1
      30: ldc_w         #269                // String \r\n\r\n
      33: aastore
      34: aload_2
      35: iconst_0
      36: iconst_0
      37: bipush        6
      39: aconst_null
      40: invokestatic  #272                // Method kotlin/text/StringsKt.split$default:(Ljava/lang/CharSequence;[Ljava/lang/String;ZIILjava/lang/Object;)Ljava/util/List;
      43: astore_1
      44: aload_1
      45: iconst_0
      46: invokeinterface #146,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      51: checkcast     #123                // class java/lang/String
      54: astore_2
      55: aload_1
      56: iconst_1
      57: invokeinterface #146,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      62: checkcast     #123                // class java/lang/String
      65: astore_3
      66: aload_2
      67: checkcast     #89                 // class java/lang/CharSequence
      70: invokestatic  #276                // Method kotlin/text/StringsKt.lines:(Ljava/lang/CharSequence;)Ljava/util/List;
      73: checkcast     #95                 // class java/lang/Iterable
      76: astore        5
      78: nop
      79: iconst_0
      80: istore        6
      82: aload         5
      84: astore        7
      86: new           #97                 // class java/util/ArrayList
      89: dup
      90: aload         5
      92: bipush        10
      94: invokestatic  #103                // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
      97: invokespecial #106                // Method java/util/ArrayList."<init>":(I)V
     100: checkcast     #108                // class java/util/Collection
     103: astore        8
     105: iconst_0
     106: istore        9
     108: aload         7
     110: invokeinterface #112,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     115: astore        10
     117: aload         10
     119: invokeinterface #118,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
     124: ifeq          167
     127: aload         10
     129: invokeinterface #121,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     134: astore        11
     136: aload         8
     138: aload         11
     140: checkcast     #123                // class java/lang/String
     143: astore        12
     145: astore        18
     147: iconst_0
     148: istore        13
     150: aload         12
     152: invokestatic  #278                // Method toWorkflow:(Ljava/lang/String;)Lkotlin/Pair;
     155: aload         18
     157: swap
     158: invokeinterface #127,  2          // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     163: pop
     164: goto          117
     167: aload         8
     169: checkcast     #25                 // class java/util/List
     172: nop
     173: checkcast     #95                 // class java/lang/Iterable
     176: astore        5
     178: nop
     179: iconst_0
     180: istore        6
     182: aload         5
     184: bipush        10
     186: invokestatic  #103                // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
     189: invokestatic  #133                // Method kotlin/collections/MapsKt.mapCapacity:(I)I
     192: bipush        16
     194: invokestatic  #139                // Method kotlin/ranges/RangesKt.coerceAtLeast:(II)I
     197: istore        7
     199: aload         5
     201: astore        8
     203: new           #141                // class java/util/LinkedHashMap
     206: dup
     207: iload         7
     209: invokespecial #142                // Method java/util/LinkedHashMap."<init>":(I)V
     212: checkcast     #20                 // class java/util/Map
     215: astore        9
     217: iconst_0
     218: istore        10
     220: aload         8
     222: invokeinterface #112,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     227: astore        11
     229: aload         11
     231: invokeinterface #118,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
     236: ifeq          312
     239: aload         11
     241: invokeinterface #121,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     246: astore        12
     248: aload         9
     250: astore        13
     252: aload         12
     254: checkcast     #14                 // class kotlin/Pair
     257: astore        14
     259: iconst_0
     260: istore        15
     262: aload         14
     264: invokevirtual #18                 // Method kotlin/Pair.component1:()Ljava/lang/Object;
     267: checkcast     #123                // class java/lang/String
     270: astore        16
     272: aload         14
     274: invokevirtual #23                 // Method kotlin/Pair.component2:()Ljava/lang/Object;
     277: checkcast     #25                 // class java/util/List
     280: astore        17
     282: aload         16
     284: aload         17
     286: invokestatic  #171                // Method kotlin/TuplesKt.to:(Ljava/lang/Object;Ljava/lang/Object;)Lkotlin/Pair;
     289: astore        14
     291: aload         13
     293: aload         14
     295: invokevirtual #174                // Method kotlin/Pair.getFirst:()Ljava/lang/Object;
     298: aload         14
     300: invokevirtual #177                // Method kotlin/Pair.getSecond:()Ljava/lang/Object;
     303: invokeinterface #181,  3          // InterfaceMethod java/util/Map.put:(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
     308: pop
     309: goto          229
     312: aload         9
     314: nop
     315: astore        4
     317: aload_3
     318: checkcast     #89                 // class java/lang/CharSequence
     321: invokestatic  #276                // Method kotlin/text/StringsKt.lines:(Ljava/lang/CharSequence;)Ljava/util/List;
     324: checkcast     #95                 // class java/lang/Iterable
     327: astore        6
     329: nop
     330: iconst_0
     331: istore        7
     333: aload         6
     335: astore        8
     337: new           #97                 // class java/util/ArrayList
     340: dup
     341: aload         6
     343: bipush        10
     345: invokestatic  #103                // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
     348: invokespecial #106                // Method java/util/ArrayList."<init>":(I)V
     351: checkcast     #108                // class java/util/Collection
     354: astore        9
     356: iconst_0
     357: istore        10
     359: aload         8
     361: invokeinterface #112,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     366: astore        11
     368: aload         11
     370: invokeinterface #118,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
     375: ifeq          418
     378: aload         11
     380: invokeinterface #121,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     385: astore        12
     387: aload         9
     389: aload         12
     391: checkcast     #123                // class java/lang/String
     394: astore        13
     396: astore        18
     398: iconst_0
     399: istore        14
     401: aload         13
     403: invokestatic  #280                // Method toPart:(Ljava/lang/String;)Ljava/util/Map;
     406: aload         18
     408: swap
     409: invokeinterface #127,  2          // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     414: pop
     415: goto          368
     418: aload         9
     420: checkcast     #25                 // class java/util/List
     423: nop
     424: astore        5
     426: new           #14                 // class kotlin/Pair
     429: dup
     430: aload         4
     432: aload         5
     434: invokespecial #248                // Method kotlin/Pair."<init>":(Ljava/lang/Object;Ljava/lang/Object;)V
     437: areturn

  private static final int count(java.util.Map<java.lang.Character, java.lang.Integer>, java.util.Map<java.lang.String, ? extends java.util.List<day19.Rule>>);
    Code:
       0: ldc_w         #291                // String in
       3: astore_2
       4: aload_2
       5: ldc_w         #293                // String A
       8: invokestatic  #297                // Method kotlin/jvm/internal/Intrinsics.areEqual:(Ljava/lang/Object;Ljava/lang/Object;)Z
      11: ifne          182
      14: aload_2
      15: ldc_w         #299                // String R
      18: invokestatic  #297                // Method kotlin/jvm/internal/Intrinsics.areEqual:(Ljava/lang/Object;Ljava/lang/Object;)Z
      21: ifne          182
      24: aload_1
      25: aload_2
      26: invokestatic  #303                // Method kotlin/collections/MapsKt.getValue:(Ljava/util/Map;Ljava/lang/Object;)Ljava/lang/Object;
      29: checkcast     #25                 // class java/util/List
      32: astore_3
      33: aload_3
      34: invokeinterface #304,  1          // InterfaceMethod java/util/List.iterator:()Ljava/util/Iterator;
      39: astore        4
      41: aload         4
      43: invokeinterface #118,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
      48: ifeq          4
      51: aload         4
      53: invokeinterface #121,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      58: checkcast     #212                // class day19/Rule
      61: astore        5
      63: aload         5
      65: invokevirtual #307                // Method day19/Rule.component1:()Lday19/Condition;
      68: astore        6
      70: aload         5
      72: invokevirtual #309                // Method day19/Rule.component2:()Ljava/lang/String;
      75: astore        7
      77: aload         6
      79: ifnonnull     88
      82: aload         7
      84: astore_2
      85: goto          4
      88: aload         6
      90: invokevirtual #312                // Method day19/Condition.component1:()C
      93: istore        8
      95: aload         6
      97: invokevirtual #314                // Method day19/Condition.component2:()C
     100: istore        9
     102: aload         6
     104: invokevirtual #318                // Method day19/Condition.component3:()I
     107: istore        10
     109: iload         9
     111: bipush        62
     113: if_icmpne     144
     116: aload_0
     117: iload         8
     119: invokestatic  #156                // Method java/lang/Character.valueOf:(C)Ljava/lang/Character;
     122: invokestatic  #303                // Method kotlin/collections/MapsKt.getValue:(Ljava/util/Map;Ljava/lang/Object;)Ljava/lang/Object;
     125: checkcast     #320                // class java/lang/Number
     128: invokevirtual #323                // Method java/lang/Number.intValue:()I
     131: iload         10
     133: if_icmple     140
     136: iconst_1
     137: goto          169
     140: iconst_0
     141: goto          169
     144: aload_0
     145: iload         8
     147: invokestatic  #156                // Method java/lang/Character.valueOf:(C)Ljava/lang/Character;
     150: invokestatic  #303                // Method kotlin/collections/MapsKt.getValue:(Ljava/util/Map;Ljava/lang/Object;)Ljava/lang/Object;
     153: checkcast     #320                // class java/lang/Number
     156: invokevirtual #323                // Method java/lang/Number.intValue:()I
     159: iload         10
     161: if_icmpge     168
     164: iconst_1
     165: goto          169
     168: iconst_0
     169: istore        11
     171: iload         11
     173: ifeq          41
     176: aload         7
     178: astore_2
     179: goto          4
     182: aload_2
     183: ldc_w         #293                // String A
     186: invokestatic  #297                // Method kotlin/jvm/internal/Intrinsics.areEqual:(Ljava/lang/Object;Ljava/lang/Object;)Z
     189: ifeq          207
     192: aload_0
     193: invokeinterface #327,  1          // InterfaceMethod java/util/Map.values:()Ljava/util/Collection;
     198: checkcast     #95                 // class java/lang/Iterable
     201: invokestatic  #331                // Method kotlin/collections/CollectionsKt.sumOfInt:(Ljava/lang/Iterable;)I
     204: goto          208
     207: iconst_0
     208: ireturn

  private static final int part1(java.util.Map<java.lang.String, ? extends java.util.List<day19.Rule>>, java.util.List<? extends java.util.Map<java.lang.Character, java.lang.Integer>>);
    Code:
       0: aload_1
       1: checkcast     #95                 // class java/lang/Iterable
       4: astore_2
       5: iconst_0
       6: istore_3
       7: aload_2
       8: invokeinterface #112,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      13: astore        4
      15: aload         4
      17: invokeinterface #118,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
      22: ifeq          64
      25: aload         4
      27: invokeinterface #121,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      32: astore        5
      34: iload_3
      35: aload         5
      37: checkcast     #20                 // class java/util/Map
      40: astore        6
      42: istore        8
      44: iconst_0
      45: istore        7
      47: aload         6
      49: aload_0
      50: invokestatic  #342                // Method count:(Ljava/util/Map;Ljava/util/Map;)I
      53: istore        9
      55: iload         8
      57: iload         9
      59: iadd
      60: istore_3
      61: goto          15
      64: iload_3
      65: ireturn

  private static final long part2(java.util.Map<java.lang.String, ? extends java.util.List<day19.Rule>>);
    Code:
       0: new           #346                // class kotlin/collections/ArrayDeque
       3: dup
       4: invokespecial #347                // Method kotlin/collections/ArrayDeque."<init>":()V
       7: astore_2
       8: aload_2
       9: astore_3
      10: iconst_0
      11: istore        4
      13: aload_3
      14: ldc_w         #291                // String in
      17: iconst_4
      18: anewarray     #14                 // class kotlin/Pair
      21: astore        5
      23: aload         5
      25: iconst_0
      26: bipush        120
      28: invokestatic  #156                // Method java/lang/Character.valueOf:(C)Ljava/lang/Character;
      31: new           #349                // class kotlin/ranges/IntRange
      34: dup
      35: iconst_1
      36: sipush        4000
      39: invokespecial #352                // Method kotlin/ranges/IntRange."<init>":(II)V
      42: invokestatic  #171                // Method kotlin/TuplesKt.to:(Ljava/lang/Object;Ljava/lang/Object;)Lkotlin/Pair;
      45: aastore
      46: aload         5
      48: iconst_1
      49: bipush        109
      51: invokestatic  #156                // Method java/lang/Character.valueOf:(C)Ljava/lang/Character;
      54: new           #349                // class kotlin/ranges/IntRange
      57: dup
      58: iconst_1
      59: sipush        4000
      62: invokespecial #352                // Method kotlin/ranges/IntRange."<init>":(II)V
      65: invokestatic  #171                // Method kotlin/TuplesKt.to:(Ljava/lang/Object;Ljava/lang/Object;)Lkotlin/Pair;
      68: aastore
      69: aload         5
      71: iconst_2
      72: bipush        97
      74: invokestatic  #156                // Method java/lang/Character.valueOf:(C)Ljava/lang/Character;
      77: new           #349                // class kotlin/ranges/IntRange
      80: dup
      81: iconst_1
      82: sipush        4000
      85: invokespecial #352                // Method kotlin/ranges/IntRange."<init>":(II)V
      88: invokestatic  #171                // Method kotlin/TuplesKt.to:(Ljava/lang/Object;Ljava/lang/Object;)Lkotlin/Pair;
      91: aastore
      92: aload         5
      94: iconst_3
      95: bipush        115
      97: invokestatic  #156                // Method java/lang/Character.valueOf:(C)Ljava/lang/Character;
     100: new           #349                // class kotlin/ranges/IntRange
     103: dup
     104: iconst_1
     105: sipush        4000
     108: invokespecial #352                // Method kotlin/ranges/IntRange."<init>":(II)V
     111: invokestatic  #171                // Method kotlin/TuplesKt.to:(Ljava/lang/Object;Ljava/lang/Object;)Lkotlin/Pair;
     114: aastore
     115: aload         5
     117: invokestatic  #356                // Method kotlin/collections/MapsKt.mapOf:([Lkotlin/Pair;)Ljava/util/Map;
     120: invokestatic  #171                // Method kotlin/TuplesKt.to:(Ljava/lang/Object;Ljava/lang/Object;)Lkotlin/Pair;
     123: invokevirtual #357                // Method kotlin/collections/ArrayDeque.add:(Ljava/lang/Object;)Z
     126: pop
     127: nop
     128: aload_2
     129: astore_1
     130: lconst_0
     131: lstore_2
     132: aload_1
     133: checkcast     #108                // class java/util/Collection
     136: invokeinterface #360,  1          // InterfaceMethod java/util/Collection.isEmpty:()Z
     141: ifne          148
     144: iconst_1
     145: goto          149
     148: iconst_0
     149: ifeq          754
     152: aload_1
     153: invokevirtual #363                // Method kotlin/collections/ArrayDeque.removeFirst:()Ljava/lang/Object;
     156: checkcast     #14                 // class kotlin/Pair
     159: astore        4
     161: aload         4
     163: invokevirtual #18                 // Method kotlin/Pair.component1:()Ljava/lang/Object;
     166: checkcast     #123                // class java/lang/String
     169: astore        5
     171: aload         4
     173: invokevirtual #23                 // Method kotlin/Pair.component2:()Ljava/lang/Object;
     176: checkcast     #20                 // class java/util/Map
     179: astore        6
     181: aload         5
     183: ldc_w         #293                // String A
     186: invokestatic  #297                // Method kotlin/jvm/internal/Intrinsics.areEqual:(Ljava/lang/Object;Ljava/lang/Object;)Z
     189: ifne          203
     192: aload         5
     194: ldc_w         #299                // String R
     197: invokestatic  #297                // Method kotlin/jvm/internal/Intrinsics.areEqual:(Ljava/lang/Object;Ljava/lang/Object;)Z
     200: ifeq          450
     203: aload         5
     205: ldc_w         #293                // String A
     208: invokestatic  #297                // Method kotlin/jvm/internal/Intrinsics.areEqual:(Ljava/lang/Object;Ljava/lang/Object;)Z
     211: ifeq          447
     214: aload         6
     216: invokeinterface #327,  1          // InterfaceMethod java/util/Map.values:()Ljava/util/Collection;
     221: checkcast     #95                 // class java/lang/Iterable
     224: astore        9
     226: nop
     227: iconst_0
     228: istore        10
     230: aload         9
     232: astore        11
     234: new           #97                 // class java/util/ArrayList
     237: dup
     238: aload         9
     240: bipush        10
     242: invokestatic  #103                // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
     245: invokespecial #106                // Method java/util/ArrayList."<init>":(I)V
     248: checkcast     #108                // class java/util/Collection
     251: astore        12
     253: iconst_0
     254: istore        13
     256: aload         11
     258: invokeinterface #112,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     263: astore        14
     265: aload         14
     267: invokeinterface #118,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
     272: ifeq          327
     275: aload         14
     277: invokeinterface #121,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     282: astore        15
     284: aload         12
     286: aload         15
     288: checkcast     #349                // class kotlin/ranges/IntRange
     291: astore        16
     293: astore        25
     295: iconst_0
     296: istore        17
     298: aload         16
     300: invokevirtual #366                // Method kotlin/ranges/IntRange.getLast:()I
     303: aload         16
     305: invokevirtual #368                // Method kotlin/ranges/IntRange.getFirst:()I
     308: isub
     309: iconst_1
     310: iadd
     311: i2l
     312: invokestatic  #373                // Method java/lang/Long.valueOf:(J)Ljava/lang/Long;
     315: aload         25
     317: swap
     318: invokeinterface #127,  2          // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     323: pop
     324: goto          265
     327: aload         12
     329: checkcast     #25                 // class java/util/List
     332: nop
     333: checkcast     #95                 // class java/lang/Iterable
     336: astore        9
     338: nop
     339: iconst_0
     340: istore        10
     342: aload         9
     344: invokeinterface #112,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     349: astore        11
     351: aload         11
     353: invokeinterface #118,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
     358: ifne          372
     361: new           #375                // class java/lang/UnsupportedOperationException
     364: dup
     365: ldc_w         #377                // String Empty collection can\'t be reduced.
     368: invokespecial #378                // Method java/lang/UnsupportedOperationException."<init>":(Ljava/lang/String;)V
     371: athrow
     372: aload         11
     374: invokeinterface #121,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     379: astore        12
     381: aload         11
     383: invokeinterface #118,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
     388: ifeq          432
     391: aload         12
     393: aload         11
     395: invokeinterface #121,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     400: checkcast     #320                // class java/lang/Number
     403: invokevirtual #382                // Method java/lang/Number.longValue:()J
     406: lstore        13
     408: checkcast     #320                // class java/lang/Number
     411: invokevirtual #382                // Method java/lang/Number.longValue:()J
     414: lstore        15
     416: iconst_0
     417: istore        17
     419: lload         15
     421: lload         13
     423: lmul
     424: invokestatic  #373                // Method java/lang/Long.valueOf:(J)Ljava/lang/Long;
     427: astore        12
     429: goto          381
     432: aload         12
     434: checkcast     #320                // class java/lang/Number
     437: invokevirtual #382                // Method java/lang/Number.longValue:()J
     440: lstore        7
     442: lload_2
     443: lload         7
     445: ladd
     446: lstore_2
     447: goto          132
     450: aload         6
     452: invokestatic  #386                // Method kotlin/collections/MapsKt.toMutableMap:(Ljava/util/Map;)Ljava/util/Map;
     455: astore        7
     457: aload_0
     458: aload         5
     460: invokestatic  #303                // Method kotlin/collections/MapsKt.getValue:(Ljava/util/Map;Ljava/lang/Object;)Ljava/lang/Object;
     463: checkcast     #25                 // class java/util/List
     466: astore        8
     468: aload         8
     470: invokeinterface #304,  1          // InterfaceMethod java/util/List.iterator:()Ljava/util/Iterator;
     475: astore        9
     477: aload         9
     479: invokeinterface #118,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
     484: ifeq          132
     487: aload         9
     489: invokeinterface #121,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     494: checkcast     #212                // class day19/Rule
     497: astore        10
     499: aload         10
     501: invokevirtual #307                // Method day19/Rule.component1:()Lday19/Condition;
     504: astore        11
     506: aload         10
     508: invokevirtual #309                // Method day19/Rule.component2:()Ljava/lang/String;
     511: astore        12
     513: aload         11
     515: ifnonnull     538
     518: aload_1
     519: checkcast     #108                // class java/util/Collection
     522: aload         12
     524: aload         7
     526: invokestatic  #171                // Method kotlin/TuplesKt.to:(Ljava/lang/Object;Ljava/lang/Object;)Lkotlin/Pair;
     529: invokeinterface #127,  2          // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     534: pop
     535: goto          132
     538: aload         11
     540: invokevirtual #312                // Method day19/Condition.component1:()C
     543: istore        13
     545: aload         11
     547: invokevirtual #314                // Method day19/Condition.component2:()C
     550: istore        14
     552: aload         11
     554: invokevirtual #318                // Method day19/Condition.component3:()I
     557: istore        15
     559: aload         7
     561: iload         13
     563: invokestatic  #156                // Method java/lang/Character.valueOf:(C)Ljava/lang/Character;
     566: invokestatic  #303                // Method kotlin/collections/MapsKt.getValue:(Ljava/util/Map;Ljava/lang/Object;)Ljava/lang/Object;
     569: checkcast     #349                // class kotlin/ranges/IntRange
     572: astore        16
     574: iload         14
     576: bipush        62
     578: if_icmpne     617
     581: new           #349                // class kotlin/ranges/IntRange
     584: dup
     585: iload         15
     587: iconst_1
     588: iadd
     589: aload         16
     591: invokevirtual #366                // Method kotlin/ranges/IntRange.getLast:()I
     594: invokespecial #352                // Method kotlin/ranges/IntRange."<init>":(II)V
     597: new           #349                // class kotlin/ranges/IntRange
     600: dup
     601: aload         16
     603: invokevirtual #368                // Method kotlin/ranges/IntRange.getFirst:()I
     606: iload         15
     608: invokespecial #352                // Method kotlin/ranges/IntRange."<init>":(II)V
     611: invokestatic  #171                // Method kotlin/TuplesKt.to:(Ljava/lang/Object;Ljava/lang/Object;)Lkotlin/Pair;
     614: goto          650
     617: new           #349                // class kotlin/ranges/IntRange
     620: dup
     621: aload         16
     623: invokevirtual #368                // Method kotlin/ranges/IntRange.getFirst:()I
     626: iload         15
     628: iconst_1
     629: isub
     630: invokespecial #352                // Method kotlin/ranges/IntRange."<init>":(II)V
     633: new           #349                // class kotlin/ranges/IntRange
     636: dup
     637: iload         15
     639: aload         16
     641: invokevirtual #366                // Method kotlin/ranges/IntRange.getLast:()I
     644: invokespecial #352                // Method kotlin/ranges/IntRange."<init>":(II)V
     647: invokestatic  #171                // Method kotlin/TuplesKt.to:(Ljava/lang/Object;Ljava/lang/Object;)Lkotlin/Pair;
     650: astore        17
     652: aload         17
     654: invokevirtual #18                 // Method kotlin/Pair.component1:()Ljava/lang/Object;
     657: checkcast     #349                // class kotlin/ranges/IntRange
     660: astore        18
     662: aload         17
     664: invokevirtual #23                 // Method kotlin/Pair.component2:()Ljava/lang/Object;
     667: checkcast     #349                // class kotlin/ranges/IntRange
     670: astore        19
     672: aload_1
     673: checkcast     #108                // class java/util/Collection
     676: astore        20
     678: aload         12
     680: aload         7
     682: invokestatic  #386                // Method kotlin/collections/MapsKt.toMutableMap:(Ljava/util/Map;)Ljava/util/Map;
     685: astore        21
     687: aload         21
     689: astore        22
     691: astore        25
     693: iconst_0
     694: istore        23
     696: iload         13
     698: invokestatic  #156                // Method java/lang/Character.valueOf:(C)Ljava/lang/Character;
     701: astore        24
     703: aload         22
     705: aload         24
     707: aload         18
     709: invokeinterface #181,  3          // InterfaceMethod java/util/Map.put:(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
     714: pop
     715: nop
     716: aload         25
     718: aload         21
     720: invokestatic  #171                // Method kotlin/TuplesKt.to:(Ljava/lang/Object;Ljava/lang/Object;)Lkotlin/Pair;
     723: aload         20
     725: swap
     726: invokeinterface #127,  2          // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     731: pop
     732: iload         13
     734: invokestatic  #156                // Method java/lang/Character.valueOf:(C)Ljava/lang/Character;
     737: astore        21
     739: aload         7
     741: aload         21
     743: aload         19
     745: invokeinterface #181,  3          // InterfaceMethod java/util/Map.put:(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
     750: pop
     751: goto          477
     754: lload_2
     755: lreturn

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


package org.mjurisic.aoc2020.day12

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

class Day12 {

    companion object {
        @JvmStatic
        fun main(args: Array<String>) = try {

            val ship = Ship(0, 0)
            val waypoint = Waypoint(10, -1)
            File(ClassLoader.getSystemResource("resources/input12.txt").file).forEachLine {
                val command = it.first()
                val value = it.substring(1, it.length).toInt()
                moveWaypointInDirection(command, waypoint, value)
                if (command == 'F') {
                    moveShipForward(ship, waypoint, value)
                } else if (command == 'R' || command == 'L') {
                    rotateWaypoint(command, ship, waypoint, value)
                }
            }

            val manhattan = abs(ship.x) + abs(ship.y)
            println(manhattan)

        } catch (e: Exception) {
            e.printStackTrace()
        }

        private fun moveShipForward(ship: Ship, waypoint: Waypoint, value: Int) {
            val dx = waypoint.x - ship.x
            val dy = waypoint.y - ship.y

            repeat(value) {
                ship.x += dx
                ship.y += dy
            }
            waypoint.x = ship.x + dx
            waypoint.y = ship.y + dy
        }

        private fun rotateWaypoint(direction: Char, ship: Ship, waypoint: Waypoint, value: Int) {
            val dx = waypoint.x - ship.x
            val dy = waypoint.y - ship.y

            if (direction == 'L' && value == 90 || direction == 'R' && value == 270) {
                waypoint.y = ship.y - dx
                waypoint.x = ship.x + dy
            } else if (direction == 'R' && value == 90 || direction == 'L' && value == 270) {
                waypoint.y = ship.y + dx
                waypoint.x = ship.x - dy
            } else if (value == 180) {
                waypoint.y = ship.y - dy
                waypoint.x = ship.x - dx
            }

        }

        private fun moveWaypointInDirection(dir: Char, waypoint: Waypoint, value: Int) {
            when (dir) {
                'N' -> waypoint.y = waypoint.y - value
                'S' -> waypoint.y = waypoint.y + value
                'E' -> waypoint.x = waypoint.x + value
                'W' -> waypoint.x = waypoint.x - value
            }
        }

    }
}

class Ship(var x: Int, var y: Int) {
    override fun toString(): String {
        return "Ship(x=$x, y=$y)"
    }
}

class Waypoint(var x: Int, var y: Int) {
    override fun toString(): String {
        return "Waypoint(x=$x, y=$y)"
    }
}

Compiled from "Day12.kt"
public final class org.mjurisic.aoc2020.day12.Day12$Companion {
  private org.mjurisic.aoc2020.day12.Day12$Companion();
    Code:
       0: aload_0
       1: invokespecial #8                  // Method java/lang/Object."<init>":()V
       4: return

  public final void main(java.lang.String[]);
    Code:
       0: aload_1
       1: ldc           #18                 // String args
       3: invokestatic  #24                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: nop
       7: new           #26                 // class org/mjurisic/aoc2020/day12/Ship
      10: dup
      11: iconst_0
      12: iconst_0
      13: invokespecial #29                 // Method org/mjurisic/aoc2020/day12/Ship."<init>":(II)V
      16: astore_2
      17: new           #31                 // class org/mjurisic/aoc2020/day12/Waypoint
      20: dup
      21: bipush        10
      23: iconst_m1
      24: invokespecial #32                 // Method org/mjurisic/aoc2020/day12/Waypoint."<init>":(II)V
      27: astore_3
      28: new           #34                 // class java/io/File
      31: dup
      32: ldc           #36                 // String resources/input12.txt
      34: invokestatic  #42                 // Method java/lang/ClassLoader.getSystemResource:(Ljava/lang/String;)Ljava/net/URL;
      37: invokevirtual #48                 // Method java/net/URL.getFile:()Ljava/lang/String;
      40: invokespecial #51                 // Method java/io/File."<init>":(Ljava/lang/String;)V
      43: aconst_null
      44: aload_3
      45: aload_2
      46: invokedynamic #71,  0             // InvokeDynamic #0:invoke:(Lorg/mjurisic/aoc2020/day12/Waypoint;Lorg/mjurisic/aoc2020/day12/Ship;)Lkotlin/jvm/functions/Function1;
      51: iconst_1
      52: aconst_null
      53: invokestatic  #77                 // Method kotlin/io/FilesKt.forEachLine$default:(Ljava/io/File;Ljava/nio/charset/Charset;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)V
      56: aload_2
      57: invokevirtual #81                 // Method org/mjurisic/aoc2020/day12/Ship.getX:()I
      60: invokestatic  #87                 // Method java/lang/Math.abs:(I)I
      63: aload_2
      64: invokevirtual #90                 // Method org/mjurisic/aoc2020/day12/Ship.getY:()I
      67: invokestatic  #87                 // Method java/lang/Math.abs:(I)I
      70: iadd
      71: istore        4
      73: getstatic     #96                 // Field java/lang/System.out:Ljava/io/PrintStream;
      76: iload         4
      78: invokevirtual #102                // Method java/io/PrintStream.println:(I)V
      81: goto          89
      84: astore_2
      85: aload_2
      86: invokevirtual #105                // Method java/lang/Exception.printStackTrace:()V
      89: return
    Exception table:
       from    to  target type
           6    81    84   Class java/lang/Exception

  private final void moveShipForward(org.mjurisic.aoc2020.day12.Ship, org.mjurisic.aoc2020.day12.Waypoint, int);
    Code:
       0: aload_2
       1: invokevirtual #117                // Method org/mjurisic/aoc2020/day12/Waypoint.getX:()I
       4: aload_1
       5: invokevirtual #81                 // Method org/mjurisic/aoc2020/day12/Ship.getX:()I
       8: isub
       9: istore        4
      11: aload_2
      12: invokevirtual #118                // Method org/mjurisic/aoc2020/day12/Waypoint.getY:()I
      15: aload_1
      16: invokevirtual #90                 // Method org/mjurisic/aoc2020/day12/Ship.getY:()I
      19: isub
      20: istore        5
      22: iconst_0
      23: istore        6
      25: iload         6
      27: iload_3
      28: if_icmpge     67
      31: iload         6
      33: istore        7
      35: iconst_0
      36: istore        8
      38: aload_1
      39: aload_1
      40: invokevirtual #81                 // Method org/mjurisic/aoc2020/day12/Ship.getX:()I
      43: iload         4
      45: iadd
      46: invokevirtual #121                // Method org/mjurisic/aoc2020/day12/Ship.setX:(I)V
      49: aload_1
      50: aload_1
      51: invokevirtual #90                 // Method org/mjurisic/aoc2020/day12/Ship.getY:()I
      54: iload         5
      56: iadd
      57: invokevirtual #124                // Method org/mjurisic/aoc2020/day12/Ship.setY:(I)V
      60: nop
      61: iinc          6, 1
      64: goto          25
      67: aload_2
      68: aload_1
      69: invokevirtual #81                 // Method org/mjurisic/aoc2020/day12/Ship.getX:()I
      72: iload         4
      74: iadd
      75: invokevirtual #125                // Method org/mjurisic/aoc2020/day12/Waypoint.setX:(I)V
      78: aload_2
      79: aload_1
      80: invokevirtual #90                 // Method org/mjurisic/aoc2020/day12/Ship.getY:()I
      83: iload         5
      85: iadd
      86: invokevirtual #126                // Method org/mjurisic/aoc2020/day12/Waypoint.setY:(I)V
      89: return

  private final void rotateWaypoint(char, org.mjurisic.aoc2020.day12.Ship, org.mjurisic.aoc2020.day12.Waypoint, int);
    Code:
       0: aload_3
       1: invokevirtual #117                // Method org/mjurisic/aoc2020/day12/Waypoint.getX:()I
       4: aload_2
       5: invokevirtual #81                 // Method org/mjurisic/aoc2020/day12/Ship.getX:()I
       8: isub
       9: istore        5
      11: aload_3
      12: invokevirtual #118                // Method org/mjurisic/aoc2020/day12/Waypoint.getY:()I
      15: aload_2
      16: invokevirtual #90                 // Method org/mjurisic/aoc2020/day12/Ship.getY:()I
      19: isub
      20: istore        6
      22: iload_1
      23: bipush        76
      25: if_icmpne     35
      28: iload         4
      30: bipush        90
      32: if_icmpeq     49
      35: iload_1
      36: bipush        82
      38: if_icmpne     74
      41: iload         4
      43: sipush        270
      46: if_icmpne     74
      49: aload_3
      50: aload_2
      51: invokevirtual #90                 // Method org/mjurisic/aoc2020/day12/Ship.getY:()I
      54: iload         5
      56: isub
      57: invokevirtual #126                // Method org/mjurisic/aoc2020/day12/Waypoint.setY:(I)V
      60: aload_3
      61: aload_2
      62: invokevirtual #81                 // Method org/mjurisic/aoc2020/day12/Ship.getX:()I
      65: iload         6
      67: iadd
      68: invokevirtual #125                // Method org/mjurisic/aoc2020/day12/Waypoint.setX:(I)V
      71: goto          156
      74: iload_1
      75: bipush        82
      77: if_icmpne     87
      80: iload         4
      82: bipush        90
      84: if_icmpeq     101
      87: iload_1
      88: bipush        76
      90: if_icmpne     126
      93: iload         4
      95: sipush        270
      98: if_icmpne     126
     101: aload_3
     102: aload_2
     103: invokevirtual #90                 // Method org/mjurisic/aoc2020/day12/Ship.getY:()I
     106: iload         5
     108: iadd
     109: invokevirtual #126                // Method org/mjurisic/aoc2020/day12/Waypoint.setY:(I)V
     112: aload_3
     113: aload_2
     114: invokevirtual #81                 // Method org/mjurisic/aoc2020/day12/Ship.getX:()I
     117: iload         6
     119: isub
     120: invokevirtual #125                // Method org/mjurisic/aoc2020/day12/Waypoint.setX:(I)V
     123: goto          156
     126: iload         4
     128: sipush        180
     131: if_icmpne     156
     134: aload_3
     135: aload_2
     136: invokevirtual #90                 // Method org/mjurisic/aoc2020/day12/Ship.getY:()I
     139: iload         6
     141: isub
     142: invokevirtual #126                // Method org/mjurisic/aoc2020/day12/Waypoint.setY:(I)V
     145: aload_3
     146: aload_2
     147: invokevirtual #81                 // Method org/mjurisic/aoc2020/day12/Ship.getX:()I
     150: iload         5
     152: isub
     153: invokevirtual #125                // Method org/mjurisic/aoc2020/day12/Waypoint.setX:(I)V
     156: return

  private final void moveWaypointInDirection(char, org.mjurisic.aoc2020.day12.Waypoint, int);
    Code:
       0: iload_1
       1: lookupswitch  { // 4
                    69: 70
                    78: 44
                    83: 57
                    87: 83
               default: 93
          }
      44: aload_2
      45: aload_2
      46: invokevirtual #118                // Method org/mjurisic/aoc2020/day12/Waypoint.getY:()I
      49: iload_3
      50: isub
      51: invokevirtual #126                // Method org/mjurisic/aoc2020/day12/Waypoint.setY:(I)V
      54: goto          93
      57: aload_2
      58: aload_2
      59: invokevirtual #118                // Method org/mjurisic/aoc2020/day12/Waypoint.getY:()I
      62: iload_3
      63: iadd
      64: invokevirtual #126                // Method org/mjurisic/aoc2020/day12/Waypoint.setY:(I)V
      67: goto          93
      70: aload_2
      71: aload_2
      72: invokevirtual #117                // Method org/mjurisic/aoc2020/day12/Waypoint.getX:()I
      75: iload_3
      76: iadd
      77: invokevirtual #125                // Method org/mjurisic/aoc2020/day12/Waypoint.setX:(I)V
      80: goto          93
      83: aload_2
      84: aload_2
      85: invokevirtual #117                // Method org/mjurisic/aoc2020/day12/Waypoint.getX:()I
      88: iload_3
      89: isub
      90: invokevirtual #125                // Method org/mjurisic/aoc2020/day12/Waypoint.setX:(I)V
      93: return

  private static final kotlin.Unit main$lambda$0(org.mjurisic.aoc2020.day12.Waypoint, org.mjurisic.aoc2020.day12.Ship, java.lang.String);
    Code:
       0: aload_2
       1: ldc           #139                // String it
       3: invokestatic  #24                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_2
       7: checkcast     #141                // class java/lang/CharSequence
      10: invokestatic  #147                // Method kotlin/text/StringsKt.first:(Ljava/lang/CharSequence;)C
      13: istore_3
      14: nop
      15: aload_2
      16: iconst_1
      17: aload_2
      18: invokevirtual #152                // Method java/lang/String.length:()I
      21: invokevirtual #156                // Method java/lang/String.substring:(II)Ljava/lang/String;
      24: dup
      25: ldc           #158                // String substring(...)
      27: invokestatic  #161                // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
      30: invokestatic  #167                // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
      33: istore        4
      35: getstatic     #172                // Field org/mjurisic/aoc2020/day12/Day12.Companion:Lorg/mjurisic/aoc2020/day12/Day12$Companion;
      38: iload_3
      39: aload_0
      40: iload         4
      42: invokespecial #174                // Method moveWaypointInDirection:(CLorg/mjurisic/aoc2020/day12/Waypoint;I)V
      45: iload_3
      46: lookupswitch  { // 3
                    70: 80
                    76: 93
                    82: 93
               default: 104
          }
      80: getstatic     #172                // Field org/mjurisic/aoc2020/day12/Day12.Companion:Lorg/mjurisic/aoc2020/day12/Day12$Companion;
      83: aload_1
      84: aload_0
      85: iload         4
      87: invokespecial #176                // Method moveShipForward:(Lorg/mjurisic/aoc2020/day12/Ship;Lorg/mjurisic/aoc2020/day12/Waypoint;I)V
      90: goto          104
      93: getstatic     #172                // Field org/mjurisic/aoc2020/day12/Day12.Companion:Lorg/mjurisic/aoc2020/day12/Day12$Companion;
      96: iload_3
      97: aload_1
      98: aload_0
      99: iload         4
     101: invokespecial #178                // Method rotateWaypoint:(CLorg/mjurisic/aoc2020/day12/Ship;Lorg/mjurisic/aoc2020/day12/Waypoint;I)V
     104: getstatic     #184                // Field kotlin/Unit.INSTANCE:Lkotlin/Unit;
     107: areturn

  public org.mjurisic.aoc2020.day12.Day12$Companion(kotlin.jvm.internal.DefaultConstructorMarker);
    Code:
       0: aload_0
       1: invokespecial #190                // Method "<init>":()V
       4: return
}

Compiled from "Day12.kt"
public final class org.mjurisic.aoc2020.day12.Day12 {
  public static final org.mjurisic.aoc2020.day12.Day12$Companion Companion;

  public org.mjurisic.aoc2020.day12.Day12();
    Code:
       0: aload_0
       1: invokespecial #8                  // Method java/lang/Object."<init>":()V
       4: return

  public static final void main(java.lang.String[]);
    Code:
       0: getstatic     #18                 // Field Companion:Lorg/mjurisic/aoc2020/day12/Day12$Companion;
       3: aload_0
       4: invokevirtual #22                 // Method org/mjurisic/aoc2020/day12/Day12$Companion.main:([Ljava/lang/String;)V
       7: return

  static {};
    Code:
       0: new           #20                 // class org/mjurisic/aoc2020/day12/Day12$Companion
       3: dup
       4: aconst_null
       5: invokespecial #28                 // Method org/mjurisic/aoc2020/day12/Day12$Companion."<init>":(Lkotlin/jvm/internal/DefaultConstructorMarker;)V
       8: putstatic     #18                 // Field Companion:Lorg/mjurisic/aoc2020/day12/Day12$Companion;
      11: return
}


package org.mjurisic.aoc2020.day14

import java.io.File
import java.math.BigInteger

class Day14 {

    companion object {
        @JvmStatic
        fun main(args: Array<String>) = try {

            var mask = ""
            val mem = HashMap<String, BigInteger>()

            File(ClassLoader.getSystemResource("resources/input14.txt").file).forEachLine {
                if (it.startsWith("mask")) {
                    mask = it.substring(7)
                } else {
                    val groups = Regex("mem\\[(\\d+)] = (\\d+)").find(it)!!.groups
                    val address = groups[1]!!.value
                    val value = BigInteger(groups[2]!!.value)

                    writeValues(mem, address, mask, value)
                }

            }

            var sum = BigInteger("0")
            mem.values.forEach {
                sum = sum.add(it)
            }

            println(sum)

        } catch (e: Exception) {
            e.printStackTrace()
        }

        private fun writeValues(
            mem: java.util.HashMap<String, BigInteger>,
            address: String,
            bitmask: String,
            value: BigInteger
        ) {
            val applyBitmask = applyBitmask(bitmask, BigInteger(address))
            val explodeBitmask = explodeBitmask(applyBitmask, 0, listOf(applyBitmask.replace('X', '0')))
            explodeBitmask.forEach{
                mem[it] = value
            }
        }

        private fun explodeBitmask(bitmask: String, start: Int, input: List<String>): List<String> {
            val exploded = ArrayList<String>()

            exploded.addAll(input)
            if (start == bitmask.length) {
                return exploded
            }

            if (bitmask[start] == 'X') {
                for (i in 0 until exploded.size) {
                    val replaced = exploded[i].replaceRange(start, start + 1, "1")
                    exploded.add(replaced)
                }
            }
            return explodeBitmask(bitmask, start + 1, exploded)

        }

        private fun applyBitmask(bitmask: String, address: BigInteger): String {
            var result = ""
            val reversedAddress = address.toString(2).reversed()
            bitmask.reversed().forEachIndexed { i, c ->
                var charValue = '0'
                if (i < reversedAddress.length) {
                    charValue = reversedAddress[i]
                }
                when (c) {
                    '1' -> result += '1'
                    '0' -> result += charValue
                    'X' -> result += 'X'
                }
            }
            return result.reversed()
        }

        private fun calculateValue(mask: String, value: BigInteger): BigInteger {
            var result = ""
            val stringValue = value.toString(2).reversed()
            mask.reversed().forEachIndexed { i, c ->
                var charValue = '0'
                if (i < stringValue.length) {
                    charValue = stringValue[i]
                }
                when (c) {
                    '0' -> result += '0'
                    '1' -> result += '1'
                    'X' -> result += charValue
                }
            }
            return BigInteger(result.reversed(), 2)
        }

    }
}

Compiled from "Day14.kt"
public final class org.mjurisic.aoc2020.day14.Day14$Companion {
  private org.mjurisic.aoc2020.day14.Day14$Companion();
    Code:
       0: aload_0
       1: invokespecial #8                  // Method java/lang/Object."<init>":()V
       4: return

  public final void main(java.lang.String[]);
    Code:
       0: aload_1
       1: ldc           #18                 // String args
       3: invokestatic  #24                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: nop
       7: new           #26                 // class kotlin/jvm/internal/Ref$ObjectRef
      10: dup
      11: invokespecial #27                 // Method kotlin/jvm/internal/Ref$ObjectRef."<init>":()V
      14: astore_2
      15: aload_2
      16: ldc           #29                 // String
      18: putfield      #33                 // Field kotlin/jvm/internal/Ref$ObjectRef.element:Ljava/lang/Object;
      21: new           #35                 // class java/util/HashMap
      24: dup
      25: invokespecial #36                 // Method java/util/HashMap."<init>":()V
      28: astore_3
      29: new           #38                 // class java/io/File
      32: dup
      33: ldc           #40                 // String resources/input14.txt
      35: invokestatic  #46                 // Method java/lang/ClassLoader.getSystemResource:(Ljava/lang/String;)Ljava/net/URL;
      38: invokevirtual #52                 // Method java/net/URL.getFile:()Ljava/lang/String;
      41: invokespecial #55                 // Method java/io/File."<init>":(Ljava/lang/String;)V
      44: aconst_null
      45: aload_2
      46: aload_3
      47: invokedynamic #75,  0             // InvokeDynamic #0:invoke:(Lkotlin/jvm/internal/Ref$ObjectRef;Ljava/util/HashMap;)Lkotlin/jvm/functions/Function1;
      52: iconst_1
      53: aconst_null
      54: invokestatic  #81                 // Method kotlin/io/FilesKt.forEachLine$default:(Ljava/io/File;Ljava/nio/charset/Charset;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)V
      57: aconst_null
      58: astore        4
      60: new           #83                 // class java/math/BigInteger
      63: dup
      64: ldc           #85                 // String 0
      66: invokespecial #86                 // Method java/math/BigInteger."<init>":(Ljava/lang/String;)V
      69: astore        4
      71: aload_3
      72: invokevirtual #90                 // Method java/util/HashMap.values:()Ljava/util/Collection;
      75: dup
      76: ldc           #92                 // String <get-values>(...)
      78: invokestatic  #95                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
      81: checkcast     #97                 // class java/lang/Iterable
      84: astore        5
      86: iconst_0
      87: istore        6
      89: aload         5
      91: invokeinterface #101,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      96: astore        7
      98: aload         7
     100: invokeinterface #107,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
     105: ifeq          147
     108: aload         7
     110: invokeinterface #111,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     115: astore        8
     117: aload         8
     119: checkcast     #83                 // class java/math/BigInteger
     122: astore        9
     124: iconst_0
     125: istore        10
     127: aload         4
     129: aload         9
     131: invokevirtual #115                // Method java/math/BigInteger.add:(Ljava/math/BigInteger;)Ljava/math/BigInteger;
     134: dup
     135: ldc           #117                // String add(...)
     137: invokestatic  #95                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
     140: astore        4
     142: nop
     143: nop
     144: goto          98
     147: nop
     148: aload         4
     150: getstatic     #123                // Field java/lang/System.out:Ljava/io/PrintStream;
     153: swap
     154: invokevirtual #129                // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
     157: goto          165
     160: astore_2
     161: aload_2
     162: invokevirtual #132                // Method java/lang/Exception.printStackTrace:()V
     165: return
    Exception table:
       from    to  target type
           6   157   160   Class java/lang/Exception

  private final void writeValues(java.util.HashMap<java.lang.String, java.math.BigInteger>, java.lang.String, java.lang.String, java.math.BigInteger);
    Code:
       0: aload_0
       1: aload_3
       2: new           #83                 // class java/math/BigInteger
       5: dup
       6: aload_2
       7: invokespecial #86                 // Method java/math/BigInteger."<init>":(Ljava/lang/String;)V
      10: invokespecial #156                // Method applyBitmask:(Ljava/lang/String;Ljava/math/BigInteger;)Ljava/lang/String;
      13: astore        5
      15: aload_0
      16: aload         5
      18: iconst_0
      19: aload         5
      21: bipush        88
      23: bipush        48
      25: iconst_0
      26: iconst_4
      27: aconst_null
      28: invokestatic  #162                // Method kotlin/text/StringsKt.replace$default:(Ljava/lang/String;CCZILjava/lang/Object;)Ljava/lang/String;
      31: invokestatic  #168                // Method kotlin/collections/CollectionsKt.listOf:(Ljava/lang/Object;)Ljava/util/List;
      34: invokespecial #172                // Method explodeBitmask:(Ljava/lang/String;ILjava/util/List;)Ljava/util/List;
      37: astore        6
      39: aload         6
      41: checkcast     #97                 // class java/lang/Iterable
      44: astore        7
      46: iconst_0
      47: istore        8
      49: aload         7
      51: invokeinterface #101,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      56: astore        9
      58: aload         9
      60: invokeinterface #107,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
      65: ifeq          106
      68: aload         9
      70: invokeinterface #111,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      75: astore        10
      77: aload         10
      79: checkcast     #174                // class java/lang/String
      82: astore        11
      84: iconst_0
      85: istore        12
      87: aload_1
      88: checkcast     #176                // class java/util/Map
      91: aload         11
      93: aload         4
      95: invokeinterface #180,  3          // InterfaceMethod java/util/Map.put:(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
     100: pop
     101: nop
     102: nop
     103: goto          58
     106: nop
     107: return

  private final java.util.List<java.lang.String> explodeBitmask(java.lang.String, int, java.util.List<java.lang.String>);
    Code:
       0: new           #191                // class java/util/ArrayList
       3: dup
       4: invokespecial #192                // Method java/util/ArrayList."<init>":()V
       7: astore        4
       9: aload         4
      11: aload_3
      12: checkcast     #194                // class java/util/Collection
      15: invokevirtual #198                // Method java/util/ArrayList.addAll:(Ljava/util/Collection;)Z
      18: pop
      19: iload_2
      20: aload_1
      21: invokevirtual #202                // Method java/lang/String.length:()I
      24: if_icmpne     33
      27: aload         4
      29: checkcast     #188                // class java/util/List
      32: areturn
      33: aload_1
      34: iload_2
      35: invokevirtual #206                // Method java/lang/String.charAt:(I)C
      38: bipush        88
      40: if_icmpne     122
      43: iconst_0
      44: istore        5
      46: aload         4
      48: invokevirtual #209                // Method java/util/ArrayList.size:()I
      51: istore        6
      53: iload         5
      55: iload         6
      57: if_icmpge     122
      60: aload         4
      62: iload         5
      64: invokevirtual #213                // Method java/util/ArrayList.get:(I)Ljava/lang/Object;
      67: dup
      68: ldc           #215                // String get(...)
      70: invokestatic  #95                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
      73: checkcast     #174                // class java/lang/String
      76: astore        8
      78: iload_2
      79: iconst_1
      80: iadd
      81: istore        9
      83: ldc           #217                // String 1
      85: checkcast     #219                // class java/lang/CharSequence
      88: astore        10
      90: aload         8
      92: checkcast     #219                // class java/lang/CharSequence
      95: iload_2
      96: iload         9
      98: aload         10
     100: invokestatic  #223                // Method kotlin/text/StringsKt.replaceRange:(Ljava/lang/CharSequence;IILjava/lang/CharSequence;)Ljava/lang/CharSequence;
     103: invokevirtual #226                // Method java/lang/Object.toString:()Ljava/lang/String;
     106: astore        7
     108: aload         4
     110: aload         7
     112: invokevirtual #229                // Method java/util/ArrayList.add:(Ljava/lang/Object;)Z
     115: pop
     116: iinc          5, 1
     119: goto          53
     122: aload_0
     123: aload_1
     124: iload_2
     125: iconst_1
     126: iadd
     127: aload         4
     129: checkcast     #188                // class java/util/List
     132: invokespecial #172                // Method explodeBitmask:(Ljava/lang/String;ILjava/util/List;)Ljava/util/List;
     135: areturn

  private final java.lang.String applyBitmask(java.lang.String, java.math.BigInteger);
    Code:
       0: aconst_null
       1: astore_3
       2: ldc           #29                 // String
       4: astore_3
       5: aload_2
       6: iconst_2
       7: invokevirtual #238                // Method java/math/BigInteger.toString:(I)Ljava/lang/String;
      10: dup
      11: ldc           #240                // String toString(...)
      13: invokestatic  #95                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
      16: checkcast     #219                // class java/lang/CharSequence
      19: invokestatic  #244                // Method kotlin/text/StringsKt.reversed:(Ljava/lang/CharSequence;)Ljava/lang/CharSequence;
      22: invokevirtual #226                // Method java/lang/Object.toString:()Ljava/lang/String;
      25: astore        4
      27: aload_1
      28: checkcast     #219                // class java/lang/CharSequence
      31: invokestatic  #244                // Method kotlin/text/StringsKt.reversed:(Ljava/lang/CharSequence;)Ljava/lang/CharSequence;
      34: invokevirtual #226                // Method java/lang/Object.toString:()Ljava/lang/String;
      37: checkcast     #219                // class java/lang/CharSequence
      40: astore        5
      42: nop
      43: iconst_0
      44: istore        6
      46: iconst_0
      47: istore        7
      49: iconst_0
      50: istore        8
      52: iload         8
      54: aload         5
      56: invokeinterface #245,  1          // InterfaceMethod java/lang/CharSequence.length:()I
      61: if_icmpge     222
      64: aload         5
      66: iload         8
      68: invokeinterface #246,  2          // InterfaceMethod java/lang/CharSequence.charAt:(I)C
      73: istore        9
      75: iload         7
      77: iinc          7, 1
      80: iload         9
      82: istore        10
      84: istore        11
      86: iconst_0
      87: istore        12
      89: bipush        48
      91: istore        13
      93: iload         11
      95: aload         4
      97: invokevirtual #202                // Method java/lang/String.length:()I
     100: if_icmpge     112
     103: aload         4
     105: iload         11
     107: invokevirtual #206                // Method java/lang/String.charAt:(I)C
     110: istore        13
     112: iload         10
     114: lookupswitch  { // 3
                    48: 171
                    49: 148
                    88: 194
               default: 214
          }
     148: new           #248                // class java/lang/StringBuilder
     151: dup
     152: invokespecial #249                // Method java/lang/StringBuilder."<init>":()V
     155: aload_3
     156: invokevirtual #253                // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     159: bipush        49
     161: invokevirtual #256                // Method java/lang/StringBuilder.append:(C)Ljava/lang/StringBuilder;
     164: invokevirtual #257                // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
     167: astore_3
     168: goto          214
     171: new           #248                // class java/lang/StringBuilder
     174: dup
     175: invokespecial #249                // Method java/lang/StringBuilder."<init>":()V
     178: aload_3
     179: invokevirtual #253                // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     182: iload         13
     184: invokevirtual #256                // Method java/lang/StringBuilder.append:(C)Ljava/lang/StringBuilder;
     187: invokevirtual #257                // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
     190: astore_3
     191: goto          214
     194: new           #248                // class java/lang/StringBuilder
     197: dup
     198: invokespecial #249                // Method java/lang/StringBuilder."<init>":()V
     201: aload_3
     202: invokevirtual #253                // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     205: bipush        88
     207: invokevirtual #256                // Method java/lang/StringBuilder.append:(C)Ljava/lang/StringBuilder;
     210: invokevirtual #257                // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
     213: astore_3
     214: nop
     215: nop
     216: iinc          8, 1
     219: goto          52
     222: nop
     223: aload_3
     224: checkcast     #219                // class java/lang/CharSequence
     227: invokestatic  #244                // Method kotlin/text/StringsKt.reversed:(Ljava/lang/CharSequence;)Ljava/lang/CharSequence;
     230: invokevirtual #226                // Method java/lang/Object.toString:()Ljava/lang/String;
     233: areturn

  private final java.math.BigInteger calculateValue(java.lang.String, java.math.BigInteger);
    Code:
       0: aconst_null
       1: astore_3
       2: ldc           #29                 // String
       4: astore_3
       5: aload_2
       6: iconst_2
       7: invokevirtual #238                // Method java/math/BigInteger.toString:(I)Ljava/lang/String;
      10: dup
      11: ldc           #240                // String toString(...)
      13: invokestatic  #95                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
      16: checkcast     #219                // class java/lang/CharSequence
      19: invokestatic  #244                // Method kotlin/text/StringsKt.reversed:(Ljava/lang/CharSequence;)Ljava/lang/CharSequence;
      22: invokevirtual #226                // Method java/lang/Object.toString:()Ljava/lang/String;
      25: astore        4
      27: aload_1
      28: checkcast     #219                // class java/lang/CharSequence
      31: invokestatic  #244                // Method kotlin/text/StringsKt.reversed:(Ljava/lang/CharSequence;)Ljava/lang/CharSequence;
      34: invokevirtual #226                // Method java/lang/Object.toString:()Ljava/lang/String;
      37: checkcast     #219                // class java/lang/CharSequence
      40: astore        5
      42: nop
      43: iconst_0
      44: istore        6
      46: iconst_0
      47: istore        7
      49: iconst_0
      50: istore        8
      52: iload         8
      54: aload         5
      56: invokeinterface #245,  1          // InterfaceMethod java/lang/CharSequence.length:()I
      61: if_icmpge     222
      64: aload         5
      66: iload         8
      68: invokeinterface #246,  2          // InterfaceMethod java/lang/CharSequence.charAt:(I)C
      73: istore        9
      75: iload         7
      77: iinc          7, 1
      80: iload         9
      82: istore        10
      84: istore        11
      86: iconst_0
      87: istore        12
      89: bipush        48
      91: istore        13
      93: iload         11
      95: aload         4
      97: invokevirtual #202                // Method java/lang/String.length:()I
     100: if_icmpge     112
     103: aload         4
     105: iload         11
     107: invokevirtual #206                // Method java/lang/String.charAt:(I)C
     110: istore        13
     112: iload         10
     114: lookupswitch  { // 3
                    48: 148
                    49: 171
                    88: 194
               default: 214
          }
     148: new           #248                // class java/lang/StringBuilder
     151: dup
     152: invokespecial #249                // Method java/lang/StringBuilder."<init>":()V
     155: aload_3
     156: invokevirtual #253                // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     159: bipush        48
     161: invokevirtual #256                // Method java/lang/StringBuilder.append:(C)Ljava/lang/StringBuilder;
     164: invokevirtual #257                // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
     167: astore_3
     168: goto          214
     171: new           #248                // class java/lang/StringBuilder
     174: dup
     175: invokespecial #249                // Method java/lang/StringBuilder."<init>":()V
     178: aload_3
     179: invokevirtual #253                // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     182: bipush        49
     184: invokevirtual #256                // Method java/lang/StringBuilder.append:(C)Ljava/lang/StringBuilder;
     187: invokevirtual #257                // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
     190: astore_3
     191: goto          214
     194: new           #248                // class java/lang/StringBuilder
     197: dup
     198: invokespecial #249                // Method java/lang/StringBuilder."<init>":()V
     201: aload_3
     202: invokevirtual #253                // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     205: iload         13
     207: invokevirtual #256                // Method java/lang/StringBuilder.append:(C)Ljava/lang/StringBuilder;
     210: invokevirtual #257                // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
     213: astore_3
     214: nop
     215: nop
     216: iinc          8, 1
     219: goto          52
     222: nop
     223: new           #83                 // class java/math/BigInteger
     226: dup
     227: aload_3
     228: checkcast     #219                // class java/lang/CharSequence
     231: invokestatic  #244                // Method kotlin/text/StringsKt.reversed:(Ljava/lang/CharSequence;)Ljava/lang/CharSequence;
     234: invokevirtual #226                // Method java/lang/Object.toString:()Ljava/lang/String;
     237: iconst_2
     238: invokespecial #273                // Method java/math/BigInteger."<init>":(Ljava/lang/String;I)V
     241: areturn

  private static final kotlin.Unit main$lambda$0(kotlin.jvm.internal.Ref$ObjectRef, java.util.HashMap, java.lang.String);
    Code:
       0: aload_2
       1: ldc_w         #276                // String it
       4: invokestatic  #24                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       7: aload_2
       8: ldc_w         #277                // String mask
      11: iconst_0
      12: iconst_2
      13: aconst_null
      14: invokestatic  #281                // Method kotlin/text/StringsKt.startsWith$default:(Ljava/lang/String;Ljava/lang/String;ZILjava/lang/Object;)Z
      17: ifeq          40
      20: aload_0
      21: aload_2
      22: bipush        7
      24: invokevirtual #284                // Method java/lang/String.substring:(I)Ljava/lang/String;
      27: dup
      28: ldc_w         #286                // String substring(...)
      31: invokestatic  #95                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
      34: putfield      #33                 // Field kotlin/jvm/internal/Ref$ObjectRef.element:Ljava/lang/Object;
      37: goto          127
      40: new           #288                // class kotlin/text/Regex
      43: dup
      44: ldc_w         #290                // String mem\\[(\\d+)] = (\\d+)
      47: invokespecial #291                // Method kotlin/text/Regex."<init>":(Ljava/lang/String;)V
      50: aload_2
      51: checkcast     #219                // class java/lang/CharSequence
      54: iconst_0
      55: iconst_2
      56: aconst_null
      57: invokestatic  #295                // Method kotlin/text/Regex.find$default:(Lkotlin/text/Regex;Ljava/lang/CharSequence;IILjava/lang/Object;)Lkotlin/text/MatchResult;
      60: dup
      61: invokestatic  #298                // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
      64: invokeinterface #304,  1          // InterfaceMethod kotlin/text/MatchResult.getGroups:()Lkotlin/text/MatchGroupCollection;
      69: astore_3
      70: aload_3
      71: iconst_1
      72: invokeinterface #309,  2          // InterfaceMethod kotlin/text/MatchGroupCollection.get:(I)Lkotlin/text/MatchGroup;
      77: dup
      78: invokestatic  #298                // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
      81: invokevirtual #314                // Method kotlin/text/MatchGroup.getValue:()Ljava/lang/String;
      84: astore        4
      86: new           #83                 // class java/math/BigInteger
      89: dup
      90: aload_3
      91: iconst_2
      92: invokeinterface #309,  2          // InterfaceMethod kotlin/text/MatchGroupCollection.get:(I)Lkotlin/text/MatchGroup;
      97: dup
      98: invokestatic  #298                // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
     101: invokevirtual #314                // Method kotlin/text/MatchGroup.getValue:()Ljava/lang/String;
     104: invokespecial #86                 // Method java/math/BigInteger."<init>":(Ljava/lang/String;)V
     107: astore        5
     109: getstatic     #319                // Field org/mjurisic/aoc2020/day14/Day14.Companion:Lorg/mjurisic/aoc2020/day14/Day14$Companion;
     112: aload_1
     113: aload         4
     115: aload_0
     116: getfield      #33                 // Field kotlin/jvm/internal/Ref$ObjectRef.element:Ljava/lang/Object;
     119: checkcast     #174                // class java/lang/String
     122: aload         5
     124: invokespecial #321                // Method writeValues:(Ljava/util/HashMap;Ljava/lang/String;Ljava/lang/String;Ljava/math/BigInteger;)V
     127: getstatic     #327                // Field kotlin/Unit.INSTANCE:Lkotlin/Unit;
     130: areturn

  public org.mjurisic.aoc2020.day14.Day14$Companion(kotlin.jvm.internal.DefaultConstructorMarker);
    Code:
       0: aload_0
       1: invokespecial #333                // Method "<init>":()V
       4: return
}

Compiled from "Day14.kt"
public final class org.mjurisic.aoc2020.day14.Day14 {
  public static final org.mjurisic.aoc2020.day14.Day14$Companion Companion;

  public org.mjurisic.aoc2020.day14.Day14();
    Code:
       0: aload_0
       1: invokespecial #8                  // Method java/lang/Object."<init>":()V
       4: return

  public static final void main(java.lang.String[]);
    Code:
       0: getstatic     #18                 // Field Companion:Lorg/mjurisic/aoc2020/day14/Day14$Companion;
       3: aload_0
       4: invokevirtual #22                 // Method org/mjurisic/aoc2020/day14/Day14$Companion.main:([Ljava/lang/String;)V
       7: return

  static {};
    Code:
       0: new           #20                 // class org/mjurisic/aoc2020/day14/Day14$Companion
       3: dup
       4: aconst_null
       5: invokespecial #28                 // Method org/mjurisic/aoc2020/day14/Day14$Companion."<init>":(Lkotlin/jvm/internal/DefaultConstructorMarker;)V
       8: putstatic     #18                 // Field Companion:Lorg/mjurisic/aoc2020/day14/Day14$Companion;
      11: return
}