package org.clechasseur.adventofcode2017.math

import kotlin.math.abs

fun <T> permutations(elements: List<T>): Sequence<List<T>> {
    if (elements.size == 1) {
        return sequenceOf(listOf(elements.first()))
    }

    return elements.asSequence().flatMap { elem ->
        val subIt = permutations(elements - elem).iterator()
        generateSequence { when (subIt.hasNext()) {
            true -> listOf(elem) + subIt.next()
            false -> null
        } }
    }
}

fun generatePairSequence(firstRange: IntRange, secondRange: IntRange): Sequence<Pair<Int, Int>> {
    return generateSequence(firstRange.first to secondRange.first) { when (it.second) {
        secondRange.last -> when (it.first) {
            firstRange.last -> null
            else -> it.first + 1 to secondRange.first
        }
        else -> it.first to it.second + 1
    } }
}

fun factors(n: Long): List<Long> {
    return generateSequence(1L) { when {
        it < n -> it + 1L
        else -> null
    } }.filter { n % it == 0L }.toList()
}

fun greatestCommonDenominator(a: Long, b: Long): Long {
    // https://en.wikipedia.org/wiki/Euclidean_algorithm
    require(a > 0L && b > 0L) { "Can only find GCD for positive numbers" }
    var rm = b
    var r = a % rm
    while (r != 0L) {
        val rm2 = rm
        rm = r
        r = rm2 % rm
    }
    return rm
}

fun leastCommonMultiple(a: Long, b: Long): Long {
    // https://en.wikipedia.org/wiki/Least_common_multiple
    require(a > 0L && b > 0L) { "Can only find LCM for positive numbers" }
    return a / greatestCommonDenominator(a, b) * b
}

fun reduceFraction(numerator: Long, denominator: Long): Pair<Long, Long> {
    require(denominator != 0L) { "Divide by zero error" }
    return when (numerator) {
        0L -> 0L to 1L
        else -> {
            val gcd = greatestCommonDenominator(abs(numerator), abs(denominator))
            (numerator / gcd) to (denominator / gcd)
        }
    }
}

Compiled from "Math.kt"
public final class org.clechasseur.adventofcode2017.math.MathKt {
  public static final <T> kotlin.sequences.Sequence<java.util.List<T>> permutations(java.util.List<? extends T>);
    Code:
       0: aload_0
       1: ldc           #10                 // String elements
       3: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: invokeinterface #22,  1           // InterfaceMethod java/util/List.size:()I
      12: iconst_1
      13: if_icmpne     36
      16: iconst_1
      17: anewarray     #18                 // class java/util/List
      20: astore_1
      21: aload_1
      22: iconst_0
      23: aload_0
      24: invokestatic  #28                 // Method kotlin/collections/CollectionsKt.first:(Ljava/util/List;)Ljava/lang/Object;
      27: invokestatic  #32                 // Method kotlin/collections/CollectionsKt.listOf:(Ljava/lang/Object;)Ljava/util/List;
      30: aastore
      31: aload_1
      32: invokestatic  #38                 // Method kotlin/sequences/SequencesKt.sequenceOf:([Ljava/lang/Object;)Lkotlin/sequences/Sequence;
      35: areturn
      36: aload_0
      37: checkcast     #40                 // class java/lang/Iterable
      40: invokestatic  #44                 // Method kotlin/collections/CollectionsKt.asSequence:(Ljava/lang/Iterable;)Lkotlin/sequences/Sequence;
      43: aload_0
      44: invokedynamic #64,  0             // InvokeDynamic #0:invoke:(Ljava/util/List;)Lkotlin/jvm/functions/Function1;
      49: invokestatic  #68                 // Method kotlin/sequences/SequencesKt.flatMap:(Lkotlin/sequences/Sequence;Lkotlin/jvm/functions/Function1;)Lkotlin/sequences/Sequence;
      52: areturn

  public static final kotlin.sequences.Sequence<kotlin.Pair<java.lang.Integer, java.lang.Integer>> generatePairSequence(kotlin.ranges.IntRange, kotlin.ranges.IntRange);
    Code:
       0: aload_0
       1: ldc           #74                 // String firstRange
       3: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_1
       7: ldc           #76                 // String secondRange
       9: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
      12: aload_0
      13: invokevirtual #81                 // Method kotlin/ranges/IntRange.getFirst:()I
      16: invokestatic  #87                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      19: aload_1
      20: invokevirtual #81                 // Method kotlin/ranges/IntRange.getFirst:()I
      23: invokestatic  #87                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      26: invokestatic  #93                 // Method kotlin/TuplesKt.to:(Ljava/lang/Object;Ljava/lang/Object;)Lkotlin/Pair;
      29: aload_1
      30: aload_0
      31: invokedynamic #103,  0            // InvokeDynamic #1:invoke:(Lkotlin/ranges/IntRange;Lkotlin/ranges/IntRange;)Lkotlin/jvm/functions/Function1;
      36: invokestatic  #107                // Method kotlin/sequences/SequencesKt.generateSequence:(Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Lkotlin/sequences/Sequence;
      39: areturn

  public static final java.util.List<java.lang.Long> factors(long);
    Code:
       0: lconst_1
       1: invokestatic  #116                // Method java/lang/Long.valueOf:(J)Ljava/lang/Long;
       4: lload_0
       5: invokedynamic #126,  0            // InvokeDynamic #2:invoke:(J)Lkotlin/jvm/functions/Function1;
      10: invokestatic  #107                // Method kotlin/sequences/SequencesKt.generateSequence:(Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Lkotlin/sequences/Sequence;
      13: lload_0
      14: invokedynamic #134,  0            // InvokeDynamic #3:invoke:(J)Lkotlin/jvm/functions/Function1;
      19: invokestatic  #137                // Method kotlin/sequences/SequencesKt.filter:(Lkotlin/sequences/Sequence;Lkotlin/jvm/functions/Function1;)Lkotlin/sequences/Sequence;
      22: invokestatic  #141                // Method kotlin/sequences/SequencesKt.toList:(Lkotlin/sequences/Sequence;)Ljava/util/List;
      25: areturn

  public static final long greatestCommonDenominator(long, long);
    Code:
       0: lload_0
       1: lconst_0
       2: lcmp
       3: ifle          16
       6: lload_2
       7: lconst_0
       8: lcmp
       9: ifle          16
      12: iconst_1
      13: goto          17
      16: iconst_0
      17: ifne          40
      20: iconst_0
      21: istore        5
      23: ldc           #147                // String Can only find GCD for positive numbers
      25: astore        5
      27: new           #149                // class java/lang/IllegalArgumentException
      30: dup
      31: aload         5
      33: invokevirtual #153                // Method java/lang/Object.toString:()Ljava/lang/String;
      36: invokespecial #157                // Method java/lang/IllegalArgumentException."<init>":(Ljava/lang/String;)V
      39: athrow
      40: lload_2
      41: lstore        4
      43: lload_0
      44: lload         4
      46: lrem
      47: lstore        6
      49: lload         6
      51: lconst_0
      52: lcmp
      53: ifeq          74
      56: lload         4
      58: lstore        8
      60: lload         6
      62: lstore        4
      64: lload         8
      66: lload         4
      68: lrem
      69: lstore        6
      71: goto          49
      74: lload         4
      76: lreturn

  public static final long leastCommonMultiple(long, long);
    Code:
       0: lload_0
       1: lconst_0
       2: lcmp
       3: ifle          16
       6: lload_2
       7: lconst_0
       8: lcmp
       9: ifle          16
      12: iconst_1
      13: goto          17
      16: iconst_0
      17: ifne          40
      20: iconst_0
      21: istore        4
      23: ldc           #167                // String Can only find LCM for positive numbers
      25: astore        4
      27: new           #149                // class java/lang/IllegalArgumentException
      30: dup
      31: aload         4
      33: invokevirtual #153                // Method java/lang/Object.toString:()Ljava/lang/String;
      36: invokespecial #157                // Method java/lang/IllegalArgumentException."<init>":(Ljava/lang/String;)V
      39: athrow
      40: lload_0
      41: lload_0
      42: lload_2
      43: invokestatic  #169                // Method greatestCommonDenominator:(JJ)J
      46: ldiv
      47: lload_2
      48: lmul
      49: lreturn

  public static final kotlin.Pair<java.lang.Long, java.lang.Long> reduceFraction(long, long);
    Code:
       0: lload_2
       1: lconst_0
       2: lcmp
       3: ifeq          10
       6: iconst_1
       7: goto          11
      10: iconst_0
      11: ifne          34
      14: iconst_0
      15: istore        4
      17: ldc           #175                // String Divide by zero error
      19: astore        4
      21: new           #149                // class java/lang/IllegalArgumentException
      24: dup
      25: aload         4
      27: invokevirtual #153                // Method java/lang/Object.toString:()Ljava/lang/String;
      30: invokespecial #157                // Method java/lang/IllegalArgumentException."<init>":(Ljava/lang/String;)V
      33: athrow
      34: lload_0
      35: lconst_0
      36: lcmp
      37: ifne          54
      40: lconst_0
      41: invokestatic  #116                // Method java/lang/Long.valueOf:(J)Ljava/lang/Long;
      44: lconst_1
      45: invokestatic  #116                // Method java/lang/Long.valueOf:(J)Ljava/lang/Long;
      48: invokestatic  #93                 // Method kotlin/TuplesKt.to:(Ljava/lang/Object;Ljava/lang/Object;)Lkotlin/Pair;
      51: goto          84
      54: lload_0
      55: invokestatic  #181                // Method java/lang/Math.abs:(J)J
      58: lload_2
      59: invokestatic  #181                // Method java/lang/Math.abs:(J)J
      62: invokestatic  #169                // Method greatestCommonDenominator:(JJ)J
      65: lstore        5
      67: lload_0
      68: lload         5
      70: ldiv
      71: invokestatic  #116                // Method java/lang/Long.valueOf:(J)Ljava/lang/Long;
      74: lload_2
      75: lload         5
      77: ldiv
      78: invokestatic  #116                // Method java/lang/Long.valueOf:(J)Ljava/lang/Long;
      81: invokestatic  #93                 // Method kotlin/TuplesKt.to:(Ljava/lang/Object;Ljava/lang/Object;)Lkotlin/Pair;
      84: areturn

  private static final java.util.List permutations$lambda$1$lambda$0(java.util.Iterator, java.lang.Object);
    Code:
       0: aload_0
       1: invokeinterface #195,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
       6: istore_2
       7: iload_2
       8: iconst_1
       9: if_icmpne     34
      12: aload_1
      13: invokestatic  #32                 // Method kotlin/collections/CollectionsKt.listOf:(Ljava/lang/Object;)Ljava/util/List;
      16: checkcast     #197                // class java/util/Collection
      19: aload_0
      20: invokeinterface #201,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      25: checkcast     #40                 // class java/lang/Iterable
      28: invokestatic  #205                // Method kotlin/collections/CollectionsKt.plus:(Ljava/util/Collection;Ljava/lang/Iterable;)Ljava/util/List;
      31: goto          50
      34: iload_2
      35: ifne          42
      38: aconst_null
      39: goto          50
      42: new           #207                // class kotlin/NoWhenBranchMatchedException
      45: dup
      46: invokespecial #210                // Method kotlin/NoWhenBranchMatchedException."<init>":()V
      49: athrow
      50: areturn

  private static final kotlin.sequences.Sequence permutations$lambda$1(java.util.List, java.lang.Object);
    Code:
       0: aload_0
       1: checkcast     #40                 // class java/lang/Iterable
       4: aload_1
       5: invokestatic  #218                // Method kotlin/collections/CollectionsKt.minus:(Ljava/lang/Iterable;Ljava/lang/Object;)Ljava/util/List;
       8: invokestatic  #220                // Method permutations:(Ljava/util/List;)Lkotlin/sequences/Sequence;
      11: invokeinterface #226,  1          // InterfaceMethod kotlin/sequences/Sequence.iterator:()Ljava/util/Iterator;
      16: astore_2
      17: aload_2
      18: aload_1
      19: invokedynamic #235,  0            // InvokeDynamic #4:invoke:(Ljava/util/Iterator;Ljava/lang/Object;)Lkotlin/jvm/functions/Function0;
      24: invokestatic  #238                // Method kotlin/sequences/SequencesKt.generateSequence:(Lkotlin/jvm/functions/Function0;)Lkotlin/sequences/Sequence;
      27: areturn

  private static final kotlin.Pair generatePairSequence$lambda$2(kotlin.ranges.IntRange, kotlin.ranges.IntRange, kotlin.Pair);
    Code:
       0: aload_2
       1: ldc           #243                // String it
       3: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_2
       7: invokevirtual #246                // Method kotlin/Pair.getSecond:()Ljava/lang/Object;
      10: checkcast     #248                // class java/lang/Number
      13: invokevirtual #251                // Method java/lang/Number.intValue:()I
      16: aload_0
      17: invokevirtual #254                // Method kotlin/ranges/IntRange.getLast:()I
      20: if_icmpne     72
      23: aload_2
      24: invokevirtual #256                // Method kotlin/Pair.getFirst:()Ljava/lang/Object;
      27: checkcast     #248                // class java/lang/Number
      30: invokevirtual #251                // Method java/lang/Number.intValue:()I
      33: aload_1
      34: invokevirtual #254                // Method kotlin/ranges/IntRange.getLast:()I
      37: if_icmpne     44
      40: aconst_null
      41: goto          94
      44: aload_2
      45: invokevirtual #256                // Method kotlin/Pair.getFirst:()Ljava/lang/Object;
      48: checkcast     #248                // class java/lang/Number
      51: invokevirtual #251                // Method java/lang/Number.intValue:()I
      54: iconst_1
      55: iadd
      56: invokestatic  #87                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      59: aload_0
      60: invokevirtual #81                 // Method kotlin/ranges/IntRange.getFirst:()I
      63: invokestatic  #87                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      66: invokestatic  #93                 // Method kotlin/TuplesKt.to:(Ljava/lang/Object;Ljava/lang/Object;)Lkotlin/Pair;
      69: goto          94
      72: aload_2
      73: invokevirtual #256                // Method kotlin/Pair.getFirst:()Ljava/lang/Object;
      76: aload_2
      77: invokevirtual #246                // Method kotlin/Pair.getSecond:()Ljava/lang/Object;
      80: checkcast     #248                // class java/lang/Number
      83: invokevirtual #251                // Method java/lang/Number.intValue:()I
      86: iconst_1
      87: iadd
      88: invokestatic  #87                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      91: invokestatic  #93                 // Method kotlin/TuplesKt.to:(Ljava/lang/Object;Ljava/lang/Object;)Lkotlin/Pair;
      94: areturn

  private static final java.lang.Long factors$lambda$3(long, long);
    Code:
       0: nop
       1: lload_2
       2: lload_0
       3: lcmp
       4: ifge          16
       7: lload_2
       8: lconst_1
       9: ladd
      10: invokestatic  #116                // Method java/lang/Long.valueOf:(J)Ljava/lang/Long;
      13: goto          17
      16: aconst_null
      17: areturn

  private static final boolean factors$lambda$4(long, long);
    Code:
       0: lload_0
       1: lload_2
       2: lrem
       3: lconst_0
       4: lcmp
       5: ifne          12
       8: iconst_1
       9: goto          13
      12: iconst_0
      13: ireturn
}


package day09

import java.io.File

fun main() {
    val numbers = File("src/main/kotlin/day09/day09-input.txt").readLines().map { it.toLong() }.toLongArray()
    val window = 25

    val invalidNumber = answer1(numbers, window)
    println("Answer 1: $invalidNumber")
    println("Answer 2: " + answer2(numbers, invalidNumber))
}

private fun answer1(numbers: LongArray, window: Int): Long {
    for (start in 0 until numbers.size - window) {
        val next = numbers[start + window]
        if (!numbers.containsSum(start, start + window, next)) {
            return next
        }
    }
    error("No answer found")
}

private fun LongArray.containsSum(start: Int, end: Int, sum: Long): Boolean {
    for (i in start until end) {
        val first = get(i)
        for (j in i + 1 until end) {
            val second = get(j)
            if (first != second && first + second == sum) {
                return true
            }
        }
    }
    return false
}

private fun answer2(numbers: LongArray, sum: Long): Long {
    for (start in numbers.indices) {
        numbers.findRangeSumsTo(start, sum)?.let {
            return it.minOrNull()!! + it.maxOrNull()!!
        }
    }
    error("No answer found")
}

private fun LongArray.findRangeSumsTo(start: Int, sum: Long): List<Long>? {
    val range = mutableListOf<Long>()
    var rangeSum = 0L
        for (i in start until size) {
            range += get(i)
            rangeSum += get(i)
            if (rangeSum == sum) {
                return range
            }
            if (rangeSum > sum) {
                return null
            }
        }
    return null
}

Compiled from "Day09.kt"
public final class day09.Day09Kt {
  public static final void main();
    Code:
       0: new           #8                  // class java/io/File
       3: dup
       4: ldc           #10                 // String src/main/kotlin/day09/day09-input.txt
       6: invokespecial #14                 // Method java/io/File."<init>":(Ljava/lang/String;)V
       9: aconst_null
      10: iconst_1
      11: aconst_null
      12: invokestatic  #20                 // Method kotlin/io/FilesKt.readLines$default:(Ljava/io/File;Ljava/nio/charset/Charset;ILjava/lang/Object;)Ljava/util/List;
      15: checkcast     #22                 // class java/lang/Iterable
      18: astore_1
      19: iconst_0
      20: istore_2
      21: aload_1
      22: astore_3
      23: new           #24                 // class java/util/ArrayList
      26: dup
      27: aload_1
      28: bipush        10
      30: invokestatic  #30                 // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
      33: invokespecial #33                 // Method java/util/ArrayList."<init>":(I)V
      36: checkcast     #35                 // class java/util/Collection
      39: astore        4
      41: iconst_0
      42: istore        5
      44: aload_3
      45: invokeinterface #39,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      50: astore        6
      52: aload         6
      54: invokeinterface #45,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      59: ifeq          106
      62: aload         6
      64: invokeinterface #49,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      69: astore        7
      71: aload         4
      73: aload         7
      75: checkcast     #51                 // class java/lang/String
      78: astore        8
      80: astore        10
      82: iconst_0
      83: istore        9
      85: aload         8
      87: invokestatic  #57                 // Method java/lang/Long.parseLong:(Ljava/lang/String;)J
      90: nop
      91: invokestatic  #61                 // Method java/lang/Long.valueOf:(J)Ljava/lang/Long;
      94: aload         10
      96: swap
      97: invokeinterface #65,  2           // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     102: pop
     103: goto          52
     106: aload         4
     108: checkcast     #67                 // class java/util/List
     111: nop
     112: checkcast     #35                 // class java/util/Collection
     115: invokestatic  #71                 // Method kotlin/collections/CollectionsKt.toLongArray:(Ljava/util/Collection;)[J
     118: astore_0
     119: bipush        25
     121: istore_1
     122: aload_0
     123: iload_1
     124: invokestatic  #75                 // Method answer1:([JI)J
     127: lstore_2
     128: new           #77                 // class java/lang/StringBuilder
     131: dup
     132: invokespecial #79                 // Method java/lang/StringBuilder."<init>":()V
     135: ldc           #81                 // String Answer 1:
     137: invokevirtual #85                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     140: lload_2
     141: invokevirtual #88                 // Method java/lang/StringBuilder.append:(J)Ljava/lang/StringBuilder;
     144: invokevirtual #92                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
     147: getstatic     #98                 // Field java/lang/System.out:Ljava/io/PrintStream;
     150: swap
     151: invokevirtual #104                // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
     154: new           #77                 // class java/lang/StringBuilder
     157: dup
     158: invokespecial #79                 // Method java/lang/StringBuilder."<init>":()V
     161: ldc           #106                // String Answer 2:
     163: invokevirtual #85                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     166: aload_0
     167: lload_2
     168: invokestatic  #110                // Method answer2:([JJ)J
     171: invokevirtual #88                 // Method java/lang/StringBuilder.append:(J)Ljava/lang/StringBuilder;
     174: invokevirtual #92                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
     177: getstatic     #98                 // Field java/lang/System.out:Ljava/io/PrintStream;
     180: swap
     181: invokevirtual #104                // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
     184: return

  private static final long answer1(long[], int);
    Code:
       0: iconst_0
       1: istore_2
       2: aload_0
       3: arraylength
       4: iload_1
       5: isub
       6: istore_3
       7: iload_2
       8: iload_3
       9: if_icmpge     41
      12: aload_0
      13: iload_2
      14: iload_1
      15: iadd
      16: laload
      17: lstore        4
      19: aload_0
      20: iload_2
      21: iload_2
      22: iload_1
      23: iadd
      24: lload         4
      26: invokestatic  #132                // Method containsSum:([JIIJ)Z
      29: ifne          35
      32: lload         4
      34: lreturn
      35: iinc          2, 1
      38: goto          7
      41: new           #134                // class java/lang/IllegalStateException
      44: dup
      45: ldc           #136                // String No answer found
      47: invokevirtual #137                // Method java/lang/Object.toString:()Ljava/lang/String;
      50: invokespecial #138                // Method java/lang/IllegalStateException."<init>":(Ljava/lang/String;)V
      53: athrow

  private static final boolean containsSum(long[], int, int, long);
    Code:
       0: iload_1
       1: istore        5
       3: iload         5
       5: iload_2
       6: if_icmpge     65
       9: aload_0
      10: iload         5
      12: laload
      13: lstore        6
      15: iload         5
      17: iconst_1
      18: iadd
      19: istore        8
      21: iload         8
      23: iload_2
      24: if_icmpge     59
      27: aload_0
      28: iload         8
      30: laload
      31: lstore        9
      33: lload         6
      35: lload         9
      37: lcmp
      38: ifeq          53
      41: lload         6
      43: lload         9
      45: ladd
      46: lload_3
      47: lcmp
      48: ifne          53
      51: iconst_1
      52: ireturn
      53: iinc          8, 1
      56: goto          21
      59: iinc          5, 1
      62: goto          3
      65: iconst_0
      66: ireturn

  private static final long answer2(long[], long);
    Code:
       0: iconst_0
       1: istore_3
       2: aload_0
       3: arraylength
       4: istore        4
       6: iload_3
       7: iload         4
       9: if_icmpge     76
      12: aload_0
      13: iload_3
      14: lload_1
      15: invokestatic  #150                // Method findRangeSumsTo:([JIJ)Ljava/util/List;
      18: astore        5
      20: aload         5
      22: ifnull        70
      25: aload         5
      27: astore        6
      29: iconst_0
      30: istore        7
      32: aload         6
      34: checkcast     #22                 // class java/lang/Iterable
      37: invokestatic  #154                // Method kotlin/collections/CollectionsKt.minOrNull:(Ljava/lang/Iterable;)Ljava/lang/Comparable;
      40: dup
      41: invokestatic  #159                // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
      44: checkcast     #161                // class java/lang/Number
      47: invokevirtual #165                // Method java/lang/Number.longValue:()J
      50: aload         6
      52: checkcast     #22                 // class java/lang/Iterable
      55: invokestatic  #168                // Method kotlin/collections/CollectionsKt.maxOrNull:(Ljava/lang/Iterable;)Ljava/lang/Comparable;
      58: dup
      59: invokestatic  #159                // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
      62: checkcast     #161                // class java/lang/Number
      65: invokevirtual #165                // Method java/lang/Number.longValue:()J
      68: ladd
      69: lreturn
      70: iinc          3, 1
      73: goto          6
      76: new           #134                // class java/lang/IllegalStateException
      79: dup
      80: ldc           #136                // String No answer found
      82: invokevirtual #137                // Method java/lang/Object.toString:()Ljava/lang/String;
      85: invokespecial #138                // Method java/lang/IllegalStateException."<init>":(Ljava/lang/String;)V
      88: athrow

  private static final java.util.List<java.lang.Long> findRangeSumsTo(long[], int, long);
    Code:
       0: new           #24                 // class java/util/ArrayList
       3: dup
       4: invokespecial #172                // Method java/util/ArrayList."<init>":()V
       7: checkcast     #67                 // class java/util/List
      10: astore        4
      12: lconst_0
      13: lstore        5
      15: iload_1
      16: istore        7
      18: aload_0
      19: arraylength
      20: istore        8
      22: iload         7
      24: iload         8
      26: if_icmpge     81
      29: aload         4
      31: checkcast     #35                 // class java/util/Collection
      34: aload_0
      35: iload         7
      37: laload
      38: invokestatic  #61                 // Method java/lang/Long.valueOf:(J)Ljava/lang/Long;
      41: invokeinterface #65,  2           // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
      46: pop
      47: lload         5
      49: aload_0
      50: iload         7
      52: laload
      53: ladd
      54: lstore        5
      56: lload         5
      58: lload_2
      59: lcmp
      60: ifne          66
      63: aload         4
      65: areturn
      66: lload         5
      68: lload_2
      69: lcmp
      70: ifle          75
      73: aconst_null
      74: areturn
      75: iinc          7, 1
      78: goto          22
      81: aconst_null
      82: areturn

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


package day08

import java.io.File

data class Operation(val name: String, val argument: Int)

class Program(private val operations: List<Operation>) {
    private var instruction = 0
    private var accumulator = 0
    private val visited = BooleanArray(operations.size)

    fun run(): Int {
        while (instruction < operations.size && !visited[instruction]) {
            visited[instruction] = true
            val operation = operations[instruction]
            when (operation.name) {
                "nop" -> instruction += 1
                "acc" -> {
                    accumulator += operation.argument
                    instruction += 1
                }
                "jmp" -> instruction += operation.argument
                else -> error("unknown operation: ${operation.name}")
            }
        }
        return accumulator
    }

    fun terminated() = instruction >= operations.size
}

fun main() {
    val operations = File("src/main/kotlin/day08/day08-input.txt").readLines().map {
        val (operationName, argument) = it.split(" ")
        Operation(operationName, argument.toInt())
    }

    println("Answer 1: " + operations.run())
    println("Answer 2: " + operations.modifyAndRun())
}

fun List<Operation>.run() = Program(this).run()

fun List<Operation>.modifyAndRun(): Int? {
    forEachIndexed { index, operation ->
        when (operation.name) {
            "jmp", "nop" -> {
                val modifiedOperations = toMutableList()
                val modifiedOperationName = when (operation.name) {
                    "jmp" -> "nop"
                    "nop" -> "jmp"
                    else -> operation.name
                }
                modifiedOperations[index] = Operation(modifiedOperationName, operation.argument)

                val program = Program(modifiedOperations)
                val result = program.run()

                if (program.terminated()) {
                    return result
                }
            }
        }
    }
    return null
}

Compiled from "Day08.kt"
public final class day08.Day08Kt {
  public static final void main();
    Code:
       0: new           #8                  // class java/io/File
       3: dup
       4: ldc           #10                 // String src/main/kotlin/day08/day08-input.txt
       6: invokespecial #14                 // Method java/io/File."<init>":(Ljava/lang/String;)V
       9: aconst_null
      10: iconst_1
      11: aconst_null
      12: invokestatic  #20                 // Method kotlin/io/FilesKt.readLines$default:(Ljava/io/File;Ljava/nio/charset/Charset;ILjava/lang/Object;)Ljava/util/List;
      15: checkcast     #22                 // class java/lang/Iterable
      18: astore_1
      19: iconst_0
      20: istore_2
      21: aload_1
      22: astore_3
      23: new           #24                 // class java/util/ArrayList
      26: dup
      27: aload_1
      28: bipush        10
      30: invokestatic  #30                 // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
      33: invokespecial #33                 // Method java/util/ArrayList."<init>":(I)V
      36: checkcast     #35                 // class java/util/Collection
      39: astore        4
      41: iconst_0
      42: istore        5
      44: aload_3
      45: invokeinterface #39,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      50: astore        6
      52: aload         6
      54: invokeinterface #45,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      59: ifeq          166
      62: aload         6
      64: invokeinterface #49,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      69: astore        7
      71: aload         4
      73: aload         7
      75: checkcast     #51                 // class java/lang/String
      78: astore        8
      80: astore        13
      82: iconst_0
      83: istore        9
      85: aload         8
      87: checkcast     #53                 // class java/lang/CharSequence
      90: iconst_1
      91: anewarray     #51                 // class java/lang/String
      94: astore        10
      96: aload         10
      98: iconst_0
      99: ldc           #55                 // String
     101: aastore
     102: aload         10
     104: iconst_0
     105: iconst_0
     106: bipush        6
     108: aconst_null
     109: invokestatic  #61                 // Method kotlin/text/StringsKt.split$default:(Ljava/lang/CharSequence;[Ljava/lang/String;ZIILjava/lang/Object;)Ljava/util/List;
     112: astore        11
     114: aload         11
     116: iconst_0
     117: invokeinterface #67,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     122: checkcast     #51                 // class java/lang/String
     125: astore        10
     127: aload         11
     129: iconst_1
     130: invokeinterface #67,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     135: checkcast     #51                 // class java/lang/String
     138: astore        12
     140: new           #69                 // class day08/Operation
     143: dup
     144: aload         10
     146: aload         12
     148: invokestatic  #75                 // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
     151: invokespecial #78                 // Method day08/Operation."<init>":(Ljava/lang/String;I)V
     154: aload         13
     156: swap
     157: invokeinterface #82,  2           // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     162: pop
     163: goto          52
     166: aload         4
     168: checkcast     #63                 // class java/util/List
     171: nop
     172: astore_0
     173: new           #84                 // class java/lang/StringBuilder
     176: dup
     177: invokespecial #86                 // Method java/lang/StringBuilder."<init>":()V
     180: ldc           #88                 // String Answer 1:
     182: invokevirtual #92                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     185: aload_0
     186: invokestatic  #96                 // Method run:(Ljava/util/List;)I
     189: invokevirtual #99                 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
     192: invokevirtual #103                // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
     195: getstatic     #109                // Field java/lang/System.out:Ljava/io/PrintStream;
     198: swap
     199: invokevirtual #115                // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
     202: new           #84                 // class java/lang/StringBuilder
     205: dup
     206: invokespecial #86                 // Method java/lang/StringBuilder."<init>":()V
     209: ldc           #117                // String Answer 2:
     211: invokevirtual #92                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     214: aload_0
     215: invokestatic  #121                // Method modifyAndRun:(Ljava/util/List;)Ljava/lang/Integer;
     218: invokevirtual #124                // Method java/lang/StringBuilder.append:(Ljava/lang/Object;)Ljava/lang/StringBuilder;
     221: invokevirtual #103                // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
     224: getstatic     #109                // Field java/lang/System.out:Ljava/io/PrintStream;
     227: swap
     228: invokevirtual #115                // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
     231: return

  public static final int run(java.util.List<day08.Operation>);
    Code:
       0: aload_0
       1: ldc           #145                // String <this>
       3: invokestatic  #151                // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: new           #153                // class day08/Program
       9: dup
      10: aload_0
      11: invokespecial #156                // Method day08/Program."<init>":(Ljava/util/List;)V
      14: invokevirtual #159                // Method day08/Program.run:()I
      17: ireturn

  public static final java.lang.Integer modifyAndRun(java.util.List<day08.Operation>);
    Code:
       0: aload_0
       1: ldc           #145                // String <this>
       3: invokestatic  #151                // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: checkcast     #22                 // class java/lang/Iterable
      10: astore_1
      11: iconst_0
      12: istore_2
      13: iconst_0
      14: istore_3
      15: aload_1
      16: invokeinterface #39,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      21: astore        4
      23: aload         4
      25: invokeinterface #45,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      30: ifeq          211
      33: aload         4
      35: invokeinterface #49,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      40: astore        5
      42: iload_3
      43: iinc          3, 1
      46: istore        6
      48: iload         6
      50: ifge          56
      53: invokestatic  #165                // Method kotlin/collections/CollectionsKt.throwIndexOverflow:()V
      56: iload         6
      58: aload         5
      60: checkcast     #69                 // class day08/Operation
      63: astore        7
      65: istore        8
      67: iconst_0
      68: istore        9
      70: aload         7
      72: invokevirtual #168                // Method day08/Operation.getName:()Ljava/lang/String;
      75: astore        10
      77: aload         10
      79: ldc           #170                // String jmp
      81: invokestatic  #174                // Method kotlin/jvm/internal/Intrinsics.areEqual:(Ljava/lang/Object;Ljava/lang/Object;)Z
      84: ifne          97
      87: aload         10
      89: ldc           #176                // String nop
      91: invokestatic  #174                // Method kotlin/jvm/internal/Intrinsics.areEqual:(Ljava/lang/Object;Ljava/lang/Object;)Z
      94: ifeq          206
      97: aload_0
      98: checkcast     #35                 // class java/util/Collection
     101: invokestatic  #180                // Method kotlin/collections/CollectionsKt.toMutableList:(Ljava/util/Collection;)Ljava/util/List;
     104: astore        11
     106: aload         7
     108: invokevirtual #168                // Method day08/Operation.getName:()Ljava/lang/String;
     111: astore        12
     113: aload         12
     115: ldc           #170                // String jmp
     117: invokestatic  #174                // Method kotlin/jvm/internal/Intrinsics.areEqual:(Ljava/lang/Object;Ljava/lang/Object;)Z
     120: ifeq          128
     123: ldc           #176                // String nop
     125: goto          148
     128: aload         12
     130: ldc           #176                // String nop
     132: invokestatic  #174                // Method kotlin/jvm/internal/Intrinsics.areEqual:(Ljava/lang/Object;Ljava/lang/Object;)Z
     135: ifeq          143
     138: ldc           #170                // String jmp
     140: goto          148
     143: aload         7
     145: invokevirtual #168                // Method day08/Operation.getName:()Ljava/lang/String;
     148: astore        13
     150: aload         11
     152: iload         8
     154: new           #69                 // class day08/Operation
     157: dup
     158: aload         13
     160: aload         7
     162: invokevirtual #183                // Method day08/Operation.getArgument:()I
     165: invokespecial #78                 // Method day08/Operation."<init>":(Ljava/lang/String;I)V
     168: invokeinterface #187,  3          // InterfaceMethod java/util/List.set:(ILjava/lang/Object;)Ljava/lang/Object;
     173: pop
     174: new           #153                // class day08/Program
     177: dup
     178: aload         11
     180: invokespecial #156                // Method day08/Program."<init>":(Ljava/util/List;)V
     183: astore        12
     185: aload         12
     187: invokevirtual #159                // Method day08/Program.run:()I
     190: istore        14
     192: aload         12
     194: invokevirtual #190                // Method day08/Program.terminated:()Z
     197: ifeq          206
     200: iload         14
     202: invokestatic  #194                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     205: areturn
     206: nop
     207: nop
     208: goto          23
     211: nop
     212: aconst_null
     213: areturn

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


package day10

import java.io.File
import kotlin.math.pow

fun main() {
    val differences = File("src/main/kotlin/day10/day10-input.txt")
        .readLines()
        .map { it.toInt() }
        .sorted()
        .let { listOf(0) + it + listOf(it.last() + 3) }
        .windowed(2)
        .map { it[1] - it[0] }

    val permutationsOfOnes = listOf(0, 1, 2, 4, 7)
    val sublistCount = longArrayOf(0, 0, 0, 0, 0)

    var numberOfOnes = 0
    differences.forEach { diff ->
        if (diff == 1) {
            numberOfOnes++
        } else {
            sublistCount[numberOfOnes]++
            numberOfOnes = 0
        }
    }

    var answer = 1L

    for (i in 2 until sublistCount.size) {
        val permutations = permutationsOfOnes[i].pow(sublistCount[i])
        if (permutations > 0) {
            answer *= permutations
        }
    }

    println(answer)
}

fun Int.pow(n: Long) = toDouble().pow(n.toDouble()).toLong()

Compiled from "Day10Second.kt"
public final class day10.Day10SecondKt {
  public static final void main();
    Code:
       0: new           #8                  // class java/io/File
       3: dup
       4: ldc           #10                 // String src/main/kotlin/day10/day10-input.txt
       6: invokespecial #14                 // Method java/io/File."<init>":(Ljava/lang/String;)V
       9: aconst_null
      10: iconst_1
      11: aconst_null
      12: invokestatic  #20                 // Method kotlin/io/FilesKt.readLines$default:(Ljava/io/File;Ljava/nio/charset/Charset;ILjava/lang/Object;)Ljava/util/List;
      15: checkcast     #22                 // class java/lang/Iterable
      18: astore_1
      19: nop
      20: iconst_0
      21: istore_2
      22: aload_1
      23: astore_3
      24: new           #24                 // class java/util/ArrayList
      27: dup
      28: aload_1
      29: bipush        10
      31: invokestatic  #30                 // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
      34: invokespecial #33                 // Method java/util/ArrayList."<init>":(I)V
      37: checkcast     #35                 // class java/util/Collection
      40: astore        4
      42: iconst_0
      43: istore        5
      45: aload_3
      46: invokeinterface #39,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      51: astore        6
      53: aload         6
      55: invokeinterface #45,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      60: ifeq          107
      63: aload         6
      65: invokeinterface #49,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      70: astore        7
      72: aload         4
      74: aload         7
      76: checkcast     #51                 // class java/lang/String
      79: astore        8
      81: astore        13
      83: iconst_0
      84: istore        9
      86: aload         8
      88: invokestatic  #57                 // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
      91: nop
      92: invokestatic  #61                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      95: aload         13
      97: swap
      98: invokeinterface #65,  2           // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     103: pop
     104: goto          53
     107: aload         4
     109: checkcast     #67                 // class java/util/List
     112: nop
     113: checkcast     #22                 // class java/lang/Iterable
     116: invokestatic  #71                 // Method kotlin/collections/CollectionsKt.sorted:(Ljava/lang/Iterable;)Ljava/util/List;
     119: astore_2
     120: iconst_0
     121: istore_3
     122: iconst_0
     123: invokestatic  #61                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     126: invokestatic  #75                 // Method kotlin/collections/CollectionsKt.listOf:(Ljava/lang/Object;)Ljava/util/List;
     129: checkcast     #35                 // class java/util/Collection
     132: aload_2
     133: checkcast     #22                 // class java/lang/Iterable
     136: invokestatic  #79                 // Method kotlin/collections/CollectionsKt.plus:(Ljava/util/Collection;Ljava/lang/Iterable;)Ljava/util/List;
     139: checkcast     #35                 // class java/util/Collection
     142: aload_2
     143: invokestatic  #83                 // Method kotlin/collections/CollectionsKt.last:(Ljava/util/List;)Ljava/lang/Object;
     146: checkcast     #85                 // class java/lang/Number
     149: invokevirtual #89                 // Method java/lang/Number.intValue:()I
     152: iconst_3
     153: iadd
     154: invokestatic  #61                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     157: invokestatic  #75                 // Method kotlin/collections/CollectionsKt.listOf:(Ljava/lang/Object;)Ljava/util/List;
     160: checkcast     #22                 // class java/lang/Iterable
     163: invokestatic  #79                 // Method kotlin/collections/CollectionsKt.plus:(Ljava/util/Collection;Ljava/lang/Iterable;)Ljava/util/List;
     166: checkcast     #22                 // class java/lang/Iterable
     169: iconst_2
     170: iconst_0
     171: iconst_0
     172: bipush        6
     174: aconst_null
     175: invokestatic  #93                 // Method kotlin/collections/CollectionsKt.windowed$default:(Ljava/lang/Iterable;IIZILjava/lang/Object;)Ljava/util/List;
     178: checkcast     #22                 // class java/lang/Iterable
     181: astore_1
     182: nop
     183: iconst_0
     184: istore_2
     185: aload_1
     186: astore_3
     187: new           #24                 // class java/util/ArrayList
     190: dup
     191: aload_1
     192: bipush        10
     194: invokestatic  #30                 // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
     197: invokespecial #33                 // Method java/util/ArrayList."<init>":(I)V
     200: checkcast     #35                 // class java/util/Collection
     203: astore        4
     205: iconst_0
     206: istore        5
     208: aload_3
     209: invokeinterface #39,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     214: astore        6
     216: aload         6
     218: invokeinterface #45,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     223: ifeq          293
     226: aload         6
     228: invokeinterface #49,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     233: astore        7
     235: aload         4
     237: aload         7
     239: checkcast     #67                 // class java/util/List
     242: astore        8
     244: astore        13
     246: iconst_0
     247: istore        9
     249: aload         8
     251: iconst_1
     252: invokeinterface #97,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     257: checkcast     #85                 // class java/lang/Number
     260: invokevirtual #89                 // Method java/lang/Number.intValue:()I
     263: aload         8
     265: iconst_0
     266: invokeinterface #97,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     271: checkcast     #85                 // class java/lang/Number
     274: invokevirtual #89                 // Method java/lang/Number.intValue:()I
     277: isub
     278: invokestatic  #61                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     281: aload         13
     283: swap
     284: invokeinterface #65,  2           // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     289: pop
     290: goto          216
     293: aload         4
     295: checkcast     #67                 // class java/util/List
     298: nop
     299: astore_0
     300: iconst_5
     301: anewarray     #53                 // class java/lang/Integer
     304: astore_2
     305: aload_2
     306: iconst_0
     307: iconst_0
     308: invokestatic  #61                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     311: aastore
     312: aload_2
     313: iconst_1
     314: iconst_1
     315: invokestatic  #61                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     318: aastore
     319: aload_2
     320: iconst_2
     321: iconst_2
     322: invokestatic  #61                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     325: aastore
     326: aload_2
     327: iconst_3
     328: iconst_4
     329: invokestatic  #61                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     332: aastore
     333: aload_2
     334: iconst_4
     335: bipush        7
     337: invokestatic  #61                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     340: aastore
     341: aload_2
     342: invokestatic  #100                // Method kotlin/collections/CollectionsKt.listOf:([Ljava/lang/Object;)Ljava/util/List;
     345: astore_1
     346: iconst_5
     347: newarray       long
     349: astore_3
     350: aload_3
     351: iconst_0
     352: lconst_0
     353: lastore
     354: aload_3
     355: iconst_1
     356: lconst_0
     357: lastore
     358: aload_3
     359: iconst_2
     360: lconst_0
     361: lastore
     362: aload_3
     363: iconst_3
     364: lconst_0
     365: lastore
     366: aload_3
     367: iconst_4
     368: lconst_0
     369: lastore
     370: aload_3
     371: astore_2
     372: iconst_0
     373: istore_3
     374: aload_0
     375: checkcast     #22                 // class java/lang/Iterable
     378: astore        4
     380: iconst_0
     381: istore        5
     383: aload         4
     385: invokeinterface #39,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     390: astore        6
     392: aload         6
     394: invokeinterface #45,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     399: ifeq          468
     402: aload         6
     404: invokeinterface #49,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     409: astore        7
     411: aload         7
     413: checkcast     #85                 // class java/lang/Number
     416: invokevirtual #89                 // Method java/lang/Number.intValue:()I
     419: istore        8
     421: iconst_0
     422: istore        9
     424: iload         8
     426: iconst_1
     427: if_icmpne     444
     430: iload_3
     431: istore        10
     433: iload         10
     435: iconst_1
     436: iadd
     437: istore_3
     438: iload         10
     440: pop
     441: goto          463
     444: iload_3
     445: istore        10
     447: aload_2
     448: iload         10
     450: laload
     451: lstore        11
     453: aload_2
     454: iload         10
     456: lload         11
     458: lconst_1
     459: ladd
     460: lastore
     461: iconst_0
     462: istore_3
     463: nop
     464: nop
     465: goto          392
     468: nop
     469: lconst_1
     470: lstore        4
     472: iconst_2
     473: istore        6
     475: aload_2
     476: arraylength
     477: istore        7
     479: iload         6
     481: iload         7
     483: if_icmpge     529
     486: aload_1
     487: iload         6
     489: invokeinterface #97,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     494: checkcast     #85                 // class java/lang/Number
     497: invokevirtual #89                 // Method java/lang/Number.intValue:()I
     500: aload_2
     501: iload         6
     503: laload
     504: invokestatic  #104                // Method pow:(IJ)J
     507: lstore        8
     509: lload         8
     511: lconst_0
     512: lcmp
     513: ifle          523
     516: lload         4
     518: lload         8
     520: lmul
     521: lstore        4
     523: iinc          6, 1
     526: goto          479
     529: getstatic     #110                // Field java/lang/System.out:Ljava/io/PrintStream;
     532: lload         4
     534: invokevirtual #116                // Method java/io/PrintStream.println:(J)V
     537: return

  public static final long pow(int, long);
    Code:
       0: iload_0
       1: i2d
       2: lload_1
       3: l2d
       4: invokestatic  #152                // Method java/lang/Math.pow:(DD)D
       7: d2l
       8: lreturn

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


package day2

class Computer(val program: Array<Int>) {
  private var pointer: Int = 0

  fun execute() {
    while (pointer < program.size) {
      when (program[pointer]) {
        1 -> {
          add()
          pointer += 4
        }
        2 -> {
          mult()
          pointer += 4
        }
        99 -> pointer = program.size
      }
    }
  }

  override fun toString(): String {
    return program.joinToString(" ")
  }

  private fun add() {
    val op1 = program[program[pointer + 1]]
    val op2 = program[program[pointer + 2]]
    val loc = program[pointer + 3]
    program[loc] = op1 + op2
  }

  private fun mult() {
    val op1 = program[program[pointer + 1]]
    val op2 = program[program[pointer + 2]]
    val loc = program[pointer + 3]
    program[loc] = op1 * op2
  }
}

fun copyOfProgram(): Array<Int> {
  return arrayOf(1, 0, 0, 3, 1, 1, 2, 3, 1, 3, 4, 3, 1, 5, 0, 3, 2, 1, 10, 19, 1, 6, 19, 23, 2, 23, 6, 27, 2, 6, 27, 31, 2, 13, 31, 35, 1, 10, 35, 39, 2, 39, 13, 43, 1, 43, 13, 47, 1, 6, 47, 51, 1, 10, 51, 55, 2, 55, 6, 59, 1, 5, 59, 63, 2, 9, 63, 67, 1, 6, 67, 71, 2, 9, 71, 75, 1, 6, 75, 79, 2, 79, 13, 83, 1, 83, 10, 87, 1, 13, 87, 91, 1, 91, 10, 95, 2, 9, 95, 99, 1, 5, 99, 103, 2, 10, 103, 107, 1, 107, 2, 111, 1, 111, 5, 0, 99, 2, 14, 0, 0)
}

fun main(args: Array<String>) {
  for (noun in 0..99) {
    print('*')
    for (verb in 0..99) {
      print('.')
      val testProgram = copyOfProgram()
      testProgram[1] = noun
      testProgram[2] = verb

      val comp = Computer(testProgram)
      comp.execute()
      if (comp.program[0] == 19690720) {
        println("Noun: $noun Verb: $verb")
      }
    }
  }
}

Compiled from "App.kt"
public final class day2.AppKt {
  public static final java.lang.Integer[] copyOfProgram();
    Code:
       0: bipush        121
       2: anewarray     #9                  // class java/lang/Integer
       5: astore_0
       6: aload_0
       7: iconst_0
       8: iconst_1
       9: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      12: aastore
      13: aload_0
      14: iconst_1
      15: iconst_0
      16: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      19: aastore
      20: aload_0
      21: iconst_2
      22: iconst_0
      23: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      26: aastore
      27: aload_0
      28: iconst_3
      29: iconst_3
      30: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      33: aastore
      34: aload_0
      35: iconst_4
      36: iconst_1
      37: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      40: aastore
      41: aload_0
      42: iconst_5
      43: iconst_1
      44: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      47: aastore
      48: aload_0
      49: bipush        6
      51: iconst_2
      52: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      55: aastore
      56: aload_0
      57: bipush        7
      59: iconst_3
      60: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      63: aastore
      64: aload_0
      65: bipush        8
      67: iconst_1
      68: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      71: aastore
      72: aload_0
      73: bipush        9
      75: iconst_3
      76: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      79: aastore
      80: aload_0
      81: bipush        10
      83: iconst_4
      84: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      87: aastore
      88: aload_0
      89: bipush        11
      91: iconst_3
      92: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      95: aastore
      96: aload_0
      97: bipush        12
      99: iconst_1
     100: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     103: aastore
     104: aload_0
     105: bipush        13
     107: iconst_5
     108: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     111: aastore
     112: aload_0
     113: bipush        14
     115: iconst_0
     116: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     119: aastore
     120: aload_0
     121: bipush        15
     123: iconst_3
     124: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     127: aastore
     128: aload_0
     129: bipush        16
     131: iconst_2
     132: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     135: aastore
     136: aload_0
     137: bipush        17
     139: iconst_1
     140: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     143: aastore
     144: aload_0
     145: bipush        18
     147: bipush        10
     149: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     152: aastore
     153: aload_0
     154: bipush        19
     156: bipush        19
     158: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     161: aastore
     162: aload_0
     163: bipush        20
     165: iconst_1
     166: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     169: aastore
     170: aload_0
     171: bipush        21
     173: bipush        6
     175: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     178: aastore
     179: aload_0
     180: bipush        22
     182: bipush        19
     184: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     187: aastore
     188: aload_0
     189: bipush        23
     191: bipush        23
     193: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     196: aastore
     197: aload_0
     198: bipush        24
     200: iconst_2
     201: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     204: aastore
     205: aload_0
     206: bipush        25
     208: bipush        23
     210: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     213: aastore
     214: aload_0
     215: bipush        26
     217: bipush        6
     219: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     222: aastore
     223: aload_0
     224: bipush        27
     226: bipush        27
     228: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     231: aastore
     232: aload_0
     233: bipush        28
     235: iconst_2
     236: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     239: aastore
     240: aload_0
     241: bipush        29
     243: bipush        6
     245: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     248: aastore
     249: aload_0
     250: bipush        30
     252: bipush        27
     254: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     257: aastore
     258: aload_0
     259: bipush        31
     261: bipush        31
     263: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     266: aastore
     267: aload_0
     268: bipush        32
     270: iconst_2
     271: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     274: aastore
     275: aload_0
     276: bipush        33
     278: bipush        13
     280: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     283: aastore
     284: aload_0
     285: bipush        34
     287: bipush        31
     289: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     292: aastore
     293: aload_0
     294: bipush        35
     296: bipush        35
     298: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     301: aastore
     302: aload_0
     303: bipush        36
     305: iconst_1
     306: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     309: aastore
     310: aload_0
     311: bipush        37
     313: bipush        10
     315: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     318: aastore
     319: aload_0
     320: bipush        38
     322: bipush        35
     324: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     327: aastore
     328: aload_0
     329: bipush        39
     331: bipush        39
     333: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     336: aastore
     337: aload_0
     338: bipush        40
     340: iconst_2
     341: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     344: aastore
     345: aload_0
     346: bipush        41
     348: bipush        39
     350: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     353: aastore
     354: aload_0
     355: bipush        42
     357: bipush        13
     359: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     362: aastore
     363: aload_0
     364: bipush        43
     366: bipush        43
     368: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     371: aastore
     372: aload_0
     373: bipush        44
     375: iconst_1
     376: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     379: aastore
     380: aload_0
     381: bipush        45
     383: bipush        43
     385: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     388: aastore
     389: aload_0
     390: bipush        46
     392: bipush        13
     394: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     397: aastore
     398: aload_0
     399: bipush        47
     401: bipush        47
     403: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     406: aastore
     407: aload_0
     408: bipush        48
     410: iconst_1
     411: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     414: aastore
     415: aload_0
     416: bipush        49
     418: bipush        6
     420: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     423: aastore
     424: aload_0
     425: bipush        50
     427: bipush        47
     429: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     432: aastore
     433: aload_0
     434: bipush        51
     436: bipush        51
     438: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     441: aastore
     442: aload_0
     443: bipush        52
     445: iconst_1
     446: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     449: aastore
     450: aload_0
     451: bipush        53
     453: bipush        10
     455: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     458: aastore
     459: aload_0
     460: bipush        54
     462: bipush        51
     464: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     467: aastore
     468: aload_0
     469: bipush        55
     471: bipush        55
     473: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     476: aastore
     477: aload_0
     478: bipush        56
     480: iconst_2
     481: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     484: aastore
     485: aload_0
     486: bipush        57
     488: bipush        55
     490: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     493: aastore
     494: aload_0
     495: bipush        58
     497: bipush        6
     499: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     502: aastore
     503: aload_0
     504: bipush        59
     506: bipush        59
     508: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     511: aastore
     512: aload_0
     513: bipush        60
     515: iconst_1
     516: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     519: aastore
     520: aload_0
     521: bipush        61
     523: iconst_5
     524: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     527: aastore
     528: aload_0
     529: bipush        62
     531: bipush        59
     533: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     536: aastore
     537: aload_0
     538: bipush        63
     540: bipush        63
     542: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     545: aastore
     546: aload_0
     547: bipush        64
     549: iconst_2
     550: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     553: aastore
     554: aload_0
     555: bipush        65
     557: bipush        9
     559: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     562: aastore
     563: aload_0
     564: bipush        66
     566: bipush        63
     568: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     571: aastore
     572: aload_0
     573: bipush        67
     575: bipush        67
     577: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     580: aastore
     581: aload_0
     582: bipush        68
     584: iconst_1
     585: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     588: aastore
     589: aload_0
     590: bipush        69
     592: bipush        6
     594: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     597: aastore
     598: aload_0
     599: bipush        70
     601: bipush        67
     603: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     606: aastore
     607: aload_0
     608: bipush        71
     610: bipush        71
     612: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     615: aastore
     616: aload_0
     617: bipush        72
     619: iconst_2
     620: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     623: aastore
     624: aload_0
     625: bipush        73
     627: bipush        9
     629: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     632: aastore
     633: aload_0
     634: bipush        74
     636: bipush        71
     638: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     641: aastore
     642: aload_0
     643: bipush        75
     645: bipush        75
     647: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     650: aastore
     651: aload_0
     652: bipush        76
     654: iconst_1
     655: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     658: aastore
     659: aload_0
     660: bipush        77
     662: bipush        6
     664: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     667: aastore
     668: aload_0
     669: bipush        78
     671: bipush        75
     673: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     676: aastore
     677: aload_0
     678: bipush        79
     680: bipush        79
     682: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     685: aastore
     686: aload_0
     687: bipush        80
     689: iconst_2
     690: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     693: aastore
     694: aload_0
     695: bipush        81
     697: bipush        79
     699: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     702: aastore
     703: aload_0
     704: bipush        82
     706: bipush        13
     708: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     711: aastore
     712: aload_0
     713: bipush        83
     715: bipush        83
     717: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     720: aastore
     721: aload_0
     722: bipush        84
     724: iconst_1
     725: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     728: aastore
     729: aload_0
     730: bipush        85
     732: bipush        83
     734: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     737: aastore
     738: aload_0
     739: bipush        86
     741: bipush        10
     743: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     746: aastore
     747: aload_0
     748: bipush        87
     750: bipush        87
     752: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     755: aastore
     756: aload_0
     757: bipush        88
     759: iconst_1
     760: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     763: aastore
     764: aload_0
     765: bipush        89
     767: bipush        13
     769: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     772: aastore
     773: aload_0
     774: bipush        90
     776: bipush        87
     778: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     781: aastore
     782: aload_0
     783: bipush        91
     785: bipush        91
     787: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     790: aastore
     791: aload_0
     792: bipush        92
     794: iconst_1
     795: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     798: aastore
     799: aload_0
     800: bipush        93
     802: bipush        91
     804: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     807: aastore
     808: aload_0
     809: bipush        94
     811: bipush        10
     813: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     816: aastore
     817: aload_0
     818: bipush        95
     820: bipush        95
     822: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     825: aastore
     826: aload_0
     827: bipush        96
     829: iconst_2
     830: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     833: aastore
     834: aload_0
     835: bipush        97
     837: bipush        9
     839: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     842: aastore
     843: aload_0
     844: bipush        98
     846: bipush        95
     848: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     851: aastore
     852: aload_0
     853: bipush        99
     855: bipush        99
     857: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     860: aastore
     861: aload_0
     862: bipush        100
     864: iconst_1
     865: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     868: aastore
     869: aload_0
     870: bipush        101
     872: iconst_5
     873: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     876: aastore
     877: aload_0
     878: bipush        102
     880: bipush        99
     882: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     885: aastore
     886: aload_0
     887: bipush        103
     889: bipush        103
     891: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     894: aastore
     895: aload_0
     896: bipush        104
     898: iconst_2
     899: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     902: aastore
     903: aload_0
     904: bipush        105
     906: bipush        10
     908: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     911: aastore
     912: aload_0
     913: bipush        106
     915: bipush        103
     917: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     920: aastore
     921: aload_0
     922: bipush        107
     924: bipush        107
     926: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     929: aastore
     930: aload_0
     931: bipush        108
     933: iconst_1
     934: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     937: aastore
     938: aload_0
     939: bipush        109
     941: bipush        107
     943: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     946: aastore
     947: aload_0
     948: bipush        110
     950: iconst_2
     951: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     954: aastore
     955: aload_0
     956: bipush        111
     958: bipush        111
     960: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     963: aastore
     964: aload_0
     965: bipush        112
     967: iconst_1
     968: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     971: aastore
     972: aload_0
     973: bipush        113
     975: bipush        111
     977: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     980: aastore
     981: aload_0
     982: bipush        114
     984: iconst_5
     985: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     988: aastore
     989: aload_0
     990: bipush        115
     992: iconst_0
     993: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     996: aastore
     997: aload_0
     998: bipush        116
    1000: bipush        99
    1002: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
    1005: aastore
    1006: aload_0
    1007: bipush        117
    1009: iconst_2
    1010: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
    1013: aastore
    1014: aload_0
    1015: bipush        118
    1017: bipush        14
    1019: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
    1022: aastore
    1023: aload_0
    1024: bipush        119
    1026: iconst_0
    1027: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
    1030: aastore
    1031: aload_0
    1032: bipush        120
    1034: iconst_0
    1035: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
    1038: aastore
    1039: aload_0
    1040: areturn

  public static final void main(java.lang.String[]);
    Code:
       0: aload_0
       1: ldc           #17                 // String args
       3: invokestatic  #23                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: iconst_0
       7: istore_1
       8: iload_1
       9: bipush        100
      11: if_icmpge     137
      14: bipush        42
      16: istore_2
      17: getstatic     #29                 // Field java/lang/System.out:Ljava/io/PrintStream;
      20: iload_2
      21: invokevirtual #35                 // Method java/io/PrintStream.print:(C)V
      24: iconst_0
      25: istore_2
      26: iload_2
      27: bipush        100
      29: if_icmpge     131
      32: bipush        46
      34: istore_3
      35: getstatic     #29                 // Field java/lang/System.out:Ljava/io/PrintStream;
      38: iload_3
      39: invokevirtual #35                 // Method java/io/PrintStream.print:(C)V
      42: invokestatic  #37                 // Method copyOfProgram:()[Ljava/lang/Integer;
      45: astore_3
      46: aload_3
      47: iconst_1
      48: iload_1
      49: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      52: aastore
      53: aload_3
      54: iconst_2
      55: iload_2
      56: invokestatic  #13                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      59: aastore
      60: new           #39                 // class day2/Computer
      63: dup
      64: aload_3
      65: invokespecial #43                 // Method day2/Computer."<init>":([Ljava/lang/Integer;)V
      68: astore        4
      70: aload         4
      72: invokevirtual #47                 // Method day2/Computer.execute:()V
      75: aload         4
      77: invokevirtual #50                 // Method day2/Computer.getProgram:()[Ljava/lang/Integer;
      80: iconst_0
      81: aaload
      82: invokevirtual #54                 // Method java/lang/Integer.intValue:()I
      85: ldc           #55                 // int 19690720
      87: if_icmpne     125
      90: new           #57                 // class java/lang/StringBuilder
      93: dup
      94: invokespecial #59                 // Method java/lang/StringBuilder."<init>":()V
      97: ldc           #61                 // String Noun:
      99: invokevirtual #65                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     102: iload_1
     103: invokevirtual #68                 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
     106: ldc           #70                 // String  Verb:
     108: invokevirtual #65                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     111: iload_2
     112: invokevirtual #68                 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
     115: invokevirtual #74                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
     118: getstatic     #29                 // Field java/lang/System.out:Ljava/io/PrintStream;
     121: swap
     122: invokevirtual #78                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
     125: iinc          2, 1
     128: goto          26
     131: iinc          1, 1
     134: goto          8
     137: return
}


package de.havemann.transformer.domain.sortkey

import java.util.*
import java.util.Comparator.comparing
import kotlin.reflect.KProperty1

/**
 * Sorting is determined through the use of the ‘sort’ query string parameter. The value of this parameter is a
 * comma-separated list of sort keys. Sort directions can optionally be appended to each sort key, separated by the ‘:’
 * character.
 *
 * The supported sort directions are either ‘asc’ for ascending or ‘desc’ for descending.
 *
 * The caller may (but is not required to) specify a sort direction for each key. If a sort direction is not specified
 * for a key, then a default is set by the server.
 */
data class SortKey(val key: String, val ordering: Ordering) {
    companion object {

        /**
         * Transforms a list of {@link SortKey} to a {@link Comparator}
         */
        fun <Entity, Value : Comparable<Value>> toComparator(
            sortKeys: List<SortKey>,
            sortKeyToProperty: Map<String, KProperty1<Entity, Value?>>
        ): Comparator<Entity> {
            return sortKeys
                .stream()
                .filter { sortKeyToProperty[it.key] != null }
                .map { sortKey ->
                    val property = sortKeyToProperty[sortKey.key]
                    val sortingFunction: (t: Entity) -> Value = { property?.call(it)!! }
                    sortKey.ordering.adjust(comparing(sortingFunction))
                }
                .reduce { a, b -> a.thenComparing(b) }
                .orElseThrow { IllegalArgumentException("list shouldn't be empty") }
        }
    }
}

enum class Ordering(val key: String) {
    ASCENDING("asc"),
    DESCENDING("desc");

    /**
     * reverses the given comparator if ordering is descending
     */
    fun <T> adjust(comparator: Comparator<T>): Comparator<T> {
        return if (this == DESCENDING) comparator.reversed() else comparator
    }

    companion object {
        /**
         * determines Ordering form given string
         */
        fun detect(token: String): Ordering {
            return values().asSequence()
                .filter { it.key == token }
                .firstOrNull()
                ?: throw IllegalArgumentException(token)
        }
    }
}

Compiled from "SortKey.kt"
public final class de.havemann.transformer.domain.sortkey.SortKey$Companion {
  private de.havemann.transformer.domain.sortkey.SortKey$Companion();
    Code:
       0: aload_0
       1: invokespecial #8                  // Method java/lang/Object."<init>":()V
       4: return

  public final <Entity, Value extends java.lang.Comparable<? super Value>> java.util.Comparator<Entity> toComparator(java.util.List<de.havemann.transformer.domain.sortkey.SortKey>, java.util.Map<java.lang.String, ? extends kotlin.reflect.KProperty1<Entity, ? extends Value>>);
    Code:
       0: aload_1
       1: ldc           #16                 // String sortKeys
       3: invokestatic  #22                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_2
       7: ldc           #24                 // String sortKeyToProperty
       9: invokestatic  #22                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
      12: aload_1
      13: invokeinterface #30,  1           // InterfaceMethod java/util/List.stream:()Ljava/util/stream/Stream;
      18: aload_2
      19: invokedynamic #50,  0             // InvokeDynamic #0:invoke:(Ljava/util/Map;)Lkotlin/jvm/functions/Function1;
      24: invokedynamic #61,  0             // InvokeDynamic #1:test:(Lkotlin/jvm/functions/Function1;)Ljava/util/function/Predicate;
      29: invokeinterface #67,  2           // InterfaceMethod java/util/stream/Stream.filter:(Ljava/util/function/Predicate;)Ljava/util/stream/Stream;
      34: aload_2
      35: invokedynamic #75,  0             // InvokeDynamic #2:invoke:(Ljava/util/Map;)Lkotlin/jvm/functions/Function1;
      40: invokedynamic #86,  0             // InvokeDynamic #3:apply:(Lkotlin/jvm/functions/Function1;)Ljava/util/function/Function;
      45: invokeinterface #90,  2           // InterfaceMethod java/util/stream/Stream.map:(Ljava/util/function/Function;)Ljava/util/stream/Stream;
      50: invokedynamic #101,  0            // InvokeDynamic #4:apply:()Ljava/util/function/BinaryOperator;
      55: invokeinterface #105,  2          // InterfaceMethod java/util/stream/Stream.reduce:(Ljava/util/function/BinaryOperator;)Ljava/util/Optional;
      60: invokedynamic #117,  0            // InvokeDynamic #5:get:()Ljava/util/function/Supplier;
      65: invokevirtual #123                // Method java/util/Optional.orElseThrow:(Ljava/util/function/Supplier;)Ljava/lang/Object;
      68: astore_3
      69: aload_3
      70: ldc           #125                // String orElseThrow(...)
      72: invokestatic  #128                // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
      75: aload_3
      76: checkcast     #130                // class java/util/Comparator
      79: areturn

  private static final boolean toComparator$lambda$0(java.util.Map, de.havemann.transformer.domain.sortkey.SortKey);
    Code:
       0: aload_0
       1: aload_1
       2: invokevirtual #138                // Method de/havemann/transformer/domain/sortkey/SortKey.getKey:()Ljava/lang/String;
       5: invokeinterface #142,  2          // InterfaceMethod java/util/Map.get:(Ljava/lang/Object;)Ljava/lang/Object;
      10: ifnull        17
      13: iconst_1
      14: goto          18
      17: iconst_0
      18: ireturn

  private static final boolean toComparator$lambda$1(kotlin.jvm.functions.Function1, java.lang.Object);
    Code:
       0: aload_0
       1: aload_1
       2: invokeinterface #149,  2          // InterfaceMethod kotlin/jvm/functions/Function1.invoke:(Ljava/lang/Object;)Ljava/lang/Object;
       7: checkcast     #151                // class java/lang/Boolean
      10: invokevirtual #155                // Method java/lang/Boolean.booleanValue:()Z
      13: ireturn

  private static final java.lang.Comparable toComparator$lambda$4$lambda$2(kotlin.reflect.KProperty1, java.lang.Object);
    Code:
       0: aload_0
       1: dup
       2: ifnull        26
       5: iconst_1
       6: anewarray     #4                  // class java/lang/Object
       9: astore_2
      10: aload_2
      11: iconst_0
      12: aload_1
      13: aastore
      14: aload_2
      15: invokeinterface #167,  2          // InterfaceMethod kotlin/reflect/KProperty1.call:([Ljava/lang/Object;)Ljava/lang/Object;
      20: checkcast     #169                // class java/lang/Comparable
      23: goto          28
      26: pop
      27: aconst_null
      28: dup
      29: invokestatic  #173                // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
      32: areturn

  private static final java.lang.Comparable toComparator$lambda$4$lambda$3(kotlin.jvm.functions.Function1, java.lang.Object);
    Code:
       0: aload_0
       1: aload_1
       2: invokeinterface #149,  2          // InterfaceMethod kotlin/jvm/functions/Function1.invoke:(Ljava/lang/Object;)Ljava/lang/Object;
       7: checkcast     #169                // class java/lang/Comparable
      10: areturn

  private static final java.util.Comparator toComparator$lambda$4(java.util.Map, de.havemann.transformer.domain.sortkey.SortKey);
    Code:
       0: aload_0
       1: aload_1
       2: invokevirtual #138                // Method de/havemann/transformer/domain/sortkey/SortKey.getKey:()Ljava/lang/String;
       5: invokeinterface #142,  2          // InterfaceMethod java/util/Map.get:(Ljava/lang/Object;)Ljava/lang/Object;
      10: checkcast     #163                // class kotlin/reflect/KProperty1
      13: astore_2
      14: aload_2
      15: invokedynamic #185,  0            // InvokeDynamic #6:invoke:(Lkotlin/reflect/KProperty1;)Lkotlin/jvm/functions/Function1;
      20: astore_3
      21: aload_1
      22: invokevirtual #189                // Method de/havemann/transformer/domain/sortkey/SortKey.getOrdering:()Lde/havemann/transformer/domain/sortkey/Ordering;
      25: aload_3
      26: invokedynamic #193,  0            // InvokeDynamic #7:apply:(Lkotlin/jvm/functions/Function1;)Ljava/util/function/Function;
      31: invokestatic  #197                // InterfaceMethod java/util/Comparator.comparing:(Ljava/util/function/Function;)Ljava/util/Comparator;
      34: dup
      35: ldc           #199                // String comparing(...)
      37: invokestatic  #128                // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
      40: invokevirtual #205                // Method de/havemann/transformer/domain/sortkey/Ordering.adjust:(Ljava/util/Comparator;)Ljava/util/Comparator;
      43: areturn

  private static final java.util.Comparator toComparator$lambda$5(kotlin.jvm.functions.Function1, java.lang.Object);
    Code:
       0: aload_0
       1: aload_1
       2: invokeinterface #149,  2          // InterfaceMethod kotlin/jvm/functions/Function1.invoke:(Ljava/lang/Object;)Ljava/lang/Object;
       7: checkcast     #130                // class java/util/Comparator
      10: areturn

  private static final java.util.Comparator toComparator$lambda$6(java.util.Comparator, java.util.Comparator);
    Code:
       0: aload_0
       1: aload_1
       2: invokeinterface #211,  2          // InterfaceMethod java/util/Comparator.thenComparing:(Ljava/util/Comparator;)Ljava/util/Comparator;
       7: areturn

  private static final java.lang.IllegalArgumentException toComparator$lambda$7();
    Code:
       0: new           #216                // class java/lang/IllegalArgumentException
       3: dup
       4: ldc           #218                // String list shouldn\'t be empty
       6: invokespecial #221                // Method java/lang/IllegalArgumentException."<init>":(Ljava/lang/String;)V
       9: areturn

  public de.havemann.transformer.domain.sortkey.SortKey$Companion(kotlin.jvm.internal.DefaultConstructorMarker);
    Code:
       0: aload_0
       1: invokespecial #223                // Method "<init>":()V
       4: return
}

Compiled from "SortKey.kt"
public final class de.havemann.transformer.domain.sortkey.SortKey {
  public static final de.havemann.transformer.domain.sortkey.SortKey$Companion Companion;

  private final java.lang.String key;

  private final de.havemann.transformer.domain.sortkey.Ordering ordering;

  public de.havemann.transformer.domain.sortkey.SortKey(java.lang.String, de.havemann.transformer.domain.sortkey.Ordering);
    Code:
       0: aload_1
       1: ldc           #9                  // String key
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_2
       7: ldc           #17                 // String ordering
       9: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
      12: aload_0
      13: invokespecial #20                 // Method java/lang/Object."<init>":()V
      16: aload_0
      17: aload_1
      18: putfield      #23                 // Field key:Ljava/lang/String;
      21: aload_0
      22: aload_2
      23: putfield      #26                 // Field ordering:Lde/havemann/transformer/domain/sortkey/Ordering;
      26: return

  public final java.lang.String getKey();
    Code:
       0: aload_0
       1: getfield      #23                 // Field key:Ljava/lang/String;
       4: areturn

  public final de.havemann.transformer.domain.sortkey.Ordering getOrdering();
    Code:
       0: aload_0
       1: getfield      #26                 // Field ordering:Lde/havemann/transformer/domain/sortkey/Ordering;
       4: areturn

  public final java.lang.String component1();
    Code:
       0: aload_0
       1: getfield      #23                 // Field key:Ljava/lang/String;
       4: areturn

  public final de.havemann.transformer.domain.sortkey.Ordering component2();
    Code:
       0: aload_0
       1: getfield      #26                 // Field ordering:Lde/havemann/transformer/domain/sortkey/Ordering;
       4: areturn

  public final de.havemann.transformer.domain.sortkey.SortKey copy(java.lang.String, de.havemann.transformer.domain.sortkey.Ordering);
    Code:
       0: aload_1
       1: ldc           #9                  // String key
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_2
       7: ldc           #17                 // String ordering
       9: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
      12: new           #2                  // class de/havemann/transformer/domain/sortkey/SortKey
      15: dup
      16: aload_1
      17: aload_2
      18: invokespecial #38                 // Method "<init>":(Ljava/lang/String;Lde/havemann/transformer/domain/sortkey/Ordering;)V
      21: areturn

  public static de.havemann.transformer.domain.sortkey.SortKey copy$default(de.havemann.transformer.domain.sortkey.SortKey, java.lang.String, de.havemann.transformer.domain.sortkey.Ordering, int, java.lang.Object);
    Code:
       0: iload_3
       1: iconst_1
       2: iand
       3: ifeq          11
       6: aload_0
       7: getfield      #23                 // Field key:Ljava/lang/String;
      10: astore_1
      11: iload_3
      12: iconst_2
      13: iand
      14: ifeq          22
      17: aload_0
      18: getfield      #26                 // Field ordering:Lde/havemann/transformer/domain/sortkey/Ordering;
      21: astore_2
      22: aload_0
      23: aload_1
      24: aload_2
      25: invokevirtual #42                 // Method copy:(Ljava/lang/String;Lde/havemann/transformer/domain/sortkey/Ordering;)Lde/havemann/transformer/domain/sortkey/SortKey;
      28: areturn

  public java.lang.String toString();
    Code:
       0: new           #45                 // class java/lang/StringBuilder
       3: dup
       4: invokespecial #46                 // Method java/lang/StringBuilder."<init>":()V
       7: ldc           #48                 // String SortKey(key=
       9: invokevirtual #52                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      12: aload_0
      13: getfield      #23                 // Field key:Ljava/lang/String;
      16: invokevirtual #52                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      19: ldc           #54                 // String , ordering=
      21: invokevirtual #52                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      24: aload_0
      25: getfield      #26                 // Field ordering:Lde/havemann/transformer/domain/sortkey/Ordering;
      28: invokevirtual #57                 // Method java/lang/StringBuilder.append:(Ljava/lang/Object;)Ljava/lang/StringBuilder;
      31: bipush        41
      33: invokevirtual #60                 // Method java/lang/StringBuilder.append:(C)Ljava/lang/StringBuilder;
      36: invokevirtual #62                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
      39: areturn

  public int hashCode();
    Code:
       0: aload_0
       1: getfield      #23                 // Field key:Ljava/lang/String;
       4: invokevirtual #68                 // Method java/lang/String.hashCode:()I
       7: istore_1
       8: iload_1
       9: bipush        31
      11: imul
      12: aload_0
      13: getfield      #26                 // Field ordering:Lde/havemann/transformer/domain/sortkey/Ordering;
      16: invokevirtual #71                 // Method de/havemann/transformer/domain/sortkey/Ordering.hashCode:()I
      19: iadd
      20: istore_1
      21: iload_1
      22: ireturn

  public boolean equals(java.lang.Object);
    Code:
       0: aload_0
       1: aload_1
       2: if_acmpne     7
       5: iconst_1
       6: ireturn
       7: aload_1
       8: instanceof    #2                  // class de/havemann/transformer/domain/sortkey/SortKey
      11: ifne          16
      14: iconst_0
      15: ireturn
      16: aload_1
      17: checkcast     #2                  // class de/havemann/transformer/domain/sortkey/SortKey
      20: astore_2
      21: aload_0
      22: getfield      #23                 // Field key:Ljava/lang/String;
      25: aload_2
      26: getfield      #23                 // Field key:Ljava/lang/String;
      29: invokestatic  #80                 // Method kotlin/jvm/internal/Intrinsics.areEqual:(Ljava/lang/Object;Ljava/lang/Object;)Z
      32: ifne          37
      35: iconst_0
      36: ireturn
      37: aload_0
      38: getfield      #26                 // Field ordering:Lde/havemann/transformer/domain/sortkey/Ordering;
      41: aload_2
      42: getfield      #26                 // Field ordering:Lde/havemann/transformer/domain/sortkey/Ordering;
      45: if_acmpeq     50
      48: iconst_0
      49: ireturn
      50: iconst_1
      51: ireturn

  static {};
    Code:
       0: new           #85                 // class de/havemann/transformer/domain/sortkey/SortKey$Companion
       3: dup
       4: aconst_null
       5: invokespecial #88                 // Method de/havemann/transformer/domain/sortkey/SortKey$Companion."<init>":(Lkotlin/jvm/internal/DefaultConstructorMarker;)V
       8: putstatic     #92                 // Field Companion:Lde/havemann/transformer/domain/sortkey/SortKey$Companion;
      11: return
}