/**
 * Given a set of candidate numbers (C) (without duplicates) and a target number (T),
 * find all unique combinations in C where the candidate numbers sums to T.
 *
 * The same repeated number may be chosen from C unlimited number of times.
 *
 * Note:
 * All numbers (including target) will be positive integers.
 * The solution set must not contain duplicate combinations.
 * For example, given candidate set [2, 3, 6, 7] and target 7,
 * A solution set is:
 * [
 * [7],
 * [2, 2, 3]
 * ]
 *
 * Accepted.
 */
class CombinationSum {

    fun combinationSum(candidates: IntArray, target: Int): List<List<Int>> {
        if (candidates.isEmpty()) {
            return emptyList()
        }
        val lists = mutableListOf<List<Int>>()

        candidates.sort()

        dfs(candidates, target, mutableListOf(), lists, 0)

        return lists
    }

    private fun dfs(candidates: IntArray, target: Int, path: MutableList<Int>, ret: MutableList<List<Int>>, index: Int) {
        if (target < 0) {
            return
        }

        if (target == 0) {
            ret.add(ArrayList(path))
            return
        }

        for (i in index until candidates.size) {
            path.add(candidates[i])
            dfs(candidates, target - candidates[i], path, ret, i)
            path.removeAt(path.size - 1)
        }
    }

}

Compiled from "CombinationSum.kt"
public final class CombinationSum {
  public CombinationSum();
    Code:
       0: aload_0
       1: invokespecial #8                  // Method java/lang/Object."<init>":()V
       4: return

  public final java.util.List<java.util.List<java.lang.Integer>> combinationSum(int[], int);
    Code:
       0: aload_1
       1: ldc           #16                 // String candidates
       3: invokestatic  #22                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_1
       7: arraylength
       8: ifne          15
      11: iconst_1
      12: goto          16
      15: iconst_0
      16: ifeq          23
      19: invokestatic  #28                 // Method kotlin/collections/CollectionsKt.emptyList:()Ljava/util/List;
      22: areturn
      23: new           #30                 // class java/util/ArrayList
      26: dup
      27: invokespecial #31                 // Method java/util/ArrayList."<init>":()V
      30: checkcast     #33                 // class java/util/List
      33: astore_3
      34: aload_1
      35: invokestatic  #39                 // Method kotlin/collections/ArraysKt.sort:([I)V
      38: aload_0
      39: aload_1
      40: iload_2
      41: new           #30                 // class java/util/ArrayList
      44: dup
      45: invokespecial #31                 // Method java/util/ArrayList."<init>":()V
      48: checkcast     #33                 // class java/util/List
      51: aload_3
      52: iconst_0
      53: invokespecial #43                 // Method dfs:([IILjava/util/List;Ljava/util/List;I)V
      56: aload_3
      57: areturn

  private final void dfs(int[], int, java.util.List<java.lang.Integer>, java.util.List<java.util.List<java.lang.Integer>>, int);
    Code:
       0: iload_2
       1: ifge          5
       4: return
       5: iload_2
       6: ifne          29
       9: aload         4
      11: new           #30                 // class java/util/ArrayList
      14: dup
      15: aload_3
      16: checkcast     #51                 // class java/util/Collection
      19: invokespecial #54                 // Method java/util/ArrayList."<init>":(Ljava/util/Collection;)V
      22: invokeinterface #58,  2           // InterfaceMethod java/util/List.add:(Ljava/lang/Object;)Z
      27: pop
      28: return
      29: iload         5
      31: istore        6
      33: aload_1
      34: arraylength
      35: istore        7
      37: iload         6
      39: iload         7
      41: if_icmpge     95
      44: aload_3
      45: aload_1
      46: iload         6
      48: iaload
      49: invokestatic  #64                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      52: invokeinterface #58,  2           // InterfaceMethod java/util/List.add:(Ljava/lang/Object;)Z
      57: pop
      58: aload_0
      59: aload_1
      60: iload_2
      61: aload_1
      62: iload         6
      64: iaload
      65: isub
      66: aload_3
      67: aload         4
      69: iload         6
      71: invokespecial #43                 // Method dfs:([IILjava/util/List;Ljava/util/List;I)V
      74: aload_3
      75: aload_3
      76: invokeinterface #68,  1           // InterfaceMethod java/util/List.size:()I
      81: iconst_1
      82: isub
      83: invokeinterface #72,  2           // InterfaceMethod java/util/List.remove:(I)Ljava/lang/Object;
      88: pop
      89: iinc          6, 1
      92: goto          37
      95: return
}


/**
 * Given a collection of numbers that might contain duplicates, return all possible unique permutations.
 *
 * For example,
 * [1,1,2] have the following unique permutations:
 * [
 * [1,1,2],
 * [1,2,1],
 * [2,1,1]
 * ]
 *
 * Accepted.
 */
class PermutationsII {

    fun permuteUnique(nums: IntArray): List<List<Int>> {
        val results = mutableListOf<List<Int>>()

        if (nums.isEmpty()) {
            return results
        }
        if (nums.size == 1) {
            return results.apply {
                add(mutableListOf(nums[0]))
            }
        }

        val ints = IntArray(nums.size - 1)
        System.arraycopy(nums, 0, ints, 0, nums.size - 1)

        val set = mutableSetOf<List<Int>>()

        for (list in permuteUnique(ints)) {
            for (i in 0..list.size) {
                val tmp = mutableListOf<Int>()
                tmp.addAll(list)
                tmp.add(i, nums[nums.size - 1])
                set.add(tmp)
            }
        }

        return results.apply { addAll(set) }
    }

}

Compiled from "PermutationsII.kt"
public final class PermutationsII {
  public PermutationsII();
    Code:
       0: aload_0
       1: invokespecial #8                  // Method java/lang/Object."<init>":()V
       4: return

  public final java.util.List<java.util.List<java.lang.Integer>> permuteUnique(int[]);
    Code:
       0: aload_1
       1: ldc           #16                 // String nums
       3: invokestatic  #22                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: new           #24                 // class java/util/ArrayList
       9: dup
      10: invokespecial #25                 // Method java/util/ArrayList."<init>":()V
      13: checkcast     #27                 // class java/util/List
      16: astore_2
      17: aload_1
      18: arraylength
      19: ifne          26
      22: iconst_1
      23: goto          27
      26: iconst_0
      27: ifeq          32
      30: aload_2
      31: areturn
      32: aload_1
      33: arraylength
      34: iconst_1
      35: if_icmpne     78
      38: aload_2
      39: astore_3
      40: aload_3
      41: astore        4
      43: iconst_0
      44: istore        5
      46: aload         4
      48: iconst_1
      49: anewarray     #29                 // class java/lang/Integer
      52: astore        6
      54: aload         6
      56: iconst_0
      57: aload_1
      58: iconst_0
      59: iaload
      60: invokestatic  #33                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      63: aastore
      64: aload         6
      66: invokestatic  #39                 // Method kotlin/collections/CollectionsKt.mutableListOf:([Ljava/lang/Object;)Ljava/util/List;
      69: invokeinterface #43,  2           // InterfaceMethod java/util/List.add:(Ljava/lang/Object;)Z
      74: pop
      75: nop
      76: aload_3
      77: areturn
      78: aload_1
      79: arraylength
      80: iconst_1
      81: isub
      82: newarray       int
      84: astore_3
      85: aload_1
      86: iconst_0
      87: aload_3
      88: iconst_0
      89: aload_1
      90: arraylength
      91: iconst_1
      92: isub
      93: invokestatic  #49                 // Method java/lang/System.arraycopy:(Ljava/lang/Object;ILjava/lang/Object;II)V
      96: new           #51                 // class java/util/LinkedHashSet
      99: dup
     100: invokespecial #52                 // Method java/util/LinkedHashSet."<init>":()V
     103: checkcast     #54                 // class java/util/Set
     106: astore        4
     108: aload_0
     109: aload_3
     110: invokevirtual #56                 // Method permuteUnique:([I)Ljava/util/List;
     113: invokeinterface #60,  1           // InterfaceMethod java/util/List.iterator:()Ljava/util/Iterator;
     118: astore        5
     120: aload         5
     122: invokeinterface #66,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     127: ifeq          227
     130: aload         5
     132: invokeinterface #70,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     137: checkcast     #27                 // class java/util/List
     140: astore        6
     142: iconst_0
     143: istore        7
     145: aload         6
     147: invokeinterface #74,  1           // InterfaceMethod java/util/List.size:()I
     152: istore        8
     154: iload         7
     156: iload         8
     158: if_icmpgt     120
     161: new           #24                 // class java/util/ArrayList
     164: dup
     165: invokespecial #25                 // Method java/util/ArrayList."<init>":()V
     168: checkcast     #27                 // class java/util/List
     171: astore        9
     173: aload         9
     175: aload         6
     177: checkcast     #76                 // class java/util/Collection
     180: invokeinterface #80,  2           // InterfaceMethod java/util/List.addAll:(Ljava/util/Collection;)Z
     185: pop
     186: aload         9
     188: iload         7
     190: aload_1
     191: aload_1
     192: arraylength
     193: iconst_1
     194: isub
     195: iaload
     196: invokestatic  #33                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     199: invokeinterface #83,  3           // InterfaceMethod java/util/List.add:(ILjava/lang/Object;)V
     204: aload         4
     206: aload         9
     208: invokeinterface #84,  2           // InterfaceMethod java/util/Set.add:(Ljava/lang/Object;)Z
     213: pop
     214: iload         7
     216: iload         8
     218: if_icmpeq     120
     221: iinc          7, 1
     224: goto          161
     227: aload_2
     228: astore        5
     230: aload         5
     232: astore        6
     234: iconst_0
     235: istore        7
     237: aload         6
     239: aload         4
     241: checkcast     #76                 // class java/util/Collection
     244: invokeinterface #80,  2           // InterfaceMethod java/util/List.addAll:(Ljava/util/Collection;)Z
     249: pop
     250: aload         5
     252: areturn
}


/**
 * Given a string s, find the longest palindromic substring in s.
 * You may assume that the maximum length of s is 1000.
 *
 * Example:
 *
 * Input: "babad"
 *
 * Output: "bab"
 *
 * Note: "aba" is also a valid answer.
 * Example:
 *
 * Input: "cbbd"
 *
 * Output: "bb"
 *
 * Accepted.
 */
class LongestPalindromicSubstring {

    fun longestPalindrome(s: String?): String? {
        if (s == null || s.length <= 1) {
            return s
        }

        var maxLength = 0
        var startIndex = 0
        for (index in 0 until s.length) {
            var leftIndex = index
            var rightIndex = index
            while (leftIndex >= 0 && rightIndex < s.length && s[leftIndex] == s[rightIndex]) {
                val current = rightIndex - leftIndex + 1
                if (current > maxLength) {
                    maxLength = current
                    startIndex = leftIndex
                }
                leftIndex--
                rightIndex++
            }

            leftIndex = index
            rightIndex = index + 1
            while (leftIndex >= 0 && rightIndex < s.length && s[leftIndex] == s[rightIndex]) {
                val current = rightIndex - leftIndex + 1
                if (current > maxLength) {
                    maxLength = current
                    startIndex = leftIndex
                }
                leftIndex--
                rightIndex++
            }
        }

        return s.substring(startIndex, maxLength + startIndex)
    }

}

Compiled from "LongestPalindromicSubstring.kt"
public final class LongestPalindromicSubstring {
  public LongestPalindromicSubstring();
    Code:
       0: aload_0
       1: invokespecial #8                  // Method java/lang/Object."<init>":()V
       4: return

  public final java.lang.String longestPalindrome(java.lang.String);
    Code:
       0: aload_1
       1: ifnull        12
       4: aload_1
       5: invokevirtual #19                 // Method java/lang/String.length:()I
       8: iconst_1
       9: if_icmpgt     14
      12: aload_1
      13: areturn
      14: iconst_0
      15: istore_2
      16: iconst_0
      17: istore_3
      18: iconst_0
      19: istore        4
      21: aload_1
      22: invokevirtual #19                 // Method java/lang/String.length:()I
      25: istore        5
      27: iload         4
      29: iload         5
      31: if_icmpge     176
      34: iload         4
      36: istore        6
      38: iload         4
      40: istore        7
      42: iload         6
      44: iflt          101
      47: iload         7
      49: aload_1
      50: invokevirtual #19                 // Method java/lang/String.length:()I
      53: if_icmpge     101
      56: aload_1
      57: iload         6
      59: invokevirtual #23                 // Method java/lang/String.charAt:(I)C
      62: aload_1
      63: iload         7
      65: invokevirtual #23                 // Method java/lang/String.charAt:(I)C
      68: if_icmpne     101
      71: iload         7
      73: iload         6
      75: isub
      76: iconst_1
      77: iadd
      78: istore        8
      80: iload         8
      82: iload_2
      83: if_icmple     92
      86: iload         8
      88: istore_2
      89: iload         6
      91: istore_3
      92: iinc          6, -1
      95: iinc          7, 1
      98: goto          42
     101: iload         4
     103: istore        6
     105: iload         4
     107: iconst_1
     108: iadd
     109: istore        7
     111: iload         6
     113: iflt          170
     116: iload         7
     118: aload_1
     119: invokevirtual #19                 // Method java/lang/String.length:()I
     122: if_icmpge     170
     125: aload_1
     126: iload         6
     128: invokevirtual #23                 // Method java/lang/String.charAt:(I)C
     131: aload_1
     132: iload         7
     134: invokevirtual #23                 // Method java/lang/String.charAt:(I)C
     137: if_icmpne     170
     140: iload         7
     142: iload         6
     144: isub
     145: iconst_1
     146: iadd
     147: istore        8
     149: iload         8
     151: iload_2
     152: if_icmple     161
     155: iload         8
     157: istore_2
     158: iload         6
     160: istore_3
     161: iinc          6, -1
     164: iinc          7, 1
     167: goto          111
     170: iinc          4, 1
     173: goto          27
     176: aload_1
     177: iload_3
     178: iload_2
     179: iload_3
     180: iadd
     181: invokevirtual #27                 // Method java/lang/String.substring:(II)Ljava/lang/String;
     184: dup
     185: ldc           #29                 // String substring(...)
     187: invokestatic  #35                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
     190: areturn
}


/**
 * Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:
 *
 * Integers in each row are sorted from left to right.
 * The first integer of each row is greater than the last integer of the previous row.
 * For example,
 *
 * Consider the following matrix:
 *
 * [
 * [1,   3,  5,  7],
 * [10, 11, 16, 20],
 * [23, 30, 34, 50]
 * ]
 * Given target = 3, return true.
 *
 * Accepted.
 */
class SearchA2DMatrix {

    fun searchMatrix(matrix: Array<IntArray>, target: Int): Boolean {
        if (matrix.isEmpty() || matrix[0].isEmpty()) {
            return false
        }

        for (i in 0 until matrix.size - 1) {
            if (matrix[i][0] == target || matrix[i + 1][0] == target) {
                return true
            } else if (matrix[i][0] < target && matrix[i + 1][0] > target) {
                return matrix[i].binarySearch(target) >= 0
            }
        }

        return matrix[matrix.size - 1].binarySearch(target) >= 0
    }

}

Compiled from "SearchA2DMatrix.kt"
public final class SearchA2DMatrix {
  public SearchA2DMatrix();
    Code:
       0: aload_0
       1: invokespecial #8                  // Method java/lang/Object."<init>":()V
       4: return

  public final boolean searchMatrix(int[][], int);
    Code:
       0: aload_1
       1: ldc           #15                 // String matrix
       3: invokestatic  #21                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_1
       7: checkcast     #23                 // class "[Ljava/lang/Object;"
      10: arraylength
      11: ifne          18
      14: iconst_1
      15: goto          19
      18: iconst_0
      19: ifne          37
      22: aload_1
      23: iconst_0
      24: aaload
      25: arraylength
      26: ifne          33
      29: iconst_1
      30: goto          34
      33: iconst_0
      34: ifeq          39
      37: iconst_0
      38: ireturn
      39: iconst_0
      40: istore_3
      41: aload_1
      42: checkcast     #23                 // class "[Ljava/lang/Object;"
      45: arraylength
      46: iconst_1
      47: isub
      48: istore        4
      50: iload_3
      51: iload         4
      53: if_icmpge     125
      56: aload_1
      57: iload_3
      58: aaload
      59: iconst_0
      60: iaload
      61: iload_2
      62: if_icmpeq     76
      65: aload_1
      66: iload_3
      67: iconst_1
      68: iadd
      69: aaload
      70: iconst_0
      71: iaload
      72: iload_2
      73: if_icmpne     78
      76: iconst_1
      77: ireturn
      78: aload_1
      79: iload_3
      80: aaload
      81: iconst_0
      82: iaload
      83: iload_2
      84: if_icmpge     119
      87: aload_1
      88: iload_3
      89: iconst_1
      90: iadd
      91: aaload
      92: iconst_0
      93: iaload
      94: iload_2
      95: if_icmple     119
      98: aload_1
      99: iload_3
     100: aaload
     101: iload_2
     102: iconst_0
     103: iconst_0
     104: bipush        6
     106: aconst_null
     107: invokestatic  #29                 // Method kotlin/collections/ArraysKt.binarySearch$default:([IIIIILjava/lang/Object;)I
     110: iflt          117
     113: iconst_1
     114: goto          118
     117: iconst_0
     118: ireturn
     119: iinc          3, 1
     122: goto          50
     125: aload_1
     126: aload_1
     127: checkcast     #23                 // class "[Ljava/lang/Object;"
     130: arraylength
     131: iconst_1
     132: isub
     133: aaload
     134: iload_2
     135: iconst_0
     136: iconst_0
     137: bipush        6
     139: aconst_null
     140: invokestatic  #29                 // Method kotlin/collections/ArraysKt.binarySearch$default:([IIIIILjava/lang/Object;)I
     143: iflt          150
     146: iconst_1
     147: goto          151
     150: iconst_0
     151: ireturn
}


// GATHERING A LIST OF VALUES
fun getListOfNumbers() : List<Int> {
  var myList = mutableListOf <Int> ()

  for(i in 1..7){
    println("Enter a number:")
    var Number = Integer.valueOf(readLine())
    myList.add(Number)
  }

 return myList
}

// FINDING THE MAX VALUE
fun findMax(maxList : List<Int>) : Int {
   var largestNumber = maxList[0]

   // The loop runs for every element in the list and compares it with the largest number. If the conditional is true then, the largest number is updated.

   for(j in maxList){
     if(j > largestNumber){
       largestNumber = j
     }
   }

  return largestNumber
}

 // FINDING THE MIN VALUE
fun findMin(minList : List<Int>) : Int {
   var smallestNumber = minList[0]

   // The loop runs for every element in the list and compares it with the smallest number. If the conditional is true then, the smallest number is updated.

   for(k in minList){
     if(k < smallestNumber){
       smallestNumber = k
     }
   }

  return smallestNumber
}

 // FINDING THE AVERAGE
fun findAverage(listAvg : List<Int>) : Int {
   var sum = 0

   for(l in listAvg){
     sum += l
   }

  return sum / listAvg.size
}

 // FINDING IF A LIST CONTAINS A VALUE
fun checkIfListContains(checkList : List<Int>, numToCheck : Int) : Boolean {

   for(m in checkList){
     if(m == numToCheck){
       return true
     }
   }

  return false
}

// MAIN FUNCTION
fun main() {

  // OUTPUT OF THE LIST
  var values = getListOfNumbers()
  println(values)

  // OUTPUT FOR THE MAX VALUE
  var largestValue = findMax(values)
  println("The largest number is $largestValue")

  // OUTPUT FOR THE MIN VALUE
  var smallestValue = findMin(values)
  println("The smallest number is $smallestValue")

  // OUTPUT OF THE AVERAGE
  var average = findAverage(values)
  println("The average is $average")

  // TO CHECK IF A VALUE CONTAINS
  println("Enter the number you want to check:")
  var numToFind = Integer.valueOf(readLine())

  var  containsValue = checkIfListContains(values, numToFind)

  if(containsValue == true){
    println("Number Found !!!!")
  } else {
    println("Number Not Found ???? WTF !!!")
  }

}

// ADDITIONAL CHALLENGES
// 1. Create a function that returns the difference between the largest and smallest number in the list.
// ANS: For the first prompt, use the logic from findMin() and findMax() to find the smallest and largest numbers. Then, return the difference between the two.
/*
  // FUNC THAT RETURNS THE DIFF BW MIN AND MAX

 fun minMaxDiff(minMaxDiffList : List<Int>) : Int{

    //LARGEST
    var largest = findMax(minMaxDiffList)

    //SMALLEST
    var smallest = findMin(minMaxDiffList)

    // DIFF
    var difference = largest - smallest

  return difference
 }

 fun main() {

 // LARGEST - SMALLEST
  var diff = minMaxDiff(values)
  println("The minMaxDiff is $diff")
 }

 */

// 2. Create a function that takes in a list of numbers and returns a list with all the values squared.
// ANS: For the second prompt, create an empty list. Then, loop through each element of the list argument; inside the loop, add the value of the element multiplied by itself.
/*
 //SQUARE

 fun squareNum(sqList : List<Int>) : List<Int> {
  for(z in sqList){
   // sqList[z] = z * z
    var num = z * z
    // sqList[0]
    // sqList.remove()
    sqList.add(num)
  }

  return sqList
}
EROORS WORK IT OUT
*/

Compiled from "listFunctions.kt"
public final class ListFunctionsKt {
  public static final java.util.List<java.lang.Integer> getListOfNumbers();
    Code:
       0: new           #10                 // class java/util/ArrayList
       3: dup
       4: invokespecial #14                 // Method java/util/ArrayList."<init>":()V
       7: checkcast     #16                 // class java/util/List
      10: astore_0
      11: iconst_1
      12: istore_1
      13: iload_1
      14: bipush        8
      16: if_icmpge     55
      19: ldc           #18                 // String Enter a number:
      21: getstatic     #24                 // Field java/lang/System.out:Ljava/io/PrintStream;
      24: swap
      25: invokevirtual #30                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
      28: invokestatic  #36                 // Method kotlin/io/ConsoleKt.readLine:()Ljava/lang/String;
      31: invokestatic  #42                 // Method java/lang/Integer.valueOf:(Ljava/lang/String;)Ljava/lang/Integer;
      34: astore_2
      35: aload_0
      36: aload_2
      37: astore_3
      38: aload_3
      39: invokestatic  #47                 // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
      42: aload_3
      43: invokeinterface #51,  2           // InterfaceMethod java/util/List.add:(Ljava/lang/Object;)Z
      48: pop
      49: iinc          1, 1
      52: goto          13
      55: aload_0
      56: areturn

  public static final int findMax(java.util.List<java.lang.Integer>);
    Code:
       0: aload_0
       1: ldc           #62                 // String maxList
       3: invokestatic  #66                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: iconst_0
       8: invokeinterface #70,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      13: checkcast     #72                 // class java/lang/Number
      16: invokevirtual #76                 // Method java/lang/Number.intValue:()I
      19: istore_1
      20: aload_0
      21: invokeinterface #80,  1           // InterfaceMethod java/util/List.iterator:()Ljava/util/Iterator;
      26: astore_2
      27: aload_2
      28: invokeinterface #86,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      33: ifeq          59
      36: aload_2
      37: invokeinterface #90,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      42: checkcast     #72                 // class java/lang/Number
      45: invokevirtual #76                 // Method java/lang/Number.intValue:()I
      48: istore_3
      49: iload_3
      50: iload_1
      51: if_icmple     27
      54: iload_3
      55: istore_1
      56: goto          27
      59: iload_1
      60: ireturn

  public static final int findMin(java.util.List<java.lang.Integer>);
    Code:
       0: aload_0
       1: ldc           #95                 // String minList
       3: invokestatic  #66                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: iconst_0
       8: invokeinterface #70,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      13: checkcast     #72                 // class java/lang/Number
      16: invokevirtual #76                 // Method java/lang/Number.intValue:()I
      19: istore_1
      20: aload_0
      21: invokeinterface #80,  1           // InterfaceMethod java/util/List.iterator:()Ljava/util/Iterator;
      26: astore_2
      27: aload_2
      28: invokeinterface #86,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      33: ifeq          59
      36: aload_2
      37: invokeinterface #90,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      42: checkcast     #72                 // class java/lang/Number
      45: invokevirtual #76                 // Method java/lang/Number.intValue:()I
      48: istore_3
      49: iload_3
      50: iload_1
      51: if_icmpge     27
      54: iload_3
      55: istore_1
      56: goto          27
      59: iload_1
      60: ireturn

  public static final int findAverage(java.util.List<java.lang.Integer>);
    Code:
       0: aload_0
       1: ldc           #100                // String listAvg
       3: invokestatic  #66                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: iconst_0
       7: istore_1
       8: aload_0
       9: invokeinterface #80,  1           // InterfaceMethod java/util/List.iterator:()Ljava/util/Iterator;
      14: astore_2
      15: aload_2
      16: invokeinterface #86,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      21: ifeq          44
      24: aload_2
      25: invokeinterface #90,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      30: checkcast     #72                 // class java/lang/Number
      33: invokevirtual #76                 // Method java/lang/Number.intValue:()I
      36: istore_3
      37: iload_1
      38: iload_3
      39: iadd
      40: istore_1
      41: goto          15
      44: iload_1
      45: aload_0
      46: invokeinterface #103,  1          // InterfaceMethod java/util/List.size:()I
      51: idiv
      52: ireturn

  public static final boolean checkIfListContains(java.util.List<java.lang.Integer>, int);
    Code:
       0: aload_0
       1: ldc           #110                // String checkList
       3: invokestatic  #66                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: invokeinterface #80,  1           // InterfaceMethod java/util/List.iterator:()Ljava/util/Iterator;
      12: astore_2
      13: aload_2
      14: invokeinterface #86,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      19: ifeq          42
      22: aload_2
      23: invokeinterface #90,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      28: checkcast     #72                 // class java/lang/Number
      31: invokevirtual #76                 // Method java/lang/Number.intValue:()I
      34: istore_3
      35: iload_3
      36: iload_1
      37: if_icmpne     13
      40: iconst_1
      41: ireturn
      42: iconst_0
      43: ireturn

  public static final void main();
    Code:
       0: invokestatic  #115                // Method getListOfNumbers:()Ljava/util/List;
       3: astore_0
       4: getstatic     #24                 // Field java/lang/System.out:Ljava/io/PrintStream;
       7: aload_0
       8: invokevirtual #30                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
      11: aload_0
      12: invokestatic  #117                // Method findMax:(Ljava/util/List;)I
      15: istore_1
      16: new           #119                // class java/lang/StringBuilder
      19: dup
      20: invokespecial #120                // Method java/lang/StringBuilder."<init>":()V
      23: ldc           #122                // String The largest number is
      25: invokevirtual #126                // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      28: iload_1
      29: invokevirtual #129                // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
      32: invokevirtual #132                // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
      35: getstatic     #24                 // Field java/lang/System.out:Ljava/io/PrintStream;
      38: swap
      39: invokevirtual #30                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
      42: aload_0
      43: invokestatic  #134                // Method findMin:(Ljava/util/List;)I
      46: istore_2
      47: new           #119                // class java/lang/StringBuilder
      50: dup
      51: invokespecial #120                // Method java/lang/StringBuilder."<init>":()V
      54: ldc           #136                // String The smallest number is
      56: invokevirtual #126                // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      59: iload_2
      60: invokevirtual #129                // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
      63: invokevirtual #132                // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
      66: getstatic     #24                 // Field java/lang/System.out:Ljava/io/PrintStream;
      69: swap
      70: invokevirtual #30                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
      73: aload_0
      74: invokestatic  #138                // Method findAverage:(Ljava/util/List;)I
      77: istore_3
      78: new           #119                // class java/lang/StringBuilder
      81: dup
      82: invokespecial #120                // Method java/lang/StringBuilder."<init>":()V
      85: ldc           #140                // String The average is
      87: invokevirtual #126                // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      90: iload_3
      91: invokevirtual #129                // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
      94: invokevirtual #132                // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
      97: getstatic     #24                 // Field java/lang/System.out:Ljava/io/PrintStream;
     100: swap
     101: invokevirtual #30                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
     104: ldc           #142                // String Enter the number you want to check:
     106: getstatic     #24                 // Field java/lang/System.out:Ljava/io/PrintStream;
     109: swap
     110: invokevirtual #30                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
     113: invokestatic  #36                 // Method kotlin/io/ConsoleKt.readLine:()Ljava/lang/String;
     116: invokestatic  #42                 // Method java/lang/Integer.valueOf:(Ljava/lang/String;)Ljava/lang/Integer;
     119: astore        4
     121: aload_0
     122: aload         4
     124: astore        6
     126: aload         6
     128: invokestatic  #47                 // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
     131: aload         6
     133: checkcast     #72                 // class java/lang/Number
     136: invokevirtual #76                 // Method java/lang/Number.intValue:()I
     139: invokestatic  #144                // Method checkIfListContains:(Ljava/util/List;I)Z
     142: istore        5
     144: iload         5
     146: iconst_1
     147: if_icmpne     162
     150: ldc           #146                // String Number Found !!!!
     152: getstatic     #24                 // Field java/lang/System.out:Ljava/io/PrintStream;
     155: swap
     156: invokevirtual #30                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
     159: goto          171
     162: ldc           #148                // String Number Not Found ???? WTF !!!
     164: getstatic     #24                 // Field java/lang/System.out:Ljava/io/PrintStream;
     167: swap
     168: invokevirtual #30                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
     171: return

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


fun main() {
    val matrixA = listOf(
        listOf(1, 2, 3),
        listOf(4, 5, 6),
        listOf(7, 8, 9)
    )

    val matrixAdjoint = calculateAdjoint(matrixA)

    println("Input (A): $matrixA")
    println("Matrix of Adjoint (A*): $matrixAdjoint")
}

fun calculateAdjoint(matrix: List<List<Int>>): List<List<Int>> {
    val matrixCofactors = calculateCofactors(matrix)
    return transpose(matrixCofactors)
}

fun calculateCofactors(matrix: List<List<Int>>): List<List<Int>> {
    return matrix.mapIndexed { i, row ->
        row.indices.map { j ->
            getCofactor(matrix, i, j)
        }
    }
}

fun transpose(matrix: List<List<Int>>): List<List<Int>> {
    if (matrix.isEmpty() || matrix[0].isEmpty())
        return emptyList()

    val numRows = matrix.size
    val numCols = matrix[0].size

    return List(numCols) { col ->
        List(numRows) { row ->
            matrix[row][col]
        }
    }
}

fun getCofactor(matrix: List<List<Int>>, row: Int, col: Int): Int {
    val sign = if ((row + col) % 2 == 0) 1 else -1
    val subMatrix = getSubMatrix(matrix, row, col)
    return sign * determinant(subMatrix)
}

fun getSubMatrix(matrix: List<List<Int>>, rowToRemove: Int, colToRemove: Int): List<List<Int>> {
    return matrix
        .filterIndexed { i, _ -> i != rowToRemove }
        .map { row -> row.filterIndexed { j, _ -> j != colToRemove } }
}

fun determinant(matrix: List<List<Int>>): Int {
    if (matrix.size == 2)
        return matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0]

    return matrix.indices.sumOf { i ->
        val sign = if (i % 2 == 0) 1 else -1
        val subMatrix = getSubMatrix(matrix, 0, i)
        sign * matrix[0][i] * determinant(subMatrix)
    }
}

Compiled from "FindTheAdjointOfAMatrix.kt"
public final class FindTheAdjointOfAMatrixKt {
  public static final void main();
    Code:
       0: iconst_3
       1: anewarray     #8                  // class java/util/List
       4: astore_1
       5: aload_1
       6: iconst_0
       7: iconst_3
       8: anewarray     #10                 // class java/lang/Integer
      11: astore_2
      12: aload_2
      13: iconst_0
      14: iconst_1
      15: invokestatic  #14                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      18: aastore
      19: aload_2
      20: iconst_1
      21: iconst_2
      22: invokestatic  #14                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      25: aastore
      26: aload_2
      27: iconst_2
      28: iconst_3
      29: invokestatic  #14                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      32: aastore
      33: aload_2
      34: invokestatic  #20                 // Method kotlin/collections/CollectionsKt.listOf:([Ljava/lang/Object;)Ljava/util/List;
      37: aastore
      38: aload_1
      39: iconst_1
      40: iconst_3
      41: anewarray     #10                 // class java/lang/Integer
      44: astore_2
      45: aload_2
      46: iconst_0
      47: iconst_4
      48: invokestatic  #14                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      51: aastore
      52: aload_2
      53: iconst_1
      54: iconst_5
      55: invokestatic  #14                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      58: aastore
      59: aload_2
      60: iconst_2
      61: bipush        6
      63: invokestatic  #14                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      66: aastore
      67: aload_2
      68: invokestatic  #20                 // Method kotlin/collections/CollectionsKt.listOf:([Ljava/lang/Object;)Ljava/util/List;
      71: aastore
      72: aload_1
      73: iconst_2
      74: iconst_3
      75: anewarray     #10                 // class java/lang/Integer
      78: astore_2
      79: aload_2
      80: iconst_0
      81: bipush        7
      83: invokestatic  #14                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      86: aastore
      87: aload_2
      88: iconst_1
      89: bipush        8
      91: invokestatic  #14                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      94: aastore
      95: aload_2
      96: iconst_2
      97: bipush        9
      99: invokestatic  #14                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     102: aastore
     103: aload_2
     104: invokestatic  #20                 // Method kotlin/collections/CollectionsKt.listOf:([Ljava/lang/Object;)Ljava/util/List;
     107: aastore
     108: aload_1
     109: invokestatic  #20                 // Method kotlin/collections/CollectionsKt.listOf:([Ljava/lang/Object;)Ljava/util/List;
     112: astore_0
     113: aload_0
     114: invokestatic  #24                 // Method calculateAdjoint:(Ljava/util/List;)Ljava/util/List;
     117: astore_1
     118: new           #26                 // class java/lang/StringBuilder
     121: dup
     122: invokespecial #29                 // Method java/lang/StringBuilder."<init>":()V
     125: ldc           #31                 // String Input (A):
     127: invokevirtual #35                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     130: aload_0
     131: invokevirtual #38                 // Method java/lang/StringBuilder.append:(Ljava/lang/Object;)Ljava/lang/StringBuilder;
     134: invokevirtual #42                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
     137: getstatic     #48                 // Field java/lang/System.out:Ljava/io/PrintStream;
     140: swap
     141: invokevirtual #54                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
     144: new           #26                 // class java/lang/StringBuilder
     147: dup
     148: invokespecial #29                 // Method java/lang/StringBuilder."<init>":()V
     151: ldc           #56                 // String Matrix of Adjoint (A*):
     153: invokevirtual #35                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     156: aload_1
     157: invokevirtual #38                 // Method java/lang/StringBuilder.append:(Ljava/lang/Object;)Ljava/lang/StringBuilder;
     160: invokevirtual #42                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
     163: getstatic     #48                 // Field java/lang/System.out:Ljava/io/PrintStream;
     166: swap
     167: invokevirtual #54                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
     170: return

  public static final java.util.List<java.util.List<java.lang.Integer>> calculateAdjoint(java.util.List<? extends java.util.List<java.lang.Integer>>);
    Code:
       0: aload_0
       1: ldc           #63                 // String matrix
       3: invokestatic  #69                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: invokestatic  #72                 // Method calculateCofactors:(Ljava/util/List;)Ljava/util/List;
      10: astore_1
      11: aload_1
      12: invokestatic  #75                 // Method transpose:(Ljava/util/List;)Ljava/util/List;
      15: areturn

  public static final java.util.List<java.util.List<java.lang.Integer>> calculateCofactors(java.util.List<? extends java.util.List<java.lang.Integer>>);
    Code:
       0: aload_0
       1: ldc           #63                 // String matrix
       3: invokestatic  #69                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: checkcast     #78                 // class java/lang/Iterable
      10: astore_1
      11: iconst_0
      12: istore_2
      13: aload_1
      14: astore_3
      15: new           #80                 // class java/util/ArrayList
      18: dup
      19: aload_1
      20: bipush        10
      22: invokestatic  #84                 // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
      25: invokespecial #87                 // Method java/util/ArrayList."<init>":(I)V
      28: checkcast     #89                 // class java/util/Collection
      31: astore        4
      33: iconst_0
      34: istore        5
      36: iconst_0
      37: istore        6
      39: aload_3
      40: invokeinterface #93,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      45: astore        7
      47: aload         7
      49: invokeinterface #99,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      54: ifeq          223
      57: aload         7
      59: invokeinterface #103,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      64: astore        8
      66: aload         4
      68: iload         6
      70: iinc          6, 1
      73: istore        9
      75: iload         9
      77: ifge          83
      80: invokestatic  #106                // Method kotlin/collections/CollectionsKt.throwIndexOverflow:()V
      83: iload         9
      85: aload         8
      87: checkcast     #8                  // class java/util/List
      90: astore        10
      92: istore        11
      94: astore        23
      96: iconst_0
      97: istore        12
      99: aload         10
     101: checkcast     #89                 // class java/util/Collection
     104: invokestatic  #110                // Method kotlin/collections/CollectionsKt.getIndices:(Ljava/util/Collection;)Lkotlin/ranges/IntRange;
     107: checkcast     #78                 // class java/lang/Iterable
     110: astore        13
     112: iconst_0
     113: istore        14
     115: aload         13
     117: astore        15
     119: new           #80                 // class java/util/ArrayList
     122: dup
     123: aload         13
     125: bipush        10
     127: invokestatic  #84                 // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
     130: invokespecial #87                 // Method java/util/ArrayList."<init>":(I)V
     133: checkcast     #89                 // class java/util/Collection
     136: astore        16
     138: iconst_0
     139: istore        17
     141: aload         15
     143: invokeinterface #93,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     148: astore        18
     150: aload         18
     152: invokeinterface #99,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     157: ifeq          204
     160: aload         18
     162: checkcast     #112                // class kotlin/collections/IntIterator
     165: invokevirtual #116                // Method kotlin/collections/IntIterator.nextInt:()I
     168: istore        19
     170: aload         16
     172: iload         19
     174: istore        20
     176: astore        21
     178: iconst_0
     179: istore        22
     181: aload_0
     182: iload         11
     184: iload         20
     186: invokestatic  #120                // Method getCofactor:(Ljava/util/List;II)I
     189: invokestatic  #14                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     192: aload         21
     194: swap
     195: invokeinterface #124,  2          // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     200: pop
     201: goto          150
     204: aload         16
     206: checkcast     #8                  // class java/util/List
     209: nop
     210: nop
     211: aload         23
     213: swap
     214: invokeinterface #124,  2          // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     219: pop
     220: goto          47
     223: aload         4
     225: checkcast     #8                  // class java/util/List
     228: nop
     229: areturn

  public static final java.util.List<java.util.List<java.lang.Integer>> transpose(java.util.List<? extends java.util.List<java.lang.Integer>>);
    Code:
       0: aload_0
       1: ldc           #63                 // String matrix
       3: invokestatic  #69                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: invokeinterface #147,  1          // InterfaceMethod java/util/List.isEmpty:()Z
      12: ifne          33
      15: aload_0
      16: iconst_0
      17: invokeinterface #151,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      22: checkcast     #8                  // class java/util/List
      25: invokeinterface #147,  1          // InterfaceMethod java/util/List.isEmpty:()Z
      30: ifeq          37
      33: invokestatic  #155                // Method kotlin/collections/CollectionsKt.emptyList:()Ljava/util/List;
      36: areturn
      37: aload_0
      38: invokeinterface #158,  1          // InterfaceMethod java/util/List.size:()I
      43: istore_1
      44: aload_0
      45: iconst_0
      46: invokeinterface #151,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      51: checkcast     #8                  // class java/util/List
      54: invokeinterface #158,  1          // InterfaceMethod java/util/List.size:()I
      59: istore_2
      60: new           #80                 // class java/util/ArrayList
      63: dup
      64: iload_2
      65: invokespecial #87                 // Method java/util/ArrayList."<init>":(I)V
      68: astore_3
      69: iconst_0
      70: istore        4
      72: iload         4
      74: iload_2
      75: if_icmpge     185
      78: iload         4
      80: istore        5
      82: aload_3
      83: iload         5
      85: istore        6
      87: astore        14
      89: iconst_0
      90: istore        7
      92: new           #80                 // class java/util/ArrayList
      95: dup
      96: iload_1
      97: invokespecial #87                 // Method java/util/ArrayList."<init>":(I)V
     100: astore        8
     102: iconst_0
     103: istore        9
     105: iload         9
     107: iload_1
     108: if_icmpge     166
     111: iload         9
     113: istore        10
     115: aload         8
     117: iload         10
     119: istore        11
     121: astore        12
     123: iconst_0
     124: istore        13
     126: aload_0
     127: iload         11
     129: invokeinterface #151,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     134: checkcast     #8                  // class java/util/List
     137: iload         6
     139: invokeinterface #151,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     144: checkcast     #160                // class java/lang/Number
     147: invokevirtual #163                // Method java/lang/Number.intValue:()I
     150: invokestatic  #14                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     153: aload         12
     155: swap
     156: invokevirtual #164                // Method java/util/ArrayList.add:(Ljava/lang/Object;)Z
     159: pop
     160: iinc          9, 1
     163: goto          105
     166: aload         8
     168: checkcast     #8                  // class java/util/List
     171: nop
     172: aload         14
     174: swap
     175: invokevirtual #164                // Method java/util/ArrayList.add:(Ljava/lang/Object;)Z
     178: pop
     179: iinc          4, 1
     182: goto          72
     185: aload_3
     186: checkcast     #8                  // class java/util/List
     189: areturn

  public static final int getCofactor(java.util.List<? extends java.util.List<java.lang.Integer>>, int, int);
    Code:
       0: aload_0
       1: ldc           #63                 // String matrix
       3: invokestatic  #69                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: iload_1
       7: iload_2
       8: iadd
       9: iconst_2
      10: irem
      11: ifne          18
      14: iconst_1
      15: goto          19
      18: iconst_m1
      19: istore_3
      20: aload_0
      21: iload_1
      22: iload_2
      23: invokestatic  #174                // Method getSubMatrix:(Ljava/util/List;II)Ljava/util/List;
      26: astore        4
      28: iload_3
      29: aload         4
      31: invokestatic  #178                // Method determinant:(Ljava/util/List;)I
      34: imul
      35: ireturn

  public static final java.util.List<java.util.List<java.lang.Integer>> getSubMatrix(java.util.List<? extends java.util.List<java.lang.Integer>>, int, int);
    Code:
       0: aload_0
       1: ldc           #63                 // String matrix
       3: invokestatic  #69                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: checkcast     #78                 // class java/lang/Iterable
      10: astore_3
      11: nop
      12: iconst_0
      13: istore        4
      15: aload_3
      16: astore        5
      18: new           #80                 // class java/util/ArrayList
      21: dup
      22: invokespecial #182                // Method java/util/ArrayList."<init>":()V
      25: checkcast     #89                 // class java/util/Collection
      28: astore        6
      30: iconst_0
      31: istore        7
      33: aload         5
      35: astore        8
      37: iconst_0
      38: istore        9
      40: iconst_0
      41: istore        10
      43: aload         8
      45: invokeinterface #93,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      50: astore        11
      52: aload         11
      54: invokeinterface #99,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      59: ifeq          139
      62: aload         11
      64: invokeinterface #103,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      69: astore        12
      71: iload         10
      73: iinc          10, 1
      76: istore        13
      78: iload         13
      80: ifge          86
      83: invokestatic  #106                // Method kotlin/collections/CollectionsKt.throwIndexOverflow:()V
      86: iload         13
      88: aload         12
      90: astore        14
      92: istore        15
      94: iconst_0
      95: istore        16
      97: iload         15
      99: aload         14
     101: checkcast     #8                  // class java/util/List
     104: pop
     105: istore        18
     107: iconst_0
     108: istore        19
     110: iload         18
     112: iload_1
     113: if_icmpeq     120
     116: iconst_1
     117: goto          121
     120: iconst_0
     121: ifeq          134
     124: aload         6
     126: aload         14
     128: invokeinterface #124,  2          // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     133: pop
     134: nop
     135: nop
     136: goto          52
     139: nop
     140: aload         6
     142: checkcast     #8                  // class java/util/List
     145: nop
     146: checkcast     #78                 // class java/lang/Iterable
     149: astore_3
     150: nop
     151: iconst_0
     152: istore        4
     154: aload_3
     155: astore        5
     157: new           #80                 // class java/util/ArrayList
     160: dup
     161: aload_3
     162: bipush        10
     164: invokestatic  #84                 // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
     167: invokespecial #87                 // Method java/util/ArrayList."<init>":(I)V
     170: checkcast     #89                 // class java/util/Collection
     173: astore        6
     175: iconst_0
     176: istore        7
     178: aload         5
     180: invokeinterface #93,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     185: astore        8
     187: aload         8
     189: invokeinterface #99,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     194: ifeq          378
     197: aload         8
     199: invokeinterface #103,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     204: astore        9
     206: aload         6
     208: aload         9
     210: checkcast     #8                  // class java/util/List
     213: astore        10
     215: astore        28
     217: iconst_0
     218: istore        11
     220: aload         10
     222: checkcast     #78                 // class java/lang/Iterable
     225: astore        12
     227: iconst_0
     228: istore        13
     230: aload         12
     232: astore        14
     234: new           #80                 // class java/util/ArrayList
     237: dup
     238: invokespecial #182                // Method java/util/ArrayList."<init>":()V
     241: checkcast     #89                 // class java/util/Collection
     244: astore        15
     246: iconst_0
     247: istore        16
     249: aload         14
     251: astore        17
     253: iconst_0
     254: istore        18
     256: iconst_0
     257: istore        19
     259: aload         17
     261: invokeinterface #93,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     266: astore        20
     268: aload         20
     270: invokeinterface #99,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     275: ifeq          358
     278: aload         20
     280: invokeinterface #103,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     285: astore        21
     287: iload         19
     289: iinc          19, 1
     292: istore        22
     294: iload         22
     296: ifge          302
     299: invokestatic  #106                // Method kotlin/collections/CollectionsKt.throwIndexOverflow:()V
     302: iload         22
     304: aload         21
     306: astore        23
     308: istore        24
     310: iconst_0
     311: istore        25
     313: iload         24
     315: aload         23
     317: checkcast     #160                // class java/lang/Number
     320: invokevirtual #163                // Method java/lang/Number.intValue:()I
     323: pop
     324: istore        26
     326: iconst_0
     327: istore        27
     329: iload         26
     331: iload_2
     332: if_icmpeq     339
     335: iconst_1
     336: goto          340
     339: iconst_0
     340: ifeq          353
     343: aload         15
     345: aload         23
     347: invokeinterface #124,  2          // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     352: pop
     353: nop
     354: nop
     355: goto          268
     358: nop
     359: aload         15
     361: checkcast     #8                  // class java/util/List
     364: nop
     365: nop
     366: aload         28
     368: swap
     369: invokeinterface #124,  2          // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     374: pop
     375: goto          187
     378: aload         6
     380: checkcast     #8                  // class java/util/List
     383: nop
     384: areturn

  public static final int determinant(java.util.List<? extends java.util.List<java.lang.Integer>>);
    Code:
       0: aload_0
       1: ldc           #63                 // String matrix
       3: invokestatic  #69                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: invokeinterface #158,  1          // InterfaceMethod java/util/List.size:()I
      12: iconst_2
      13: if_icmpne     108
      16: aload_0
      17: iconst_0
      18: invokeinterface #151,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      23: checkcast     #8                  // class java/util/List
      26: iconst_0
      27: invokeinterface #151,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      32: checkcast     #160                // class java/lang/Number
      35: invokevirtual #163                // Method java/lang/Number.intValue:()I
      38: aload_0
      39: iconst_1
      40: invokeinterface #151,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      45: checkcast     #8                  // class java/util/List
      48: iconst_1
      49: invokeinterface #151,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      54: checkcast     #160                // class java/lang/Number
      57: invokevirtual #163                // Method java/lang/Number.intValue:()I
      60: imul
      61: aload_0
      62: iconst_0
      63: invokeinterface #151,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      68: checkcast     #8                  // class java/util/List
      71: iconst_1
      72: invokeinterface #151,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      77: checkcast     #160                // class java/lang/Number
      80: invokevirtual #163                // Method java/lang/Number.intValue:()I
      83: aload_0
      84: iconst_1
      85: invokeinterface #151,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      90: checkcast     #8                  // class java/util/List
      93: iconst_0
      94: invokeinterface #151,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      99: checkcast     #160                // class java/lang/Number
     102: invokevirtual #163                // Method java/lang/Number.intValue:()I
     105: imul
     106: isub
     107: ireturn
     108: aload_0
     109: checkcast     #89                 // class java/util/Collection
     112: invokestatic  #110                // Method kotlin/collections/CollectionsKt.getIndices:(Ljava/util/Collection;)Lkotlin/ranges/IntRange;
     115: checkcast     #78                 // class java/lang/Iterable
     118: astore_1
     119: iconst_0
     120: istore_2
     121: aload_1
     122: invokeinterface #93,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     127: astore_3
     128: aload_3
     129: invokeinterface #99,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     134: ifeq          222
     137: aload_3
     138: checkcast     #112                // class kotlin/collections/IntIterator
     141: invokevirtual #116                // Method kotlin/collections/IntIterator.nextInt:()I
     144: istore        4
     146: iload_2
     147: iload         4
     149: istore        5
     151: istore        9
     153: iconst_0
     154: istore        6
     156: iload         5
     158: iconst_2
     159: irem
     160: ifne          167
     163: iconst_1
     164: goto          168
     167: iconst_m1
     168: istore        7
     170: aload_0
     171: iconst_0
     172: iload         5
     174: invokestatic  #174                // Method getSubMatrix:(Ljava/util/List;II)Ljava/util/List;
     177: astore        8
     179: iload         7
     181: aload_0
     182: iconst_0
     183: invokeinterface #151,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     188: checkcast     #8                  // class java/util/List
     191: iload         5
     193: invokeinterface #151,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     198: checkcast     #160                // class java/lang/Number
     201: invokevirtual #163                // Method java/lang/Number.intValue:()I
     204: imul
     205: aload         8
     207: invokestatic  #178                // Method determinant:(Ljava/util/List;)I
     210: imul
     211: istore        10
     213: iload         9
     215: iload         10
     217: iadd
     218: istore_2
     219: goto          128
     222: iload_2
     223: ireturn

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


import java.io.File
import java.math.BigInteger
import java.security.MessageDigest
import kotlin.math.abs

typealias Matrix = Array<IntArray>
typealias BooleanMatrix = Array<BooleanArray>
typealias Int3DMatrix = Array<Array<IntArray>>
typealias Boolean3DMatrix = Array<Array<BooleanArray>>

/**
 * Reads lines from the given input txt file.
 */
fun readInput(name: String) = File("src", "$name.txt")
    .readLines()

/**
 * Converts string to md5 hash.
 */
fun String.md5() = BigInteger(1, MessageDigest.getInstance("MD5").digest(toByteArray()))
    .toString(16)
    .padStart(32, '0')

operator fun IntRange.contains(other: IntRange): Boolean = first <= other.first && other.last <= last
val IntRange.size
    get() = abs(this.last - this.first) + 1

data class XY(val x: Int, val y: Int)
data class MutableXY(var x: Int, var y: Int) {
    fun toXY() = XY(x = x, y = y)
}

data class IJ(val i: Int, val j: Int)
data class MutableIJ(var i: Int, var j: Int) {
    fun toIJ() = IJ(i = i, j = j)
}

fun BooleanMatrix.deepCopyOf(): BooleanMatrix = Array(this.size) { i -> this[i].copyOf() }

data class Item(val steps: Int, val ij: IJ) : Comparable<Item> {
    override fun compareTo(other: Item): Int {
        return this.steps - other.steps
    }
}

/**
 * Create all possible combinations from the list's elements
 */
fun <T> List<T>.allCombinations(): List<List<T>> {
    val results = mutableListOf<List<T>>()

    repeat(this.size + 1) { count ->
        combine(data = ArrayList(count), results, start = 0, index = 0, count = count)
    }

    return results
}

/**
 * Create all possible combinations with size "count " from the list's elements
 * @param count - count of the elements in each combination
 */
fun <T> List<T>.combinations(count: Int): List<List<T>> {
    val results = mutableListOf<List<T>>()

    combine(data = ArrayList(count), results, start = 0, index = 0, count = count)

    return results
}

private fun <T> List<T>.combine(
    data: ArrayList<T>,
    results: MutableList<List<T>>,
    start: Int,
    index: Int,
    count: Int,
) {
    // Current combination is ready to be added to the result
    if (index == count) {
        results.add(ArrayList(data))
        return
    }

    // replace index with all possible elements. The condition
    // "this.lastIndex - i + 1 >= (count - 1) - index" makes sure that including one element
    // at index will make a combination with remaining elements
    // at remaining positions
    var i = start
    while (i <= this.lastIndex && (this.lastIndex - i + 1 >= (count - 1) - index)) {
        if (data.lastIndex < index) {
            data.add(this[i])
        } else {
            data[index] = this[i]
        }

        combine(data, results, start = i + 1, index = index + 1, count = count)
        i++
    }
}

Compiled from "Utils.kt"
public final class UtilsKt {
  public static final java.util.List<java.lang.String> readInput(java.lang.String);
    Code:
       0: aload_0
       1: ldc           #10                 // String name
       3: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: new           #18                 // class java/io/File
       9: dup
      10: ldc           #20                 // String src
      12: new           #22                 // class java/lang/StringBuilder
      15: dup
      16: invokespecial #26                 // Method java/lang/StringBuilder."<init>":()V
      19: aload_0
      20: invokevirtual #30                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      23: ldc           #32                 // String .txt
      25: invokevirtual #30                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      28: invokevirtual #36                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
      31: invokespecial #39                 // Method java/io/File."<init>":(Ljava/lang/String;Ljava/lang/String;)V
      34: aconst_null
      35: iconst_1
      36: aconst_null
      37: invokestatic  #45                 // Method kotlin/io/FilesKt.readLines$default:(Ljava/io/File;Ljava/nio/charset/Charset;ILjava/lang/Object;)Ljava/util/List;
      40: areturn

  public static final java.lang.String md5(java.lang.String);
    Code:
       0: aload_0
       1: ldc           #50                 // String <this>
       3: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: new           #52                 // class java/math/BigInteger
       9: dup
      10: iconst_1
      11: ldc           #54                 // String MD5
      13: invokestatic  #60                 // Method java/security/MessageDigest.getInstance:(Ljava/lang/String;)Ljava/security/MessageDigest;
      16: aload_0
      17: astore_1
      18: getstatic     #66                 // Field kotlin/text/Charsets.UTF_8:Ljava/nio/charset/Charset;
      21: aload_1
      22: swap
      23: invokevirtual #72                 // Method java/lang/String.getBytes:(Ljava/nio/charset/Charset;)[B
      26: dup
      27: ldc           #74                 // String getBytes(...)
      29: invokestatic  #77                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
      32: invokevirtual #81                 // Method java/security/MessageDigest.digest:([B)[B
      35: invokespecial #84                 // Method java/math/BigInteger."<init>":(I[B)V
      38: bipush        16
      40: invokevirtual #87                 // Method java/math/BigInteger.toString:(I)Ljava/lang/String;
      43: dup
      44: ldc           #89                 // String toString(...)
      46: invokestatic  #77                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
      49: bipush        32
      51: bipush        48
      53: invokestatic  #95                 // Method kotlin/text/StringsKt.padStart:(Ljava/lang/String;IC)Ljava/lang/String;
      56: areturn

  public static final boolean contains(kotlin.ranges.IntRange, kotlin.ranges.IntRange);
    Code:
       0: aload_0
       1: ldc           #50                 // String <this>
       3: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_1
       7: ldc           #100                // String other
       9: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
      12: aload_0
      13: invokevirtual #106                // Method kotlin/ranges/IntRange.getFirst:()I
      16: aload_1
      17: invokevirtual #106                // Method kotlin/ranges/IntRange.getFirst:()I
      20: if_icmpgt     38
      23: aload_1
      24: invokevirtual #109                // Method kotlin/ranges/IntRange.getLast:()I
      27: aload_0
      28: invokevirtual #109                // Method kotlin/ranges/IntRange.getLast:()I
      31: if_icmpgt     38
      34: iconst_1
      35: goto          39
      38: iconst_0
      39: ireturn

  public static final int getSize(kotlin.ranges.IntRange);
    Code:
       0: aload_0
       1: ldc           #50                 // String <this>
       3: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: invokevirtual #109                // Method kotlin/ranges/IntRange.getLast:()I
      10: aload_0
      11: invokevirtual #106                // Method kotlin/ranges/IntRange.getFirst:()I
      14: isub
      15: invokestatic  #119                // Method java/lang/Math.abs:(I)I
      18: iconst_1
      19: iadd
      20: ireturn

  public static final boolean[][] deepCopyOf(boolean[][]);
    Code:
       0: aload_0
       1: ldc           #50                 // String <this>
       3: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: iconst_0
       7: istore_1
       8: aload_0
       9: checkcast     #124                // class "[Ljava/lang/Object;"
      12: arraylength
      13: istore_2
      14: iload_2
      15: anewarray     #126                // class "[Z"
      18: astore_3
      19: iload_1
      20: iload_2
      21: if_icmpge     52
      24: iload_1
      25: istore        4
      27: aload_3
      28: iload         4
      30: aload_0
      31: iload         4
      33: aaload
      34: dup
      35: arraylength
      36: invokestatic  #132                // Method java/util/Arrays.copyOf:([ZI)[Z
      39: dup
      40: ldc           #134                // String copyOf(...)
      42: invokestatic  #77                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
      45: aastore
      46: iinc          1, 1
      49: goto          19
      52: aload_3
      53: areturn

  public static final <T> java.util.List<java.util.List<T>> allCombinations(java.util.List<? extends T>);
    Code:
       0: aload_0
       1: ldc           #50                 // String <this>
       3: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: new           #142                // class java/util/ArrayList
       9: dup
      10: invokespecial #143                // Method java/util/ArrayList."<init>":()V
      13: checkcast     #145                // class java/util/List
      16: astore_1
      17: aload_0
      18: invokeinterface #148,  1          // InterfaceMethod java/util/List.size:()I
      23: iconst_1
      24: iadd
      25: istore_2
      26: iconst_0
      27: istore_3
      28: iload_3
      29: iload_2
      30: if_icmpge     64
      33: iload_3
      34: istore        4
      36: iconst_0
      37: istore        5
      39: aload_0
      40: new           #142                // class java/util/ArrayList
      43: dup
      44: iload         4
      46: invokespecial #151                // Method java/util/ArrayList."<init>":(I)V
      49: aload_1
      50: iconst_0
      51: iconst_0
      52: iload         4
      54: invokestatic  #155                // Method combine:(Ljava/util/List;Ljava/util/ArrayList;Ljava/util/List;III)V
      57: nop
      58: iinc          3, 1
      61: goto          28
      64: aload_1
      65: areturn

  public static final <T> java.util.List<java.util.List<T>> combinations(java.util.List<? extends T>, int);
    Code:
       0: aload_0
       1: ldc           #50                 // String <this>
       3: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: new           #142                // class java/util/ArrayList
       9: dup
      10: invokespecial #143                // Method java/util/ArrayList."<init>":()V
      13: checkcast     #145                // class java/util/List
      16: astore_2
      17: aload_0
      18: new           #142                // class java/util/ArrayList
      21: dup
      22: iload_1
      23: invokespecial #151                // Method java/util/ArrayList."<init>":(I)V
      26: aload_2
      27: iconst_0
      28: iconst_0
      29: iload_1
      30: invokestatic  #155                // Method combine:(Ljava/util/List;Ljava/util/ArrayList;Ljava/util/List;III)V
      33: aload_2
      34: areturn

  private static final <T> void combine(java.util.List<? extends T>, java.util.ArrayList<T>, java.util.List<java.util.List<T>>, int, int, int);
    Code:
       0: iload         4
       2: iload         5
       4: if_icmpne     26
       7: aload_2
       8: new           #142                // class java/util/ArrayList
      11: dup
      12: aload_1
      13: checkcast     #168                // class java/util/Collection
      16: invokespecial #171                // Method java/util/ArrayList."<init>":(Ljava/util/Collection;)V
      19: invokeinterface #175,  2          // InterfaceMethod java/util/List.add:(Ljava/lang/Object;)Z
      24: pop
      25: return
      26: iload_3
      27: istore        6
      29: iload         6
      31: aload_0
      32: invokestatic  #181                // Method kotlin/collections/CollectionsKt.getLastIndex:(Ljava/util/List;)I
      35: if_icmpgt     122
      38: aload_0
      39: invokestatic  #181                // Method kotlin/collections/CollectionsKt.getLastIndex:(Ljava/util/List;)I
      42: iload         6
      44: isub
      45: iconst_1
      46: iadd
      47: iload         5
      49: iconst_1
      50: isub
      51: iload         4
      53: isub
      54: if_icmplt     122
      57: aload_1
      58: checkcast     #145                // class java/util/List
      61: invokestatic  #181                // Method kotlin/collections/CollectionsKt.getLastIndex:(Ljava/util/List;)I
      64: iload         4
      66: if_icmpge     85
      69: aload_1
      70: aload_0
      71: iload         6
      73: invokeinterface #185,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      78: invokevirtual #186                // Method java/util/ArrayList.add:(Ljava/lang/Object;)Z
      81: pop
      82: goto          100
      85: aload_1
      86: iload         4
      88: aload_0
      89: iload         6
      91: invokeinterface #185,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      96: invokevirtual #190                // Method java/util/ArrayList.set:(ILjava/lang/Object;)Ljava/lang/Object;
      99: pop
     100: aload_0
     101: aload_1
     102: aload_2
     103: iload         6
     105: iconst_1
     106: iadd
     107: iload         4
     109: iconst_1
     110: iadd
     111: iload         5
     113: invokestatic  #155                // Method combine:(Ljava/util/List;Ljava/util/ArrayList;Ljava/util/List;III)V
     116: iinc          6, 1
     119: goto          29
     122: return
}


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

fun main() {
    val testInput = readInput("Day14_test")
    check(part1(testInput) == 24)
    check(part2(testInput) == 93)

    val input = readInput("Day14")
    println(part1(input))
    println(part2(input))
}

private val SAND_START_POINT = XY(500, 0)

private fun part1(input: List<String>): Int {
    val points = mutableSetOf<XY>()
    parseInput(input, points)

    val bottom = points.maxOf { it.y }
    val rocksCount = points.size

    solve(bottom, points, SAND_START_POINT, exitOnBottom = true)

    return points.size - rocksCount
}

private fun part2(input: List<String>): Int {
    val points = mutableSetOf<XY>()
    parseInput(input, points)

    val bottom = points.maxOf { it.y } + 2
    val rocksCount = points.size

    solve(bottom, points, SAND_START_POINT, exitOnBottom = false)

    return points.size - rocksCount
}

private fun parseInput(input: List<String>, points: MutableSet<XY>) {
    input
        .map { it.split(" -> ") }
        .forEach { row ->
            row.windowed(2, 1) { (from, to) ->
                val xFrom = from.substringBefore(",").toInt()
                val yFrom = from.substringAfter(",").toInt()

                val xTo = to.substringBefore(",").toInt()
                val yTo = to.substringAfter(",").toInt()

                for (x in min(xFrom, xTo)..max(xFrom, xTo)) {
                    for (y in min(yFrom, yTo)..max(yFrom, yTo)) {
                        points.add(XY(x = x, y = y))
                    }
                }
            }
        }
}

private fun solve(bottom: Int, points: MutableSet<XY>, point: XY, exitOnBottom: Boolean): Boolean {
    if (points.contains(point)) {
        return false
    }

    if (point.y == bottom) {
        return exitOnBottom
    }

    if (solve(bottom, points, XY(x = point.x, y = point.y + 1), exitOnBottom)) return true
    if (solve(bottom, points, XY(x = point.x - 1, y = point.y + 1), exitOnBottom)) return true
    if (solve(bottom, points, XY(x = point.x + 1, y = point.y + 1), exitOnBottom)) return true

    points.add(point)

    return false
}

Compiled from "Day14.kt"
public final class Day14Kt {
  private static final XY SAND_START_POINT;

  public static final void main();
    Code:
       0: ldc           #8                  // String Day14_test
       2: invokestatic  #14                 // Method UtilsKt.readInput:(Ljava/lang/String;)Ljava/util/List;
       5: astore_0
       6: aload_0
       7: invokestatic  #18                 // Method part1:(Ljava/util/List;)I
      10: bipush        24
      12: if_icmpne     19
      15: iconst_1
      16: goto          20
      19: iconst_0
      20: ifne          33
      23: new           #20                 // class java/lang/IllegalStateException
      26: dup
      27: ldc           #22                 // String Check failed.
      29: invokespecial #26                 // Method java/lang/IllegalStateException."<init>":(Ljava/lang/String;)V
      32: athrow
      33: aload_0
      34: invokestatic  #29                 // Method part2:(Ljava/util/List;)I
      37: bipush        93
      39: if_icmpne     46
      42: iconst_1
      43: goto          47
      46: iconst_0
      47: ifne          60
      50: new           #20                 // class java/lang/IllegalStateException
      53: dup
      54: ldc           #22                 // String Check failed.
      56: invokespecial #26                 // Method java/lang/IllegalStateException."<init>":(Ljava/lang/String;)V
      59: athrow
      60: ldc           #31                 // String Day14
      62: invokestatic  #14                 // Method UtilsKt.readInput:(Ljava/lang/String;)Ljava/util/List;
      65: astore_1
      66: aload_1
      67: invokestatic  #18                 // Method part1:(Ljava/util/List;)I
      70: istore_2
      71: getstatic     #37                 // Field java/lang/System.out:Ljava/io/PrintStream;
      74: iload_2
      75: invokevirtual #43                 // Method java/io/PrintStream.println:(I)V
      78: aload_1
      79: invokestatic  #29                 // Method part2:(Ljava/util/List;)I
      82: istore_2
      83: getstatic     #37                 // Field java/lang/System.out:Ljava/io/PrintStream;
      86: iload_2
      87: invokevirtual #43                 // Method java/io/PrintStream.println:(I)V
      90: return

  private static final int part1(java.util.List<java.lang.String>);
    Code:
       0: new           #51                 // class java/util/LinkedHashSet
       3: dup
       4: invokespecial #53                 // Method java/util/LinkedHashSet."<init>":()V
       7: checkcast     #55                 // class java/util/Set
      10: astore_1
      11: aload_0
      12: aload_1
      13: invokestatic  #59                 // Method parseInput:(Ljava/util/List;Ljava/util/Set;)V
      16: aload_1
      17: checkcast     #61                 // class java/lang/Iterable
      20: invokeinterface #65,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      25: astore        4
      27: aload         4
      29: invokeinterface #71,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      34: ifne          45
      37: new           #73                 // class java/util/NoSuchElementException
      40: dup
      41: invokespecial #74                 // Method java/util/NoSuchElementException."<init>":()V
      44: athrow
      45: aload         4
      47: invokeinterface #78,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      52: checkcast     #80                 // class XY
      55: astore        5
      57: iconst_0
      58: istore        6
      60: aload         5
      62: invokevirtual #84                 // Method XY.getY:()I
      65: istore        5
      67: aload         4
      69: invokeinterface #71,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      74: ifeq          113
      77: aload         4
      79: invokeinterface #78,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      84: checkcast     #80                 // class XY
      87: astore        6
      89: iconst_0
      90: istore        7
      92: aload         6
      94: invokevirtual #84                 // Method XY.getY:()I
      97: istore        6
      99: iload         5
     101: iload         6
     103: if_icmpge     67
     106: iload         6
     108: istore        5
     110: goto          67
     113: iload         5
     115: istore_2
     116: aload_1
     117: invokeinterface #87,  1           // InterfaceMethod java/util/Set.size:()I
     122: istore_3
     123: iload_2
     124: aload_1
     125: getstatic     #91                 // Field SAND_START_POINT:LXY;
     128: iconst_1
     129: invokestatic  #95                 // Method solve:(ILjava/util/Set;LXY;Z)Z
     132: pop
     133: aload_1
     134: invokeinterface #87,  1           // InterfaceMethod java/util/Set.size:()I
     139: iload_3
     140: isub
     141: ireturn

  private static final int part2(java.util.List<java.lang.String>);
    Code:
       0: new           #51                 // class java/util/LinkedHashSet
       3: dup
       4: invokespecial #53                 // Method java/util/LinkedHashSet."<init>":()V
       7: checkcast     #55                 // class java/util/Set
      10: astore_1
      11: aload_0
      12: aload_1
      13: invokestatic  #59                 // Method parseInput:(Ljava/util/List;Ljava/util/Set;)V
      16: aload_1
      17: checkcast     #61                 // class java/lang/Iterable
      20: invokeinterface #65,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      25: astore        4
      27: aload         4
      29: invokeinterface #71,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      34: ifne          45
      37: new           #73                 // class java/util/NoSuchElementException
      40: dup
      41: invokespecial #74                 // Method java/util/NoSuchElementException."<init>":()V
      44: athrow
      45: aload         4
      47: invokeinterface #78,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      52: checkcast     #80                 // class XY
      55: astore        5
      57: iconst_0
      58: istore        6
      60: aload         5
      62: invokevirtual #84                 // Method XY.getY:()I
      65: istore        5
      67: aload         4
      69: invokeinterface #71,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      74: ifeq          113
      77: aload         4
      79: invokeinterface #78,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      84: checkcast     #80                 // class XY
      87: astore        6
      89: iconst_0
      90: istore        7
      92: aload         6
      94: invokevirtual #84                 // Method XY.getY:()I
      97: istore        6
      99: iload         5
     101: iload         6
     103: if_icmpge     67
     106: iload         6
     108: istore        5
     110: goto          67
     113: iload         5
     115: iconst_2
     116: iadd
     117: istore_2
     118: aload_1
     119: invokeinterface #87,  1           // InterfaceMethod java/util/Set.size:()I
     124: istore_3
     125: iload_2
     126: aload_1
     127: getstatic     #91                 // Field SAND_START_POINT:LXY;
     130: iconst_0
     131: invokestatic  #95                 // Method solve:(ILjava/util/Set;LXY;Z)Z
     134: pop
     135: aload_1
     136: invokeinterface #87,  1           // InterfaceMethod java/util/Set.size:()I
     141: iload_3
     142: isub
     143: ireturn

  private static final void parseInput(java.util.List<java.lang.String>, java.util.Set<XY>);
    Code:
       0: aload_0
       1: checkcast     #61                 // class java/lang/Iterable
       4: astore_2
       5: nop
       6: iconst_0
       7: istore_3
       8: aload_2
       9: astore        4
      11: new           #106                // class java/util/ArrayList
      14: dup
      15: aload_2
      16: bipush        10
      18: invokestatic  #112                // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
      21: invokespecial #114                // Method java/util/ArrayList."<init>":(I)V
      24: checkcast     #116                // class java/util/Collection
      27: astore        5
      29: iconst_0
      30: istore        6
      32: aload         4
      34: invokeinterface #65,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      39: astore        7
      41: aload         7
      43: invokeinterface #71,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      48: ifeq          113
      51: aload         7
      53: invokeinterface #78,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      58: astore        8
      60: aload         5
      62: aload         8
      64: checkcast     #118                // class java/lang/String
      67: astore        9
      69: astore        12
      71: iconst_0
      72: istore        10
      74: aload         9
      76: checkcast     #120                // class java/lang/CharSequence
      79: iconst_1
      80: anewarray     #118                // class java/lang/String
      83: astore        11
      85: aload         11
      87: iconst_0
      88: ldc           #122                // String  ->
      90: aastore
      91: aload         11
      93: iconst_0
      94: iconst_0
      95: bipush        6
      97: aconst_null
      98: invokestatic  #128                // Method kotlin/text/StringsKt.split$default:(Ljava/lang/CharSequence;[Ljava/lang/String;ZIILjava/lang/Object;)Ljava/util/List;
     101: aload         12
     103: swap
     104: invokeinterface #132,  2          // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     109: pop
     110: goto          41
     113: aload         5
     115: checkcast     #48                 // class java/util/List
     118: nop
     119: checkcast     #61                 // class java/lang/Iterable
     122: astore_2
     123: nop
     124: iconst_0
     125: istore_3
     126: aload_2
     127: invokeinterface #65,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     132: astore        4
     134: aload         4
     136: invokeinterface #71,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     141: ifeq          188
     144: aload         4
     146: invokeinterface #78,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     151: astore        5
     153: aload         5
     155: checkcast     #48                 // class java/util/List
     158: astore        6
     160: iconst_0
     161: istore        7
     163: aload         6
     165: checkcast     #61                 // class java/lang/Iterable
     168: iconst_2
     169: iconst_1
     170: iconst_0
     171: aload_1
     172: invokedynamic #152,  0            // InvokeDynamic #0:invoke:(Ljava/util/Set;)Lkotlin/jvm/functions/Function1;
     177: iconst_4
     178: aconst_null
     179: invokestatic  #156                // Method kotlin/collections/CollectionsKt.windowed$default:(Ljava/lang/Iterable;IIZLkotlin/jvm/functions/Function1;ILjava/lang/Object;)Ljava/util/List;
     182: pop
     183: nop
     184: nop
     185: goto          134
     188: nop
     189: return

  private static final boolean solve(int, java.util.Set<XY>, XY, boolean);
    Code:
       0: aload_1
       1: aload_2
       2: invokeinterface #176,  2          // InterfaceMethod java/util/Set.contains:(Ljava/lang/Object;)Z
       7: ifeq          12
      10: iconst_0
      11: ireturn
      12: aload_2
      13: invokevirtual #84                 // Method XY.getY:()I
      16: iload_0
      17: if_icmpne     22
      20: iload_3
      21: ireturn
      22: iload_0
      23: aload_1
      24: new           #80                 // class XY
      27: dup
      28: aload_2
      29: invokevirtual #179                // Method XY.getX:()I
      32: aload_2
      33: invokevirtual #84                 // Method XY.getY:()I
      36: iconst_1
      37: iadd
      38: invokespecial #182                // Method XY."<init>":(II)V
      41: iload_3
      42: invokestatic  #95                 // Method solve:(ILjava/util/Set;LXY;Z)Z
      45: ifeq          50
      48: iconst_1
      49: ireturn
      50: iload_0
      51: aload_1
      52: new           #80                 // class XY
      55: dup
      56: aload_2
      57: invokevirtual #179                // Method XY.getX:()I
      60: iconst_1
      61: isub
      62: aload_2
      63: invokevirtual #84                 // Method XY.getY:()I
      66: iconst_1
      67: iadd
      68: invokespecial #182                // Method XY."<init>":(II)V
      71: iload_3
      72: invokestatic  #95                 // Method solve:(ILjava/util/Set;LXY;Z)Z
      75: ifeq          80
      78: iconst_1
      79: ireturn
      80: iload_0
      81: aload_1
      82: new           #80                 // class XY
      85: dup
      86: aload_2
      87: invokevirtual #179                // Method XY.getX:()I
      90: iconst_1
      91: iadd
      92: aload_2
      93: invokevirtual #84                 // Method XY.getY:()I
      96: iconst_1
      97: iadd
      98: invokespecial #182                // Method XY."<init>":(II)V
     101: iload_3
     102: invokestatic  #95                 // Method solve:(ILjava/util/Set;LXY;Z)Z
     105: ifeq          110
     108: iconst_1
     109: ireturn
     110: aload_1
     111: aload_2
     112: invokeinterface #183,  2          // InterfaceMethod java/util/Set.add:(Ljava/lang/Object;)Z
     117: pop
     118: iconst_0
     119: ireturn

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

  private static final kotlin.Unit parseInput$lambda$4$lambda$3(java.util.Set, java.util.List);
    Code:
       0: aload_1
       1: ldc           #193                // String <destruct>
       3: invokestatic  #199                // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_1
       7: iconst_0
       8: invokeinterface #203,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      13: checkcast     #118                // class java/lang/String
      16: astore_2
      17: aload_1
      18: iconst_1
      19: invokeinterface #203,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      24: checkcast     #118                // class java/lang/String
      27: astore_3
      28: aload_2
      29: ldc           #205                // String ,
      31: aconst_null
      32: iconst_2
      33: aconst_null
      34: invokestatic  #209                // Method kotlin/text/StringsKt.substringBefore$default:(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;ILjava/lang/Object;)Ljava/lang/String;
      37: invokestatic  #215                // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
      40: istore        4
      42: aload_2
      43: ldc           #205                // String ,
      45: aconst_null
      46: iconst_2
      47: aconst_null
      48: invokestatic  #218                // Method kotlin/text/StringsKt.substringAfter$default:(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;ILjava/lang/Object;)Ljava/lang/String;
      51: invokestatic  #215                // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
      54: istore        5
      56: aload_3
      57: ldc           #205                // String ,
      59: aconst_null
      60: iconst_2
      61: aconst_null
      62: invokestatic  #209                // Method kotlin/text/StringsKt.substringBefore$default:(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;ILjava/lang/Object;)Ljava/lang/String;
      65: invokestatic  #215                // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
      68: istore        6
      70: aload_3
      71: ldc           #205                // String ,
      73: aconst_null
      74: iconst_2
      75: aconst_null
      76: invokestatic  #218                // Method kotlin/text/StringsKt.substringAfter$default:(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;ILjava/lang/Object;)Ljava/lang/String;
      79: invokestatic  #215                // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
      82: istore        7
      84: iload         4
      86: iload         6
      88: invokestatic  #224                // Method java/lang/Math.min:(II)I
      91: istore        8
      93: iload         4
      95: iload         6
      97: invokestatic  #227                // Method java/lang/Math.max:(II)I
     100: istore        9
     102: iload         8
     104: iload         9
     106: if_icmpgt     178
     109: iload         5
     111: iload         7
     113: invokestatic  #224                // Method java/lang/Math.min:(II)I
     116: istore        10
     118: iload         5
     120: iload         7
     122: invokestatic  #227                // Method java/lang/Math.max:(II)I
     125: istore        11
     127: iload         10
     129: iload         11
     131: if_icmpgt     165
     134: aload_0
     135: new           #80                 // class XY
     138: dup
     139: iload         8
     141: iload         10
     143: invokespecial #182                // Method XY."<init>":(II)V
     146: invokeinterface #183,  2          // InterfaceMethod java/util/Set.add:(Ljava/lang/Object;)Z
     151: pop
     152: iload         10
     154: iload         11
     156: if_icmpeq     165
     159: iinc          10, 1
     162: goto          134
     165: iload         8
     167: iload         9
     169: if_icmpeq     178
     172: iinc          8, 1
     175: goto          109
     178: getstatic     #233                // Field kotlin/Unit.INSTANCE:Lkotlin/Unit;
     181: areturn

  static {};
    Code:
       0: new           #80                 // class XY
       3: dup
       4: sipush        500
       7: iconst_0
       8: invokespecial #182                // Method XY."<init>":(II)V
      11: putstatic     #91                 // Field SAND_START_POINT:LXY;
      14: return
}