package sep_challenge2021

import java.util.*

fun main() {
    val grid = arrayOf(
        intArrayOf(0, 0, 0),
        intArrayOf(1, 1, 0),
        intArrayOf(0, 0, 0),
        intArrayOf(0, 1, 1),
        intArrayOf(0, 0, 0),
    )
    val result = shortestPath(grid, 1)
    assert(result == 6)
    println(result)
}

val directions = arrayOf(intArrayOf(1, 0), intArrayOf(-1, 0), intArrayOf(0, -1), intArrayOf(0, 1))

fun shortestPath(grid: Array<IntArray>, k: Int): Int {
    val m = grid.size
    val n = grid[0].size
    val visited = Array(grid.size) { Array(grid[0].size) { IntArray(grid[0].size * grid.size + 1) } }
    val queue: Queue<IntArray> = LinkedList()
    queue.add(intArrayOf(0, 0, k, 0))
    while (!queue.isEmpty()) {
        val current = queue.poll()
        val i = current[0]
        val j = current[1]
        val currentRemain = current[2]
        val currentDist = current[3]
        if (visited[i][j][currentRemain] == 1) continue
        visited[i][j][currentRemain] = 1
        if (i == m - 1 && j == n - 1) return currentDist
        for (direction in directions) {
            val newI = i + direction[0]
            val newJ = j + direction[1]
            if (newI in 0 until m && newJ in 0 until n) {
                if (grid[newI][newJ] == 1 && currentRemain > 0) {
                    queue.add(intArrayOf(newI, newJ, currentRemain - 1, currentDist + 1))
                } else if (grid[newI][newJ] == 0) {
                    queue.add(intArrayOf(newI, newJ, currentRemain, currentDist + 1))
                }
            }
        }

    }
    return -1
}

Compiled from "ShortestPathGridWithObstacles.kt"
public final class sep_challenge2021.ShortestPathGridWithObstaclesKt {
  private static final int[][] directions;

  public static final void main();
    Code:
       0: iconst_5
       1: anewarray     #8                  // class "[I"
       4: astore_1
       5: aload_1
       6: iconst_0
       7: iconst_3
       8: newarray       int
      10: astore_2
      11: aload_2
      12: iconst_0
      13: iconst_0
      14: iastore
      15: aload_2
      16: iconst_1
      17: iconst_0
      18: iastore
      19: aload_2
      20: iconst_2
      21: iconst_0
      22: iastore
      23: aload_2
      24: aastore
      25: aload_1
      26: iconst_1
      27: iconst_3
      28: newarray       int
      30: astore_2
      31: aload_2
      32: iconst_0
      33: iconst_1
      34: iastore
      35: aload_2
      36: iconst_1
      37: iconst_1
      38: iastore
      39: aload_2
      40: iconst_2
      41: iconst_0
      42: iastore
      43: aload_2
      44: aastore
      45: aload_1
      46: iconst_2
      47: iconst_3
      48: newarray       int
      50: astore_2
      51: aload_2
      52: iconst_0
      53: iconst_0
      54: iastore
      55: aload_2
      56: iconst_1
      57: iconst_0
      58: iastore
      59: aload_2
      60: iconst_2
      61: iconst_0
      62: iastore
      63: aload_2
      64: aastore
      65: aload_1
      66: iconst_3
      67: iconst_3
      68: newarray       int
      70: astore_2
      71: aload_2
      72: iconst_0
      73: iconst_0
      74: iastore
      75: aload_2
      76: iconst_1
      77: iconst_1
      78: iastore
      79: aload_2
      80: iconst_2
      81: iconst_1
      82: iastore
      83: aload_2
      84: aastore
      85: aload_1
      86: iconst_4
      87: iconst_3
      88: newarray       int
      90: astore_2
      91: aload_2
      92: iconst_0
      93: iconst_0
      94: iastore
      95: aload_2
      96: iconst_1
      97: iconst_0
      98: iastore
      99: aload_2
     100: iconst_2
     101: iconst_0
     102: iastore
     103: aload_2
     104: aastore
     105: aload_1
     106: astore_0
     107: aload_0
     108: iconst_1
     109: invokestatic  #12                 // Method shortestPath:([[II)I
     112: istore_1
     113: iload_1
     114: bipush        6
     116: if_icmpne     123
     119: iconst_1
     120: goto          124
     123: iconst_0
     124: istore_2
     125: getstatic     #18                 // Field kotlin/_Assertions.ENABLED:Z
     128: ifeq          147
     131: iload_2
     132: ifne          147
     135: ldc           #20                 // String Assertion failed
     137: astore_3
     138: new           #22                 // class java/lang/AssertionError
     141: dup
     142: aload_3
     143: invokespecial #26                 // Method java/lang/AssertionError."<init>":(Ljava/lang/Object;)V
     146: athrow
     147: getstatic     #32                 // Field java/lang/System.out:Ljava/io/PrintStream;
     150: iload_1
     151: invokevirtual #38                 // Method java/io/PrintStream.println:(I)V
     154: return

  public static final int[][] getDirections();
    Code:
       0: getstatic     #49                 // Field directions:[[I
       3: areturn

  public static final int shortestPath(int[][], int);
    Code:
       0: aload_0
       1: ldc           #50                 // String grid
       3: invokestatic  #56                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: checkcast     #58                 // class "[Ljava/lang/Object;"
      10: arraylength
      11: istore_2
      12: aload_0
      13: iconst_0
      14: aaload
      15: arraylength
      16: istore_3
      17: iconst_0
      18: istore        5
      20: aload_0
      21: checkcast     #58                 // class "[Ljava/lang/Object;"
      24: arraylength
      25: istore        6
      27: iload         6
      29: anewarray     #43                 // class "[[I"
      32: astore        7
      34: iload         5
      36: iload         6
      38: if_icmpge     118
      41: iload         5
      43: istore        8
      45: aload         7
      47: iload         8
      49: iconst_0
      50: istore        9
      52: aload_0
      53: iconst_0
      54: aaload
      55: arraylength
      56: istore        10
      58: iload         10
      60: anewarray     #8                  // class "[I"
      63: astore        11
      65: istore        19
      67: astore        18
      69: iload         9
      71: iload         10
      73: if_icmpge     105
      76: iload         9
      78: istore        12
      80: aload         11
      82: iload         12
      84: aload_0
      85: iconst_0
      86: aaload
      87: arraylength
      88: aload_0
      89: checkcast     #58                 // class "[Ljava/lang/Object;"
      92: arraylength
      93: imul
      94: iconst_1
      95: iadd
      96: newarray       int
      98: aastore
      99: iinc          9, 1
     102: goto          69
     105: aload         18
     107: iload         19
     109: aload         11
     111: aastore
     112: iinc          5, 1
     115: goto          34
     118: aload         7
     120: astore        4
     122: new           #60                 // class java/util/LinkedList
     125: dup
     126: invokespecial #62                 // Method java/util/LinkedList."<init>":()V
     129: checkcast     #64                 // class java/util/Queue
     132: astore        5
     134: aload         5
     136: iconst_4
     137: newarray       int
     139: astore        6
     141: aload         6
     143: iconst_0
     144: iconst_0
     145: iastore
     146: aload         6
     148: iconst_1
     149: iconst_0
     150: iastore
     151: aload         6
     153: iconst_2
     154: iload_1
     155: iastore
     156: aload         6
     158: iconst_3
     159: iconst_0
     160: iastore
     161: aload         6
     163: invokeinterface #68,  2           // InterfaceMethod java/util/Queue.add:(Ljava/lang/Object;)Z
     168: pop
     169: aload         5
     171: invokeinterface #72,  1           // InterfaceMethod java/util/Queue.isEmpty:()Z
     176: ifne          476
     179: aload         5
     181: invokeinterface #76,  1           // InterfaceMethod java/util/Queue.poll:()Ljava/lang/Object;
     186: checkcast     #8                  // class "[I"
     189: astore        6
     191: aload         6
     193: iconst_0
     194: iaload
     195: istore        7
     197: aload         6
     199: iconst_1
     200: iaload
     201: istore        8
     203: aload         6
     205: iconst_2
     206: iaload
     207: istore        9
     209: aload         6
     211: iconst_3
     212: iaload
     213: istore        10
     215: aload         4
     217: iload         7
     219: aaload
     220: iload         8
     222: aaload
     223: iload         9
     225: iaload
     226: iconst_1
     227: if_icmpeq     169
     230: aload         4
     232: iload         7
     234: aaload
     235: iload         8
     237: aaload
     238: iload         9
     240: iconst_1
     241: iastore
     242: iload         7
     244: iload_2
     245: iconst_1
     246: isub
     247: if_icmpne     261
     250: iload         8
     252: iload_3
     253: iconst_1
     254: isub
     255: if_icmpne     261
     258: iload         10
     260: ireturn
     261: getstatic     #49                 // Field directions:[[I
     264: astore        11
     266: iconst_0
     267: istore        12
     269: aload         11
     271: checkcast     #58                 // class "[Ljava/lang/Object;"
     274: arraylength
     275: istore        13
     277: iload         12
     279: iload         13
     281: if_icmpge     169
     284: aload         11
     286: iload         12
     288: aaload
     289: astore        14
     291: iload         7
     293: aload         14
     295: iconst_0
     296: iaload
     297: iadd
     298: istore        15
     300: iload         8
     302: aload         14
     304: iconst_1
     305: iaload
     306: iadd
     307: istore        16
     309: iconst_0
     310: iload         15
     312: if_icmpgt     329
     315: iload         15
     317: iload_2
     318: if_icmpge     325
     321: iconst_1
     322: goto          330
     325: iconst_0
     326: goto          330
     329: iconst_0
     330: ifeq          470
     333: iconst_0
     334: iload         16
     336: if_icmpgt     353
     339: iload         16
     341: iload_3
     342: if_icmpge     349
     345: iconst_1
     346: goto          354
     349: iconst_0
     350: goto          354
     353: iconst_0
     354: ifeq          470
     357: aload_0
     358: iload         15
     360: aaload
     361: iload         16
     363: iaload
     364: iconst_1
     365: if_icmpne     419
     368: iload         9
     370: ifle          419
     373: aload         5
     375: iconst_4
     376: newarray       int
     378: astore        17
     380: aload         17
     382: iconst_0
     383: iload         15
     385: iastore
     386: aload         17
     388: iconst_1
     389: iload         16
     391: iastore
     392: aload         17
     394: iconst_2
     395: iload         9
     397: iconst_1
     398: isub
     399: iastore
     400: aload         17
     402: iconst_3
     403: iload         10
     405: iconst_1
     406: iadd
     407: iastore
     408: aload         17
     410: invokeinterface #68,  2           // InterfaceMethod java/util/Queue.add:(Ljava/lang/Object;)Z
     415: pop
     416: goto          470
     419: aload_0
     420: iload         15
     422: aaload
     423: iload         16
     425: iaload
     426: ifne          470
     429: aload         5
     431: iconst_4
     432: newarray       int
     434: astore        17
     436: aload         17
     438: iconst_0
     439: iload         15
     441: iastore
     442: aload         17
     444: iconst_1
     445: iload         16
     447: iastore
     448: aload         17
     450: iconst_2
     451: iload         9
     453: iastore
     454: aload         17
     456: iconst_3
     457: iload         10
     459: iconst_1
     460: iadd
     461: iastore
     462: aload         17
     464: invokeinterface #68,  2           // InterfaceMethod java/util/Queue.add:(Ljava/lang/Object;)Z
     469: pop
     470: iinc          12, 1
     473: goto          277
     476: iconst_m1
     477: ireturn

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

  static {};
    Code:
       0: iconst_4
       1: anewarray     #8                  // class "[I"
       4: astore_0
       5: aload_0
       6: iconst_0
       7: iconst_2
       8: newarray       int
      10: astore_1
      11: aload_1
      12: iconst_0
      13: iconst_1
      14: iastore
      15: aload_1
      16: iconst_1
      17: iconst_0
      18: iastore
      19: aload_1
      20: aastore
      21: aload_0
      22: iconst_1
      23: iconst_2
      24: newarray       int
      26: astore_1
      27: aload_1
      28: iconst_0
      29: iconst_m1
      30: iastore
      31: aload_1
      32: iconst_1
      33: iconst_0
      34: iastore
      35: aload_1
      36: aastore
      37: aload_0
      38: iconst_2
      39: iconst_2
      40: newarray       int
      42: astore_1
      43: aload_1
      44: iconst_0
      45: iconst_0
      46: iastore
      47: aload_1
      48: iconst_1
      49: iconst_m1
      50: iastore
      51: aload_1
      52: aastore
      53: aload_0
      54: iconst_3
      55: iconst_2
      56: newarray       int
      58: astore_1
      59: aload_1
      60: iconst_0
      61: iconst_0
      62: iastore
      63: aload_1
      64: iconst_1
      65: iconst_1
      66: iastore
      67: aload_1
      68: aastore
      69: aload_0
      70: putstatic     #49                 // Field directions:[[I
      73: return
}


package wk269

//
fun main() {
    val result = Problem2().getAverages(intArrayOf(7, 4, 3, 9, 1, 8, 5, 2, 6), 3)
//    val result = Problem2().getAverages(intArr, 10)
    println(result)
}

class Problem2 {
    fun getAverages(nums: IntArray, k: Int): IntArray {
        if (k * 2 > nums.size - 1) return IntArray(nums.size) { -1 }
        var initSum: Long = 0
        val result = IntArray(nums.size)

        for (index in 0..k * 2) {
            initSum += nums[index]
        }

        result[k] = (initSum / (k * 2 + 1)).toInt()

        for (index in nums.indices) {
            val prevIndex = index - 1 - k
            val nextIndex = index + k
            if (index == k) continue
            if (prevIndex < 0 || nextIndex >= nums.size) {
                result[index] = -1
                continue
            }
            val prev = nums[prevIndex]
            val next = nums[nextIndex]
            initSum = initSum - prev + next
            result[index] = (initSum / (k * 2 + 1)).toInt()
        }
        return result
    }
}

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

  public final int[] getAverages(int[], int);
    Code:
       0: aload_1
       1: ldc           #15                 // String nums
       3: invokestatic  #21                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: iload_2
       7: iconst_2
       8: imul
       9: aload_1
      10: arraylength
      11: iconst_1
      12: isub
      13: if_icmple     52
      16: iconst_0
      17: istore_3
      18: aload_1
      19: arraylength
      20: istore        4
      22: iload         4
      24: newarray       int
      26: astore        5
      28: iload_3
      29: iload         4
      31: if_icmpge     49
      34: iload_3
      35: istore        6
      37: aload         5
      39: iload         6
      41: iconst_m1
      42: iastore
      43: iinc          3, 1
      46: goto          28
      49: aload         5
      51: areturn
      52: lconst_0
      53: lstore_3
      54: aload_1
      55: arraylength
      56: newarray       int
      58: astore        5
      60: iconst_0
      61: istore        6
      63: iload_2
      64: iconst_2
      65: imul
      66: istore        7
      68: iload         6
      70: iload         7
      72: if_icmpgt     96
      75: lload_3
      76: aload_1
      77: iload         6
      79: iaload
      80: i2l
      81: ladd
      82: lstore_3
      83: iload         6
      85: iload         7
      87: if_icmpeq     96
      90: iinc          6, 1
      93: goto          75
      96: aload         5
      98: iload_2
      99: lload_3
     100: iload_2
     101: iconst_2
     102: imul
     103: iconst_1
     104: iadd
     105: i2l
     106: ldiv
     107: l2i
     108: iastore
     109: iconst_0
     110: istore        6
     112: aload_1
     113: arraylength
     114: istore        7
     116: iload         6
     118: iload         7
     120: if_icmpge     206
     123: iload         6
     125: iconst_1
     126: isub
     127: iload_2
     128: isub
     129: istore        8
     131: iload         6
     133: iload_2
     134: iadd
     135: istore        9
     137: iload         6
     139: iload_2
     140: if_icmpeq     200
     143: iload         8
     145: iflt          155
     148: iload         9
     150: aload_1
     151: arraylength
     152: if_icmplt     164
     155: aload         5
     157: iload         6
     159: iconst_m1
     160: iastore
     161: goto          200
     164: aload_1
     165: iload         8
     167: iaload
     168: istore        10
     170: aload_1
     171: iload         9
     173: iaload
     174: istore        11
     176: lload_3
     177: iload         10
     179: i2l
     180: lsub
     181: iload         11
     183: i2l
     184: ladd
     185: lstore_3
     186: aload         5
     188: iload         6
     190: lload_3
     191: iload_2
     192: iconst_2
     193: imul
     194: iconst_1
     195: iadd
     196: i2l
     197: ldiv
     198: l2i
     199: iastore
     200: iinc          6, 1
     203: goto          116
     206: aload         5
     208: areturn
}

Compiled from "Problem2.kt"
public final class wk269.Problem2Kt {
  public static final void main();
    Code:
       0: new           #8                  // class wk269/Problem2
       3: dup
       4: invokespecial #11                 // Method wk269/Problem2."<init>":()V
       7: bipush        9
       9: newarray       int
      11: astore_1
      12: aload_1
      13: iconst_0
      14: bipush        7
      16: iastore
      17: aload_1
      18: iconst_1
      19: iconst_4
      20: iastore
      21: aload_1
      22: iconst_2
      23: iconst_3
      24: iastore
      25: aload_1
      26: iconst_3
      27: bipush        9
      29: iastore
      30: aload_1
      31: iconst_4
      32: iconst_1
      33: iastore
      34: aload_1
      35: iconst_5
      36: bipush        8
      38: iastore
      39: aload_1
      40: bipush        6
      42: iconst_5
      43: iastore
      44: aload_1
      45: bipush        7
      47: iconst_2
      48: iastore
      49: aload_1
      50: bipush        8
      52: bipush        6
      54: iastore
      55: aload_1
      56: iconst_3
      57: invokevirtual #15                 // Method wk269/Problem2.getAverages:([II)[I
      60: astore_0
      61: getstatic     #21                 // Field java/lang/System.out:Ljava/io/PrintStream;
      64: aload_0
      65: invokevirtual #27                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
      68: return

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


package wk269

import kotlin.math.min

fun main() {
    val result = Problem3().minimumDeletions(intArrayOf(1, 2))
    println(result)
}

class Problem3 {
    fun minimumDeletions(nums: IntArray): Int {
        // Find indices of min and max elements
        var minIndex = 0
        var maxIndex = 0
        for ((index, num) in nums.withIndex()) {
            if (num > nums[maxIndex]) maxIndex = index
            if (num < nums[minIndex]) minIndex = index
        }

        // Find in what order min and max elements appear in the array
        val first = if (minIndex < maxIndex) minIndex else maxIndex
        val second = if (minIndex > maxIndex) minIndex else maxIndex

        // There are 3 cases when we can remove 2 elements:

        // 1. Remove all elements from the first appeared element to the right.
        // It will include both elements in any case:
        //   _   _
        // 2,1,3,6,5
        //  |_______
        val removeLessToTheLeft = first + 1

        // 2. Remove all elements from the second appeared element to the left.
        // It will include both elements in any case:
        //   _   _
        // 2,1,3,6,5
        // _______|
        val removeLessToTheRight = nums.size - first

        // 3. Remove all elements from the first to the left
        // and from the second to the right
        //   _   _
        // 2,1,3,6,5
        // ___| |___
        val removeBiggerToTheLeft = second + 1
        val removeBiggerToTheRight = nums.size - second

        // Then just find the minimum between all 3 cases:
        return min(
            removeLessToTheLeft + removeBiggerToTheRight,
            min(removeLessToTheRight, removeBiggerToTheLeft)
        )
    }
}

Compiled from "Problem3.kt"
public final class wk269.Problem3Kt {
  public static final void main();
    Code:
       0: new           #8                  // class wk269/Problem3
       3: dup
       4: invokespecial #11                 // Method wk269/Problem3."<init>":()V
       7: iconst_2
       8: newarray       int
      10: astore_1
      11: aload_1
      12: iconst_0
      13: iconst_1
      14: iastore
      15: aload_1
      16: iconst_1
      17: iconst_2
      18: iastore
      19: aload_1
      20: invokevirtual #15                 // Method wk269/Problem3.minimumDeletions:([I)I
      23: istore_0
      24: getstatic     #21                 // Field java/lang/System.out:Ljava/io/PrintStream;
      27: iload_0
      28: invokevirtual #27                 // Method java/io/PrintStream.println:(I)V
      31: return

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

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

  public final int minimumDeletions(int[]);
    Code:
       0: aload_1
       1: ldc           #15                 // String nums
       3: invokestatic  #21                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: iconst_0
       7: istore_2
       8: iconst_0
       9: istore_3
      10: iconst_0
      11: istore        4
      13: aload_1
      14: arraylength
      15: istore        5
      17: iload         4
      19: iload         5
      21: if_icmpge     62
      24: iload         4
      26: istore        6
      28: aload_1
      29: iload         4
      31: iaload
      32: istore        7
      34: iload         7
      36: aload_1
      37: iload_3
      38: iaload
      39: if_icmple     45
      42: iload         6
      44: istore_3
      45: iload         7
      47: aload_1
      48: iload_2
      49: iaload
      50: if_icmpge     56
      53: iload         6
      55: istore_2
      56: iinc          4, 1
      59: goto          17
      62: iload_2
      63: iload_3
      64: if_icmpge     71
      67: iload_2
      68: goto          72
      71: iload_3
      72: istore        4
      74: iload_2
      75: iload_3
      76: if_icmple     83
      79: iload_2
      80: goto          84
      83: iload_3
      84: istore        5
      86: iload         4
      88: iconst_1
      89: iadd
      90: istore        6
      92: aload_1
      93: arraylength
      94: iload         4
      96: isub
      97: istore        7
      99: iload         5
     101: iconst_1
     102: iadd
     103: istore        8
     105: aload_1
     106: arraylength
     107: iload         5
     109: isub
     110: istore        9
     112: nop
     113: iload         6
     115: iload         9
     117: iadd
     118: iload         7
     120: iload         8
     122: invokestatic  #27                 // Method java/lang/Math.min:(II)I
     125: invokestatic  #27                 // Method java/lang/Math.min:(II)I
     128: ireturn
}


package feb_challenge2023

import java.util.*

fun main() {
    val result =
        ShortestPathWithAltColors()
            .shortestAlternatingPaths(
                5,
                arrayOf(intArrayOf(0, 1), intArrayOf(1, 2), intArrayOf(2, 3), intArrayOf(3, 4)),
                arrayOf(intArrayOf(1, 2), intArrayOf(2, 3), intArrayOf(3, 1))
            )

//    val result =
//        ShortestPathWithAltColors()
//            .shortestAlternatingPaths(
//                3,
//                arrayOf(intArrayOf(0, 1), intArrayOf(0, 2)),
//                arrayOf(intArrayOf(1,0))
//            )
    println(result)
}

class ShortestPathWithAltColors {
    private val graph = mutableMapOf<Int, MutableList<Point>>()
    private var graphSize = 0
    private var result = intArrayOf()
    fun shortestAlternatingPaths(n: Int, redEdges: Array<IntArray>, blueEdges: Array<IntArray>): IntArray {
        graphSize = n
        result = IntArray(n) { -1 }
        buildGraph(redEdges, blueEdges)
        bfs()

        return result
    }

    private fun bfs() {
        val visited = mutableSetOf<String>()
        val queue: Queue<Point> = LinkedList()
        queue.add(Point(0, -1))
        visited.add("c-1v0")
        result[0] = 0

        var distance = -1
        while (!queue.isEmpty()) {
            distance++
            val size = queue.size
            for (i in 0 until size) {
                val curr = queue.poll()
                if (result[curr.value] == -1) result[curr.value] = distance
                val children = graph.getOrDefault(curr.value, mutableListOf())
                for (child in children) {
                    val visitedKey = "c${child.color}v${child.value}"
                    if (!visited.contains(visitedKey) and (curr.color != child.color)) {
                        queue.add(child)
                        visited.add(visitedKey)
                    }
                }
            }
        }
    }

    private fun buildGraph(redEdges: Array<IntArray>, blueEdges: Array<IntArray>) {
        for (redEdge: IntArray in redEdges) processEdge(0, redEdge)
        for (blueEdge: IntArray in blueEdges) processEdge(1, blueEdge)
    }

    private fun processEdge(color: Int, edge: IntArray) {
        val from = edge[0]
        val to = edge[1]
        val existingEdges = graph.getOrDefault(from, mutableListOf())
        existingEdges.add(Point(to, color))
        graph[from] = existingEdges
    }

    internal data class Point(val value: Int = -1, val color: Int = -1)
}

Compiled from "ShortestPathWithAltColors.kt"
public final class feb_challenge2023.ShortestPathWithAltColors$Point {
  private final int value;

  private final int color;

  public feb_challenge2023.ShortestPathWithAltColors$Point(int, int);
    Code:
       0: aload_0
       1: invokespecial #9                  // Method java/lang/Object."<init>":()V
       4: aload_0
       5: iload_1
       6: putfield      #13                 // Field value:I
       9: aload_0
      10: iload_2
      11: putfield      #16                 // Field color:I
      14: return

  public feb_challenge2023.ShortestPathWithAltColors$Point(int, int, int, kotlin.jvm.internal.DefaultConstructorMarker);
    Code:
       0: iload_3
       1: iconst_1
       2: iand
       3: ifeq          8
       6: iconst_m1
       7: istore_1
       8: iload_3
       9: iconst_2
      10: iand
      11: ifeq          16
      14: iconst_m1
      15: istore_2
      16: aload_0
      17: iload_1
      18: iload_2
      19: invokespecial #21                 // Method "<init>":(II)V
      22: return

  public final int getValue();
    Code:
       0: aload_0
       1: getfield      #13                 // Field value:I
       4: ireturn

  public final int getColor();
    Code:
       0: aload_0
       1: getfield      #16                 // Field color:I
       4: ireturn

  public final int component1();
    Code:
       0: aload_0
       1: getfield      #13                 // Field value:I
       4: ireturn

  public final int component2();
    Code:
       0: aload_0
       1: getfield      #16                 // Field color:I
       4: ireturn

  public final feb_challenge2023.ShortestPathWithAltColors$Point copy(int, int);
    Code:
       0: new           #2                  // class feb_challenge2023/ShortestPathWithAltColors$Point
       3: dup
       4: iload_1
       5: iload_2
       6: invokespecial #21                 // Method "<init>":(II)V
       9: areturn

  public static feb_challenge2023.ShortestPathWithAltColors$Point copy$default(feb_challenge2023.ShortestPathWithAltColors$Point, int, int, int, java.lang.Object);
    Code:
       0: iload_3
       1: iconst_1
       2: iand
       3: ifeq          11
       6: aload_0
       7: getfield      #13                 // Field value:I
      10: istore_1
      11: iload_3
      12: iconst_2
      13: iand
      14: ifeq          22
      17: aload_0
      18: getfield      #16                 // Field color:I
      21: istore_2
      22: aload_0
      23: iload_1
      24: iload_2
      25: invokevirtual #33                 // Method copy:(II)Lfeb_challenge2023/ShortestPathWithAltColors$Point;
      28: areturn

  public java.lang.String toString();
    Code:
       0: new           #37                 // class java/lang/StringBuilder
       3: dup
       4: invokespecial #38                 // Method java/lang/StringBuilder."<init>":()V
       7: ldc           #40                 // String Point(value=
       9: invokevirtual #44                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      12: aload_0
      13: getfield      #13                 // Field value:I
      16: invokevirtual #47                 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
      19: ldc           #49                 // String , color=
      21: invokevirtual #44                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      24: aload_0
      25: getfield      #16                 // Field color:I
      28: invokevirtual #47                 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
      31: bipush        41
      33: invokevirtual #52                 // Method java/lang/StringBuilder.append:(C)Ljava/lang/StringBuilder;
      36: invokevirtual #54                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
      39: areturn

  public int hashCode();
    Code:
       0: aload_0
       1: getfield      #13                 // Field value:I
       4: invokestatic  #60                 // Method java/lang/Integer.hashCode:(I)I
       7: istore_1
       8: iload_1
       9: bipush        31
      11: imul
      12: aload_0
      13: getfield      #16                 // Field color:I
      16: invokestatic  #60                 // Method java/lang/Integer.hashCode:(I)I
      19: iadd
      20: istore_1
      21: iload_1
      22: ireturn

  public boolean equals(java.lang.Object);
    Code:
       0: aload_0
       1: aload_1
       2: if_acmpne     7
       5: iconst_1
       6: ireturn
       7: aload_1
       8: instanceof    #2                  // class feb_challenge2023/ShortestPathWithAltColors$Point
      11: ifne          16
      14: iconst_0
      15: ireturn
      16: aload_1
      17: checkcast     #2                  // class feb_challenge2023/ShortestPathWithAltColors$Point
      20: astore_2
      21: aload_0
      22: getfield      #13                 // Field value:I
      25: aload_2
      26: getfield      #13                 // Field value:I
      29: if_icmpeq     34
      32: iconst_0
      33: ireturn
      34: aload_0
      35: getfield      #16                 // Field color:I
      38: aload_2
      39: getfield      #16                 // Field color:I
      42: if_icmpeq     47
      45: iconst_0
      46: ireturn
      47: iconst_1
      48: ireturn

  public feb_challenge2023.ShortestPathWithAltColors$Point();
    Code:
       0: aload_0
       1: iconst_0
       2: iconst_0
       3: iconst_3
       4: aconst_null
       5: invokespecial #68                 // Method "<init>":(IIILkotlin/jvm/internal/DefaultConstructorMarker;)V
       8: return
}

Compiled from "ShortestPathWithAltColors.kt"
public final class feb_challenge2023.ShortestPathWithAltColorsKt {
  public static final void main();
    Code:
       0: new           #8                  // class feb_challenge2023/ShortestPathWithAltColors
       3: dup
       4: invokespecial #11                 // Method feb_challenge2023/ShortestPathWithAltColors."<init>":()V
       7: iconst_5
       8: iconst_4
       9: anewarray     #13                 // class "[I"
      12: astore_1
      13: aload_1
      14: iconst_0
      15: iconst_2
      16: newarray       int
      18: astore_2
      19: aload_2
      20: iconst_0
      21: iconst_0
      22: iastore
      23: aload_2
      24: iconst_1
      25: iconst_1
      26: iastore
      27: aload_2
      28: aastore
      29: aload_1
      30: iconst_1
      31: iconst_2
      32: newarray       int
      34: astore_2
      35: aload_2
      36: iconst_0
      37: iconst_1
      38: iastore
      39: aload_2
      40: iconst_1
      41: iconst_2
      42: iastore
      43: aload_2
      44: aastore
      45: aload_1
      46: iconst_2
      47: iconst_2
      48: newarray       int
      50: astore_2
      51: aload_2
      52: iconst_0
      53: iconst_2
      54: iastore
      55: aload_2
      56: iconst_1
      57: iconst_3
      58: iastore
      59: aload_2
      60: aastore
      61: aload_1
      62: iconst_3
      63: iconst_2
      64: newarray       int
      66: astore_2
      67: aload_2
      68: iconst_0
      69: iconst_3
      70: iastore
      71: aload_2
      72: iconst_1
      73: iconst_4
      74: iastore
      75: aload_2
      76: aastore
      77: aload_1
      78: iconst_3
      79: anewarray     #13                 // class "[I"
      82: astore_1
      83: aload_1
      84: iconst_0
      85: iconst_2
      86: newarray       int
      88: astore_2
      89: aload_2
      90: iconst_0
      91: iconst_1
      92: iastore
      93: aload_2
      94: iconst_1
      95: iconst_2
      96: iastore
      97: aload_2
      98: aastore
      99: aload_1
     100: iconst_1
     101: iconst_2
     102: newarray       int
     104: astore_2
     105: aload_2
     106: iconst_0
     107: iconst_2
     108: iastore
     109: aload_2
     110: iconst_1
     111: iconst_3
     112: iastore
     113: aload_2
     114: aastore
     115: aload_1
     116: iconst_2
     117: iconst_2
     118: newarray       int
     120: astore_2
     121: aload_2
     122: iconst_0
     123: iconst_3
     124: iastore
     125: aload_2
     126: iconst_1
     127: iconst_1
     128: iastore
     129: aload_2
     130: aastore
     131: aload_1
     132: invokevirtual #17                 // Method feb_challenge2023/ShortestPathWithAltColors.shortestAlternatingPaths:(I[[I[[I)[I
     135: astore_0
     136: getstatic     #23                 // Field java/lang/System.out:Ljava/io/PrintStream;
     139: aload_0
     140: invokevirtual #29                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
     143: return

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

Compiled from "ShortestPathWithAltColors.kt"
public final class feb_challenge2023.ShortestPathWithAltColors {
  private final java.util.Map<java.lang.Integer, java.util.List<feb_challenge2023.ShortestPathWithAltColors$Point>> graph;

  private int graphSize;

  private int[] result;

  public feb_challenge2023.ShortestPathWithAltColors();
    Code:
       0: aload_0
       1: invokespecial #8                  // Method java/lang/Object."<init>":()V
       4: aload_0
       5: new           #10                 // class java/util/LinkedHashMap
       8: dup
       9: invokespecial #11                 // Method java/util/LinkedHashMap."<init>":()V
      12: checkcast     #13                 // class java/util/Map
      15: putfield      #17                 // Field graph:Ljava/util/Map;
      18: aload_0
      19: iconst_0
      20: newarray       int
      22: putfield      #21                 // Field result:[I
      25: return

  public final int[] shortestAlternatingPaths(int, int[][], int[][]);
    Code:
       0: aload_2
       1: ldc           #28                 // String redEdges
       3: invokestatic  #34                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_3
       7: ldc           #36                 // String blueEdges
       9: invokestatic  #34                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
      12: aload_0
      13: iload_1
      14: putfield      #40                 // Field graphSize:I
      17: aload_0
      18: iconst_0
      19: istore        4
      21: iload_1
      22: newarray       int
      24: astore        5
      26: astore        7
      28: iload         4
      30: iload_1
      31: if_icmpge     50
      34: iload         4
      36: istore        6
      38: aload         5
      40: iload         6
      42: iconst_m1
      43: iastore
      44: iinc          4, 1
      47: goto          28
      50: aload         7
      52: aload         5
      54: putfield      #21                 // Field result:[I
      57: aload_0
      58: aload_2
      59: aload_3
      60: invokespecial #44                 // Method buildGraph:([[I[[I)V
      63: aload_0
      64: invokespecial #47                 // Method bfs:()V
      67: aload_0
      68: getfield      #21                 // Field result:[I
      71: areturn

  private final void bfs();
    Code:
       0: new           #53                 // class java/util/LinkedHashSet
       3: dup
       4: invokespecial #54                 // Method java/util/LinkedHashSet."<init>":()V
       7: checkcast     #56                 // class java/util/Set
      10: astore_1
      11: new           #58                 // class java/util/LinkedList
      14: dup
      15: invokespecial #59                 // Method java/util/LinkedList."<init>":()V
      18: checkcast     #61                 // class java/util/Queue
      21: astore_2
      22: aload_2
      23: new           #63                 // class feb_challenge2023/ShortestPathWithAltColors$Point
      26: dup
      27: iconst_0
      28: iconst_m1
      29: invokespecial #66                 // Method feb_challenge2023/ShortestPathWithAltColors$Point."<init>":(II)V
      32: invokeinterface #70,  2           // InterfaceMethod java/util/Queue.add:(Ljava/lang/Object;)Z
      37: pop
      38: aload_1
      39: ldc           #72                 // String c-1v0
      41: invokeinterface #73,  2           // InterfaceMethod java/util/Set.add:(Ljava/lang/Object;)Z
      46: pop
      47: aload_0
      48: getfield      #21                 // Field result:[I
      51: iconst_0
      52: iconst_0
      53: iastore
      54: iconst_m1
      55: istore_3
      56: aload_2
      57: invokeinterface #77,  1           // InterfaceMethod java/util/Queue.isEmpty:()Z
      62: ifne          288
      65: iinc          3, 1
      68: aload_2
      69: invokeinterface #81,  1           // InterfaceMethod java/util/Queue.size:()I
      74: istore        4
      76: iconst_0
      77: istore        5
      79: iload         5
      81: iload         4
      83: if_icmpge     56
      86: aload_2
      87: invokeinterface #85,  1           // InterfaceMethod java/util/Queue.poll:()Ljava/lang/Object;
      92: checkcast     #63                 // class feb_challenge2023/ShortestPathWithAltColors$Point
      95: astore        6
      97: aload_0
      98: getfield      #21                 // Field result:[I
     101: aload         6
     103: invokevirtual #88                 // Method feb_challenge2023/ShortestPathWithAltColors$Point.getValue:()I
     106: iaload
     107: iconst_m1
     108: if_icmpne     122
     111: aload_0
     112: getfield      #21                 // Field result:[I
     115: aload         6
     117: invokevirtual #88                 // Method feb_challenge2023/ShortestPathWithAltColors$Point.getValue:()I
     120: iload_3
     121: iastore
     122: aload_0
     123: getfield      #17                 // Field graph:Ljava/util/Map;
     126: aload         6
     128: invokevirtual #88                 // Method feb_challenge2023/ShortestPathWithAltColors$Point.getValue:()I
     131: invokestatic  #94                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     134: new           #96                 // class java/util/ArrayList
     137: dup
     138: invokespecial #97                 // Method java/util/ArrayList."<init>":()V
     141: checkcast     #99                 // class java/util/List
     144: invokeinterface #103,  3          // InterfaceMethod java/util/Map.getOrDefault:(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
     149: checkcast     #99                 // class java/util/List
     152: astore        7
     154: aload         7
     156: invokeinterface #107,  1          // InterfaceMethod java/util/List.iterator:()Ljava/util/Iterator;
     161: astore        8
     163: aload         8
     165: invokeinterface #112,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
     170: ifeq          282
     173: aload         8
     175: invokeinterface #115,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     180: checkcast     #63                 // class feb_challenge2023/ShortestPathWithAltColors$Point
     183: astore        9
     185: new           #117                // class java/lang/StringBuilder
     188: dup
     189: invokespecial #118                // Method java/lang/StringBuilder."<init>":()V
     192: bipush        99
     194: invokevirtual #122                // Method java/lang/StringBuilder.append:(C)Ljava/lang/StringBuilder;
     197: aload         9
     199: invokevirtual #125                // Method feb_challenge2023/ShortestPathWithAltColors$Point.getColor:()I
     202: invokevirtual #128                // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
     205: bipush        118
     207: invokevirtual #122                // Method java/lang/StringBuilder.append:(C)Ljava/lang/StringBuilder;
     210: aload         9
     212: invokevirtual #88                 // Method feb_challenge2023/ShortestPathWithAltColors$Point.getValue:()I
     215: invokevirtual #128                // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
     218: invokevirtual #132                // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
     221: astore        10
     223: aload_1
     224: aload         10
     226: invokeinterface #135,  2          // InterfaceMethod java/util/Set.contains:(Ljava/lang/Object;)Z
     231: ifne          238
     234: iconst_1
     235: goto          239
     238: iconst_0
     239: aload         6
     241: invokevirtual #125                // Method feb_challenge2023/ShortestPathWithAltColors$Point.getColor:()I
     244: aload         9
     246: invokevirtual #125                // Method feb_challenge2023/ShortestPathWithAltColors$Point.getColor:()I
     249: if_icmpeq     256
     252: iconst_1
     253: goto          257
     256: iconst_0
     257: iand
     258: ifeq          163
     261: aload_2
     262: aload         9
     264: invokeinterface #70,  2           // InterfaceMethod java/util/Queue.add:(Ljava/lang/Object;)Z
     269: pop
     270: aload_1
     271: aload         10
     273: invokeinterface #73,  2           // InterfaceMethod java/util/Set.add:(Ljava/lang/Object;)Z
     278: pop
     279: goto          163
     282: iinc          5, 1
     285: goto          79
     288: return

  private final void buildGraph(int[][], int[][]);
    Code:
       0: iconst_0
       1: istore_3
       2: aload_1
       3: checkcast     #152                // class "[Ljava/lang/Object;"
       6: arraylength
       7: istore        4
       9: iload_3
      10: iload         4
      12: if_icmpge     33
      15: aload_1
      16: iload_3
      17: aaload
      18: astore        5
      20: aload_0
      21: iconst_0
      22: aload         5
      24: invokespecial #156                // Method processEdge:(I[I)V
      27: iinc          3, 1
      30: goto          9
      33: iconst_0
      34: istore_3
      35: aload_2
      36: checkcast     #152                // class "[Ljava/lang/Object;"
      39: arraylength
      40: istore        4
      42: iload_3
      43: iload         4
      45: if_icmpge     66
      48: aload_2
      49: iload_3
      50: aaload
      51: astore        5
      53: aload_0
      54: iconst_1
      55: aload         5
      57: invokespecial #156                // Method processEdge:(I[I)V
      60: iinc          3, 1
      63: goto          42
      66: return

  private final void processEdge(int, int[]);
    Code:
       0: aload_2
       1: iconst_0
       2: iaload
       3: istore_3
       4: aload_2
       5: iconst_1
       6: iaload
       7: istore        4
       9: aload_0
      10: getfield      #17                 // Field graph:Ljava/util/Map;
      13: iload_3
      14: invokestatic  #94                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      17: new           #96                 // class java/util/ArrayList
      20: dup
      21: invokespecial #97                 // Method java/util/ArrayList."<init>":()V
      24: checkcast     #99                 // class java/util/List
      27: invokeinterface #103,  3          // InterfaceMethod java/util/Map.getOrDefault:(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
      32: checkcast     #99                 // class java/util/List
      35: astore        5
      37: aload         5
      39: new           #63                 // class feb_challenge2023/ShortestPathWithAltColors$Point
      42: dup
      43: iload         4
      45: iload_1
      46: invokespecial #66                 // Method feb_challenge2023/ShortestPathWithAltColors$Point."<init>":(II)V
      49: invokeinterface #159,  2          // InterfaceMethod java/util/List.add:(Ljava/lang/Object;)Z
      54: pop
      55: iload_3
      56: invokestatic  #94                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      59: astore        6
      61: aload_0
      62: getfield      #17                 // Field graph:Ljava/util/Map;
      65: aload         6
      67: aload         5
      69: invokeinterface #162,  3          // InterfaceMethod java/util/Map.put:(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
      74: pop
      75: return
}


package bk69

// ["dd","aa","bb","dd","aa","dd","bb","dd","aa","cc","bb","cc","dd","cc"]
fun main() {
    val res = Problem3().longestPalindrome(
        arrayOf(
            "xx", "zz"
        )
    )
    println(res)
}

class Problem3 {
    fun longestPalindrome(words: Array<String>): Int {
        val countMap = words.toList().groupingBy { it }.eachCount() as MutableMap<String, Int>
        var sameMax = 0
        var res = 0
        for (entry in countMap) {
            val str = entry.key
            val reversed = str.reversed()
            val num = entry.value
            val reversedCount = countMap.getOrDefault(str.reversed(), 0)
            if (str == reversed && num == 1) sameMax = 1
            if (num >= 1 && reversedCount >= 1) {
                var minNum = minOf(num, reversedCount)
                if (minNum > 1 && minNum % 2 == 1) minNum--
                res += minNum * if (str == reversed) 2 else 4
                countMap[str] = num - minNum
                if (countMap.containsKey(reversed)) {
                    countMap[reversed] = reversedCount - minNum
                }
            }
        }
        for (entry in countMap) {
            if (entry.key == entry.key.reversed()) sameMax = maxOf(entry.value, sameMax)
        }

        return res + sameMax * 2
    }
}

Compiled from "_Collections.kt"
public final class bk69.Problem3$longestPalindrome$$inlined$groupingBy$1 implements kotlin.collections.Grouping<java.lang.String, java.lang.String> {
  final java.lang.Iterable $this_groupingBy;

  public bk69.Problem3$longestPalindrome$$inlined$groupingBy$1(java.lang.Iterable);
    Code:
       0: aload_0
       1: aload_1
       2: putfield      #18                 // Field $this_groupingBy:Ljava/lang/Iterable;
       5: aload_0
       6: invokespecial #21                 // Method java/lang/Object."<init>":()V
       9: return

  public java.util.Iterator<java.lang.String> sourceIterator();
    Code:
       0: aload_0
       1: getfield      #18                 // Field $this_groupingBy:Ljava/lang/Iterable;
       4: invokeinterface #32,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
       9: areturn

  public java.lang.String keyOf(java.lang.String);
    Code:
       0: aload_1
       1: checkcast     #37                 // class java/lang/String
       4: astore_2
       5: iconst_0
       6: istore_3
       7: aload_2
       8: areturn
}

Compiled from "Problem3.kt"
public final class bk69.Problem3Kt {
  public static final void main();
    Code:
       0: new           #8                  // class bk69/Problem3
       3: dup
       4: invokespecial #11                 // Method bk69/Problem3."<init>":()V
       7: iconst_2
       8: anewarray     #13                 // class java/lang/String
      11: astore_1
      12: aload_1
      13: iconst_0
      14: ldc           #15                 // String xx
      16: aastore
      17: aload_1
      18: iconst_1
      19: ldc           #17                 // String zz
      21: aastore
      22: aload_1
      23: invokevirtual #21                 // Method bk69/Problem3.longestPalindrome:([Ljava/lang/String;)I
      26: istore_0
      27: getstatic     #27                 // Field java/lang/System.out:Ljava/io/PrintStream;
      30: iload_0
      31: invokevirtual #33                 // Method java/io/PrintStream.println:(I)V
      34: return

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

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

  public final int longestPalindrome(java.lang.String[]);
    Code:
       0: aload_1
       1: ldc           #15                 // String words
       3: invokestatic  #21                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_1
       7: invokestatic  #27                 // Method kotlin/collections/ArraysKt.toList:([Ljava/lang/Object;)Ljava/util/List;
      10: checkcast     #29                 // class java/lang/Iterable
      13: astore        4
      15: iconst_0
      16: istore        5
      18: new           #31                 // class bk69/Problem3$longestPalindrome$$inlined$groupingBy$1
      21: dup
      22: aload         4
      24: invokespecial #34                 // Method bk69/Problem3$longestPalindrome$$inlined$groupingBy$1."<init>":(Ljava/lang/Iterable;)V
      27: checkcast     #36                 // class kotlin/collections/Grouping
      30: invokestatic  #42                 // Method kotlin/collections/GroupingKt.eachCount:(Lkotlin/collections/Grouping;)Ljava/util/Map;
      33: dup
      34: ldc           #44                 // String null cannot be cast to non-null type kotlin.collections.MutableMap<kotlin.String, kotlin.Int>
      36: invokestatic  #47                 // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;Ljava/lang/String;)V
      39: invokestatic  #53                 // Method kotlin/jvm/internal/TypeIntrinsics.asMutableMap:(Ljava/lang/Object;)Ljava/util/Map;
      42: astore_2
      43: iconst_0
      44: istore_3
      45: iconst_0
      46: istore        4
      48: aload_2
      49: invokeinterface #59,  1           // InterfaceMethod java/util/Map.entrySet:()Ljava/util/Set;
      54: invokeinterface #65,  1           // InterfaceMethod java/util/Set.iterator:()Ljava/util/Iterator;
      59: astore        5
      61: aload         5
      63: invokeinterface #71,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      68: ifeq          279
      71: aload         5
      73: invokeinterface #75,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      78: checkcast     #77                 // class java/util/Map$Entry
      81: astore        6
      83: aload         6
      85: invokeinterface #80,  1           // InterfaceMethod java/util/Map$Entry.getKey:()Ljava/lang/Object;
      90: checkcast     #82                 // class java/lang/String
      93: astore        7
      95: aload         7
      97: checkcast     #84                 // class java/lang/CharSequence
     100: invokestatic  #90                 // Method kotlin/text/StringsKt.reversed:(Ljava/lang/CharSequence;)Ljava/lang/CharSequence;
     103: invokevirtual #94                 // Method java/lang/Object.toString:()Ljava/lang/String;
     106: astore        8
     108: aload         6
     110: invokeinterface #97,  1           // InterfaceMethod java/util/Map$Entry.getValue:()Ljava/lang/Object;
     115: checkcast     #99                 // class java/lang/Number
     118: invokevirtual #103                // Method java/lang/Number.intValue:()I
     121: istore        9
     123: aload_2
     124: aload         7
     126: checkcast     #84                 // class java/lang/CharSequence
     129: invokestatic  #90                 // Method kotlin/text/StringsKt.reversed:(Ljava/lang/CharSequence;)Ljava/lang/CharSequence;
     132: invokevirtual #94                 // Method java/lang/Object.toString:()Ljava/lang/String;
     135: iconst_0
     136: invokestatic  #109                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     139: invokeinterface #113,  3          // InterfaceMethod java/util/Map.getOrDefault:(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
     144: checkcast     #99                 // class java/lang/Number
     147: invokevirtual #103                // Method java/lang/Number.intValue:()I
     150: istore        10
     152: aload         7
     154: aload         8
     156: invokestatic  #117                // Method kotlin/jvm/internal/Intrinsics.areEqual:(Ljava/lang/Object;Ljava/lang/Object;)Z
     159: ifeq          170
     162: iload         9
     164: iconst_1
     165: if_icmpne     170
     168: iconst_1
     169: istore_3
     170: iload         9
     172: iconst_1
     173: if_icmplt     61
     176: iload         10
     178: iconst_1
     179: if_icmplt     61
     182: iload         9
     184: iload         10
     186: invokestatic  #123                // Method java/lang/Math.min:(II)I
     189: istore        11
     191: iload         11
     193: iconst_1
     194: if_icmple     208
     197: iload         11
     199: iconst_2
     200: irem
     201: iconst_1
     202: if_icmpne     208
     205: iinc          11, -1
     208: iload         4
     210: iload         11
     212: aload         7
     214: aload         8
     216: invokestatic  #117                // Method kotlin/jvm/internal/Intrinsics.areEqual:(Ljava/lang/Object;Ljava/lang/Object;)Z
     219: ifeq          226
     222: iconst_2
     223: goto          227
     226: iconst_4
     227: imul
     228: iadd
     229: istore        4
     231: aload_2
     232: aload         7
     234: iload         9
     236: iload         11
     238: isub
     239: invokestatic  #109                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     242: invokeinterface #126,  3          // InterfaceMethod java/util/Map.put:(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
     247: pop
     248: aload_2
     249: aload         8
     251: invokeinterface #130,  2          // InterfaceMethod java/util/Map.containsKey:(Ljava/lang/Object;)Z
     256: ifeq          61
     259: aload_2
     260: aload         8
     262: iload         10
     264: iload         11
     266: isub
     267: invokestatic  #109                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     270: invokeinterface #126,  3          // InterfaceMethod java/util/Map.put:(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
     275: pop
     276: goto          61
     279: aload_2
     280: invokeinterface #59,  1           // InterfaceMethod java/util/Map.entrySet:()Ljava/util/Set;
     285: invokeinterface #65,  1           // InterfaceMethod java/util/Set.iterator:()Ljava/util/Iterator;
     290: astore        5
     292: aload         5
     294: invokeinterface #71,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     299: ifeq          367
     302: aload         5
     304: invokeinterface #75,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     309: checkcast     #77                 // class java/util/Map$Entry
     312: astore        6
     314: aload         6
     316: invokeinterface #80,  1           // InterfaceMethod java/util/Map$Entry.getKey:()Ljava/lang/Object;
     321: aload         6
     323: invokeinterface #80,  1           // InterfaceMethod java/util/Map$Entry.getKey:()Ljava/lang/Object;
     328: checkcast     #82                 // class java/lang/String
     331: checkcast     #84                 // class java/lang/CharSequence
     334: invokestatic  #90                 // Method kotlin/text/StringsKt.reversed:(Ljava/lang/CharSequence;)Ljava/lang/CharSequence;
     337: invokevirtual #94                 // Method java/lang/Object.toString:()Ljava/lang/String;
     340: invokestatic  #117                // Method kotlin/jvm/internal/Intrinsics.areEqual:(Ljava/lang/Object;Ljava/lang/Object;)Z
     343: ifeq          292
     346: aload         6
     348: invokeinterface #97,  1           // InterfaceMethod java/util/Map$Entry.getValue:()Ljava/lang/Object;
     353: checkcast     #99                 // class java/lang/Number
     356: invokevirtual #103                // Method java/lang/Number.intValue:()I
     359: iload_3
     360: invokestatic  #133                // Method java/lang/Math.max:(II)I
     363: istore_3
     364: goto          292
     367: iload         4
     369: iload_3
     370: iconst_2
     371: imul
     372: iadd
     373: ireturn
}


package bk66

fun main() {
    val result = Problem3().minCost(intArrayOf(0, 3), intArrayOf(2, 3), intArrayOf(5, 4, 3), intArrayOf(8, 2, 6, 7))
    println(result)
}

class Problem3 {
    fun minCost(startPos: IntArray, homePos: IntArray, rowCosts: IntArray, colCosts: IntArray): Int {
        val startI = startPos[0]
        val startJ = startPos[1]
        val homeI = homePos[0]
        val homeJ = homePos[1]
        if (startI == homeI && startJ == homeJ) return 0
        var result = 0

        if (startJ < homeJ) {
            for (i in startJ + 1..homeJ) result += colCosts[i]
        } else {
            for (i in startJ - 1 downTo homeJ) result += colCosts[i]
        }

        if (startI < homeI) {
            for (i in startI + 1..homeI) result += rowCosts[i]
        } else {
            for (i in startI - 1 downTo homeI) result += rowCosts[i]
        }
        return result
    }
}

Compiled from "Problem3.kt"
public final class bk66.Problem3Kt {
  public static final void main();
    Code:
       0: new           #8                  // class bk66/Problem3
       3: dup
       4: invokespecial #11                 // Method bk66/Problem3."<init>":()V
       7: iconst_2
       8: newarray       int
      10: astore_1
      11: aload_1
      12: iconst_0
      13: iconst_0
      14: iastore
      15: aload_1
      16: iconst_1
      17: iconst_3
      18: iastore
      19: aload_1
      20: iconst_2
      21: newarray       int
      23: astore_1
      24: aload_1
      25: iconst_0
      26: iconst_2
      27: iastore
      28: aload_1
      29: iconst_1
      30: iconst_3
      31: iastore
      32: aload_1
      33: iconst_3
      34: newarray       int
      36: astore_1
      37: aload_1
      38: iconst_0
      39: iconst_5
      40: iastore
      41: aload_1
      42: iconst_1
      43: iconst_4
      44: iastore
      45: aload_1
      46: iconst_2
      47: iconst_3
      48: iastore
      49: aload_1
      50: iconst_4
      51: newarray       int
      53: astore_1
      54: aload_1
      55: iconst_0
      56: bipush        8
      58: iastore
      59: aload_1
      60: iconst_1
      61: iconst_2
      62: iastore
      63: aload_1
      64: iconst_2
      65: bipush        6
      67: iastore
      68: aload_1
      69: iconst_3
      70: bipush        7
      72: iastore
      73: aload_1
      74: invokevirtual #15                 // Method bk66/Problem3.minCost:([I[I[I[I)I
      77: istore_0
      78: getstatic     #21                 // Field java/lang/System.out:Ljava/io/PrintStream;
      81: iload_0
      82: invokevirtual #27                 // Method java/io/PrintStream.println:(I)V
      85: return

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

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

  public final int minCost(int[], int[], int[], int[]);
    Code:
       0: aload_1
       1: ldc           #15                 // String startPos
       3: invokestatic  #21                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_2
       7: ldc           #23                 // String homePos
       9: invokestatic  #21                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
      12: aload_3
      13: ldc           #25                 // String rowCosts
      15: invokestatic  #21                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
      18: aload         4
      20: ldc           #27                 // String colCosts
      22: invokestatic  #21                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
      25: aload_1
      26: iconst_0
      27: iaload
      28: istore        5
      30: aload_1
      31: iconst_1
      32: iaload
      33: istore        6
      35: aload_2
      36: iconst_0
      37: iaload
      38: istore        7
      40: aload_2
      41: iconst_1
      42: iaload
      43: istore        8
      45: iload         5
      47: iload         7
      49: if_icmpne     61
      52: iload         6
      54: iload         8
      56: if_icmpne     61
      59: iconst_0
      60: ireturn
      61: iconst_0
      62: istore        9
      64: iload         6
      66: iload         8
      68: if_icmpge     107
      71: iload         6
      73: iconst_1
      74: iadd
      75: istore        10
      77: iload         10
      79: iload         8
      81: if_icmpgt     143
      84: iload         9
      86: aload         4
      88: iload         10
      90: iaload
      91: iadd
      92: istore        9
      94: iload         10
      96: iload         8
      98: if_icmpeq     143
     101: iinc          10, 1
     104: goto          84
     107: iload         6
     109: iconst_1
     110: isub
     111: istore        10
     113: iload         8
     115: iload         10
     117: if_icmpgt     143
     120: iload         9
     122: aload         4
     124: iload         10
     126: iaload
     127: iadd
     128: istore        9
     130: iload         10
     132: iload         8
     134: if_icmpeq     143
     137: iinc          10, -1
     140: goto          120
     143: iload         5
     145: iload         7
     147: if_icmpge     185
     150: iload         5
     152: iconst_1
     153: iadd
     154: istore        10
     156: iload         10
     158: iload         7
     160: if_icmpgt     220
     163: iload         9
     165: aload_3
     166: iload         10
     168: iaload
     169: iadd
     170: istore        9
     172: iload         10
     174: iload         7
     176: if_icmpeq     220
     179: iinc          10, 1
     182: goto          163
     185: iload         5
     187: iconst_1
     188: isub
     189: istore        10
     191: iload         7
     193: iload         10
     195: if_icmpgt     220
     198: iload         9
     200: aload_3
     201: iload         10
     203: iaload
     204: iadd
     205: istore        9
     207: iload         10
     209: iload         7
     211: if_icmpeq     220
     214: iinc          10, -1
     217: goto          198
     220: iload         9
     222: ireturn
}


package nov_challenge2021

fun main() {
    val grid1 = arrayOf(
        intArrayOf(1, 0, 0, 0),
        intArrayOf(0, 0, 0, 0),
        intArrayOf(0, 0, 2, -1),
    )
    val res1 = UniquePathsIII().uniquePathsIII(grid1)
    assert(res1 == 2)

    val grid2 = arrayOf(
        intArrayOf(0, 1),
        intArrayOf(2, 0),
    )
    val res2 = UniquePathsIII().uniquePathsIII(grid2)
    assert(res2 == 0)
}

class UniquePathsIII {
    private var result = 0
    private lateinit var visited: Array<IntArray>
    private lateinit var grid: Array<IntArray>
    private var obstaclesCount = 0
    private val dirs = arrayOf(Pair(0, 1), Pair(1, 0), Pair(0, -1), Pair(-1, 0))

    fun uniquePathsIII(grid: Array<IntArray>): Int {
        var startI = 0
        var startJ = 0
        this.grid = grid
        visited = Array(grid.size) {
            IntArray(grid[0].size)
        }

        for (i in grid.indices) {
            for (j in grid[i].indices) {
                if (grid[i][j] == 1) {
                    startI = i
                    startJ = j
                }
                if (grid[i][j] == -1) {
                    obstaclesCount++
                }
            }
        }
        count(startI, startJ, 0)
        return result
    }

    private fun count(i: Int, j: Int, counter: Int) {
        if (grid[i][j] == 2 && counter == grid.size * grid[0].size - 1 - obstaclesCount) {
            result++
            return
        }
        visited[i][j] = 1
        for (dir in dirs) {
            val newI = i + dir.first
            val newJ = j + dir.second
            if (
                newI >= 0 &&
                newI < grid.size &&
                newJ >= 0 &&
                newJ < grid[0].size &&
                visited[newI][newJ] == 0 &&
                grid[newI][newJ] != -1
            ) {
                count(newI, newJ, counter + 1)
            }
        }
        visited[i][j] = 0
    }
}

Compiled from "UniquePathsIII.kt"
public final class nov_challenge2021.UniquePathsIII {
  private int result;

  private int[][] visited;

  private int[][] grid;

  private int obstaclesCount;

  private final kotlin.Pair<java.lang.Integer, java.lang.Integer>[] dirs;

  public nov_challenge2021.UniquePathsIII();
    Code:
       0: aload_0
       1: invokespecial #8                  // Method java/lang/Object."<init>":()V
       4: aload_0
       5: iconst_4
       6: anewarray     #10                 // class kotlin/Pair
       9: astore_1
      10: aload_1
      11: iconst_0
      12: new           #10                 // class kotlin/Pair
      15: dup
      16: iconst_0
      17: invokestatic  #16                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      20: iconst_1
      21: invokestatic  #16                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      24: invokespecial #19                 // Method kotlin/Pair."<init>":(Ljava/lang/Object;Ljava/lang/Object;)V
      27: aastore
      28: aload_1
      29: iconst_1
      30: new           #10                 // class kotlin/Pair
      33: dup
      34: iconst_1
      35: invokestatic  #16                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      38: iconst_0
      39: invokestatic  #16                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      42: invokespecial #19                 // Method kotlin/Pair."<init>":(Ljava/lang/Object;Ljava/lang/Object;)V
      45: aastore
      46: aload_1
      47: iconst_2
      48: new           #10                 // class kotlin/Pair
      51: dup
      52: iconst_0
      53: invokestatic  #16                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      56: iconst_m1
      57: invokestatic  #16                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      60: invokespecial #19                 // Method kotlin/Pair."<init>":(Ljava/lang/Object;Ljava/lang/Object;)V
      63: aastore
      64: aload_1
      65: iconst_3
      66: new           #10                 // class kotlin/Pair
      69: dup
      70: iconst_m1
      71: invokestatic  #16                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      74: iconst_0
      75: invokestatic  #16                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      78: invokespecial #19                 // Method kotlin/Pair."<init>":(Ljava/lang/Object;Ljava/lang/Object;)V
      81: aastore
      82: aload_1
      83: putfield      #23                 // Field dirs:[Lkotlin/Pair;
      86: return

  public final int uniquePathsIII(int[][]);
    Code:
       0: aload_1
       1: ldc           #30                 // String grid
       3: invokestatic  #36                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: iconst_0
       7: istore_2
       8: iconst_0
       9: istore_3
      10: aload_0
      11: aload_1
      12: putfield      #39                 // Field grid:[[I
      15: aload_0
      16: iconst_0
      17: istore        4
      19: aload_1
      20: checkcast     #41                 // class "[Ljava/lang/Object;"
      23: arraylength
      24: istore        5
      26: iload         5
      28: anewarray     #43                 // class "[I"
      31: astore        6
      33: astore        9
      35: iload         4
      37: iload         5
      39: if_icmpge     63
      42: iload         4
      44: istore        7
      46: aload         6
      48: iload         7
      50: aload_1
      51: iconst_0
      52: aaload
      53: arraylength
      54: newarray       int
      56: aastore
      57: iinc          4, 1
      60: goto          35
      63: aload         9
      65: aload         6
      67: putfield      #46                 // Field visited:[[I
      70: iconst_0
      71: istore        4
      73: aload_1
      74: checkcast     #41                 // class "[Ljava/lang/Object;"
      77: arraylength
      78: istore        5
      80: iload         4
      82: iload         5
      84: if_icmpge     158
      87: iconst_0
      88: istore        6
      90: aload_1
      91: iload         4
      93: aaload
      94: arraylength
      95: istore        7
      97: iload         6
      99: iload         7
     101: if_icmpge     152
     104: aload_1
     105: iload         4
     107: aaload
     108: iload         6
     110: iaload
     111: iconst_1
     112: if_icmpne     121
     115: iload         4
     117: istore_2
     118: iload         6
     120: istore_3
     121: aload_1
     122: iload         4
     124: aaload
     125: iload         6
     127: iaload
     128: iconst_m1
     129: if_icmpne     146
     132: aload_0
     133: getfield      #50                 // Field obstaclesCount:I
     136: istore        8
     138: aload_0
     139: iload         8
     141: iconst_1
     142: iadd
     143: putfield      #50                 // Field obstaclesCount:I
     146: iinc          6, 1
     149: goto          97
     152: iinc          4, 1
     155: goto          80
     158: aload_0
     159: iload_2
     160: iload_3
     161: iconst_0
     162: invokespecial #54                 // Method count:(III)V
     165: aload_0
     166: getfield      #57                 // Field result:I
     169: ireturn

  private final void count(int, int, int);
    Code:
       0: aload_0
       1: getfield      #39                 // Field grid:[[I
       4: dup
       5: ifnonnull     15
       8: pop
       9: ldc           #30                 // String grid
      11: invokestatic  #66                 // Method kotlin/jvm/internal/Intrinsics.throwUninitializedPropertyAccessException:(Ljava/lang/String;)V
      14: aconst_null
      15: iload_1
      16: aaload
      17: iload_2
      18: iaload
      19: iconst_2
      20: if_icmpne     87
      23: iload_3
      24: aload_0
      25: getfield      #39                 // Field grid:[[I
      28: dup
      29: ifnonnull     39
      32: pop
      33: ldc           #30                 // String grid
      35: invokestatic  #66                 // Method kotlin/jvm/internal/Intrinsics.throwUninitializedPropertyAccessException:(Ljava/lang/String;)V
      38: aconst_null
      39: checkcast     #41                 // class "[Ljava/lang/Object;"
      42: arraylength
      43: aload_0
      44: getfield      #39                 // Field grid:[[I
      47: dup
      48: ifnonnull     58
      51: pop
      52: ldc           #30                 // String grid
      54: invokestatic  #66                 // Method kotlin/jvm/internal/Intrinsics.throwUninitializedPropertyAccessException:(Ljava/lang/String;)V
      57: aconst_null
      58: iconst_0
      59: aaload
      60: arraylength
      61: imul
      62: iconst_1
      63: isub
      64: aload_0
      65: getfield      #50                 // Field obstaclesCount:I
      68: isub
      69: if_icmpne     87
      72: aload_0
      73: getfield      #57                 // Field result:I
      76: istore        4
      78: aload_0
      79: iload         4
      81: iconst_1
      82: iadd
      83: putfield      #57                 // Field result:I
      86: return
      87: aload_0
      88: getfield      #46                 // Field visited:[[I
      91: dup
      92: ifnonnull     102
      95: pop
      96: ldc           #67                 // String visited
      98: invokestatic  #66                 // Method kotlin/jvm/internal/Intrinsics.throwUninitializedPropertyAccessException:(Ljava/lang/String;)V
     101: aconst_null
     102: iload_1
     103: aaload
     104: iload_2
     105: iconst_1
     106: iastore
     107: aload_0
     108: getfield      #23                 // Field dirs:[Lkotlin/Pair;
     111: astore        4
     113: iconst_0
     114: istore        5
     116: aload         4
     118: arraylength
     119: istore        6
     121: iload         5
     123: iload         6
     125: if_icmpge     288
     128: aload         4
     130: iload         5
     132: aaload
     133: astore        7
     135: iload_1
     136: aload         7
     138: invokevirtual #71                 // Method kotlin/Pair.getFirst:()Ljava/lang/Object;
     141: checkcast     #73                 // class java/lang/Number
     144: invokevirtual #77                 // Method java/lang/Number.intValue:()I
     147: iadd
     148: istore        8
     150: iload_2
     151: aload         7
     153: invokevirtual #80                 // Method kotlin/Pair.getSecond:()Ljava/lang/Object;
     156: checkcast     #73                 // class java/lang/Number
     159: invokevirtual #77                 // Method java/lang/Number.intValue:()I
     162: iadd
     163: istore        9
     165: iload         8
     167: iflt          282
     170: iload         8
     172: aload_0
     173: getfield      #39                 // Field grid:[[I
     176: dup
     177: ifnonnull     187
     180: pop
     181: ldc           #30                 // String grid
     183: invokestatic  #66                 // Method kotlin/jvm/internal/Intrinsics.throwUninitializedPropertyAccessException:(Ljava/lang/String;)V
     186: aconst_null
     187: checkcast     #41                 // class "[Ljava/lang/Object;"
     190: arraylength
     191: if_icmpge     282
     194: iload         9
     196: iflt          282
     199: iload         9
     201: aload_0
     202: getfield      #39                 // Field grid:[[I
     205: dup
     206: ifnonnull     216
     209: pop
     210: ldc           #30                 // String grid
     212: invokestatic  #66                 // Method kotlin/jvm/internal/Intrinsics.throwUninitializedPropertyAccessException:(Ljava/lang/String;)V
     215: aconst_null
     216: iconst_0
     217: aaload
     218: arraylength
     219: if_icmpge     282
     222: aload_0
     223: getfield      #46                 // Field visited:[[I
     226: dup
     227: ifnonnull     237
     230: pop
     231: ldc           #67                 // String visited
     233: invokestatic  #66                 // Method kotlin/jvm/internal/Intrinsics.throwUninitializedPropertyAccessException:(Ljava/lang/String;)V
     236: aconst_null
     237: iload         8
     239: aaload
     240: iload         9
     242: iaload
     243: ifne          282
     246: aload_0
     247: getfield      #39                 // Field grid:[[I
     250: dup
     251: ifnonnull     261
     254: pop
     255: ldc           #30                 // String grid
     257: invokestatic  #66                 // Method kotlin/jvm/internal/Intrinsics.throwUninitializedPropertyAccessException:(Ljava/lang/String;)V
     260: aconst_null
     261: iload         8
     263: aaload
     264: iload         9
     266: iaload
     267: iconst_m1
     268: if_icmpeq     282
     271: aload_0
     272: iload         8
     274: iload         9
     276: iload_3
     277: iconst_1
     278: iadd
     279: invokespecial #54                 // Method count:(III)V
     282: iinc          5, 1
     285: goto          121
     288: aload_0
     289: getfield      #46                 // Field visited:[[I
     292: dup
     293: ifnonnull     303
     296: pop
     297: ldc           #67                 // String visited
     299: invokestatic  #66                 // Method kotlin/jvm/internal/Intrinsics.throwUninitializedPropertyAccessException:(Ljava/lang/String;)V
     302: aconst_null
     303: iload_1
     304: aaload
     305: iload_2
     306: iconst_0
     307: iastore
     308: return
}

Compiled from "UniquePathsIII.kt"
public final class nov_challenge2021.UniquePathsIIIKt {
  public static final void main();
    Code:
       0: iconst_3
       1: anewarray     #8                  // class "[I"
       4: astore_1
       5: aload_1
       6: iconst_0
       7: iconst_4
       8: newarray       int
      10: astore_2
      11: aload_2
      12: iconst_0
      13: iconst_1
      14: iastore
      15: aload_2
      16: iconst_1
      17: iconst_0
      18: iastore
      19: aload_2
      20: iconst_2
      21: iconst_0
      22: iastore
      23: aload_2
      24: iconst_3
      25: iconst_0
      26: iastore
      27: aload_2
      28: aastore
      29: aload_1
      30: iconst_1
      31: iconst_4
      32: newarray       int
      34: astore_2
      35: aload_2
      36: iconst_0
      37: iconst_0
      38: iastore
      39: aload_2
      40: iconst_1
      41: iconst_0
      42: iastore
      43: aload_2
      44: iconst_2
      45: iconst_0
      46: iastore
      47: aload_2
      48: iconst_3
      49: iconst_0
      50: iastore
      51: aload_2
      52: aastore
      53: aload_1
      54: iconst_2
      55: iconst_4
      56: newarray       int
      58: astore_2
      59: aload_2
      60: iconst_0
      61: iconst_0
      62: iastore
      63: aload_2
      64: iconst_1
      65: iconst_0
      66: iastore
      67: aload_2
      68: iconst_2
      69: iconst_2
      70: iastore
      71: aload_2
      72: iconst_3
      73: iconst_m1
      74: iastore
      75: aload_2
      76: aastore
      77: aload_1
      78: astore_0
      79: new           #10                 // class nov_challenge2021/UniquePathsIII
      82: dup
      83: invokespecial #13                 // Method nov_challenge2021/UniquePathsIII."<init>":()V
      86: aload_0
      87: invokevirtual #17                 // Method nov_challenge2021/UniquePathsIII.uniquePathsIII:([[I)I
      90: istore_1
      91: iload_1
      92: iconst_2
      93: if_icmpne     100
      96: iconst_1
      97: goto          101
     100: iconst_0
     101: istore_2
     102: getstatic     #23                 // Field kotlin/_Assertions.ENABLED:Z
     105: ifeq          124
     108: iload_2
     109: ifne          124
     112: ldc           #25                 // String Assertion failed
     114: astore_3
     115: new           #27                 // class java/lang/AssertionError
     118: dup
     119: aload_3
     120: invokespecial #30                 // Method java/lang/AssertionError."<init>":(Ljava/lang/Object;)V
     123: athrow
     124: iconst_2
     125: anewarray     #8                  // class "[I"
     128: astore_3
     129: aload_3
     130: iconst_0
     131: iconst_2
     132: newarray       int
     134: astore        4
     136: aload         4
     138: iconst_0
     139: iconst_0
     140: iastore
     141: aload         4
     143: iconst_1
     144: iconst_1
     145: iastore
     146: aload         4
     148: aastore
     149: aload_3
     150: iconst_1
     151: iconst_2
     152: newarray       int
     154: astore        4
     156: aload         4
     158: iconst_0
     159: iconst_2
     160: iastore
     161: aload         4
     163: iconst_1
     164: iconst_0
     165: iastore
     166: aload         4
     168: aastore
     169: aload_3
     170: astore_2
     171: new           #10                 // class nov_challenge2021/UniquePathsIII
     174: dup
     175: invokespecial #13                 // Method nov_challenge2021/UniquePathsIII."<init>":()V
     178: aload_2
     179: invokevirtual #17                 // Method nov_challenge2021/UniquePathsIII.uniquePathsIII:([[I)I
     182: istore_3
     183: iload_3
     184: ifne          191
     187: iconst_1
     188: goto          192
     191: iconst_0
     192: istore        4
     194: getstatic     #23                 // Field kotlin/_Assertions.ENABLED:Z
     197: ifeq          219
     200: iload         4
     202: ifne          219
     205: ldc           #25                 // String Assertion failed
     207: astore        5
     209: new           #27                 // class java/lang/AssertionError
     212: dup
     213: aload         5
     215: invokespecial #30                 // Method java/lang/AssertionError."<init>":(Ljava/lang/Object;)V
     218: athrow
     219: return

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


package oct_challenge2021.wordSearchII

fun main() {
    val matrix = arrayOf(
        charArrayOf('o', 'a', 'a', 'n'),
        charArrayOf('e', 't', 'a', 'e'),
        charArrayOf('i', 'h', 'k', 'r'),
        charArrayOf('i', 'f', 'l', 'v'),
    )
    val words = arrayOf("oath", "pea", "eat", "rain")
    val result = WordSearchII().findWords(matrix, words)
    println(result) // expected "oath", "eat"
}

class WordSearchII {
    private val hashResult = hashSetOf<String>()
    private lateinit var board: Array<CharArray>
    private val trie = Trie()
    fun findWords(board: Array<CharArray>, words: Array<String>): List<String> {
        this.board = board
        for (word in words) trie.insert(word)
        for (i in board.indices) {
            for (j in board[0].indices) {
                traverse(i, j, "")
            }
        }
        return hashResult.toList()
    }

    private fun traverse(i: Int, j: Int, prev: String) {
        if (i < 0 || i >= board.size || j < 0 || j >= board[0].size || board[i][j] == '#') return
        val current = prev + board[i][j]
        if (!trie.startsWith(current)) return
        if (trie.search(current)) hashResult.add(current)
        val temp = board[i][j]
        board[i][j] = '#'
        traverse(i + 1, j, current)
        traverse(i, j + 1, current)
        traverse(i - 1, j, current)
        traverse(i, j - 1, current)
        board[i][j] = temp
    }
}

private class Trie {
    private val root: TrieNode? = TrieNode()

    fun insert(word: String) {
        var node = root
        for (currChar in word) {
            if (!node!!.containsKey(currChar)) {
                node.put(currChar, TrieNode())
            }
            node = node.get(currChar)
        }
        node!!.isEnd = true
    }

    fun search(word: String): Boolean {
        val node = searchPrefix(word)
        return node != null && node.isEnd
    }

    fun startsWith(prefix: String): Boolean {
        val node = searchPrefix(prefix)
        return node != null
    }

    private fun searchPrefix(word: String): TrieNode? {
        var node = root
        for (currChar in word) {
            if (node!!.containsKey(currChar)) {
                node = node.get(currChar)
            } else {
                return null
            }
        }

        return node
    }

}

private class TrieNode {
    private val numberOfAlphabet = 26
    private var links: Array<TrieNode?> = arrayOfNulls(numberOfAlphabet)
    var isEnd = false

    fun containsKey(char: Char): Boolean {
        return links[char - 'a'] != null
    }

    fun get(char: Char): TrieNode? {
        return links[char - 'a']
    }

    fun put(char: Char, node: TrieNode) {
        links[char - 'a'] = node
    }

}

Compiled from "WordSearchII.kt"
public final class oct_challenge2021.wordSearchII.WordSearchIIKt {
  public static final void main();
    Code:
       0: iconst_4
       1: anewarray     #8                  // class "[C"
       4: astore_1
       5: aload_1
       6: iconst_0
       7: iconst_4
       8: newarray       char
      10: astore_2
      11: aload_2
      12: iconst_0
      13: bipush        111
      15: castore
      16: aload_2
      17: iconst_1
      18: bipush        97
      20: castore
      21: aload_2
      22: iconst_2
      23: bipush        97
      25: castore
      26: aload_2
      27: iconst_3
      28: bipush        110
      30: castore
      31: aload_2
      32: aastore
      33: aload_1
      34: iconst_1
      35: iconst_4
      36: newarray       char
      38: astore_2
      39: aload_2
      40: iconst_0
      41: bipush        101
      43: castore
      44: aload_2
      45: iconst_1
      46: bipush        116
      48: castore
      49: aload_2
      50: iconst_2
      51: bipush        97
      53: castore
      54: aload_2
      55: iconst_3
      56: bipush        101
      58: castore
      59: aload_2
      60: aastore
      61: aload_1
      62: iconst_2
      63: iconst_4
      64: newarray       char
      66: astore_2
      67: aload_2
      68: iconst_0
      69: bipush        105
      71: castore
      72: aload_2
      73: iconst_1
      74: bipush        104
      76: castore
      77: aload_2
      78: iconst_2
      79: bipush        107
      81: castore
      82: aload_2
      83: iconst_3
      84: bipush        114
      86: castore
      87: aload_2
      88: aastore
      89: aload_1
      90: iconst_3
      91: iconst_4
      92: newarray       char
      94: astore_2
      95: aload_2
      96: iconst_0
      97: bipush        105
      99: castore
     100: aload_2
     101: iconst_1
     102: bipush        102
     104: castore
     105: aload_2
     106: iconst_2
     107: bipush        108
     109: castore
     110: aload_2
     111: iconst_3
     112: bipush        118
     114: castore
     115: aload_2
     116: aastore
     117: aload_1
     118: astore_0
     119: iconst_4
     120: anewarray     #10                 // class java/lang/String
     123: astore_2
     124: aload_2
     125: iconst_0
     126: ldc           #12                 // String oath
     128: aastore
     129: aload_2
     130: iconst_1
     131: ldc           #14                 // String pea
     133: aastore
     134: aload_2
     135: iconst_2
     136: ldc           #16                 // String eat
     138: aastore
     139: aload_2
     140: iconst_3
     141: ldc           #18                 // String rain
     143: aastore
     144: aload_2
     145: astore_1
     146: new           #20                 // class oct_challenge2021/wordSearchII/WordSearchII
     149: dup
     150: invokespecial #23                 // Method oct_challenge2021/wordSearchII/WordSearchII."<init>":()V
     153: aload_0
     154: aload_1
     155: invokevirtual #27                 // Method oct_challenge2021/wordSearchII/WordSearchII.findWords:([[C[Ljava/lang/String;)Ljava/util/List;
     158: astore_2
     159: getstatic     #33                 // Field java/lang/System.out:Ljava/io/PrintStream;
     162: aload_2
     163: invokevirtual #39                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
     166: return

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

Compiled from "WordSearchII.kt"
public final class oct_challenge2021.wordSearchII.WordSearchII {
  private final java.util.HashSet<java.lang.String> hashResult;

  private char[][] board;

  private final oct_challenge2021.wordSearchII.Trie trie;

  public oct_challenge2021.wordSearchII.WordSearchII();
    Code:
       0: aload_0
       1: invokespecial #8                  // Method java/lang/Object."<init>":()V
       4: aload_0
       5: new           #10                 // class java/util/HashSet
       8: dup
       9: invokespecial #11                 // Method java/util/HashSet."<init>":()V
      12: putfield      #15                 // Field hashResult:Ljava/util/HashSet;
      15: aload_0
      16: new           #17                 // class oct_challenge2021/wordSearchII/Trie
      19: dup
      20: invokespecial #18                 // Method oct_challenge2021/wordSearchII/Trie."<init>":()V
      23: putfield      #22                 // Field trie:Loct_challenge2021/wordSearchII/Trie;
      26: return

  public final java.util.List<java.lang.String> findWords(char[][], java.lang.String[]);
    Code:
       0: aload_1
       1: ldc           #30                 // String board
       3: invokestatic  #36                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_2
       7: ldc           #38                 // String words
       9: invokestatic  #36                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
      12: aload_0
      13: aload_1
      14: putfield      #41                 // Field board:[[C
      17: iconst_0
      18: istore_3
      19: aload_2
      20: arraylength
      21: istore        4
      23: iload_3
      24: iload         4
      26: if_icmpge     49
      29: aload_2
      30: iload_3
      31: aaload
      32: astore        5
      34: aload_0
      35: getfield      #22                 // Field trie:Loct_challenge2021/wordSearchII/Trie;
      38: aload         5
      40: invokevirtual #45                 // Method oct_challenge2021/wordSearchII/Trie.insert:(Ljava/lang/String;)V
      43: iinc          3, 1
      46: goto          23
      49: iconst_0
      50: istore_3
      51: aload_1
      52: checkcast     #47                 // class "[Ljava/lang/Object;"
      55: arraylength
      56: istore        4
      58: iload_3
      59: iload         4
      61: if_icmpge     101
      64: iconst_0
      65: istore        5
      67: aload_1
      68: iconst_0
      69: aaload
      70: arraylength
      71: istore        6
      73: iload         5
      75: iload         6
      77: if_icmpge     95
      80: aload_0
      81: iload_3
      82: iload         5
      84: ldc           #49                 // String
      86: invokespecial #53                 // Method traverse:(IILjava/lang/String;)V
      89: iinc          5, 1
      92: goto          73
      95: iinc          3, 1
      98: goto          58
     101: aload_0
     102: getfield      #15                 // Field hashResult:Ljava/util/HashSet;
     105: checkcast     #55                 // class java/lang/Iterable
     108: invokestatic  #61                 // Method kotlin/collections/CollectionsKt.toList:(Ljava/lang/Iterable;)Ljava/util/List;
     111: areturn

  private final void traverse(int, int, java.lang.String);
    Code:
       0: iload_1
       1: iflt          77
       4: iload_1
       5: aload_0
       6: getfield      #41                 // Field board:[[C
       9: dup
      10: ifnonnull     20
      13: pop
      14: ldc           #30                 // String board
      16: invokestatic  #70                 // Method kotlin/jvm/internal/Intrinsics.throwUninitializedPropertyAccessException:(Ljava/lang/String;)V
      19: aconst_null
      20: checkcast     #47                 // class "[Ljava/lang/Object;"
      23: arraylength
      24: if_icmpge     77
      27: iload_2
      28: iflt          77
      31: iload_2
      32: aload_0
      33: getfield      #41                 // Field board:[[C
      36: dup
      37: ifnonnull     47
      40: pop
      41: ldc           #30                 // String board
      43: invokestatic  #70                 // Method kotlin/jvm/internal/Intrinsics.throwUninitializedPropertyAccessException:(Ljava/lang/String;)V
      46: aconst_null
      47: iconst_0
      48: aaload
      49: arraylength
      50: if_icmpge     77
      53: aload_0
      54: getfield      #41                 // Field board:[[C
      57: dup
      58: ifnonnull     68
      61: pop
      62: ldc           #30                 // String board
      64: invokestatic  #70                 // Method kotlin/jvm/internal/Intrinsics.throwUninitializedPropertyAccessException:(Ljava/lang/String;)V
      67: aconst_null
      68: iload_1
      69: aaload
      70: iload_2
      71: caload
      72: bipush        35
      74: if_icmpne     78
      77: return
      78: new           #72                 // class java/lang/StringBuilder
      81: dup
      82: invokespecial #73                 // Method java/lang/StringBuilder."<init>":()V
      85: aload_3
      86: invokevirtual #77                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      89: aload_0
      90: getfield      #41                 // Field board:[[C
      93: dup
      94: ifnonnull     104
      97: pop
      98: ldc           #30                 // String board
     100: invokestatic  #70                 // Method kotlin/jvm/internal/Intrinsics.throwUninitializedPropertyAccessException:(Ljava/lang/String;)V
     103: aconst_null
     104: iload_1
     105: aaload
     106: iload_2
     107: caload
     108: invokevirtual #80                 // Method java/lang/StringBuilder.append:(C)Ljava/lang/StringBuilder;
     111: invokevirtual #84                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
     114: astore        4
     116: aload_0
     117: getfield      #22                 // Field trie:Loct_challenge2021/wordSearchII/Trie;
     120: aload         4
     122: invokevirtual #88                 // Method oct_challenge2021/wordSearchII/Trie.startsWith:(Ljava/lang/String;)Z
     125: ifne          129
     128: return
     129: aload_0
     130: getfield      #22                 // Field trie:Loct_challenge2021/wordSearchII/Trie;
     133: aload         4
     135: invokevirtual #91                 // Method oct_challenge2021/wordSearchII/Trie.search:(Ljava/lang/String;)Z
     138: ifeq          151
     141: aload_0
     142: getfield      #15                 // Field hashResult:Ljava/util/HashSet;
     145: aload         4
     147: invokevirtual #95                 // Method java/util/HashSet.add:(Ljava/lang/Object;)Z
     150: pop
     151: aload_0
     152: getfield      #41                 // Field board:[[C
     155: dup
     156: ifnonnull     166
     159: pop
     160: ldc           #30                 // String board
     162: invokestatic  #70                 // Method kotlin/jvm/internal/Intrinsics.throwUninitializedPropertyAccessException:(Ljava/lang/String;)V
     165: aconst_null
     166: iload_1
     167: aaload
     168: iload_2
     169: caload
     170: istore        5
     172: aload_0
     173: getfield      #41                 // Field board:[[C
     176: dup
     177: ifnonnull     187
     180: pop
     181: ldc           #30                 // String board
     183: invokestatic  #70                 // Method kotlin/jvm/internal/Intrinsics.throwUninitializedPropertyAccessException:(Ljava/lang/String;)V
     186: aconst_null
     187: iload_1
     188: aaload
     189: iload_2
     190: bipush        35
     192: castore
     193: aload_0
     194: iload_1
     195: iconst_1
     196: iadd
     197: iload_2
     198: aload         4
     200: invokespecial #53                 // Method traverse:(IILjava/lang/String;)V
     203: aload_0
     204: iload_1
     205: iload_2
     206: iconst_1
     207: iadd
     208: aload         4
     210: invokespecial #53                 // Method traverse:(IILjava/lang/String;)V
     213: aload_0
     214: iload_1
     215: iconst_1
     216: isub
     217: iload_2
     218: aload         4
     220: invokespecial #53                 // Method traverse:(IILjava/lang/String;)V
     223: aload_0
     224: iload_1
     225: iload_2
     226: iconst_1
     227: isub
     228: aload         4
     230: invokespecial #53                 // Method traverse:(IILjava/lang/String;)V
     233: aload_0
     234: getfield      #41                 // Field board:[[C
     237: dup
     238: ifnonnull     248
     241: pop
     242: ldc           #30                 // String board
     244: invokestatic  #70                 // Method kotlin/jvm/internal/Intrinsics.throwUninitializedPropertyAccessException:(Ljava/lang/String;)V
     247: aconst_null
     248: iload_1
     249: aaload
     250: iload_2
     251: iload         5
     253: castore
     254: return
}