import kotlin.math.max
import kotlin.math.min

fun main() {
    trap(intArrayOf(0,1,0,2,1,0,1,3,2,1,2,1))
}

fun isPalindrome(s: String): Boolean {
    val stringBuilder = StringBuilder()
    for (c in s){
        if (!c.isLetterOrDigit())
            continue
        else if (c.isUpperCase())
            stringBuilder.append(c.lowercaseChar())
        else
            stringBuilder.append(c)
    }
    var start = 0
    var end = stringBuilder.length - 1
    while (start < end){
        if (stringBuilder[start] != stringBuilder[end])
            return false
        start++
        end--
    }
    return true
}

fun twoSum(numbers: IntArray, target: Int): IntArray {
    var start = 0
    var end = numbers.size - 1
    while ( start < end){
        val currentElement = numbers[start] + numbers[end]
        if (currentElement == target)
            return intArrayOf(start + 1 , end + 1)
        else if (currentElement > target)
            end--
        else start++
    }
    return intArrayOf()
}

fun threeSum(nums: IntArray): List<List<Int>> {
    nums.sort()
    val ans = mutableListOf<List<Int>>()
    for (i in nums.indices){
        val current = nums[i]
        if (i > 0 && current == nums[i-1])
            continue
        var l = i +1
        var r = nums.size - 1
        while (l < r){
            when{
                nums[i] + nums[l] + nums[r] == 0 -> {
                    ans.add(mutableListOf(i, l, r))
                    l++
                    while (nums[l] == nums[l+1] && l < r)
                        l++
                }
                nums[i] + nums[l] + nums[r] < 0 -> {
                    l++
                }
                else -> r--
            }
        }
    }
    return ans
}

fun maxArea(height: IntArray): Int {
    var max = Int.MIN_VALUE
    var l = 0
    var r = height.size - 1
    while (l < r){
        val area = minOf(height[l], height[r]) * (r - l)
        max = maxOf(max, area)
        if (height[l] < height[r]) l++ else r--
    }
    return max
}

fun trap(height: IntArray): Int {
    val maxLeft = IntArray(height.size)
    val maxRight = IntArray(height.size)
    val ans = IntArray(height.size)

    for (i in height.indices){
       if (i == 0)
           maxLeft[i] = 0
        else maxLeft[i] = maxOf(maxLeft[i-1], height[i-1])
    }
    for (i in height.size -1 downTo 0){
        if (i == height.size-1)
            maxRight[i] = 0
        else maxRight[i] = maxOf(maxRight[i+1], height[i+1])
    }

    for (i in height.indices){
        val storage = minOf(maxRight[i], maxLeft[i]) - height[i]
        if (storage > 0)
            ans[i] = storage
    }

    return ans.sum()
}

fun maxProfit(prices: IntArray): Int {
    var left = 0
    var right = 1
    var max = Int.MIN_VALUE
    while (left < prices.size - 1){
        if (prices[right] - prices[right] <= 0){
            left = right
            right++
        }
        else {
            val sp = prices[right] - prices[left]
            max = maxOf(max, sp)
            right++
        }
    }
    return max
}

Compiled from "TwoPointers.kt"
public final class TwoPointersKt {
  public static final void main();
    Code:
       0: bipush        12
       2: newarray       int
       4: astore_0
       5: aload_0
       6: iconst_0
       7: iconst_0
       8: iastore
       9: aload_0
      10: iconst_1
      11: iconst_1
      12: iastore
      13: aload_0
      14: iconst_2
      15: iconst_0
      16: iastore
      17: aload_0
      18: iconst_3
      19: iconst_2
      20: iastore
      21: aload_0
      22: iconst_4
      23: iconst_1
      24: iastore
      25: aload_0
      26: iconst_5
      27: iconst_0
      28: iastore
      29: aload_0
      30: bipush        6
      32: iconst_1
      33: iastore
      34: aload_0
      35: bipush        7
      37: iconst_3
      38: iastore
      39: aload_0
      40: bipush        8
      42: iconst_2
      43: iastore
      44: aload_0
      45: bipush        9
      47: iconst_1
      48: iastore
      49: aload_0
      50: bipush        10
      52: iconst_2
      53: iastore
      54: aload_0
      55: bipush        11
      57: iconst_1
      58: iastore
      59: aload_0
      60: invokestatic  #10                 // Method trap:([I)I
      63: pop
      64: return

  public static final boolean isPalindrome(java.lang.String);
    Code:
       0: aload_0
       1: ldc           #15                 // String s
       3: invokestatic  #21                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: new           #23                 // class java/lang/StringBuilder
       9: dup
      10: invokespecial #26                 // Method java/lang/StringBuilder."<init>":()V
      13: astore_1
      14: iconst_0
      15: istore_2
      16: aload_0
      17: invokevirtual #32                 // Method java/lang/String.length:()I
      20: istore_3
      21: iload_2
      22: iload_3
      23: if_icmpge     77
      26: aload_0
      27: iload_2
      28: invokevirtual #36                 // Method java/lang/String.charAt:(I)C
      31: istore        4
      33: iload         4
      35: invokestatic  #42                 // Method java/lang/Character.isLetterOrDigit:(C)Z
      38: ifne          44
      41: goto          71
      44: iload         4
      46: invokestatic  #45                 // Method java/lang/Character.isUpperCase:(C)Z
      49: ifeq          64
      52: aload_1
      53: iload         4
      55: invokestatic  #49                 // Method java/lang/Character.toLowerCase:(C)C
      58: invokevirtual #53                 // Method java/lang/StringBuilder.append:(C)Ljava/lang/StringBuilder;
      61: goto          70
      64: aload_1
      65: iload         4
      67: invokevirtual #53                 // Method java/lang/StringBuilder.append:(C)Ljava/lang/StringBuilder;
      70: pop
      71: iinc          2, 1
      74: goto          21
      77: iconst_0
      78: istore_2
      79: aload_1
      80: invokevirtual #54                 // Method java/lang/StringBuilder.length:()I
      83: iconst_1
      84: isub
      85: istore_3
      86: iload_2
      87: iload_3
      88: if_icmpge     115
      91: aload_1
      92: iload_2
      93: invokevirtual #55                 // Method java/lang/StringBuilder.charAt:(I)C
      96: aload_1
      97: iload_3
      98: invokevirtual #55                 // Method java/lang/StringBuilder.charAt:(I)C
     101: if_icmpeq     106
     104: iconst_0
     105: ireturn
     106: iinc          2, 1
     109: iinc          3, -1
     112: goto          86
     115: iconst_1
     116: ireturn

  public static final int[] twoSum(int[], int);
    Code:
       0: aload_0
       1: ldc           #67                 // String numbers
       3: invokestatic  #21                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: iconst_0
       7: istore_2
       8: aload_0
       9: arraylength
      10: iconst_1
      11: isub
      12: istore_3
      13: iload_2
      14: iload_3
      15: if_icmpge     73
      18: aload_0
      19: iload_2
      20: iaload
      21: aload_0
      22: iload_3
      23: iaload
      24: iadd
      25: istore        4
      27: iload         4
      29: iload_1
      30: if_icmpne     55
      33: iconst_2
      34: newarray       int
      36: astore        5
      38: aload         5
      40: iconst_0
      41: iload_2
      42: iconst_1
      43: iadd
      44: iastore
      45: aload         5
      47: iconst_1
      48: iload_3
      49: iconst_1
      50: iadd
      51: iastore
      52: aload         5
      54: areturn
      55: iload         4
      57: iload_1
      58: if_icmple     67
      61: iinc          3, -1
      64: goto          13
      67: iinc          2, 1
      70: goto          13
      73: iconst_0
      74: newarray       int
      76: areturn

  public static final java.util.List<java.util.List<java.lang.Integer>> threeSum(int[]);
    Code:
       0: aload_0
       1: ldc           #75                 // String nums
       3: invokestatic  #21                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: invokestatic  #81                 // Method kotlin/collections/ArraysKt.sort:([I)V
      10: new           #83                 // class java/util/ArrayList
      13: dup
      14: invokespecial #84                 // Method java/util/ArrayList."<init>":()V
      17: checkcast     #86                 // class java/util/List
      20: astore_1
      21: iconst_0
      22: istore_2
      23: aload_0
      24: arraylength
      25: istore_3
      26: iload_2
      27: iload_3
      28: if_icmpge     201
      31: aload_0
      32: iload_2
      33: iaload
      34: istore        4
      36: iload_2
      37: ifle          53
      40: iload         4
      42: aload_0
      43: iload_2
      44: iconst_1
      45: isub
      46: iaload
      47: if_icmpne     53
      50: goto          195
      53: iload_2
      54: iconst_1
      55: iadd
      56: istore        5
      58: aload_0
      59: arraylength
      60: iconst_1
      61: isub
      62: istore        6
      64: iload         5
      66: iload         6
      68: if_icmpge     195
      71: nop
      72: aload_0
      73: iload_2
      74: iaload
      75: aload_0
      76: iload         5
      78: iaload
      79: iadd
      80: aload_0
      81: iload         6
      83: iaload
      84: iadd
      85: ifne          161
      88: aload_1
      89: iconst_3
      90: anewarray     #88                 // class java/lang/Integer
      93: astore        7
      95: aload         7
      97: iconst_0
      98: iload_2
      99: invokestatic  #92                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     102: aastore
     103: aload         7
     105: iconst_1
     106: iload         5
     108: invokestatic  #92                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     111: aastore
     112: aload         7
     114: iconst_2
     115: iload         6
     117: invokestatic  #92                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     120: aastore
     121: aload         7
     123: invokestatic  #98                 // Method kotlin/collections/CollectionsKt.mutableListOf:([Ljava/lang/Object;)Ljava/util/List;
     126: invokeinterface #102,  2          // InterfaceMethod java/util/List.add:(Ljava/lang/Object;)Z
     131: pop
     132: iinc          5, 1
     135: aload_0
     136: iload         5
     138: iaload
     139: aload_0
     140: iload         5
     142: iconst_1
     143: iadd
     144: iaload
     145: if_icmpne     64
     148: iload         5
     150: iload         6
     152: if_icmpge     64
     155: iinc          5, 1
     158: goto          135
     161: aload_0
     162: iload_2
     163: iaload
     164: aload_0
     165: iload         5
     167: iaload
     168: iadd
     169: aload_0
     170: iload         6
     172: iaload
     173: iadd
     174: ifge          186
     177: iload         5
     179: iinc          5, 1
     182: pop
     183: goto          64
     186: iload         6
     188: iinc          6, -1
     191: pop
     192: goto          64
     195: iinc          2, 1
     198: goto          26
     201: aload_1
     202: areturn

  public static final int maxArea(int[]);
    Code:
       0: aload_0
       1: ldc           #113                // String height
       3: invokestatic  #21                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: ldc           #114                // int -2147483648
       8: istore_1
       9: iconst_0
      10: istore_2
      11: aload_0
      12: arraylength
      13: iconst_1
      14: isub
      15: istore_3
      16: iload_2
      17: iload_3
      18: if_icmpge     64
      21: aload_0
      22: iload_2
      23: iaload
      24: aload_0
      25: iload_3
      26: iaload
      27: invokestatic  #120                // Method java/lang/Math.min:(II)I
      30: iload_3
      31: iload_2
      32: isub
      33: imul
      34: istore        4
      36: iload_1
      37: iload         4
      39: invokestatic  #123                // Method java/lang/Math.max:(II)I
      42: istore_1
      43: aload_0
      44: iload_2
      45: iaload
      46: aload_0
      47: iload_3
      48: iaload
      49: if_icmpge     58
      52: iinc          2, 1
      55: goto          16
      58: iinc          3, -1
      61: goto          16
      64: iload_1
      65: ireturn

  public static final int trap(int[]);
    Code:
       0: aload_0
       1: ldc           #113                // String height
       3: invokestatic  #21                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: arraylength
       8: newarray       int
      10: astore_1
      11: aload_0
      12: arraylength
      13: newarray       int
      15: astore_2
      16: aload_0
      17: arraylength
      18: newarray       int
      20: astore_3
      21: iconst_0
      22: istore        4
      24: aload_0
      25: arraylength
      26: istore        5
      28: iload         4
      30: iload         5
      32: if_icmpge     73
      35: iload         4
      37: ifne          48
      40: aload_1
      41: iload         4
      43: iconst_0
      44: iastore
      45: goto          67
      48: aload_1
      49: iload         4
      51: aload_1
      52: iload         4
      54: iconst_1
      55: isub
      56: iaload
      57: aload_0
      58: iload         4
      60: iconst_1
      61: isub
      62: iaload
      63: invokestatic  #123                // Method java/lang/Math.max:(II)I
      66: iastore
      67: iinc          4, 1
      70: goto          28
      73: aload_0
      74: arraylength
      75: iconst_1
      76: isub
      77: istore        4
      79: iconst_m1
      80: iload         4
      82: if_icmpge     127
      85: iload         4
      87: aload_0
      88: arraylength
      89: iconst_1
      90: isub
      91: if_icmpne     102
      94: aload_2
      95: iload         4
      97: iconst_0
      98: iastore
      99: goto          121
     102: aload_2
     103: iload         4
     105: aload_2
     106: iload         4
     108: iconst_1
     109: iadd
     110: iaload
     111: aload_0
     112: iload         4
     114: iconst_1
     115: iadd
     116: iaload
     117: invokestatic  #123                // Method java/lang/Math.max:(II)I
     120: iastore
     121: iinc          4, -1
     124: goto          79
     127: iconst_0
     128: istore        4
     130: aload_0
     131: arraylength
     132: istore        5
     134: iload         4
     136: iload         5
     138: if_icmpge     176
     141: aload_2
     142: iload         4
     144: iaload
     145: aload_1
     146: iload         4
     148: iaload
     149: invokestatic  #120                // Method java/lang/Math.min:(II)I
     152: aload_0
     153: iload         4
     155: iaload
     156: isub
     157: istore        6
     159: iload         6
     161: ifle          170
     164: aload_3
     165: iload         4
     167: iload         6
     169: iastore
     170: iinc          4, 1
     173: goto          134
     176: aload_3
     177: invokestatic  #127                // Method kotlin/collections/ArraysKt.sum:([I)I
     180: ireturn

  public static final int maxProfit(int[]);
    Code:
       0: aload_0
       1: ldc           #134                // String prices
       3: invokestatic  #21                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: iconst_0
       7: istore_1
       8: iconst_1
       9: istore_2
      10: ldc           #114                // int -2147483648
      12: istore_3
      13: iload_1
      14: aload_0
      15: arraylength
      16: iconst_1
      17: isub
      18: if_icmpge     61
      21: aload_0
      22: iload_2
      23: iaload
      24: aload_0
      25: iload_2
      26: iaload
      27: isub
      28: ifgt          39
      31: iload_2
      32: istore_1
      33: iinc          2, 1
      36: goto          13
      39: aload_0
      40: iload_2
      41: iaload
      42: aload_0
      43: iload_1
      44: iaload
      45: isub
      46: istore        4
      48: iload_3
      49: iload         4
      51: invokestatic  #123                // Method java/lang/Math.max:(II)I
      54: istore_3
      55: iinc          2, 1
      58: goto          13
      61: iload_3
      62: ireturn

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


/* PROBLEM
   Implement an algorithm to determine if a string has all unique characters.
   What if you can not use additional data structures? */

/* ALGORITHM 1 - areCharsUnique
   Keep and array of 256 elements, corresponding to each ASCII character, initially set to
   false. For each letter in the original string I set the corresponding element of the
   array to true. If the string contains only unique characters, I must never find an element
   in the control array already set tu true (this would mean that the letter has already been
   found before).
   Performance: O(n)

   ALGORITHM 2 - areCharsUniqueSort
   Not optimal.
   First I sort the characters of the string in ascending order, then I scan the
   sorted string and check that there must not be 2 equal consecutive characters.
   Performance: O(n*logn)

   ALGORITHM 3 - areCharsUniqueDoubleLoop
   Not optimal.
   Compare each character of the string with all subsequent characters.
   Performance: O(n^2) */

val letters = Array(256) {false}

fun main() {
    val myString = "claudiosn"

    println("Characters are unique? -> ${areCharsUnique(myString)}")
    println("Characters are unique? -> ${areCharsUniqueSort(myString)}")
    println("Characters are unique? -> ${areCharsUniqueDoubleLoop(myString)}")
}

fun areCharsUnique(myString: String): Boolean {
    for(character in myString) {
        if(!letters[character.toInt()]) {
            letters[character.toInt()] = true
        } else {
            return false
        }
    }
    return true
}

fun areCharsUniqueSort(myString: String): Boolean {
    if(myString.length < 2) return true

    val arr = myString.toCharArray()
    val myStringSorted = arr.sorted().joinToString("")

    for(i in 0..myStringSorted.length - 2) {
        if(myStringSorted[i] == myStringSorted[i+1]) return false
    }

    return true
}

fun areCharsUniqueDoubleLoop(myString: String): Boolean {
    if(myString.length < 2) return true

    for(i in 0..myString.length-2) {
        for(j in i+1..myString.length-1) {
            if(myString[i] == myString[j]) return false
        }
    }

    return true
}

Compiled from "solution.kt"
public final class SolutionKt {
  private static final java.lang.Boolean[] letters;

  public static final java.lang.Boolean[] getLetters();
    Code:
       0: getstatic     #11                 // Field letters:[Ljava/lang/Boolean;
       3: areturn

  public static final void main();
    Code:
       0: ldc           #15                 // String claudiosn
       2: astore_0
       3: new           #17                 // class java/lang/StringBuilder
       6: dup
       7: invokespecial #20                 // Method java/lang/StringBuilder."<init>":()V
      10: ldc           #22                 // String Characters are unique? ->
      12: invokevirtual #26                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      15: aload_0
      16: invokestatic  #30                 // Method areCharsUnique:(Ljava/lang/String;)Z
      19: invokevirtual #33                 // Method java/lang/StringBuilder.append:(Z)Ljava/lang/StringBuilder;
      22: invokevirtual #37                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
      25: getstatic     #43                 // Field java/lang/System.out:Ljava/io/PrintStream;
      28: swap
      29: invokevirtual #49                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
      32: new           #17                 // class java/lang/StringBuilder
      35: dup
      36: invokespecial #20                 // Method java/lang/StringBuilder."<init>":()V
      39: ldc           #22                 // String Characters are unique? ->
      41: invokevirtual #26                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      44: aload_0
      45: invokestatic  #52                 // Method areCharsUniqueSort:(Ljava/lang/String;)Z
      48: invokevirtual #33                 // Method java/lang/StringBuilder.append:(Z)Ljava/lang/StringBuilder;
      51: invokevirtual #37                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
      54: getstatic     #43                 // Field java/lang/System.out:Ljava/io/PrintStream;
      57: swap
      58: invokevirtual #49                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
      61: new           #17                 // class java/lang/StringBuilder
      64: dup
      65: invokespecial #20                 // Method java/lang/StringBuilder."<init>":()V
      68: ldc           #22                 // String Characters are unique? ->
      70: invokevirtual #26                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      73: aload_0
      74: invokestatic  #55                 // Method areCharsUniqueDoubleLoop:(Ljava/lang/String;)Z
      77: invokevirtual #33                 // Method java/lang/StringBuilder.append:(Z)Ljava/lang/StringBuilder;
      80: invokevirtual #37                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
      83: getstatic     #43                 // Field java/lang/System.out:Ljava/io/PrintStream;
      86: swap
      87: invokevirtual #49                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
      90: return

  public static final boolean areCharsUnique(java.lang.String);
    Code:
       0: aload_0
       1: ldc           #58                 // String myString
       3: invokestatic  #64                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: iconst_0
       7: istore_1
       8: aload_0
       9: invokevirtual #70                 // Method java/lang/String.length:()I
      12: istore_2
      13: iload_1
      14: iload_2
      15: if_icmpge     55
      18: aload_0
      19: iload_1
      20: invokevirtual #74                 // Method java/lang/String.charAt:(I)C
      23: istore_3
      24: getstatic     #11                 // Field letters:[Ljava/lang/Boolean;
      27: iload_3
      28: aaload
      29: invokevirtual #80                 // Method java/lang/Boolean.booleanValue:()Z
      32: ifne          47
      35: getstatic     #11                 // Field letters:[Ljava/lang/Boolean;
      38: iload_3
      39: iconst_1
      40: invokestatic  #84                 // Method java/lang/Boolean.valueOf:(Z)Ljava/lang/Boolean;
      43: aastore
      44: goto          49
      47: iconst_0
      48: ireturn
      49: iinc          1, 1
      52: goto          13
      55: iconst_1
      56: ireturn

  public static final boolean areCharsUniqueSort(java.lang.String);
    Code:
       0: aload_0
       1: ldc           #58                 // String myString
       3: invokestatic  #64                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: invokevirtual #70                 // Method java/lang/String.length:()I
      10: iconst_2
      11: if_icmpge     16
      14: iconst_1
      15: ireturn
      16: aload_0
      17: invokevirtual #90                 // Method java/lang/String.toCharArray:()[C
      20: dup
      21: ldc           #92                 // String toCharArray(...)
      23: invokestatic  #95                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
      26: astore_1
      27: aload_1
      28: invokestatic  #101                // Method kotlin/collections/ArraysKt.sorted:([C)Ljava/util/List;
      31: checkcast     #103                // class java/lang/Iterable
      34: ldc           #105                // String
      36: checkcast     #107                // class java/lang/CharSequence
      39: aconst_null
      40: aconst_null
      41: iconst_0
      42: aconst_null
      43: aconst_null
      44: bipush        62
      46: aconst_null
      47: invokestatic  #113                // Method kotlin/collections/CollectionsKt.joinToString$default:(Ljava/lang/Iterable;Ljava/lang/CharSequence;Ljava/lang/CharSequence;Ljava/lang/CharSequence;ILjava/lang/CharSequence;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Ljava/lang/String;
      50: astore_2
      51: iconst_0
      52: istore_3
      53: aload_2
      54: invokevirtual #70                 // Method java/lang/String.length:()I
      57: iconst_2
      58: isub
      59: istore        4
      61: iload_3
      62: iload         4
      64: if_icmpgt     96
      67: aload_2
      68: iload_3
      69: invokevirtual #74                 // Method java/lang/String.charAt:(I)C
      72: aload_2
      73: iload_3
      74: iconst_1
      75: iadd
      76: invokevirtual #74                 // Method java/lang/String.charAt:(I)C
      79: if_icmpne     84
      82: iconst_0
      83: ireturn
      84: iload_3
      85: iload         4
      87: if_icmpeq     96
      90: iinc          3, 1
      93: goto          67
      96: iconst_1
      97: ireturn

  public static final boolean areCharsUniqueDoubleLoop(java.lang.String);
    Code:
       0: aload_0
       1: ldc           #58                 // String myString
       3: invokestatic  #64                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: invokevirtual #70                 // Method java/lang/String.length:()I
      10: iconst_2
      11: if_icmpge     16
      14: iconst_1
      15: ireturn
      16: iconst_0
      17: istore_1
      18: aload_0
      19: invokevirtual #70                 // Method java/lang/String.length:()I
      22: iconst_2
      23: isub
      24: istore_2
      25: iload_1
      26: iload_2
      27: if_icmpgt     78
      30: iload_1
      31: iconst_1
      32: iadd
      33: istore_3
      34: aload_0
      35: invokevirtual #70                 // Method java/lang/String.length:()I
      38: istore        4
      40: iload_3
      41: iload         4
      43: if_icmpge     67
      46: aload_0
      47: iload_1
      48: invokevirtual #74                 // Method java/lang/String.charAt:(I)C
      51: aload_0
      52: iload_3
      53: invokevirtual #74                 // Method java/lang/String.charAt:(I)C
      56: if_icmpne     61
      59: iconst_0
      60: ireturn
      61: iinc          3, 1
      64: goto          40
      67: iload_1
      68: iload_2
      69: if_icmpeq     78
      72: iinc          1, 1
      75: goto          30
      78: iconst_1
      79: ireturn

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

  static {};
    Code:
       0: iconst_0
       1: istore_0
       2: sipush        256
       5: anewarray     #76                 // class java/lang/Boolean
       8: astore_1
       9: iload_0
      10: sipush        256
      13: if_icmpge     31
      16: iload_0
      17: istore_2
      18: aload_1
      19: iload_2
      20: iconst_0
      21: invokestatic  #84                 // Method java/lang/Boolean.valueOf:(Z)Ljava/lang/Boolean;
      24: aastore
      25: iinc          0, 1
      28: goto          9
      31: aload_1
      32: putstatic     #11                 // Field letters:[Ljava/lang/Boolean;
      35: return
}


data class Note(val name: String)
{
    fun flatten() = Note(name[0].toString() + 'b')
}

val Notes by lazy {
    listOf("C", "C#", "D", "D#", "E", "F",
           "F#", "G", "G#", "A", "A#", "B")
    	.map { Note(it) }
}

enum class Interval(val value: Int) {
    Half(1),
    Whole(2),
    WholeAndHalf(3);

    operator fun times(multiplier: Int) =
        Array(multiplier) { this }
}

infix fun Note.hasSameLetterOf(note: Note) =
    this.name.startsWith(note.name[0])

/**
 * Changes note names to avoid repeated letters (e.g. "F" followed by "F#").
 */
fun uniqueNotes(notes: List<Note>): List<Note>
{
    val newNotes = notes.toMutableList()

    return notes.foldIndexed(newNotes) { i, acc, note ->
        val cycle = CyclicIterator(acc).apply { next(i) }
        val chromaticCycle = CyclicIterator(Notes)

    	if (cycle.prev() hasSameLetterOf note)
            acc.apply {
                set(i, chromaticCycle.next(Notes.indexOf(note) + 1).flatten())
            }
        else
            acc
    }
}

open class Scale(val root: Note, vararg val intervals: Interval)
{
    fun notes(): List<Note> {
        val iterator = CyclicIterator(Notes)

        iterator.next(Notes.indexOf(root))

        val notes = intervals.dropLast(1).fold(mutableListOf(root)) {
            acc, interval -> acc.apply { add(iterator.next(interval.value)) }
        }

        return uniqueNotes(notes)
    }
}

object Scales
{
    class Major(root: Note)
        : Scale(root, Interval.Whole,
                      Interval.Whole,
                      Interval.Half,
                      Interval.Whole,
                      Interval.Whole,
                      Interval.Whole,
                      Interval.Half)
    class Minor(root: Note)
    	: Scale(root, Interval.Whole,
                      Interval.Half,
                      Interval.Whole,
                      Interval.Whole,
                      Interval.Half,
                      Interval.Whole,
                      Interval.Whole)
}

open class Chord(val root: Note, val intervals: List<Array<Interval>>)
{
    fun notes(): List<Note> {
        val cyclicIterator = CyclicIterator(Notes).apply { next(Notes.indexOf(root)) }

        return intervals.fold(mutableListOf<Note>()) {
            acc, intervals -> acc.apply {
                add(cyclicIterator.next(intervals.halfStepsCount))
            }
        }.apply { add(0, root) }
    }

    private val Array<Interval>.halfStepsCount get() =
        fold(0) { sum, interval -> sum + interval.value }
}

object Chords
{
    class MajorTriad(root: Note)
        : Chord(root, listOf(Interval.Whole * 2,
                             Interval.Half * 3))
}

class CyclicIterator<T>(val list: List<T>)
{
    private var position = 0

    val current get() = list[position]

    fun next(n: Int = 1) =
        if (list.size == 0) {
            list[0]
        } else if (list.size > position + n) {
            position += n
            list[position]
        } else {
            position = position + n - list.size
            list[position]
        }

    fun prev(n: Int = 1) =
        if (position - n < 0) {
            position = list.size - n
            list[position]
        } else {
            position = position - n
            list[position]
        }
}

fun <T> assertEquals(a: T, b: T) {
    try {
        assert(a == b)
    } catch (e: AssertionError) {
        println("Expected: $a")
        println("Received: $b")
        throw e
    }
}

fun main() {
    assertEquals(
        listOf("Db", "Eb", "F", "Gb", "Ab", "Bb", "C"),
        Scales.Major(root = Note("C#")).notes().map { it.name }
    )
    assertEquals(
        listOf("B", "C#", "D#", "E", "F#", "G#", "A#"),
    	Scales.Major(root = Note("B")).notes().map { it.name }
    )
    assertEquals(
        listOf("Bb", "C", "D", "Eb", "F", "G", "A"),
    	Scales.Major(root = Note("A#")).notes().map { it.name }
    )
    assertEquals(
        listOf("Gb", "Ab", "Bb", "Cb", "Db", "Eb", "F"),
        Scales.Major(root = Note("F#")).notes().map { it.name }
    )
    assertEquals(
    	listOf("C", "D", "Eb", "F", "G", "Ab", "Bb"),
        Scales.Minor(root = Note("C")).notes().map { it.name }
    )
    assertEquals(
    	listOf("C", "E", "G"),
        Chords.MajorTriad(root = Note("C")).notes().map { it.name }
    )
    assertEquals(
    	listOf("D", "F#", "A"),
        Chords.MajorTriad(root = Note("D")).notes().map { it.name }
    )
    println("All tests passed!")
}

Compiled from "Main.kt"
public final class MainKt {
  private static final kotlin.Lazy Notes$delegate;

  public static final java.util.List<Note> getNotes();
    Code:
       0: getstatic     #12                 // Field Notes$delegate:Lkotlin/Lazy;
       3: astore_0
       4: aload_0
       5: invokeinterface #18,  1           // InterfaceMethod kotlin/Lazy.getValue:()Ljava/lang/Object;
      10: checkcast     #20                 // class java/util/List
      13: areturn

  public static final boolean hasSameLetterOf(Note, Note);
    Code:
       0: aload_0
       1: ldc           #24                 // String <this>
       3: invokestatic  #30                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_1
       7: ldc           #32                 // String note
       9: invokestatic  #30                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
      12: aload_0
      13: invokevirtual #38                 // Method Note.getName:()Ljava/lang/String;
      16: checkcast     #40                 // class java/lang/CharSequence
      19: aload_1
      20: invokevirtual #38                 // Method Note.getName:()Ljava/lang/String;
      23: iconst_0
      24: invokevirtual #46                 // Method java/lang/String.charAt:(I)C
      27: iconst_0
      28: iconst_2
      29: aconst_null
      30: invokestatic  #52                 // Method kotlin/text/StringsKt.startsWith$default:(Ljava/lang/CharSequence;CZILjava/lang/Object;)Z
      33: ireturn

  public static final java.util.List<Note> uniqueNotes(java.util.List<Note>);
    Code:
       0: aload_0
       1: ldc           #59                 // String notes
       3: invokestatic  #30                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: checkcast     #61                 // class java/util/Collection
      10: invokestatic  #67                 // Method kotlin/collections/CollectionsKt.toMutableList:(Ljava/util/Collection;)Ljava/util/List;
      13: astore_1
      14: aload_0
      15: checkcast     #69                 // class java/lang/Iterable
      18: astore_2
      19: iconst_0
      20: istore_3
      21: iconst_0
      22: istore        4
      24: aload_1
      25: astore        5
      27: aload_2
      28: invokeinterface #73,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      33: astore        6
      35: aload         6
      37: invokeinterface #79,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      42: ifeq          205
      45: aload         6
      47: invokeinterface #82,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      52: astore        7
      54: iload         4
      56: iinc          4, 1
      59: istore        8
      61: iload         8
      63: ifge          69
      66: invokestatic  #86                 // Method kotlin/collections/CollectionsKt.throwIndexOverflow:()V
      69: iload         8
      71: aload         5
      73: aload         7
      75: checkcast     #34                 // class Note
      78: astore        9
      80: astore        10
      82: istore        11
      84: iconst_0
      85: istore        12
      87: new           #88                 // class CyclicIterator
      90: dup
      91: aload         10
      93: invokespecial #92                 // Method CyclicIterator."<init>":(Ljava/util/List;)V
      96: astore        13
      98: aload         13
     100: astore        14
     102: iconst_0
     103: istore        15
     105: aload         14
     107: iload         11
     109: invokevirtual #95                 // Method CyclicIterator.next:(I)Ljava/lang/Object;
     112: pop
     113: aload         13
     115: astore        16
     117: new           #88                 // class CyclicIterator
     120: dup
     121: invokestatic  #97                 // Method getNotes:()Ljava/util/List;
     124: invokespecial #92                 // Method CyclicIterator."<init>":(Ljava/util/List;)V
     127: astore        13
     129: aload         16
     131: iconst_0
     132: iconst_1
     133: aconst_null
     134: invokestatic  #101                // Method CyclicIterator.prev$default:(LCyclicIterator;IILjava/lang/Object;)Ljava/lang/Object;
     137: checkcast     #34                 // class Note
     140: aload         9
     142: invokestatic  #103                // Method hasSameLetterOf:(LNote;LNote;)Z
     145: ifeq          198
     148: aload         10
     150: astore        14
     152: aload         14
     154: astore        15
     156: iconst_0
     157: istore        17
     159: aload         15
     161: iload         11
     163: aload         13
     165: invokestatic  #97                 // Method getNotes:()Ljava/util/List;
     168: aload         9
     170: invokeinterface #107,  2          // InterfaceMethod java/util/List.indexOf:(Ljava/lang/Object;)I
     175: iconst_1
     176: iadd
     177: invokevirtual #95                 // Method CyclicIterator.next:(I)Ljava/lang/Object;
     180: checkcast     #34                 // class Note
     183: invokevirtual #111                // Method Note.flatten:()LNote;
     186: invokeinterface #115,  3          // InterfaceMethod java/util/List.set:(ILjava/lang/Object;)Ljava/lang/Object;
     191: pop
     192: nop
     193: aload         14
     195: goto          200
     198: aload         10
     200: astore        5
     202: goto          35
     205: aload         5
     207: areturn

  public static final <T> void assertEquals(T, T);
    Code:
       0: nop
       1: aload_0
       2: aload_1
       3: invokestatic  #144                // Method kotlin/jvm/internal/Intrinsics.areEqual:(Ljava/lang/Object;Ljava/lang/Object;)Z
       6: istore_2
       7: getstatic     #150                // Field kotlin/_Assertions.ENABLED:Z
      10: ifeq          84
      13: iload_2
      14: ifne          84
      17: ldc           #152                // String Assertion failed
      19: astore_3
      20: new           #140                // class java/lang/AssertionError
      23: dup
      24: aload_3
      25: invokespecial #155                // Method java/lang/AssertionError."<init>":(Ljava/lang/Object;)V
      28: athrow
      29: astore_2
      30: new           #157                // class java/lang/StringBuilder
      33: dup
      34: invokespecial #159                // Method java/lang/StringBuilder."<init>":()V
      37: ldc           #161                // String Expected:
      39: invokevirtual #165                // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      42: aload_0
      43: invokevirtual #168                // Method java/lang/StringBuilder.append:(Ljava/lang/Object;)Ljava/lang/StringBuilder;
      46: invokevirtual #171                // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
      49: getstatic     #177                // Field java/lang/System.out:Ljava/io/PrintStream;
      52: swap
      53: invokevirtual #182                // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
      56: new           #157                // class java/lang/StringBuilder
      59: dup
      60: invokespecial #159                // Method java/lang/StringBuilder."<init>":()V
      63: ldc           #184                // String Received:
      65: invokevirtual #165                // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      68: aload_1
      69: invokevirtual #168                // Method java/lang/StringBuilder.append:(Ljava/lang/Object;)Ljava/lang/StringBuilder;
      72: invokevirtual #171                // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
      75: getstatic     #177                // Field java/lang/System.out:Ljava/io/PrintStream;
      78: swap
      79: invokevirtual #182                // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
      82: aload_2
      83: athrow
      84: return
    Exception table:
       from    to  target type
           0    29    29   Class java/lang/AssertionError

  public static final void main();
    Code:
       0: bipush        7
       2: anewarray     #42                 // class java/lang/String
       5: astore_0
       6: aload_0
       7: iconst_0
       8: ldc           #191                // String Db
      10: aastore
      11: aload_0
      12: iconst_1
      13: ldc           #193                // String Eb
      15: aastore
      16: aload_0
      17: iconst_2
      18: ldc           #195                // String F
      20: aastore
      21: aload_0
      22: iconst_3
      23: ldc           #197                // String Gb
      25: aastore
      26: aload_0
      27: iconst_4
      28: ldc           #199                // String Ab
      30: aastore
      31: aload_0
      32: iconst_5
      33: ldc           #201                // String Bb
      35: aastore
      36: aload_0
      37: bipush        6
      39: ldc           #203                // String C
      41: aastore
      42: aload_0
      43: invokestatic  #207                // Method kotlin/collections/CollectionsKt.listOf:([Ljava/lang/Object;)Ljava/util/List;
      46: new           #209                // class Scales$Major
      49: dup
      50: new           #34                 // class Note
      53: dup
      54: ldc           #211                // String C#
      56: invokespecial #214                // Method Note."<init>":(Ljava/lang/String;)V
      59: invokespecial #217                // Method Scales$Major."<init>":(LNote;)V
      62: invokevirtual #219                // Method Scales$Major.notes:()Ljava/util/List;
      65: checkcast     #69                 // class java/lang/Iterable
      68: astore_0
      69: astore        9
      71: iconst_0
      72: istore_1
      73: aload_0
      74: astore_2
      75: new           #221                // class java/util/ArrayList
      78: dup
      79: aload_0
      80: bipush        10
      82: invokestatic  #225                // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
      85: invokespecial #228                // Method java/util/ArrayList."<init>":(I)V
      88: checkcast     #61                 // class java/util/Collection
      91: astore_3
      92: iconst_0
      93: istore        4
      95: aload_2
      96: invokeinterface #73,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     101: astore        5
     103: aload         5
     105: invokeinterface #79,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     110: ifeq          152
     113: aload         5
     115: invokeinterface #82,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     120: astore        6
     122: aload_3
     123: aload         6
     125: checkcast     #34                 // class Note
     128: astore        7
     130: astore        10
     132: iconst_0
     133: istore        8
     135: aload         7
     137: invokevirtual #38                 // Method Note.getName:()Ljava/lang/String;
     140: aload         10
     142: swap
     143: invokeinterface #232,  2          // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     148: pop
     149: goto          103
     152: aload_3
     153: checkcast     #20                 // class java/util/List
     156: nop
     157: aload         9
     159: swap
     160: invokestatic  #234                // Method assertEquals:(Ljava/lang/Object;Ljava/lang/Object;)V
     163: bipush        7
     165: anewarray     #42                 // class java/lang/String
     168: astore_0
     169: aload_0
     170: iconst_0
     171: ldc           #236                // String B
     173: aastore
     174: aload_0
     175: iconst_1
     176: ldc           #211                // String C#
     178: aastore
     179: aload_0
     180: iconst_2
     181: ldc           #238                // String D#
     183: aastore
     184: aload_0
     185: iconst_3
     186: ldc           #240                // String E
     188: aastore
     189: aload_0
     190: iconst_4
     191: ldc           #242                // String F#
     193: aastore
     194: aload_0
     195: iconst_5
     196: ldc           #244                // String G#
     198: aastore
     199: aload_0
     200: bipush        6
     202: ldc           #246                // String A#
     204: aastore
     205: aload_0
     206: invokestatic  #207                // Method kotlin/collections/CollectionsKt.listOf:([Ljava/lang/Object;)Ljava/util/List;
     209: new           #209                // class Scales$Major
     212: dup
     213: new           #34                 // class Note
     216: dup
     217: ldc           #236                // String B
     219: invokespecial #214                // Method Note."<init>":(Ljava/lang/String;)V
     222: invokespecial #217                // Method Scales$Major."<init>":(LNote;)V
     225: invokevirtual #219                // Method Scales$Major.notes:()Ljava/util/List;
     228: checkcast     #69                 // class java/lang/Iterable
     231: astore_0
     232: astore        9
     234: iconst_0
     235: istore_1
     236: aload_0
     237: astore_2
     238: new           #221                // class java/util/ArrayList
     241: dup
     242: aload_0
     243: bipush        10
     245: invokestatic  #225                // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
     248: invokespecial #228                // Method java/util/ArrayList."<init>":(I)V
     251: checkcast     #61                 // class java/util/Collection
     254: astore_3
     255: iconst_0
     256: istore        4
     258: aload_2
     259: invokeinterface #73,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     264: astore        5
     266: aload         5
     268: invokeinterface #79,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     273: ifeq          315
     276: aload         5
     278: invokeinterface #82,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     283: astore        6
     285: aload_3
     286: aload         6
     288: checkcast     #34                 // class Note
     291: astore        7
     293: astore        10
     295: iconst_0
     296: istore        8
     298: aload         7
     300: invokevirtual #38                 // Method Note.getName:()Ljava/lang/String;
     303: aload         10
     305: swap
     306: invokeinterface #232,  2          // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     311: pop
     312: goto          266
     315: aload_3
     316: checkcast     #20                 // class java/util/List
     319: nop
     320: aload         9
     322: swap
     323: invokestatic  #234                // Method assertEquals:(Ljava/lang/Object;Ljava/lang/Object;)V
     326: bipush        7
     328: anewarray     #42                 // class java/lang/String
     331: astore_0
     332: aload_0
     333: iconst_0
     334: ldc           #201                // String Bb
     336: aastore
     337: aload_0
     338: iconst_1
     339: ldc           #203                // String C
     341: aastore
     342: aload_0
     343: iconst_2
     344: ldc           #248                // String D
     346: aastore
     347: aload_0
     348: iconst_3
     349: ldc           #193                // String Eb
     351: aastore
     352: aload_0
     353: iconst_4
     354: ldc           #195                // String F
     356: aastore
     357: aload_0
     358: iconst_5
     359: ldc           #250                // String G
     361: aastore
     362: aload_0
     363: bipush        6
     365: ldc           #252                // String A
     367: aastore
     368: aload_0
     369: invokestatic  #207                // Method kotlin/collections/CollectionsKt.listOf:([Ljava/lang/Object;)Ljava/util/List;
     372: new           #209                // class Scales$Major
     375: dup
     376: new           #34                 // class Note
     379: dup
     380: ldc           #246                // String A#
     382: invokespecial #214                // Method Note."<init>":(Ljava/lang/String;)V
     385: invokespecial #217                // Method Scales$Major."<init>":(LNote;)V
     388: invokevirtual #219                // Method Scales$Major.notes:()Ljava/util/List;
     391: checkcast     #69                 // class java/lang/Iterable
     394: astore_0
     395: astore        9
     397: iconst_0
     398: istore_1
     399: aload_0
     400: astore_2
     401: new           #221                // class java/util/ArrayList
     404: dup
     405: aload_0
     406: bipush        10
     408: invokestatic  #225                // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
     411: invokespecial #228                // Method java/util/ArrayList."<init>":(I)V
     414: checkcast     #61                 // class java/util/Collection
     417: astore_3
     418: iconst_0
     419: istore        4
     421: aload_2
     422: invokeinterface #73,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     427: astore        5
     429: aload         5
     431: invokeinterface #79,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     436: ifeq          478
     439: aload         5
     441: invokeinterface #82,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     446: astore        6
     448: aload_3
     449: aload         6
     451: checkcast     #34                 // class Note
     454: astore        7
     456: astore        10
     458: iconst_0
     459: istore        8
     461: aload         7
     463: invokevirtual #38                 // Method Note.getName:()Ljava/lang/String;
     466: aload         10
     468: swap
     469: invokeinterface #232,  2          // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     474: pop
     475: goto          429
     478: aload_3
     479: checkcast     #20                 // class java/util/List
     482: nop
     483: aload         9
     485: swap
     486: invokestatic  #234                // Method assertEquals:(Ljava/lang/Object;Ljava/lang/Object;)V
     489: bipush        7
     491: anewarray     #42                 // class java/lang/String
     494: astore_0
     495: aload_0
     496: iconst_0
     497: ldc           #197                // String Gb
     499: aastore
     500: aload_0
     501: iconst_1
     502: ldc           #199                // String Ab
     504: aastore
     505: aload_0
     506: iconst_2
     507: ldc           #201                // String Bb
     509: aastore
     510: aload_0
     511: iconst_3
     512: ldc           #254                // String Cb
     514: aastore
     515: aload_0
     516: iconst_4
     517: ldc           #191                // String Db
     519: aastore
     520: aload_0
     521: iconst_5
     522: ldc           #193                // String Eb
     524: aastore
     525: aload_0
     526: bipush        6
     528: ldc           #195                // String F
     530: aastore
     531: aload_0
     532: invokestatic  #207                // Method kotlin/collections/CollectionsKt.listOf:([Ljava/lang/Object;)Ljava/util/List;
     535: new           #209                // class Scales$Major
     538: dup
     539: new           #34                 // class Note
     542: dup
     543: ldc           #242                // String F#
     545: invokespecial #214                // Method Note."<init>":(Ljava/lang/String;)V
     548: invokespecial #217                // Method Scales$Major."<init>":(LNote;)V
     551: invokevirtual #219                // Method Scales$Major.notes:()Ljava/util/List;
     554: checkcast     #69                 // class java/lang/Iterable
     557: astore_0
     558: astore        9
     560: iconst_0
     561: istore_1
     562: aload_0
     563: astore_2
     564: new           #221                // class java/util/ArrayList
     567: dup
     568: aload_0
     569: bipush        10
     571: invokestatic  #225                // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
     574: invokespecial #228                // Method java/util/ArrayList."<init>":(I)V
     577: checkcast     #61                 // class java/util/Collection
     580: astore_3
     581: iconst_0
     582: istore        4
     584: aload_2
     585: invokeinterface #73,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     590: astore        5
     592: aload         5
     594: invokeinterface #79,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     599: ifeq          641
     602: aload         5
     604: invokeinterface #82,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     609: astore        6
     611: aload_3
     612: aload         6
     614: checkcast     #34                 // class Note
     617: astore        7
     619: astore        10
     621: iconst_0
     622: istore        8
     624: aload         7
     626: invokevirtual #38                 // Method Note.getName:()Ljava/lang/String;
     629: aload         10
     631: swap
     632: invokeinterface #232,  2          // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     637: pop
     638: goto          592
     641: aload_3
     642: checkcast     #20                 // class java/util/List
     645: nop
     646: aload         9
     648: swap
     649: invokestatic  #234                // Method assertEquals:(Ljava/lang/Object;Ljava/lang/Object;)V
     652: bipush        7
     654: anewarray     #42                 // class java/lang/String
     657: astore_0
     658: aload_0
     659: iconst_0
     660: ldc           #203                // String C
     662: aastore
     663: aload_0
     664: iconst_1
     665: ldc           #248                // String D
     667: aastore
     668: aload_0
     669: iconst_2
     670: ldc           #193                // String Eb
     672: aastore
     673: aload_0
     674: iconst_3
     675: ldc           #195                // String F
     677: aastore
     678: aload_0
     679: iconst_4
     680: ldc           #250                // String G
     682: aastore
     683: aload_0
     684: iconst_5
     685: ldc           #199                // String Ab
     687: aastore
     688: aload_0
     689: bipush        6
     691: ldc           #201                // String Bb
     693: aastore
     694: aload_0
     695: invokestatic  #207                // Method kotlin/collections/CollectionsKt.listOf:([Ljava/lang/Object;)Ljava/util/List;
     698: new           #256                // class Scales$Minor
     701: dup
     702: new           #34                 // class Note
     705: dup
     706: ldc           #203                // String C
     708: invokespecial #214                // Method Note."<init>":(Ljava/lang/String;)V
     711: invokespecial #257                // Method Scales$Minor."<init>":(LNote;)V
     714: invokevirtual #258                // Method Scales$Minor.notes:()Ljava/util/List;
     717: checkcast     #69                 // class java/lang/Iterable
     720: astore_0
     721: astore        9
     723: iconst_0
     724: istore_1
     725: aload_0
     726: astore_2
     727: new           #221                // class java/util/ArrayList
     730: dup
     731: aload_0
     732: bipush        10
     734: invokestatic  #225                // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
     737: invokespecial #228                // Method java/util/ArrayList."<init>":(I)V
     740: checkcast     #61                 // class java/util/Collection
     743: astore_3
     744: iconst_0
     745: istore        4
     747: aload_2
     748: invokeinterface #73,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     753: astore        5
     755: aload         5
     757: invokeinterface #79,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     762: ifeq          804
     765: aload         5
     767: invokeinterface #82,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     772: astore        6
     774: aload_3
     775: aload         6
     777: checkcast     #34                 // class Note
     780: astore        7
     782: astore        10
     784: iconst_0
     785: istore        8
     787: aload         7
     789: invokevirtual #38                 // Method Note.getName:()Ljava/lang/String;
     792: aload         10
     794: swap
     795: invokeinterface #232,  2          // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     800: pop
     801: goto          755
     804: aload_3
     805: checkcast     #20                 // class java/util/List
     808: nop
     809: aload         9
     811: swap
     812: invokestatic  #234                // Method assertEquals:(Ljava/lang/Object;Ljava/lang/Object;)V
     815: iconst_3
     816: anewarray     #42                 // class java/lang/String
     819: astore_0
     820: aload_0
     821: iconst_0
     822: ldc           #203                // String C
     824: aastore
     825: aload_0
     826: iconst_1
     827: ldc           #240                // String E
     829: aastore
     830: aload_0
     831: iconst_2
     832: ldc           #250                // String G
     834: aastore
     835: aload_0
     836: invokestatic  #207                // Method kotlin/collections/CollectionsKt.listOf:([Ljava/lang/Object;)Ljava/util/List;
     839: new           #260                // class Chords$MajorTriad
     842: dup
     843: new           #34                 // class Note
     846: dup
     847: ldc           #203                // String C
     849: invokespecial #214                // Method Note."<init>":(Ljava/lang/String;)V
     852: invokespecial #261                // Method Chords$MajorTriad."<init>":(LNote;)V
     855: invokevirtual #262                // Method Chords$MajorTriad.notes:()Ljava/util/List;
     858: checkcast     #69                 // class java/lang/Iterable
     861: astore_0
     862: astore        9
     864: iconst_0
     865: istore_1
     866: aload_0
     867: astore_2
     868: new           #221                // class java/util/ArrayList
     871: dup
     872: aload_0
     873: bipush        10
     875: invokestatic  #225                // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
     878: invokespecial #228                // Method java/util/ArrayList."<init>":(I)V
     881: checkcast     #61                 // class java/util/Collection
     884: astore_3
     885: iconst_0
     886: istore        4
     888: aload_2
     889: invokeinterface #73,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     894: astore        5
     896: aload         5
     898: invokeinterface #79,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     903: ifeq          945
     906: aload         5
     908: invokeinterface #82,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     913: astore        6
     915: aload_3
     916: aload         6
     918: checkcast     #34                 // class Note
     921: astore        7
     923: astore        10
     925: iconst_0
     926: istore        8
     928: aload         7
     930: invokevirtual #38                 // Method Note.getName:()Ljava/lang/String;
     933: aload         10
     935: swap
     936: invokeinterface #232,  2          // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     941: pop
     942: goto          896
     945: aload_3
     946: checkcast     #20                 // class java/util/List
     949: nop
     950: aload         9
     952: swap
     953: invokestatic  #234                // Method assertEquals:(Ljava/lang/Object;Ljava/lang/Object;)V
     956: iconst_3
     957: anewarray     #42                 // class java/lang/String
     960: astore_0
     961: aload_0
     962: iconst_0
     963: ldc           #248                // String D
     965: aastore
     966: aload_0
     967: iconst_1
     968: ldc           #242                // String F#
     970: aastore
     971: aload_0
     972: iconst_2
     973: ldc           #252                // String A
     975: aastore
     976: aload_0
     977: invokestatic  #207                // Method kotlin/collections/CollectionsKt.listOf:([Ljava/lang/Object;)Ljava/util/List;
     980: new           #260                // class Chords$MajorTriad
     983: dup
     984: new           #34                 // class Note
     987: dup
     988: ldc           #248                // String D
     990: invokespecial #214                // Method Note."<init>":(Ljava/lang/String;)V
     993: invokespecial #261                // Method Chords$MajorTriad."<init>":(LNote;)V
     996: invokevirtual #262                // Method Chords$MajorTriad.notes:()Ljava/util/List;
     999: checkcast     #69                 // class java/lang/Iterable
    1002: astore_0
    1003: astore        9
    1005: iconst_0
    1006: istore_1
    1007: aload_0
    1008: astore_2
    1009: new           #221                // class java/util/ArrayList
    1012: dup
    1013: aload_0
    1014: bipush        10
    1016: invokestatic  #225                // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
    1019: invokespecial #228                // Method java/util/ArrayList."<init>":(I)V
    1022: checkcast     #61                 // class java/util/Collection
    1025: astore_3
    1026: iconst_0
    1027: istore        4
    1029: aload_2
    1030: invokeinterface #73,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
    1035: astore        5
    1037: aload         5
    1039: invokeinterface #79,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
    1044: ifeq          1086
    1047: aload         5
    1049: invokeinterface #82,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
    1054: astore        6
    1056: aload_3
    1057: aload         6
    1059: checkcast     #34                 // class Note
    1062: astore        7
    1064: astore        10
    1066: iconst_0
    1067: istore        8
    1069: aload         7
    1071: invokevirtual #38                 // Method Note.getName:()Ljava/lang/String;
    1074: aload         10
    1076: swap
    1077: invokeinterface #232,  2          // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
    1082: pop
    1083: goto          1037
    1086: aload_3
    1087: checkcast     #20                 // class java/util/List
    1090: nop
    1091: aload         9
    1093: swap
    1094: invokestatic  #234                // Method assertEquals:(Ljava/lang/Object;Ljava/lang/Object;)V
    1097: ldc_w         #264                // String All tests passed!
    1100: getstatic     #177                // Field java/lang/System.out:Ljava/io/PrintStream;
    1103: swap
    1104: invokevirtual #182                // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
    1107: return

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

  private static final java.util.List Notes_delegate$lambda$1();
    Code:
       0: bipush        12
       2: anewarray     #42                 // class java/lang/String
       5: astore_0
       6: aload_0
       7: iconst_0
       8: ldc           #203                // String C
      10: aastore
      11: aload_0
      12: iconst_1
      13: ldc           #211                // String C#
      15: aastore
      16: aload_0
      17: iconst_2
      18: ldc           #248                // String D
      20: aastore
      21: aload_0
      22: iconst_3
      23: ldc           #238                // String D#
      25: aastore
      26: aload_0
      27: iconst_4
      28: ldc           #240                // String E
      30: aastore
      31: aload_0
      32: iconst_5
      33: ldc           #195                // String F
      35: aastore
      36: aload_0
      37: bipush        6
      39: ldc           #242                // String F#
      41: aastore
      42: aload_0
      43: bipush        7
      45: ldc           #250                // String G
      47: aastore
      48: aload_0
      49: bipush        8
      51: ldc           #244                // String G#
      53: aastore
      54: aload_0
      55: bipush        9
      57: ldc           #252                // String A
      59: aastore
      60: aload_0
      61: bipush        10
      63: ldc           #246                // String A#
      65: aastore
      66: aload_0
      67: bipush        11
      69: ldc           #236                // String B
      71: aastore
      72: aload_0
      73: invokestatic  #207                // Method kotlin/collections/CollectionsKt.listOf:([Ljava/lang/Object;)Ljava/util/List;
      76: checkcast     #69                 // class java/lang/Iterable
      79: astore_0
      80: nop
      81: iconst_0
      82: istore_1
      83: aload_0
      84: astore_2
      85: new           #221                // class java/util/ArrayList
      88: dup
      89: aload_0
      90: bipush        10
      92: invokestatic  #225                // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
      95: invokespecial #228                // Method java/util/ArrayList."<init>":(I)V
      98: checkcast     #61                 // class java/util/Collection
     101: astore_3
     102: iconst_0
     103: istore        4
     105: aload_2
     106: invokeinterface #73,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     111: astore        5
     113: aload         5
     115: invokeinterface #79,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     120: ifeq          166
     123: aload         5
     125: invokeinterface #82,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     130: astore        6
     132: aload_3
     133: aload         6
     135: checkcast     #42                 // class java/lang/String
     138: astore        7
     140: astore        9
     142: iconst_0
     143: istore        8
     145: new           #34                 // class Note
     148: dup
     149: aload         7
     151: invokespecial #214                // Method Note."<init>":(Ljava/lang/String;)V
     154: aload         9
     156: swap
     157: invokeinterface #232,  2          // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     162: pop
     163: goto          113
     166: aload_3
     167: checkcast     #20                 // class java/util/List
     170: nop
     171: areturn

  static {};
    Code:
       0: invokedynamic #304,  0            // InvokeDynamic #0:invoke:()Lkotlin/jvm/functions/Function0;
       5: invokestatic  #310                // Method kotlin/LazyKt.lazy:(Lkotlin/jvm/functions/Function0;)Lkotlin/Lazy;
       8: putstatic     #12                 // Field Notes$delegate:Lkotlin/Lazy;
      11: return
}


fun main() {
    val strs = arrayOf("flower","flow","flight")
    val strs2 = arrayOf("dog","racecar","car")
    val strs3 = arrayOf("a")
    val strs4 = arrayOf("reflower","flow","flight")
    val strs5 = arrayOf("aaa","aa","aaa")

    longestCommonPrefix(strs)
    longestCommonPrefix(strs2)
    longestCommonPrefix(strs3)
    longestCommonPrefix(strs4)
    longestCommonPrefix(strs5)

}

fun longestCommonPrefix(strs: Array<String>): String {
    var menorPalavra = strs[0]
    var resposta = ""
    var index = 0
    var contador = menorPalavra.length

    if(strs.isEmpty()) return ""
    if(strs.size == 1) return strs[0]

    strs.forEach {
        if(it == "") return ""
        if(it.length < menorPalavra.length){
            menorPalavra = it
        }
    }
    val strsFiltrado = strs.filter { it != menorPalavra }

    strsFiltrado.forEach{
        var i = 0
        while (i < it.length && contador > 0)
        if (index < menorPalavra.length && it[index] == menorPalavra[index]){
            index++
            i++
        } else if (index < contador) {
            contador = index
            i = it.length
            index = 0
        } else {
            i = it.length
            index = 0
        }
    }

    resposta = menorPalavra.substring(0, contador)

    return resposta

}

Compiled from "LongestCommonPrefix.kt"
public final class LongestCommonPrefixKt {
  public static final void main();
    Code:
       0: iconst_3
       1: anewarray     #8                  // class java/lang/String
       4: astore_1
       5: aload_1
       6: iconst_0
       7: ldc           #10                 // String flower
       9: aastore
      10: aload_1
      11: iconst_1
      12: ldc           #12                 // String flow
      14: aastore
      15: aload_1
      16: iconst_2
      17: ldc           #14                 // String flight
      19: aastore
      20: aload_1
      21: astore_0
      22: iconst_3
      23: anewarray     #8                  // class java/lang/String
      26: astore_2
      27: aload_2
      28: iconst_0
      29: ldc           #16                 // String dog
      31: aastore
      32: aload_2
      33: iconst_1
      34: ldc           #18                 // String racecar
      36: aastore
      37: aload_2
      38: iconst_2
      39: ldc           #20                 // String car
      41: aastore
      42: aload_2
      43: astore_1
      44: iconst_1
      45: anewarray     #8                  // class java/lang/String
      48: astore_3
      49: aload_3
      50: iconst_0
      51: ldc           #22                 // String a
      53: aastore
      54: aload_3
      55: astore_2
      56: iconst_3
      57: anewarray     #8                  // class java/lang/String
      60: astore        4
      62: aload         4
      64: iconst_0
      65: ldc           #24                 // String reflower
      67: aastore
      68: aload         4
      70: iconst_1
      71: ldc           #12                 // String flow
      73: aastore
      74: aload         4
      76: iconst_2
      77: ldc           #14                 // String flight
      79: aastore
      80: aload         4
      82: astore_3
      83: iconst_3
      84: anewarray     #8                  // class java/lang/String
      87: astore        5
      89: aload         5
      91: iconst_0
      92: ldc           #26                 // String aaa
      94: aastore
      95: aload         5
      97: iconst_1
      98: ldc           #28                 // String aa
     100: aastore
     101: aload         5
     103: iconst_2
     104: ldc           #26                 // String aaa
     106: aastore
     107: aload         5
     109: astore        4
     111: aload_0
     112: invokestatic  #32                 // Method longestCommonPrefix:([Ljava/lang/String;)Ljava/lang/String;
     115: pop
     116: aload_1
     117: invokestatic  #32                 // Method longestCommonPrefix:([Ljava/lang/String;)Ljava/lang/String;
     120: pop
     121: aload_2
     122: invokestatic  #32                 // Method longestCommonPrefix:([Ljava/lang/String;)Ljava/lang/String;
     125: pop
     126: aload_3
     127: invokestatic  #32                 // Method longestCommonPrefix:([Ljava/lang/String;)Ljava/lang/String;
     130: pop
     131: aload         4
     133: invokestatic  #32                 // Method longestCommonPrefix:([Ljava/lang/String;)Ljava/lang/String;
     136: pop
     137: return

  public static final java.lang.String longestCommonPrefix(java.lang.String[]);
    Code:
       0: aload_0
       1: ldc           #40                 // String strs
       3: invokestatic  #46                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aconst_null
       7: astore_1
       8: aload_0
       9: iconst_0
      10: aaload
      11: astore_1
      12: ldc           #48                 // String
      14: astore_2
      15: iconst_0
      16: istore_3
      17: iconst_0
      18: istore        4
      20: aload_1
      21: invokevirtual #52                 // Method java/lang/String.length:()I
      24: istore        4
      26: aload_0
      27: arraylength
      28: ifne          35
      31: iconst_1
      32: goto          36
      35: iconst_0
      36: ifeq          42
      39: ldc           #48                 // String
      41: areturn
      42: aload_0
      43: arraylength
      44: iconst_1
      45: if_icmpne     52
      48: aload_0
      49: iconst_0
      50: aaload
      51: areturn
      52: aload_0
      53: astore        5
      55: iconst_0
      56: istore        6
      58: iconst_0
      59: istore        7
      61: aload         5
      63: arraylength
      64: istore        8
      66: iload         7
      68: iload         8
      70: if_icmpge     123
      73: aload         5
      75: iload         7
      77: aaload
      78: astore        9
      80: aload         9
      82: astore        10
      84: iconst_0
      85: istore        11
      87: aload         10
      89: ldc           #48                 // String
      91: invokestatic  #56                 // Method kotlin/jvm/internal/Intrinsics.areEqual:(Ljava/lang/Object;Ljava/lang/Object;)Z
      94: ifeq          100
      97: ldc           #48                 // String
      99: areturn
     100: aload         10
     102: invokevirtual #52                 // Method java/lang/String.length:()I
     105: aload_1
     106: invokevirtual #52                 // Method java/lang/String.length:()I
     109: if_icmpge     115
     112: aload         10
     114: astore_1
     115: nop
     116: nop
     117: iinc          7, 1
     120: goto          66
     123: nop
     124: aload_0
     125: astore        6
     127: iconst_0
     128: istore        7
     130: aload         6
     132: astore        8
     134: new           #58                 // class java/util/ArrayList
     137: dup
     138: invokespecial #61                 // Method java/util/ArrayList."<init>":()V
     141: checkcast     #63                 // class java/util/Collection
     144: astore        9
     146: iconst_0
     147: istore        10
     149: iconst_0
     150: istore        11
     152: aload         8
     154: arraylength
     155: istore        12
     157: iload         11
     159: iload         12
     161: if_icmpge     211
     164: aload         8
     166: iload         11
     168: aaload
     169: astore        13
     171: aload         13
     173: astore        14
     175: iconst_0
     176: istore        15
     178: aload         14
     180: aload_1
     181: invokestatic  #56                 // Method kotlin/jvm/internal/Intrinsics.areEqual:(Ljava/lang/Object;Ljava/lang/Object;)Z
     184: ifne          191
     187: iconst_1
     188: goto          192
     191: iconst_0
     192: ifeq          205
     195: aload         9
     197: aload         13
     199: invokeinterface #67,  2           // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     204: pop
     205: iinc          11, 1
     208: goto          157
     211: aload         9
     213: checkcast     #69                 // class java/util/List
     216: nop
     217: astore        5
     219: aload         5
     221: checkcast     #71                 // class java/lang/Iterable
     224: astore        6
     226: iconst_0
     227: istore        7
     229: aload         6
     231: invokeinterface #75,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     236: astore        8
     238: aload         8
     240: invokeinterface #81,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     245: ifeq          358
     248: aload         8
     250: invokeinterface #85,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     255: astore        9
     257: aload         9
     259: checkcast     #8                  // class java/lang/String
     262: astore        10
     264: iconst_0
     265: istore        11
     267: iconst_0
     268: istore        12
     270: iload         12
     272: aload         10
     274: invokevirtual #52                 // Method java/lang/String.length:()I
     277: if_icmpge     353
     280: iload         4
     282: ifle          353
     285: iload_3
     286: aload_1
     287: invokevirtual #52                 // Method java/lang/String.length:()I
     290: if_icmpge     320
     293: aload         10
     295: iload_3
     296: invokevirtual #89                 // Method java/lang/String.charAt:(I)C
     299: aload_1
     300: iload_3
     301: invokevirtual #89                 // Method java/lang/String.charAt:(I)C
     304: if_icmpne     320
     307: iload_3
     308: iconst_1
     309: iadd
     310: istore_3
     311: iload         12
     313: iinc          12, 1
     316: pop
     317: goto          270
     320: iload_3
     321: iload         4
     323: if_icmpge     341
     326: iload_3
     327: istore        4
     329: aload         10
     331: invokevirtual #52                 // Method java/lang/String.length:()I
     334: istore        12
     336: iconst_0
     337: istore_3
     338: goto          270
     341: aload         10
     343: invokevirtual #52                 // Method java/lang/String.length:()I
     346: istore        12
     348: iconst_0
     349: istore_3
     350: goto          270
     353: nop
     354: nop
     355: goto          238
     358: nop
     359: aload_1
     360: iconst_0
     361: iload         4
     363: invokevirtual #93                 // Method java/lang/String.substring:(II)Ljava/lang/String;
     366: dup
     367: ldc           #95                 // String substring(...)
     369: invokestatic  #98                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
     372: astore_2
     373: aload_2
     374: areturn

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


import java.util.stream.Collectors

// https://adventofcode.com/2020/day/1
class Day1(
    private val values: List<Int>
) {
    fun solve(): Pair<Int, Int> {
        val pairs: MutableList<Pair<Int, Int>> = mutableListOf()

        for (outLoop in values.indices) {
            for (innerLoop in values.indices) {
                if (!outLoop.equals(innerLoop)) {
                    val first = Math.max(values[outLoop], values[innerLoop])
                    val second = Math.min(values[outLoop], values[innerLoop])
                    pairs.add(Pair(first, second))
                }
            }
        }

        return pairs
            .filter { it.first + it.second == 2020 }
            .distinct()
            .get(0)
    }
}

// https://adventofcode.com/2020/day/1
class Day1Part2(
    private val values: List<Int>
) {
    fun solve(): Triple<Int, Int, Int> {
        val tuples: MutableList<Triple<Int, Int, Int>> = mutableListOf()

        for (outLoop in values) {
            for (innerLoop in values) {
                for (innerInnerLoop in values) {
                    val points = listOf(outLoop, innerLoop, innerInnerLoop).sorted()
                    tuples.add(Triple(points[0], points[1], points[2]))
                }
            }
        }

        return tuples
            .filter { it.first.plus(it.second).plus(it.third) == 2020 }
            .distinct()
            .get(0)
    }
}

fun main() {
    val problem = Day1::class.java.getResource("day1.txt")
        .readText()
        .split('\n')
        .stream()
        .map { it.toInt() }
        .collect(Collectors.toList())
    val pair = Day1(problem).solve()
    println("solution = $pair, x*y = ${pair.first * pair.second}")
    val triple = Day1Part2(problem).solve()
    println("solution = $triple, x*y*z = ${triple.first * triple.second * triple.third}")
}

Compiled from "day1.kt"
public final class Day1 {
  private final java.util.List<java.lang.Integer> values;

  public Day1(java.util.List<java.lang.Integer>);
    Code:
       0: aload_1
       1: ldc           #10                 // String values
       3: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: invokespecial #19                 // Method java/lang/Object."<init>":()V
      10: aload_0
      11: aload_1
      12: putfield      #22                 // Field values:Ljava/util/List;
      15: return

  public final kotlin.Pair<java.lang.Integer, java.lang.Integer> solve();
    Code:
       0: new           #29                 // class java/util/ArrayList
       3: dup
       4: invokespecial #30                 // Method java/util/ArrayList."<init>":()V
       7: checkcast     #32                 // class java/util/List
      10: astore_1
      11: iconst_0
      12: istore_2
      13: aload_0
      14: getfield      #22                 // Field values:Ljava/util/List;
      17: checkcast     #34                 // class java/util/Collection
      20: invokeinterface #38,  1           // InterfaceMethod java/util/Collection.size:()I
      25: istore_3
      26: iload_2
      27: iload_3
      28: if_icmpge     182
      31: iconst_0
      32: istore        4
      34: aload_0
      35: getfield      #22                 // Field values:Ljava/util/List;
      38: checkcast     #34                 // class java/util/Collection
      41: invokeinterface #38,  1           // InterfaceMethod java/util/Collection.size:()I
      46: istore        5
      48: iload         4
      50: iload         5
      52: if_icmpge     176
      55: iload_2
      56: invokestatic  #44                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      59: iload         4
      61: invokestatic  #44                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      64: invokevirtual #48                 // Method java/lang/Object.equals:(Ljava/lang/Object;)Z
      67: ifne          170
      70: aload_0
      71: getfield      #22                 // Field values:Ljava/util/List;
      74: iload_2
      75: invokeinterface #52,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      80: checkcast     #54                 // class java/lang/Number
      83: invokevirtual #57                 // Method java/lang/Number.intValue:()I
      86: aload_0
      87: getfield      #22                 // Field values:Ljava/util/List;
      90: iload         4
      92: invokeinterface #52,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      97: checkcast     #54                 // class java/lang/Number
     100: invokevirtual #57                 // Method java/lang/Number.intValue:()I
     103: invokestatic  #63                 // Method java/lang/Math.max:(II)I
     106: istore        6
     108: aload_0
     109: getfield      #22                 // Field values:Ljava/util/List;
     112: iload_2
     113: invokeinterface #52,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     118: checkcast     #54                 // class java/lang/Number
     121: invokevirtual #57                 // Method java/lang/Number.intValue:()I
     124: aload_0
     125: getfield      #22                 // Field values:Ljava/util/List;
     128: iload         4
     130: invokeinterface #52,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     135: checkcast     #54                 // class java/lang/Number
     138: invokevirtual #57                 // Method java/lang/Number.intValue:()I
     141: invokestatic  #66                 // Method java/lang/Math.min:(II)I
     144: istore        7
     146: aload_1
     147: new           #68                 // class kotlin/Pair
     150: dup
     151: iload         6
     153: invokestatic  #44                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     156: iload         7
     158: invokestatic  #44                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     161: invokespecial #71                 // Method kotlin/Pair."<init>":(Ljava/lang/Object;Ljava/lang/Object;)V
     164: invokeinterface #74,  2           // InterfaceMethod java/util/List.add:(Ljava/lang/Object;)Z
     169: pop
     170: iinc          4, 1
     173: goto          48
     176: iinc          2, 1
     179: goto          26
     182: aload_1
     183: checkcast     #76                 // class java/lang/Iterable
     186: astore_2
     187: nop
     188: iconst_0
     189: istore_3
     190: aload_2
     191: astore        4
     193: new           #29                 // class java/util/ArrayList
     196: dup
     197: invokespecial #30                 // Method java/util/ArrayList."<init>":()V
     200: checkcast     #34                 // class java/util/Collection
     203: astore        5
     205: iconst_0
     206: istore        6
     208: aload         4
     210: invokeinterface #80,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     215: astore        7
     217: aload         7
     219: invokeinterface #86,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     224: ifeq          296
     227: aload         7
     229: invokeinterface #90,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     234: astore        8
     236: aload         8
     238: checkcast     #68                 // class kotlin/Pair
     241: astore        9
     243: iconst_0
     244: istore        10
     246: aload         9
     248: invokevirtual #93                 // Method kotlin/Pair.getFirst:()Ljava/lang/Object;
     251: checkcast     #54                 // class java/lang/Number
     254: invokevirtual #57                 // Method java/lang/Number.intValue:()I
     257: aload         9
     259: invokevirtual #96                 // Method kotlin/Pair.getSecond:()Ljava/lang/Object;
     262: checkcast     #54                 // class java/lang/Number
     265: invokevirtual #57                 // Method java/lang/Number.intValue:()I
     268: iadd
     269: sipush        2020
     272: if_icmpne     279
     275: iconst_1
     276: goto          280
     279: iconst_0
     280: ifeq          217
     283: aload         5
     285: aload         8
     287: invokeinterface #97,  2           // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     292: pop
     293: goto          217
     296: aload         5
     298: checkcast     #32                 // class java/util/List
     301: nop
     302: checkcast     #76                 // class java/lang/Iterable
     305: invokestatic  #103                // Method kotlin/collections/CollectionsKt.distinct:(Ljava/lang/Iterable;)Ljava/util/List;
     308: iconst_0
     309: invokeinterface #52,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     314: checkcast     #68                 // class kotlin/Pair
     317: areturn
}

Compiled from "day1.kt"
public final class Day1Kt {
  public static final void main();
    Code:
       0: ldc           #8                  // class Day1
       2: ldc           #10                 // String day1.txt
       4: invokevirtual #16                 // Method java/lang/Class.getResource:(Ljava/lang/String;)Ljava/net/URL;
       7: dup
       8: ldc           #18                 // String getResource(...)
      10: invokestatic  #24                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
      13: astore_1
      14: getstatic     #30                 // Field kotlin/text/Charsets.UTF_8:Ljava/nio/charset/Charset;
      17: astore_2
      18: aload_1
      19: invokestatic  #36                 // Method kotlin/io/TextStreamsKt.readBytes:(Ljava/net/URL;)[B
      22: astore_3
      23: new           #38                 // class java/lang/String
      26: dup
      27: aload_3
      28: aload_2
      29: invokespecial #42                 // Method java/lang/String."<init>":([BLjava/nio/charset/Charset;)V
      32: checkcast     #44                 // class java/lang/CharSequence
      35: iconst_1
      36: newarray       char
      38: astore_1
      39: aload_1
      40: iconst_0
      41: bipush        10
      43: castore
      44: aload_1
      45: iconst_0
      46: iconst_0
      47: bipush        6
      49: aconst_null
      50: invokestatic  #50                 // Method kotlin/text/StringsKt.split$default:(Ljava/lang/CharSequence;[CZIILjava/lang/Object;)Ljava/util/List;
      53: invokeinterface #56,  1           // InterfaceMethod java/util/List.stream:()Ljava/util/stream/Stream;
      58: invokedynamic #75,  0             // InvokeDynamic #0:invoke:()Lkotlin/jvm/functions/Function1;
      63: invokedynamic #86,  0             // InvokeDynamic #1:apply:(Lkotlin/jvm/functions/Function1;)Ljava/util/function/Function;
      68: invokeinterface #92,  2           // InterfaceMethod java/util/stream/Stream.map:(Ljava/util/function/Function;)Ljava/util/stream/Stream;
      73: invokestatic  #98                 // Method java/util/stream/Collectors.toList:()Ljava/util/stream/Collector;
      76: invokeinterface #102,  2          // InterfaceMethod java/util/stream/Stream.collect:(Ljava/util/stream/Collector;)Ljava/lang/Object;
      81: checkcast     #52                 // class java/util/List
      84: astore_0
      85: new           #8                  // class Day1
      88: dup
      89: aload_0
      90: invokestatic  #106                // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
      93: aload_0
      94: invokespecial #109                // Method Day1."<init>":(Ljava/util/List;)V
      97: invokevirtual #113                // Method Day1.solve:()Lkotlin/Pair;
     100: astore_1
     101: new           #115                // class java/lang/StringBuilder
     104: dup
     105: invokespecial #117                // Method java/lang/StringBuilder."<init>":()V
     108: ldc           #119                // String solution =
     110: invokevirtual #123                // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     113: aload_1
     114: invokevirtual #126                // Method java/lang/StringBuilder.append:(Ljava/lang/Object;)Ljava/lang/StringBuilder;
     117: ldc           #128                // String , x*y =
     119: invokevirtual #123                // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     122: aload_1
     123: invokevirtual #134                // Method kotlin/Pair.getFirst:()Ljava/lang/Object;
     126: checkcast     #136                // class java/lang/Number
     129: invokevirtual #140                // Method java/lang/Number.intValue:()I
     132: aload_1
     133: invokevirtual #143                // Method kotlin/Pair.getSecond:()Ljava/lang/Object;
     136: checkcast     #136                // class java/lang/Number
     139: invokevirtual #140                // Method java/lang/Number.intValue:()I
     142: imul
     143: invokevirtual #146                // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
     146: invokevirtual #150                // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
     149: getstatic     #156                // Field java/lang/System.out:Ljava/io/PrintStream;
     152: swap
     153: invokevirtual #161                // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
     156: new           #163                // class Day1Part2
     159: dup
     160: aload_0
     161: invokespecial #164                // Method Day1Part2."<init>":(Ljava/util/List;)V
     164: invokevirtual #167                // Method Day1Part2.solve:()Lkotlin/Triple;
     167: astore_2
     168: new           #115                // class java/lang/StringBuilder
     171: dup
     172: invokespecial #117                // Method java/lang/StringBuilder."<init>":()V
     175: ldc           #119                // String solution =
     177: invokevirtual #123                // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     180: aload_2
     181: invokevirtual #126                // Method java/lang/StringBuilder.append:(Ljava/lang/Object;)Ljava/lang/StringBuilder;
     184: ldc           #169                // String , x*y*z =
     186: invokevirtual #123                // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     189: aload_2
     190: invokevirtual #172                // Method kotlin/Triple.getFirst:()Ljava/lang/Object;
     193: checkcast     #136                // class java/lang/Number
     196: invokevirtual #140                // Method java/lang/Number.intValue:()I
     199: aload_2
     200: invokevirtual #173                // Method kotlin/Triple.getSecond:()Ljava/lang/Object;
     203: checkcast     #136                // class java/lang/Number
     206: invokevirtual #140                // Method java/lang/Number.intValue:()I
     209: imul
     210: aload_2
     211: invokevirtual #176                // Method kotlin/Triple.getThird:()Ljava/lang/Object;
     214: checkcast     #136                // class java/lang/Number
     217: invokevirtual #140                // Method java/lang/Number.intValue:()I
     220: imul
     221: invokevirtual #146                // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
     224: invokevirtual #150                // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
     227: getstatic     #156                // Field java/lang/System.out:Ljava/io/PrintStream;
     230: swap
     231: invokevirtual #161                // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
     234: return

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

  private static final java.lang.Integer main$lambda$0(java.lang.String);
    Code:
       0: aload_0
       1: invokestatic  #106                // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
       4: aload_0
       5: invokestatic  #193                // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
       8: invokestatic  #197                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      11: areturn

  private static final java.lang.Integer main$lambda$1(kotlin.jvm.functions.Function1, java.lang.Object);
    Code:
       0: aload_0
       1: aload_1
       2: invokeinterface #203,  2          // InterfaceMethod kotlin/jvm/functions/Function1.invoke:(Ljava/lang/Object;)Ljava/lang/Object;
       7: checkcast     #189                // class java/lang/Integer
      10: areturn
}