import java.io.File
import java.io.PrintWriter
import java.util.*

//val filename = "a_example"
//val filename = "b_should_be_easy"
//val filename = "c_no_hurry"
val filename = "d_metropolis"
//val filename = "e_high_bonus"

fun main(args: Array<String>) {
    val sc = Scanner(File("data/$filename.in"))

    val (_, _, V, R) = Array(4) { sc.nextInt() }
    B = sc.nextInt()
    val T = sc.nextInt()

    val rides = Array(R) { sc.nextRide(it) }

    val cars = Array(V) { Car(Point(0, 0), 0) }

    println("Max score: " + rides.sumBy { it.duration + B })

    rides.sortBy { it.end - it.duration }

    for (ride in rides) {
        cars.maxBy { it.canScore(ride) }?.take(ride)
    }

    println(cars.totalScore)

    printSolution(cars)
}

val Array<Car>.totalScore
    get() = fold(0) { acc, state -> acc + state.score }

var B = 0

class Car(var point: Point,
          var time: Int,
          val schedule: MutableList<Ride> = mutableListOf(),
          var score: Int = 0) {

    fun willFinishAt(ride: Ride) = possibleArrival(ride) + ride.duration

    fun possibleArrival(ride: Ride) = Math.max(time + (ride.start - point), ride.begin)

    fun waitTime(ride: Ride) = Math.max(possibleArrival(ride) - time, 0)

    fun canScore(ride: Ride): Int {
        return if (willFinishAt(ride) <= ride.end) {
            ride.duration + if (possibleArrival(ride) <= ride.begin) B else 0
        } else 0
    }

    fun take(ride: Ride) {
        score += canScore(ride)
        schedule += ride
        time = willFinishAt(ride)
        point = ride.finish
    }
}

fun printSolution(solution: Array<Car>) {
    PrintWriter("$filename-${solution.totalScore}.out").use { pr ->
        solution.forEach {
            pr.print(it.schedule.size)
            it.schedule.forEach {
                pr.print(" ${it.num}")
            }
            pr.println()
        }
    }
}

operator fun Point.minus(o: Point) = Math.abs(x - o.x) + Math.abs(y - o.y)

data class Point(val x: Int, val y: Int)

data class Ride(val num: Int, val start: Point, val finish: Point, val begin: Int, val end: Int) {
    val duration
        get() = finish - start
}

val Ride.longRide
    get() = finish.x > 5000 || finish.y > 5000

fun Scanner.nextPoint() = Point(nextInt(), nextInt())

fun Scanner.nextRide(num: Int) = Ride(num, nextPoint(), nextPoint(), nextInt(), nextInt())

Compiled from "Main.kt"
public final class MainKt {
  private static final java.lang.String filename;

  private static int B;

  public static final java.lang.String getFilename();
    Code:
       0: getstatic     #11                 // Field filename:Ljava/lang/String;
       3: areturn

  public static final void main(java.lang.String[]);
    Code:
       0: aload_0
       1: ldc           #15                 // String args
       3: invokestatic  #21                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: new           #23                 // class java/util/Scanner
       9: dup
      10: new           #25                 // class java/io/File
      13: dup
      14: new           #27                 // class java/lang/StringBuilder
      17: dup
      18: invokespecial #31                 // Method java/lang/StringBuilder."<init>":()V
      21: ldc           #33                 // String data/
      23: invokevirtual #37                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      26: getstatic     #11                 // Field filename:Ljava/lang/String;
      29: invokevirtual #37                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      32: ldc           #39                 // String .in
      34: invokevirtual #37                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      37: invokevirtual #42                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
      40: invokespecial #45                 // Method java/io/File."<init>":(Ljava/lang/String;)V
      43: invokespecial #48                 // Method java/util/Scanner."<init>":(Ljava/io/File;)V
      46: astore_1
      47: iconst_0
      48: istore_3
      49: iconst_4
      50: anewarray     #50                 // class java/lang/Integer
      53: astore        4
      55: iload_3
      56: iconst_4
      57: if_icmpge     81
      60: iload_3
      61: istore        5
      63: aload         4
      65: iload         5
      67: aload_1
      68: invokevirtual #54                 // Method java/util/Scanner.nextInt:()I
      71: invokestatic  #58                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      74: aastore
      75: iinc          3, 1
      78: goto          55
      81: aload         4
      83: astore_2
      84: aload_2
      85: iconst_2
      86: aaload
      87: checkcast     #60                 // class java/lang/Number
      90: invokevirtual #63                 // Method java/lang/Number.intValue:()I
      93: istore_3
      94: aload_2
      95: iconst_3
      96: aaload
      97: checkcast     #60                 // class java/lang/Number
     100: invokevirtual #63                 // Method java/lang/Number.intValue:()I
     103: istore        4
     105: aload_1
     106: invokevirtual #54                 // Method java/util/Scanner.nextInt:()I
     109: putstatic     #67                 // Field B:I
     112: aload_1
     113: invokevirtual #54                 // Method java/util/Scanner.nextInt:()I
     116: istore        5
     118: iconst_0
     119: istore        7
     121: iload         4
     123: anewarray     #69                 // class Ride
     126: astore        8
     128: iload         7
     130: iload         4
     132: if_icmpge     156
     135: iload         7
     137: istore        9
     139: aload         8
     141: iload         9
     143: aload_1
     144: iload         9
     146: invokestatic  #73                 // Method nextRide:(Ljava/util/Scanner;I)LRide;
     149: aastore
     150: iinc          7, 1
     153: goto          128
     156: aload         8
     158: astore        6
     160: iconst_0
     161: istore        8
     163: iload_3
     164: anewarray     #75                 // class Car
     167: astore        9
     169: iload         8
     171: iload_3
     172: if_icmpge     212
     175: iload         8
     177: istore        10
     179: aload         9
     181: iload         10
     183: new           #75                 // class Car
     186: dup
     187: new           #77                 // class Point
     190: dup
     191: iconst_0
     192: iconst_0
     193: invokespecial #80                 // Method Point."<init>":(II)V
     196: iconst_0
     197: aconst_null
     198: iconst_0
     199: bipush        12
     201: aconst_null
     202: invokespecial #83                 // Method Car."<init>":(LPoint;ILjava/util/List;IILkotlin/jvm/internal/DefaultConstructorMarker;)V
     205: aastore
     206: iinc          8, 1
     209: goto          169
     212: aload         9
     214: astore        7
     216: new           #27                 // class java/lang/StringBuilder
     219: dup
     220: invokespecial #31                 // Method java/lang/StringBuilder."<init>":()V
     223: ldc           #85                 // String Max score:
     225: invokevirtual #37                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     228: aload         6
     230: astore        8
     232: astore        21
     234: iconst_0
     235: istore        9
     237: iconst_0
     238: istore        10
     240: iconst_0
     241: istore        11
     243: aload         8
     245: arraylength
     246: istore        12
     248: iload         11
     250: iload         12
     252: if_icmpge     297
     255: aload         8
     257: iload         11
     259: aaload
     260: astore        13
     262: iload         10
     264: aload         13
     266: astore        14
     268: istore        22
     270: iconst_0
     271: istore        15
     273: aload         14
     275: invokevirtual #88                 // Method Ride.getDuration:()I
     278: getstatic     #67                 // Field B:I
     281: iadd
     282: istore        23
     284: iload         22
     286: iload         23
     288: iadd
     289: istore        10
     291: iinc          11, 1
     294: goto          248
     297: iload         10
     299: istore        22
     301: aload         21
     303: iload         22
     305: invokevirtual #91                 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
     308: invokevirtual #42                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
     311: getstatic     #97                 // Field java/lang/System.out:Ljava/io/PrintStream;
     314: swap
     315: invokevirtual #103                // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
     318: aload         6
     320: astore        8
     322: iconst_0
     323: istore        9
     325: aload         8
     327: arraylength
     328: iconst_1
     329: if_icmple     347
     332: aload         8
     334: new           #105                // class MainKt$main$$inlined$sortBy$1
     337: dup
     338: invokespecial #106                // Method MainKt$main$$inlined$sortBy$1."<init>":()V
     341: checkcast     #108                // class java/util/Comparator
     344: invokestatic  #114                // Method kotlin/collections/ArraysKt.sortWith:([Ljava/lang/Object;Ljava/util/Comparator;)V
     347: nop
     348: iconst_0
     349: istore        8
     351: aload         6
     353: arraylength
     354: istore        9
     356: iload         8
     358: iload         9
     360: if_icmpge     520
     363: aload         6
     365: iload         8
     367: aaload
     368: astore        10
     370: aload         7
     372: astore        12
     374: iconst_0
     375: istore        13
     377: aload         12
     379: arraylength
     380: ifne          387
     383: iconst_1
     384: goto          388
     387: iconst_0
     388: ifeq          399
     391: new           #116                // class java/util/NoSuchElementException
     394: dup
     395: invokespecial #117                // Method java/util/NoSuchElementException."<init>":()V
     398: athrow
     399: aload         12
     401: iconst_0
     402: aaload
     403: astore        14
     405: aload         12
     407: invokestatic  #121                // Method kotlin/collections/ArraysKt.getLastIndex:([Ljava/lang/Object;)I
     410: istore        15
     412: iload         15
     414: ifne          422
     417: aload         14
     419: goto          501
     422: aload         14
     424: astore        16
     426: iconst_0
     427: istore        17
     429: aload         16
     431: aload         10
     433: invokevirtual #125                // Method Car.canScore:(LRide;)I
     436: istore        16
     438: iconst_1
     439: istore        17
     441: iload         17
     443: iload         15
     445: if_icmpgt     499
     448: aload         12
     450: iload         17
     452: aaload
     453: astore        18
     455: aload         18
     457: astore        19
     459: iconst_0
     460: istore        20
     462: aload         19
     464: aload         10
     466: invokevirtual #125                // Method Car.canScore:(LRide;)I
     469: istore        19
     471: iload         16
     473: iload         19
     475: if_icmpge     486
     478: aload         18
     480: astore        14
     482: iload         19
     484: istore        16
     486: iload         17
     488: iload         15
     490: if_icmpeq     499
     493: iinc          17, 1
     496: goto          448
     499: aload         14
     501: dup
     502: ifnull        513
     505: aload         10
     507: invokevirtual #129                // Method Car.take:(LRide;)V
     510: goto          514
     513: pop
     514: iinc          8, 1
     517: goto          356
     520: aload         7
     522: invokestatic  #133                // Method getTotalScore:([LCar;)I
     525: istore        8
     527: getstatic     #97                 // Field java/lang/System.out:Ljava/io/PrintStream;
     530: iload         8
     532: invokevirtual #136                // Method java/io/PrintStream.println:(I)V
     535: aload         7
     537: invokestatic  #140                // Method printSolution:([LCar;)V
     540: return

  public static final int getTotalScore(Car[]);
    Code:
       0: aload_0
       1: ldc           #179                // String <this>
       3: invokestatic  #21                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: astore_1
       8: iconst_0
       9: istore_2
      10: iconst_0
      11: istore_3
      12: iload_2
      13: istore        4
      15: iconst_0
      16: istore        5
      18: aload_1
      19: arraylength
      20: istore        6
      22: iload         5
      24: iload         6
      26: if_icmpge     62
      29: aload_1
      30: iload         5
      32: aaload
      33: astore        7
      35: iload         4
      37: aload         7
      39: astore        8
      41: istore        9
      43: iconst_0
      44: istore        10
      46: iload         9
      48: aload         8
      50: invokevirtual #182                // Method Car.getScore:()I
      53: iadd
      54: istore        4
      56: iinc          5, 1
      59: goto          22
      62: iload         4
      64: ireturn

  public static final int getB();
    Code:
       0: getstatic     #67                 // Field B:I
       3: ireturn

  public static final void setB(int);
    Code:
       0: iload_0
       1: putstatic     #67                 // Field B:I
       4: return

  public static final void printSolution(Car[]);
    Code:
       0: aload_0
       1: ldc           #197                // String solution
       3: invokestatic  #21                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: new           #199                // class java/io/PrintWriter
       9: dup
      10: new           #27                 // class java/lang/StringBuilder
      13: dup
      14: invokespecial #31                 // Method java/lang/StringBuilder."<init>":()V
      17: getstatic     #11                 // Field filename:Ljava/lang/String;
      20: invokevirtual #37                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      23: bipush        45
      25: invokevirtual #202                // Method java/lang/StringBuilder.append:(C)Ljava/lang/StringBuilder;
      28: aload_0
      29: invokestatic  #133                // Method getTotalScore:([LCar;)I
      32: invokevirtual #91                 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
      35: ldc           #204                // String .out
      37: invokevirtual #37                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      40: invokevirtual #42                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
      43: invokespecial #205                // Method java/io/PrintWriter."<init>":(Ljava/lang/String;)V
      46: checkcast     #207                // class java/io/Closeable
      49: astore_1
      50: aconst_null
      51: astore_2
      52: nop
      53: aload_1
      54: checkcast     #199                // class java/io/PrintWriter
      57: astore_3
      58: iconst_0
      59: istore        4
      61: aload_0
      62: astore        5
      64: iconst_0
      65: istore        6
      67: iconst_0
      68: istore        7
      70: aload         5
      72: arraylength
      73: istore        8
      75: iload         7
      77: iload         8
      79: if_icmpge     206
      82: aload         5
      84: iload         7
      86: aaload
      87: astore        9
      89: aload         9
      91: astore        10
      93: iconst_0
      94: istore        11
      96: aload_3
      97: aload         10
      99: invokevirtual #211                // Method Car.getSchedule:()Ljava/util/List;
     102: invokeinterface #216,  1          // InterfaceMethod java/util/List.size:()I
     107: invokevirtual #219                // Method java/io/PrintWriter.print:(I)V
     110: aload         10
     112: invokevirtual #211                // Method Car.getSchedule:()Ljava/util/List;
     115: checkcast     #221                // class java/lang/Iterable
     118: astore        12
     120: iconst_0
     121: istore        13
     123: aload         12
     125: invokeinterface #225,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     130: astore        14
     132: aload         14
     134: invokeinterface #231,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
     139: ifeq          193
     142: aload         14
     144: invokeinterface #235,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     149: astore        15
     151: aload         15
     153: checkcast     #69                 // class Ride
     156: astore        16
     158: iconst_0
     159: istore        17
     161: aload_3
     162: new           #27                 // class java/lang/StringBuilder
     165: dup
     166: invokespecial #31                 // Method java/lang/StringBuilder."<init>":()V
     169: bipush        32
     171: invokevirtual #202                // Method java/lang/StringBuilder.append:(C)Ljava/lang/StringBuilder;
     174: aload         16
     176: invokevirtual #238                // Method Ride.getNum:()I
     179: invokevirtual #91                 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
     182: invokevirtual #42                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
     185: invokevirtual #240                // Method java/io/PrintWriter.print:(Ljava/lang/String;)V
     188: nop
     189: nop
     190: goto          132
     193: nop
     194: aload_3
     195: invokevirtual #242                // Method java/io/PrintWriter.println:()V
     198: nop
     199: nop
     200: iinc          7, 1
     203: goto          75
     206: nop
     207: nop
     208: getstatic     #248                // Field kotlin/Unit.INSTANCE:Lkotlin/Unit;
     211: astore_3
     212: aload_1
     213: aload_2
     214: invokestatic  #254                // Method kotlin/io/CloseableKt.closeFinally:(Ljava/io/Closeable;Ljava/lang/Throwable;)V
     217: goto          238
     220: astore        4
     222: aload         4
     224: astore_2
     225: aload         4
     227: athrow
     228: astore        4
     230: aload_1
     231: aload_2
     232: invokestatic  #254                // Method kotlin/io/CloseableKt.closeFinally:(Ljava/io/Closeable;Ljava/lang/Throwable;)V
     235: aload         4
     237: athrow
     238: return
    Exception table:
       from    to  target type
          52   212   220   Class java/lang/Throwable
          52   212   228   any
         220   228   228   any
         228   230   228   any

  public static final int minus(Point, Point);
    Code:
       0: aload_0
       1: ldc           #179                // String <this>
       3: invokestatic  #21                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_1
       7: ldc_w         #266                // String o
      10: invokestatic  #21                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
      13: aload_0
      14: invokevirtual #269                // Method Point.getX:()I
      17: aload_1
      18: invokevirtual #269                // Method Point.getX:()I
      21: isub
      22: invokestatic  #275                // Method java/lang/Math.abs:(I)I
      25: aload_0
      26: invokevirtual #278                // Method Point.getY:()I
      29: aload_1
      30: invokevirtual #278                // Method Point.getY:()I
      33: isub
      34: invokestatic  #275                // Method java/lang/Math.abs:(I)I
      37: iadd
      38: ireturn

  public static final boolean getLongRide(Ride);
    Code:
       0: aload_0
       1: ldc           #179                // String <this>
       3: invokestatic  #21                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: invokevirtual #286                // Method Ride.getFinish:()LPoint;
      10: invokevirtual #269                // Method Point.getX:()I
      13: sipush        5000
      16: if_icmpgt     32
      19: aload_0
      20: invokevirtual #286                // Method Ride.getFinish:()LPoint;
      23: invokevirtual #278                // Method Point.getY:()I
      26: sipush        5000
      29: if_icmple     36
      32: iconst_1
      33: goto          37
      36: iconst_0
      37: ireturn

  public static final Point nextPoint(java.util.Scanner);
    Code:
       0: aload_0
       1: ldc           #179                // String <this>
       3: invokestatic  #21                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: new           #77                 // class Point
       9: dup
      10: aload_0
      11: invokevirtual #54                 // Method java/util/Scanner.nextInt:()I
      14: aload_0
      15: invokevirtual #54                 // Method java/util/Scanner.nextInt:()I
      18: invokespecial #80                 // Method Point."<init>":(II)V
      21: areturn

  public static final Ride nextRide(java.util.Scanner, int);
    Code:
       0: aload_0
       1: ldc           #179                // String <this>
       3: invokestatic  #21                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: new           #69                 // class Ride
       9: dup
      10: iload_1
      11: aload_0
      12: invokestatic  #292                // Method nextPoint:(Ljava/util/Scanner;)LPoint;
      15: aload_0
      16: invokestatic  #292                // Method nextPoint:(Ljava/util/Scanner;)LPoint;
      19: aload_0
      20: invokevirtual #54                 // Method java/util/Scanner.nextInt:()I
      23: aload_0
      24: invokevirtual #54                 // Method java/util/Scanner.nextInt:()I
      27: invokespecial #295                // Method Ride."<init>":(ILPoint;LPoint;II)V
      30: areturn

  static {};
    Code:
       0: ldc_w         #300                // String d_metropolis
       3: putstatic     #11                 // Field filename:Ljava/lang/String;
       6: return
}

Compiled from "Comparisons.kt"
public final class MainKt$main$$inlined$sortBy$1<T> implements java.util.Comparator {
  public MainKt$main$$inlined$sortBy$1();
    Code:
       0: aload_0
       1: invokespecial #16                 // Method java/lang/Object."<init>":()V
       4: return

  public final int compare(T, T);
    Code:
       0: aload_1
       1: checkcast     #23                 // class Ride
       4: astore_3
       5: iconst_0
       6: istore        4
       8: aload_3
       9: invokevirtual #27                 // Method Ride.getEnd:()I
      12: aload_3
      13: invokevirtual #30                 // Method Ride.getDuration:()I
      16: isub
      17: invokestatic  #36                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      20: checkcast     #38                 // class java/lang/Comparable
      23: aload_2
      24: checkcast     #23                 // class Ride
      27: astore_3
      28: astore        5
      30: iconst_0
      31: istore        4
      33: aload_3
      34: invokevirtual #27                 // Method Ride.getEnd:()I
      37: aload_3
      38: invokevirtual #30                 // Method Ride.getDuration:()I
      41: isub
      42: invokestatic  #36                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      45: aload         5
      47: swap
      48: checkcast     #38                 // class java/lang/Comparable
      51: invokestatic  #44                 // Method kotlin/comparisons/ComparisonsKt.compareValues:(Ljava/lang/Comparable;Ljava/lang/Comparable;)I
      54: ireturn
}


/*
OddOccurrencesInArray
Find value that occurs in odd number of elements.

A non-empty array A consisting of N integers is given. The array contains an odd number of elements, and each element of the array can be paired with another element that has the same value, except for one element that is left unpaired.

For example, in array A such that:
  A[0] = 9  A[1] = 3  A[2] = 9
  A[3] = 3  A[4] = 9  A[5] = 7
  A[6] = 9
the elements at indexes 0 and 2 have value 9,
the elements at indexes 1 and 3 have value 3,
the elements at indexes 4 and 6 have value 9,
the element at index 5 has value 7 and is unpaired.
Write a function:

fun solution(A: IntArray): Int
that, given an array A consisting of N integers fulfilling the above conditions, returns the value of the unpaired element.
For example, given array A such that:
  A[0] = 9  A[1] = 3  A[2] = 9
  A[3] = 3  A[4] = 9  A[5] = 7
  A[6] = 9
the function should return 7, as explained in the example above.

Write an efficient algorithm for the following assumptions:

N is an odd integer within the range [1..1,000,000];
each element of array A is an integer within the range [1..1,000,000,000];
all but one of the values in A occur an even number of times.*/

fun solution(array: IntArray): Int {
    array.sort()
    val length = array.size

    for (i in array.indices step 2) {
        if (i + 1 < length && array[i] == array[i + 1]) continue
        return array[i]
    }

    return 0
}

Compiled from "OddOccurrencesInArray.kt"
public final class OddOccurrencesInArrayKt {
  public static final int solution(int[]);
    Code:
       0: aload_0
       1: ldc           #9                  // String array
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: invokestatic  #21                 // Method kotlin/collections/ArraysKt.sort:([I)V
      10: aload_0
      11: arraylength
      12: istore_1
      13: aload_0
      14: arraylength
      15: iconst_m1
      16: iadd
      17: istore_2
      18: iconst_0
      19: istore_3
      20: iconst_0
      21: iload_2
      22: iconst_2
      23: invokestatic  #27                 // Method kotlin/internal/ProgressionUtilKt.getProgressionLastElement:(III)I
      26: istore        4
      28: iload_3
      29: iload         4
      31: if_icmpgt     68
      34: iload_3
      35: iconst_1
      36: iadd
      37: iload_1
      38: if_icmpge     52
      41: aload_0
      42: iload_3
      43: iaload
      44: aload_0
      45: iload_3
      46: iconst_1
      47: iadd
      48: iaload
      49: if_icmpeq     56
      52: aload_0
      53: iload_3
      54: iaload
      55: ireturn
      56: iload_3
      57: iload         4
      59: if_icmpeq     68
      62: iinc          3, 2
      65: goto          34
      68: iconst_0
      69: ireturn
}


/*
CyclicRotation
Rotate an array to the right by a given number of steps.

An array A consisting of N integers is given. Rotation of the array means that each element is shifted right by one index, and the last element of the array is moved to the first place. For example, the rotation of array A = [3, 8, 9, 7, 6] is [6, 3, 8, 9, 7] (elements are shifted right by one index and 6 is moved to the first place).
The goal is to rotate array A K times; that is, each element of A will be shifted to the right K times.

Write a function:
class Solution { public int[] solution(int[] A, int K); }
that, given an array A consisting of N integers and an integer K, returns the array A rotated K times.

For example, given
    A = [3, 8, 9, 7, 6]
    K = 3
the function should return [9, 7, 6, 3, 8]. Three rotations were made:
    [3, 8, 9, 7, 6] -> [6, 3, 8, 9, 7]
    [6, 3, 8, 9, 7] -> [7, 6, 3, 8, 9]
    [7, 6, 3, 8, 9] -> [9, 7, 6, 3, 8]
For another example, given
    A = [0, 0, 0]
    K = 1
the function should return [0, 0, 0]
Given
    A = [1, 2, 3, 4]
    K = 4
the function should return [1, 2, 3, 4]

Assume that:
N and K are integers within the range [0..100];
each element of array A is an integer within the range [−1,000..1,000].
In your solution, focus on correctness. The performance of your solution will not be the focus of the assessment.*/

fun solution(array: IntArray, k: Int): IntArray {
    val arrayLength: Int = array.size

    val sol = IntArray(array.size)

    for (i in array.indices) {
        sol[(i + k) % arrayLength] = array.get(i)
    }

    //array.forEach { print(it) }

    return array
}

Compiled from "CyclicRotation.kt"
public final class CyclicRotationKt {
  public static final int[] solution(int[], int);
    Code:
       0: aload_0
       1: ldc           #9                  // String array
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: arraylength
       8: istore_2
       9: aload_0
      10: arraylength
      11: newarray       int
      13: astore_3
      14: iconst_0
      15: istore        4
      17: aload_0
      18: arraylength
      19: istore        5
      21: iload         4
      23: iload         5
      25: if_icmpge     46
      28: aload_3
      29: iload         4
      31: iload_1
      32: iadd
      33: iload_2
      34: irem
      35: aload_0
      36: iload         4
      38: iaload
      39: iastore
      40: iinc          4, 1
      43: goto          21
      46: aload_0
      47: areturn
}


/*
BinaryGap
Find longest sequence of zeros in binary representation of an integer.

A binary gap within a positive integer N is any maximal sequence of consecutive zeros that is surrounded by ones at both ends in the binary representation of N.
For example, number 9 has binary representation 1001 and contains a binary gap of length 2. The number 529 has binary representation 1000010001 and contains two binary gaps: one of length 4 and one of length 3. The number 20 has binary representation 10100 and contains one binary gap of length 1. The number 15 has binary representation 1111 and has no binary gaps. The number 32 has binary representation 100000 and has no binary gaps.

Write a function:
class Solution { public int solution(int N); }
that, given a positive integer N, returns the length of its longest binary gap. The function should return 0 if N doesn't contain a binary gap.
For example, given N = 1041 the function should return 5, because N has binary representation 10000010001 and so its longest binary gap is of length 5. Given N = 32 the function should return 0, because N has binary representation '100000' and thus no binary gaps.
Write an efficient algorithm for the following assumptions:
N is an integer within the range [1..2,147,483,647].*/

fun solution(n: Int): Int {
    val stringBinary = Integer.toBinaryString(n)
    var part = Integer.toBinaryString(n).substring(0, stringBinary.lastIndexOf("1"))
    val parts = part.split("1", part)
    var max = 0;

    for (p in parts) {
        if (p.length > max) max = p.length
    }
    return max;
}

Compiled from "BinaryGap.kt"
public final class BinaryGapKt {
  public static final int solution(int);
    Code:
       0: iload_0
       1: invokestatic  #12                 // Method java/lang/Integer.toBinaryString:(I)Ljava/lang/String;
       4: astore_1
       5: iload_0
       6: invokestatic  #12                 // Method java/lang/Integer.toBinaryString:(I)Ljava/lang/String;
       9: dup
      10: ldc           #14                 // String toBinaryString(...)
      12: invokestatic  #20                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
      15: astore_3
      16: iconst_0
      17: istore        4
      19: aload_1
      20: invokestatic  #24                 // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
      23: aload_1
      24: checkcast     #26                 // class java/lang/CharSequence
      27: ldc           #28                 // String 1
      29: iconst_0
      30: iconst_0
      31: bipush        6
      33: aconst_null
      34: invokestatic  #34                 // Method kotlin/text/StringsKt.lastIndexOf$default:(Ljava/lang/CharSequence;Ljava/lang/String;IZILjava/lang/Object;)I
      37: istore        5
      39: aload_3
      40: iload         4
      42: iload         5
      44: invokevirtual #40                 // Method java/lang/String.substring:(II)Ljava/lang/String;
      47: dup
      48: ldc           #42                 // String substring(...)
      50: invokestatic  #20                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
      53: astore_2
      54: aload_2
      55: checkcast     #26                 // class java/lang/CharSequence
      58: iconst_2
      59: anewarray     #36                 // class java/lang/String
      62: astore        4
      64: aload         4
      66: iconst_0
      67: ldc           #28                 // String 1
      69: aastore
      70: aload         4
      72: iconst_1
      73: aload_2
      74: aastore
      75: aload         4
      77: iconst_0
      78: iconst_0
      79: bipush        6
      81: aconst_null
      82: invokestatic  #46                 // Method kotlin/text/StringsKt.split$default:(Ljava/lang/CharSequence;[Ljava/lang/String;ZIILjava/lang/Object;)Ljava/util/List;
      85: astore_3
      86: iconst_0
      87: istore        4
      89: aload_3
      90: invokeinterface #52,  1           // InterfaceMethod java/util/List.iterator:()Ljava/util/Iterator;
      95: astore        5
      97: aload         5
      99: invokeinterface #58,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     104: ifeq          139
     107: aload         5
     109: invokeinterface #62,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     114: checkcast     #36                 // class java/lang/String
     117: astore        6
     119: aload         6
     121: invokevirtual #66                 // Method java/lang/String.length:()I
     124: iload         4
     126: if_icmple     97
     129: aload         6
     131: invokevirtual #66                 // Method java/lang/String.length:()I
     134: istore        4
     136: goto          97
     139: iload         4
     141: ireturn
}


import kotlin.math.max

fun main() {
    var tmp = readLine()!!.split(' ')
    val n = tmp[0].toInt()
    val balance = tmp[1].toInt()
    val values = Array(n) { 0 }
    val like = Array(n) { 0 }
    val rec = Array(n) { Array(balance + 1) { 0 } }
    for (i in 0 until n) {
        tmp = readLine()!!.split(' ')
        values[i] = tmp[0].toInt()
        like[i] = tmp[1].toInt()
    }
    println(getFav(n - 1, balance, values, like, rec))
}

fun getFav(i: Int, balance: Int, values: Array<Int>, like: Array<Int>, rec: Array<Array<Int>>): Int {
    if (i == -1 || balance <= 0) {
        return 0
    }
    if (rec[i][balance] == 0) {
        if (values[i] > balance) {
            rec[i][balance] = getFav(i - 1, balance, values, like, rec)
        } else {
            rec[i][balance] = max(
                getFav(i - 1, balance, values, like, rec),
                getFav(i - 1, balance - values[i], values, like, rec) + like[i],
            )
        }
    }
    return rec[i][balance]
}

Compiled from "main.kt"
public final class MainKt {
  public static final void main();
    Code:
       0: invokestatic  #12                 // Method kotlin/io/ConsoleKt.readLine:()Ljava/lang/String;
       3: dup
       4: invokestatic  #18                 // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
       7: checkcast     #20                 // class java/lang/CharSequence
      10: iconst_1
      11: newarray       char
      13: astore_1
      14: aload_1
      15: iconst_0
      16: bipush        32
      18: castore
      19: aload_1
      20: iconst_0
      21: iconst_0
      22: bipush        6
      24: aconst_null
      25: invokestatic  #26                 // Method kotlin/text/StringsKt.split$default:(Ljava/lang/CharSequence;[CZIILjava/lang/Object;)Ljava/util/List;
      28: astore_0
      29: aload_0
      30: iconst_0
      31: invokeinterface #32,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      36: checkcast     #34                 // class java/lang/String
      39: invokestatic  #40                 // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
      42: istore_1
      43: aload_0
      44: iconst_1
      45: invokeinterface #32,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      50: checkcast     #34                 // class java/lang/String
      53: invokestatic  #40                 // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
      56: istore_2
      57: iconst_0
      58: istore        4
      60: iload_1
      61: anewarray     #36                 // class java/lang/Integer
      64: astore        5
      66: iload         4
      68: iload_1
      69: if_icmpge     91
      72: iload         4
      74: istore        6
      76: aload         5
      78: iload         6
      80: iconst_0
      81: invokestatic  #44                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      84: aastore
      85: iinc          4, 1
      88: goto          66
      91: aload         5
      93: astore_3
      94: iconst_0
      95: istore        5
      97: iload_1
      98: anewarray     #36                 // class java/lang/Integer
     101: astore        6
     103: iload         5
     105: iload_1
     106: if_icmpge     128
     109: iload         5
     111: istore        7
     113: aload         6
     115: iload         7
     117: iconst_0
     118: invokestatic  #44                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     121: aastore
     122: iinc          5, 1
     125: goto          103
     128: aload         6
     130: astore        4
     132: iconst_0
     133: istore        6
     135: iload_1
     136: anewarray     #46                 // class "[Ljava/lang/Integer;"
     139: astore        7
     141: iload         6
     143: iload_1
     144: if_icmpge     213
     147: iload         6
     149: istore        8
     151: aload         7
     153: iload         8
     155: iconst_0
     156: istore        9
     158: iload_2
     159: iconst_1
     160: iadd
     161: istore        10
     163: iload         10
     165: anewarray     #36                 // class java/lang/Integer
     168: astore        11
     170: istore        14
     172: astore        13
     174: iload         9
     176: iload         10
     178: if_icmpge     200
     181: iload         9
     183: istore        12
     185: aload         11
     187: iload         12
     189: iconst_0
     190: invokestatic  #44                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     193: aastore
     194: iinc          9, 1
     197: goto          174
     200: aload         13
     202: iload         14
     204: aload         11
     206: aastore
     207: iinc          6, 1
     210: goto          141
     213: aload         7
     215: astore        5
     217: iconst_0
     218: istore        6
     220: iload         6
     222: iload_1
     223: if_icmpge     305
     226: invokestatic  #12                 // Method kotlin/io/ConsoleKt.readLine:()Ljava/lang/String;
     229: dup
     230: invokestatic  #18                 // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
     233: checkcast     #20                 // class java/lang/CharSequence
     236: iconst_1
     237: newarray       char
     239: astore        7
     241: aload         7
     243: iconst_0
     244: bipush        32
     246: castore
     247: aload         7
     249: iconst_0
     250: iconst_0
     251: bipush        6
     253: aconst_null
     254: invokestatic  #26                 // Method kotlin/text/StringsKt.split$default:(Ljava/lang/CharSequence;[CZIILjava/lang/Object;)Ljava/util/List;
     257: astore_0
     258: aload_3
     259: iload         6
     261: aload_0
     262: iconst_0
     263: invokeinterface #32,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     268: checkcast     #34                 // class java/lang/String
     271: invokestatic  #40                 // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
     274: invokestatic  #44                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     277: aastore
     278: aload         4
     280: iload         6
     282: aload_0
     283: iconst_1
     284: invokeinterface #32,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     289: checkcast     #34                 // class java/lang/String
     292: invokestatic  #40                 // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
     295: invokestatic  #44                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     298: aastore
     299: iinc          6, 1
     302: goto          220
     305: iload_1
     306: iconst_1
     307: isub
     308: iload_2
     309: aload_3
     310: aload         4
     312: aload         5
     314: invokestatic  #50                 // Method getFav:(II[Ljava/lang/Integer;[Ljava/lang/Integer;[[Ljava/lang/Integer;)I
     317: istore        6
     319: getstatic     #56                 // Field java/lang/System.out:Ljava/io/PrintStream;
     322: iload         6
     324: invokevirtual #62                 // Method java/io/PrintStream.println:(I)V
     327: return

  public static final int getFav(int, int, java.lang.Integer[], java.lang.Integer[], java.lang.Integer[][]);
    Code:
       0: aload_2
       1: ldc           #75                 // String values
       3: invokestatic  #79                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_3
       7: ldc           #80                 // String like
       9: invokestatic  #79                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
      12: aload         4
      14: ldc           #81                 // String rec
      16: invokestatic  #79                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
      19: iload_0
      20: iconst_m1
      21: if_icmpeq     28
      24: iload_1
      25: ifgt          30
      28: iconst_0
      29: ireturn
      30: aload         4
      32: iload_0
      33: aaload
      34: iload_1
      35: aaload
      36: invokevirtual #85                 // Method java/lang/Integer.intValue:()I
      39: ifne          124
      42: aload_2
      43: iload_0
      44: aaload
      45: invokevirtual #85                 // Method java/lang/Integer.intValue:()I
      48: iload_1
      49: if_icmple     75
      52: aload         4
      54: iload_0
      55: aaload
      56: iload_1
      57: iload_0
      58: iconst_1
      59: isub
      60: iload_1
      61: aload_2
      62: aload_3
      63: aload         4
      65: invokestatic  #50                 // Method getFav:(II[Ljava/lang/Integer;[Ljava/lang/Integer;[[Ljava/lang/Integer;)I
      68: invokestatic  #44                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      71: aastore
      72: goto          124
      75: aload         4
      77: iload_0
      78: aaload
      79: iload_1
      80: nop
      81: iload_0
      82: iconst_1
      83: isub
      84: iload_1
      85: aload_2
      86: aload_3
      87: aload         4
      89: invokestatic  #50                 // Method getFav:(II[Ljava/lang/Integer;[Ljava/lang/Integer;[[Ljava/lang/Integer;)I
      92: iload_0
      93: iconst_1
      94: isub
      95: iload_1
      96: aload_2
      97: iload_0
      98: aaload
      99: invokevirtual #85                 // Method java/lang/Integer.intValue:()I
     102: isub
     103: aload_2
     104: aload_3
     105: aload         4
     107: invokestatic  #50                 // Method getFav:(II[Ljava/lang/Integer;[Ljava/lang/Integer;[[Ljava/lang/Integer;)I
     110: aload_3
     111: iload_0
     112: aaload
     113: invokevirtual #85                 // Method java/lang/Integer.intValue:()I
     116: iadd
     117: invokestatic  #91                 // Method java/lang/Math.max:(II)I
     120: invokestatic  #44                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     123: aastore
     124: aload         4
     126: iload_0
     127: aaload
     128: iload_1
     129: aaload
     130: invokevirtual #85                 // Method java/lang/Integer.intValue:()I
     133: ireturn

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


import java.io.File

private val numberRegex = "\\d+".toRegex()

/**
 * With this puzzle we do not use stack data structure in part 1, even though it is maybe easier.
 * Because I want to keep most common logic from both part of the puzzle.
 */
fun puzzle5(args: Array<String>, isOnWrongShip: Boolean) {
    val graphStacks = args[0]
    val moveStrategy = args[1]

    val graphMatrix = graphStacks.getGraphMatrix()

    File(moveStrategy)
        .readLines()
        .map {
            it.getCraneMove().mapWithArrayIndex()
        }
        .forEach {
            if (isOnWrongShip) {
                moveCraneOn9000Ship(graphMatrix, it)
            } else {
                moveCraneOn9001Ship(graphMatrix, it)
            }
        }

    println(
        graphMatrix.mapNotNull { it.lastOrNull() }.joinToString("")
    )
}

private fun moveCraneOn9000Ship(
    graphMatrix: List<MutableList<Char>>,
    moveStrategy: Triple<Int, Int, Int>
) {
    val (quantity, from, to) = moveStrategy
    repeat(quantity) { _ ->
        graphMatrix[from].lastOrNull()?.let { item ->
            graphMatrix[to].add(item)
            graphMatrix[from].removeLast()
        }
    }
}

private fun moveCraneOn9001Ship(
    graphMatrix: List<MutableList<Char>>,
    moveStrategy: Triple<Int, Int, Int>
) {
    val (quantity, from, to) = moveStrategy
    graphMatrix[to].addAll(graphMatrix[from].takeLast(quantity))
    repeat(quantity) {
        graphMatrix[from].removeLastOrNull()
    }
}

private fun String.getGraphMatrix(): List<MutableList<Char>> {
    val graphMatrix = mutableListOf<MutableList<Char>>()
    File(this)
        .readLines()
        .map {
            it.toCharArray()
        }
        .forEach { row ->
            for ((columnIndex, rowIndex) in (1 until row.size step 4).withIndex()) {
                if (columnIndex > graphMatrix.lastIndex) {
                    graphMatrix.add(mutableListOf())
                }
                val item = row[rowIndex]
                if (item.isLetter()) {
                    graphMatrix[columnIndex].add(0, item)
                }
            }
        }
    return graphMatrix
}

private fun String.getCraneMove(): Triple<Int, Int, Int> {
    val (first, second, third) = numberRegex
        .findAll(this)
        .mapNotNull { it.value.toIntOrNull() }
        .toList()
    return Triple(first, second, third)
}

private fun Triple<Int, Int, Int>.mapWithArrayIndex(): Triple<Int, Int, Int> {
    return Triple(
        first = this.first,
        second = this.second - 1,
        third = this.third - 1
    )
}

Compiled from "Puzzle5.kt"
public final class Puzzle5Kt {
  private static final kotlin.text.Regex numberRegex;

  public static final void puzzle5(java.lang.String[], boolean);
    Code:
       0: aload_0
       1: ldc           #9                  // String args
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: iconst_0
       8: aaload
       9: astore_2
      10: aload_0
      11: iconst_1
      12: aaload
      13: astore_3
      14: aload_2
      15: invokestatic  #19                 // Method getGraphMatrix:(Ljava/lang/String;)Ljava/util/List;
      18: astore        4
      20: new           #21                 // class java/io/File
      23: dup
      24: aload_3
      25: invokespecial #25                 // Method java/io/File."<init>":(Ljava/lang/String;)V
      28: aconst_null
      29: iconst_1
      30: aconst_null
      31: invokestatic  #31                 // Method kotlin/io/FilesKt.readLines$default:(Ljava/io/File;Ljava/nio/charset/Charset;ILjava/lang/Object;)Ljava/util/List;
      34: checkcast     #33                 // class java/lang/Iterable
      37: astore        5
      39: nop
      40: iconst_0
      41: istore        6
      43: aload         5
      45: astore        7
      47: new           #35                 // class java/util/ArrayList
      50: dup
      51: aload         5
      53: bipush        10
      55: invokestatic  #41                 // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
      58: invokespecial #44                 // Method java/util/ArrayList."<init>":(I)V
      61: checkcast     #46                 // class java/util/Collection
      64: astore        8
      66: iconst_0
      67: istore        9
      69: aload         7
      71: invokeinterface #50,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      76: astore        10
      78: aload         10
      80: invokeinterface #56,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      85: ifeq          131
      88: aload         10
      90: invokeinterface #60,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      95: astore        11
      97: aload         8
      99: aload         11
     101: checkcast     #62                 // class java/lang/String
     104: astore        12
     106: astore        20
     108: iconst_0
     109: istore        13
     111: aload         12
     113: invokestatic  #66                 // Method getCraneMove:(Ljava/lang/String;)Lkotlin/Triple;
     116: invokestatic  #70                 // Method mapWithArrayIndex:(Lkotlin/Triple;)Lkotlin/Triple;
     119: aload         20
     121: swap
     122: invokeinterface #74,  2           // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     127: pop
     128: goto          78
     131: aload         8
     133: checkcast     #76                 // class java/util/List
     136: nop
     137: checkcast     #33                 // class java/lang/Iterable
     140: astore        5
     142: nop
     143: iconst_0
     144: istore        6
     146: aload         5
     148: invokeinterface #50,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     153: astore        7
     155: aload         7
     157: invokeinterface #56,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     162: ifeq          210
     165: aload         7
     167: invokeinterface #60,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     172: astore        8
     174: aload         8
     176: checkcast     #78                 // class kotlin/Triple
     179: astore        9
     181: iconst_0
     182: istore        10
     184: iload_1
     185: ifeq          198
     188: aload         4
     190: aload         9
     192: invokestatic  #82                 // Method moveCraneOn9000Ship:(Ljava/util/List;Lkotlin/Triple;)V
     195: goto          205
     198: aload         4
     200: aload         9
     202: invokestatic  #85                 // Method moveCraneOn9001Ship:(Ljava/util/List;Lkotlin/Triple;)V
     205: nop
     206: nop
     207: goto          155
     210: nop
     211: aload         4
     213: checkcast     #33                 // class java/lang/Iterable
     216: astore        5
     218: iconst_0
     219: istore        6
     221: aload         5
     223: astore        7
     225: new           #35                 // class java/util/ArrayList
     228: dup
     229: invokespecial #88                 // Method java/util/ArrayList."<init>":()V
     232: checkcast     #46                 // class java/util/Collection
     235: astore        8
     237: iconst_0
     238: istore        9
     240: aload         7
     242: astore        10
     244: iconst_0
     245: istore        11
     247: aload         10
     249: invokeinterface #50,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     254: astore        12
     256: aload         12
     258: invokeinterface #56,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     263: ifeq          327
     266: aload         12
     268: invokeinterface #60,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     273: astore        13
     275: aload         13
     277: astore        14
     279: iconst_0
     280: istore        15
     282: aload         14
     284: checkcast     #76                 // class java/util/List
     287: astore        16
     289: iconst_0
     290: istore        17
     292: aload         16
     294: invokestatic  #92                 // Method kotlin/collections/CollectionsKt.lastOrNull:(Ljava/util/List;)Ljava/lang/Object;
     297: checkcast     #94                 // class java/lang/Character
     300: dup
     301: ifnull        322
     304: astore        18
     306: iconst_0
     307: istore        19
     309: aload         8
     311: aload         18
     313: invokeinterface #74,  2           // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     318: pop
     319: goto          323
     322: pop
     323: nop
     324: goto          256
     327: nop
     328: aload         8
     330: checkcast     #76                 // class java/util/List
     333: nop
     334: checkcast     #33                 // class java/lang/Iterable
     337: ldc           #96                 // String
     339: checkcast     #98                 // class java/lang/CharSequence
     342: aconst_null
     343: aconst_null
     344: iconst_0
     345: aconst_null
     346: aconst_null
     347: bipush        62
     349: aconst_null
     350: invokestatic  #102                // Method kotlin/collections/CollectionsKt.joinToString$default:(Ljava/lang/Iterable;Ljava/lang/CharSequence;Ljava/lang/CharSequence;Ljava/lang/CharSequence;ILjava/lang/CharSequence;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Ljava/lang/String;
     353: astore        5
     355: getstatic     #108                // Field java/lang/System.out:Ljava/io/PrintStream;
     358: aload         5
     360: invokevirtual #114                // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
     363: return

  private static final void moveCraneOn9000Ship(java.util.List<? extends java.util.List<java.lang.Character>>, kotlin.Triple<java.lang.Integer, java.lang.Integer, java.lang.Integer>);
    Code:
       0: aload_1
       1: invokevirtual #155                // Method kotlin/Triple.component1:()Ljava/lang/Object;
       4: checkcast     #157                // class java/lang/Number
       7: invokevirtual #161                // Method java/lang/Number.intValue:()I
      10: istore_2
      11: aload_1
      12: invokevirtual #164                // Method kotlin/Triple.component2:()Ljava/lang/Object;
      15: checkcast     #157                // class java/lang/Number
      18: invokevirtual #161                // Method java/lang/Number.intValue:()I
      21: istore_3
      22: aload_1
      23: invokevirtual #167                // Method kotlin/Triple.component3:()Ljava/lang/Object;
      26: checkcast     #157                // class java/lang/Number
      29: invokevirtual #161                // Method java/lang/Number.intValue:()I
      32: istore        4
      34: iconst_0
      35: istore        5
      37: iload         5
      39: iload_2
      40: if_icmpge     125
      43: iconst_0
      44: istore        6
      46: aload_0
      47: iload_3
      48: invokeinterface #171,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      53: checkcast     #76                 // class java/util/List
      56: invokestatic  #92                 // Method kotlin/collections/CollectionsKt.lastOrNull:(Ljava/util/List;)Ljava/lang/Object;
      59: checkcast     #94                 // class java/lang/Character
      62: dup
      63: ifnull        116
      66: invokevirtual #175                // Method java/lang/Character.charValue:()C
      69: istore        7
      71: iconst_0
      72: istore        8
      74: aload_0
      75: iload         4
      77: invokeinterface #171,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      82: checkcast     #76                 // class java/util/List
      85: iload         7
      87: invokestatic  #179                // Method java/lang/Character.valueOf:(C)Ljava/lang/Character;
      90: invokeinterface #180,  2          // InterfaceMethod java/util/List.add:(Ljava/lang/Object;)Z
      95: pop
      96: aload_0
      97: iload_3
      98: invokeinterface #171,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     103: checkcast     #76                 // class java/util/List
     106: invokeinterface #183,  1          // InterfaceMethod java/util/List.removeLast:()Ljava/lang/Object;
     111: pop
     112: nop
     113: goto          118
     116: pop
     117: nop
     118: nop
     119: iinc          5, 1
     122: goto          37
     125: return

  private static final void moveCraneOn9001Ship(java.util.List<? extends java.util.List<java.lang.Character>>, kotlin.Triple<java.lang.Integer, java.lang.Integer, java.lang.Integer>);
    Code:
       0: aload_1
       1: invokevirtual #155                // Method kotlin/Triple.component1:()Ljava/lang/Object;
       4: checkcast     #157                // class java/lang/Number
       7: invokevirtual #161                // Method java/lang/Number.intValue:()I
      10: istore_2
      11: aload_1
      12: invokevirtual #164                // Method kotlin/Triple.component2:()Ljava/lang/Object;
      15: checkcast     #157                // class java/lang/Number
      18: invokevirtual #161                // Method java/lang/Number.intValue:()I
      21: istore_3
      22: aload_1
      23: invokevirtual #167                // Method kotlin/Triple.component3:()Ljava/lang/Object;
      26: checkcast     #157                // class java/lang/Number
      29: invokevirtual #161                // Method java/lang/Number.intValue:()I
      32: istore        4
      34: aload_0
      35: iload         4
      37: invokeinterface #171,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      42: checkcast     #76                 // class java/util/List
      45: aload_0
      46: iload_3
      47: invokeinterface #171,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      52: checkcast     #76                 // class java/util/List
      55: iload_2
      56: invokestatic  #194                // Method kotlin/collections/CollectionsKt.takeLast:(Ljava/util/List;I)Ljava/util/List;
      59: checkcast     #46                 // class java/util/Collection
      62: invokeinterface #198,  2          // InterfaceMethod java/util/List.addAll:(Ljava/util/Collection;)Z
      67: pop
      68: iconst_0
      69: istore        5
      71: iload         5
      73: iload_2
      74: if_icmpge     105
      77: iload         5
      79: istore        6
      81: iconst_0
      82: istore        7
      84: aload_0
      85: iload_3
      86: invokeinterface #171,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      91: checkcast     #76                 // class java/util/List
      94: invokestatic  #201                // Method kotlin/collections/CollectionsKt.removeLastOrNull:(Ljava/util/List;)Ljava/lang/Object;
      97: pop
      98: nop
      99: iinc          5, 1
     102: goto          71
     105: return

  private static final java.util.List<java.util.List<java.lang.Character>> getGraphMatrix(java.lang.String);
    Code:
       0: new           #35                 // class java/util/ArrayList
       3: dup
       4: invokespecial #88                 // Method java/util/ArrayList."<init>":()V
       7: checkcast     #76                 // class java/util/List
      10: astore_1
      11: new           #21                 // class java/io/File
      14: dup
      15: aload_0
      16: invokespecial #25                 // Method java/io/File."<init>":(Ljava/lang/String;)V
      19: aconst_null
      20: iconst_1
      21: aconst_null
      22: invokestatic  #31                 // Method kotlin/io/FilesKt.readLines$default:(Ljava/io/File;Ljava/nio/charset/Charset;ILjava/lang/Object;)Ljava/util/List;
      25: checkcast     #33                 // class java/lang/Iterable
      28: astore_2
      29: nop
      30: iconst_0
      31: istore_3
      32: aload_2
      33: astore        4
      35: new           #35                 // class java/util/ArrayList
      38: dup
      39: aload_2
      40: bipush        10
      42: invokestatic  #41                 // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
      45: invokespecial #44                 // Method java/util/ArrayList."<init>":(I)V
      48: checkcast     #46                 // class java/util/Collection
      51: astore        5
      53: iconst_0
      54: istore        6
      56: aload         4
      58: invokeinterface #50,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      63: astore        7
      65: aload         7
      67: invokeinterface #56,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      72: ifeq          122
      75: aload         7
      77: invokeinterface #60,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      82: astore        8
      84: aload         5
      86: aload         8
      88: checkcast     #62                 // class java/lang/String
      91: astore        9
      93: astore        16
      95: iconst_0
      96: istore        10
      98: aload         9
     100: invokevirtual #207                // Method java/lang/String.toCharArray:()[C
     103: dup
     104: ldc           #209                // String toCharArray(...)
     106: invokestatic  #212                // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
     109: nop
     110: aload         16
     112: swap
     113: invokeinterface #74,  2           // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     118: pop
     119: goto          65
     122: aload         5
     124: checkcast     #76                 // class java/util/List
     127: nop
     128: checkcast     #33                 // class java/lang/Iterable
     131: astore_2
     132: nop
     133: iconst_0
     134: istore_3
     135: aload_2
     136: invokeinterface #50,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     141: astore        4
     143: aload         4
     145: invokeinterface #56,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     150: ifeq          335
     153: aload         4
     155: invokeinterface #60,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     160: astore        5
     162: aload         5
     164: checkcast     #214                // class "[C"
     167: astore        6
     169: iconst_0
     170: istore        7
     172: iconst_1
     173: aload         6
     175: arraylength
     176: invokestatic  #220                // Method kotlin/ranges/RangesKt.until:(II)Lkotlin/ranges/IntRange;
     179: checkcast     #222                // class kotlin/ranges/IntProgression
     182: iconst_4
     183: invokestatic  #226                // Method kotlin/ranges/RangesKt.step:(Lkotlin/ranges/IntProgression;I)Lkotlin/ranges/IntProgression;
     186: astore        8
     188: aload         8
     190: invokevirtual #229                // Method kotlin/ranges/IntProgression.getFirst:()I
     193: istore        9
     195: aload         8
     197: invokevirtual #232                // Method kotlin/ranges/IntProgression.getLast:()I
     200: istore        10
     202: aload         8
     204: invokevirtual #235                // Method kotlin/ranges/IntProgression.getStep:()I
     207: istore        11
     209: iconst_0
     210: istore        12
     212: iload         11
     214: ifle          224
     217: iload         9
     219: iload         10
     221: if_icmple     236
     224: iload         11
     226: ifge          330
     229: iload         10
     231: iload         9
     233: if_icmpgt     330
     236: iload         12
     238: istore        13
     240: iload         12
     242: iconst_1
     243: iadd
     244: istore        12
     246: iload         9
     248: istore        14
     250: iload         13
     252: aload_1
     253: invokestatic  #239                // Method kotlin/collections/CollectionsKt.getLastIndex:(Ljava/util/List;)I
     256: if_icmple     276
     259: aload_1
     260: new           #35                 // class java/util/ArrayList
     263: dup
     264: invokespecial #88                 // Method java/util/ArrayList."<init>":()V
     267: checkcast     #76                 // class java/util/List
     270: invokeinterface #180,  2          // InterfaceMethod java/util/List.add:(Ljava/lang/Object;)Z
     275: pop
     276: aload         6
     278: iload         14
     280: caload
     281: istore        15
     283: iload         15
     285: invokestatic  #243                // Method java/lang/Character.isLetter:(C)Z
     288: ifeq          313
     291: aload_1
     292: iload         13
     294: invokeinterface #171,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     299: checkcast     #76                 // class java/util/List
     302: iconst_0
     303: iload         15
     305: invokestatic  #179                // Method java/lang/Character.valueOf:(C)Ljava/lang/Character;
     308: invokeinterface #246,  3          // InterfaceMethod java/util/List.add:(ILjava/lang/Object;)V
     313: iload         14
     315: iload         10
     317: if_icmpeq     330
     320: iload         9
     322: iload         11
     324: iadd
     325: istore        9
     327: goto          236
     330: nop
     331: nop
     332: goto          143
     335: nop
     336: aload_1
     337: areturn

  private static final kotlin.Triple<java.lang.Integer, java.lang.Integer, java.lang.Integer> getCraneMove(java.lang.String);
    Code:
       0: getstatic     #257                // Field numberRegex:Lkotlin/text/Regex;
       3: aload_0
       4: checkcast     #98                 // class java/lang/CharSequence
       7: iconst_0
       8: iconst_2
       9: aconst_null
      10: invokestatic  #263                // Method kotlin/text/Regex.findAll$default:(Lkotlin/text/Regex;Ljava/lang/CharSequence;IILjava/lang/Object;)Lkotlin/sequences/Sequence;
      13: invokedynamic #282,  0            // InvokeDynamic #0:invoke:()Lkotlin/jvm/functions/Function1;
      18: invokestatic  #288                // Method kotlin/sequences/SequencesKt.mapNotNull:(Lkotlin/sequences/Sequence;Lkotlin/jvm/functions/Function1;)Lkotlin/sequences/Sequence;
      21: invokestatic  #292                // Method kotlin/sequences/SequencesKt.toList:(Lkotlin/sequences/Sequence;)Ljava/util/List;
      24: astore_1
      25: aload_1
      26: iconst_0
      27: invokeinterface #171,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      32: checkcast     #157                // class java/lang/Number
      35: invokevirtual #161                // Method java/lang/Number.intValue:()I
      38: istore_2
      39: aload_1
      40: iconst_1
      41: invokeinterface #171,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      46: checkcast     #157                // class java/lang/Number
      49: invokevirtual #161                // Method java/lang/Number.intValue:()I
      52: istore_3
      53: aload_1
      54: iconst_2
      55: invokeinterface #171,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      60: checkcast     #157                // class java/lang/Number
      63: invokevirtual #161                // Method java/lang/Number.intValue:()I
      66: istore        4
      68: new           #78                 // class kotlin/Triple
      71: dup
      72: iload_2
      73: invokestatic  #297                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      76: iload_3
      77: invokestatic  #297                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      80: iload         4
      82: invokestatic  #297                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      85: invokespecial #300                // Method kotlin/Triple."<init>":(Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)V
      88: areturn

  private static final kotlin.Triple<java.lang.Integer, java.lang.Integer, java.lang.Integer> mapWithArrayIndex(kotlin.Triple<java.lang.Integer, java.lang.Integer, java.lang.Integer>);
    Code:
       0: new           #78                 // class kotlin/Triple
       3: dup
       4: aload_0
       5: invokevirtual #307                // Method kotlin/Triple.getFirst:()Ljava/lang/Object;
       8: aload_0
       9: invokevirtual #310                // Method kotlin/Triple.getSecond:()Ljava/lang/Object;
      12: checkcast     #157                // class java/lang/Number
      15: invokevirtual #161                // Method java/lang/Number.intValue:()I
      18: iconst_1
      19: isub
      20: invokestatic  #297                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      23: aload_0
      24: invokevirtual #313                // Method kotlin/Triple.getThird:()Ljava/lang/Object;
      27: checkcast     #157                // class java/lang/Number
      30: invokevirtual #161                // Method java/lang/Number.intValue:()I
      33: iconst_1
      34: isub
      35: invokestatic  #297                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      38: invokespecial #300                // Method kotlin/Triple."<init>":(Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)V
      41: areturn

  private static final java.lang.Integer getCraneMove$lambda$8(kotlin.text.MatchResult);
    Code:
       0: aload_0
       1: ldc_w         #315                // String it
       4: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       7: aload_0
       8: invokeinterface #321,  1          // InterfaceMethod kotlin/text/MatchResult.getValue:()Ljava/lang/String;
      13: invokestatic  #327                // Method kotlin/text/StringsKt.toIntOrNull:(Ljava/lang/String;)Ljava/lang/Integer;
      16: areturn

  static {};
    Code:
       0: new           #259                // class kotlin/text/Regex
       3: dup
       4: ldc_w         #331                // String \\d+
       7: invokespecial #332                // Method kotlin/text/Regex."<init>":(Ljava/lang/String;)V
      10: putstatic     #257                // Field numberRegex:Lkotlin/text/Regex;
      13: return
}


import java.io.File

fun puzzle3Part1(args: Array<String>) {
    var sum = 0
    File(args[0])
        .readLines()
        .forEach {
            val commonItem = it.toCharArray().getCommonItem()
            if (commonItem != null) {
                sum += commonItem.toPriorityPoint()
            }
        }
    println(sum)
}

private fun CharArray.getCommonItem(): Char? {
    // as the requirement, the array item always have size is even number
    // and always have common item
    val compartmentSize = this.size / 2
    val compartmentLeft = this.take(compartmentSize).toHashSet()
    val compartmentRight = this.takeLast(compartmentSize).toHashSet()
    compartmentLeft.forEach {
        if (it in compartmentRight) return it
    }
    return null
}

fun puzzle3Part2(args: Array<String>) {
    var sum = 0
    File(args[0])
        .readLines()
        .chunked(3)
        .forEach {
            val groupBadge = it.getGroupBadge()
            if (groupBadge != null) {
                sum += groupBadge.toPriorityPoint()
            }
        }
    println(sum)
}

private fun List<String>.getGroupBadge(): Char? {
    val (rucksack1, rucksack2, rucksack3) = this.map { it.toCharArray().toHashSet() }
    return when {
        rucksack1.size * 2 < rucksack2.size + rucksack3.size -> {
            getGroupBadge(rucksack1, rucksack2, rucksack3)
        }
        rucksack2.size * 2 < rucksack1.size + rucksack3.size -> {
            getGroupBadge(rucksack2, rucksack1, rucksack3)
        }
        else -> {
            getGroupBadge(rucksack3, rucksack1, rucksack2)
        }
    }
}

private fun getGroupBadge(rucksack1: HashSet<Char>, rucksack2: HashSet<Char>, rucksack3: HashSet<Char>): Char? {
    rucksack1.forEach {
        if (it in rucksack2 && it in rucksack3) return it
    }
    return null
}

private fun Char.toPriorityPoint(): Int {
    return if (this.isUpperCase()) {
        this.code - 38
    } else this.code - 96
}

Compiled from "Puzzle3.kt"
public final class Puzzle3Kt {
  public static final void puzzle3Part1(java.lang.String[]);
    Code:
       0: aload_0
       1: ldc           #9                  // String args
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: iconst_0
       7: istore_1
       8: new           #17                 // class java/io/File
      11: dup
      12: aload_0
      13: iconst_0
      14: aaload
      15: invokespecial #21                 // Method java/io/File."<init>":(Ljava/lang/String;)V
      18: aconst_null
      19: iconst_1
      20: aconst_null
      21: invokestatic  #27                 // Method kotlin/io/FilesKt.readLines$default:(Ljava/io/File;Ljava/nio/charset/Charset;ILjava/lang/Object;)Ljava/util/List;
      24: checkcast     #29                 // class java/lang/Iterable
      27: astore_2
      28: nop
      29: iconst_0
      30: istore_3
      31: aload_2
      32: invokeinterface #33,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      37: astore        4
      39: aload         4
      41: invokeinterface #39,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      46: ifeq          105
      49: aload         4
      51: invokeinterface #43,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      56: astore        5
      58: aload         5
      60: checkcast     #45                 // class java/lang/String
      63: astore        6
      65: iconst_0
      66: istore        7
      68: aload         6
      70: invokevirtual #49                 // Method java/lang/String.toCharArray:()[C
      73: dup
      74: ldc           #51                 // String toCharArray(...)
      76: invokestatic  #54                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
      79: invokestatic  #58                 // Method getCommonItem:([C)Ljava/lang/Character;
      82: astore        8
      84: aload         8
      86: ifnull        100
      89: iload_1
      90: aload         8
      92: invokevirtual #64                 // Method java/lang/Character.charValue:()C
      95: invokestatic  #68                 // Method toPriorityPoint:(C)I
      98: iadd
      99: istore_1
     100: nop
     101: nop
     102: goto          39
     105: nop
     106: iload_1
     107: istore_2
     108: getstatic     #74                 // Field java/lang/System.out:Ljava/io/PrintStream;
     111: iload_2
     112: invokevirtual #80                 // Method java/io/PrintStream.println:(I)V
     115: return

  private static final java.lang.Character getCommonItem(char[]);
    Code:
       0: aload_0
       1: arraylength
       2: iconst_2
       3: idiv
       4: istore_1
       5: aload_0
       6: iload_1
       7: invokestatic  #100                // Method kotlin/collections/ArraysKt.take:([CI)Ljava/util/List;
      10: checkcast     #29                 // class java/lang/Iterable
      13: invokestatic  #106                // Method kotlin/collections/CollectionsKt.toHashSet:(Ljava/lang/Iterable;)Ljava/util/HashSet;
      16: astore_2
      17: aload_0
      18: iload_1
      19: invokestatic  #109                // Method kotlin/collections/ArraysKt.takeLast:([CI)Ljava/util/List;
      22: checkcast     #29                 // class java/lang/Iterable
      25: invokestatic  #106                // Method kotlin/collections/CollectionsKt.toHashSet:(Ljava/lang/Iterable;)Ljava/util/HashSet;
      28: astore_3
      29: aload_2
      30: checkcast     #29                 // class java/lang/Iterable
      33: astore        4
      35: iconst_0
      36: istore        5
      38: aload         4
      40: invokeinterface #33,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      45: astore        6
      47: aload         6
      49: invokeinterface #39,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      54: ifeq          102
      57: aload         6
      59: invokeinterface #43,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      64: astore        7
      66: aload         7
      68: checkcast     #60                 // class java/lang/Character
      71: invokevirtual #64                 // Method java/lang/Character.charValue:()C
      74: istore        8
      76: iconst_0
      77: istore        9
      79: aload_3
      80: iload         8
      82: invokestatic  #113                // Method java/lang/Character.valueOf:(C)Ljava/lang/Character;
      85: invokevirtual #119                // Method java/util/HashSet.contains:(Ljava/lang/Object;)Z
      88: ifeq          97
      91: iload         8
      93: invokestatic  #113                // Method java/lang/Character.valueOf:(C)Ljava/lang/Character;
      96: areturn
      97: nop
      98: nop
      99: goto          47
     102: nop
     103: aconst_null
     104: areturn

  public static final void puzzle3Part2(java.lang.String[]);
    Code:
       0: aload_0
       1: ldc           #9                  // String args
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: iconst_0
       7: istore_1
       8: new           #17                 // class java/io/File
      11: dup
      12: aload_0
      13: iconst_0
      14: aaload
      15: invokespecial #21                 // Method java/io/File."<init>":(Ljava/lang/String;)V
      18: aconst_null
      19: iconst_1
      20: aconst_null
      21: invokestatic  #27                 // Method kotlin/io/FilesKt.readLines$default:(Ljava/io/File;Ljava/nio/charset/Charset;ILjava/lang/Object;)Ljava/util/List;
      24: checkcast     #29                 // class java/lang/Iterable
      27: iconst_3
      28: invokestatic  #133                // Method kotlin/collections/CollectionsKt.chunked:(Ljava/lang/Iterable;I)Ljava/util/List;
      31: checkcast     #29                 // class java/lang/Iterable
      34: astore_2
      35: nop
      36: iconst_0
      37: istore_3
      38: aload_2
      39: invokeinterface #33,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      44: astore        4
      46: aload         4
      48: invokeinterface #39,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      53: ifeq          103
      56: aload         4
      58: invokeinterface #43,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      63: astore        5
      65: aload         5
      67: checkcast     #135                // class java/util/List
      70: astore        6
      72: iconst_0
      73: istore        7
      75: aload         6
      77: invokestatic  #139                // Method getGroupBadge:(Ljava/util/List;)Ljava/lang/Character;
      80: astore        8
      82: aload         8
      84: ifnull        98
      87: iload_1
      88: aload         8
      90: invokevirtual #64                 // Method java/lang/Character.charValue:()C
      93: invokestatic  #68                 // Method toPriorityPoint:(C)I
      96: iadd
      97: istore_1
      98: nop
      99: nop
     100: goto          46
     103: nop
     104: iload_1
     105: istore_2
     106: getstatic     #74                 // Field java/lang/System.out:Ljava/io/PrintStream;
     109: iload_2
     110: invokevirtual #80                 // Method java/io/PrintStream.println:(I)V
     113: return

  private static final java.lang.Character getGroupBadge(java.util.List<java.lang.String>);
    Code:
       0: aload_0
       1: checkcast     #29                 // class java/lang/Iterable
       4: astore_2
       5: iconst_0
       6: istore_3
       7: aload_2
       8: astore        4
      10: new           #145                // class java/util/ArrayList
      13: dup
      14: aload_2
      15: bipush        10
      17: invokestatic  #149                // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
      20: invokespecial #151                // Method java/util/ArrayList."<init>":(I)V
      23: checkcast     #153                // class java/util/Collection
      26: astore        5
      28: iconst_0
      29: istore        6
      31: aload         4
      33: invokeinterface #33,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      38: astore        7
      40: aload         7
      42: invokeinterface #39,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      47: ifeq          99
      50: aload         7
      52: invokeinterface #43,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      57: astore        8
      59: aload         5
      61: aload         8
      63: checkcast     #45                 // class java/lang/String
      66: astore        9
      68: astore        11
      70: iconst_0
      71: istore        10
      73: aload         9
      75: invokevirtual #49                 // Method java/lang/String.toCharArray:()[C
      78: dup
      79: ldc           #51                 // String toCharArray(...)
      81: invokestatic  #54                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
      84: invokestatic  #156                // Method kotlin/collections/ArraysKt.toHashSet:([C)Ljava/util/HashSet;
      87: aload         11
      89: swap
      90: invokeinterface #159,  2          // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
      95: pop
      96: goto          40
      99: aload         5
     101: checkcast     #135                // class java/util/List
     104: nop
     105: astore_1
     106: aload_1
     107: iconst_0
     108: invokeinterface #163,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     113: checkcast     #115                // class java/util/HashSet
     116: astore_2
     117: aload_1
     118: iconst_1
     119: invokeinterface #163,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     124: checkcast     #115                // class java/util/HashSet
     127: astore_3
     128: aload_1
     129: iconst_2
     130: invokeinterface #163,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     135: checkcast     #115                // class java/util/HashSet
     138: astore        4
     140: nop
     141: aload_2
     142: invokevirtual #167                // Method java/util/HashSet.size:()I
     145: iconst_2
     146: imul
     147: aload_3
     148: invokevirtual #167                // Method java/util/HashSet.size:()I
     151: aload         4
     153: invokevirtual #167                // Method java/util/HashSet.size:()I
     156: iadd
     157: if_icmpge     170
     160: aload_2
     161: aload_3
     162: aload         4
     164: invokestatic  #170                // Method getGroupBadge:(Ljava/util/HashSet;Ljava/util/HashSet;Ljava/util/HashSet;)Ljava/lang/Character;
     167: goto          206
     170: aload_3
     171: invokevirtual #167                // Method java/util/HashSet.size:()I
     174: iconst_2
     175: imul
     176: aload_2
     177: invokevirtual #167                // Method java/util/HashSet.size:()I
     180: aload         4
     182: invokevirtual #167                // Method java/util/HashSet.size:()I
     185: iadd
     186: if_icmpge     199
     189: aload_3
     190: aload_2
     191: aload         4
     193: invokestatic  #170                // Method getGroupBadge:(Ljava/util/HashSet;Ljava/util/HashSet;Ljava/util/HashSet;)Ljava/lang/Character;
     196: goto          206
     199: aload         4
     201: aload_2
     202: aload_3
     203: invokestatic  #170                // Method getGroupBadge:(Ljava/util/HashSet;Ljava/util/HashSet;Ljava/util/HashSet;)Ljava/lang/Character;
     206: areturn

  private static final java.lang.Character getGroupBadge(java.util.HashSet<java.lang.Character>, java.util.HashSet<java.lang.Character>, java.util.HashSet<java.lang.Character>);
    Code:
       0: aload_0
       1: checkcast     #29                 // class java/lang/Iterable
       4: astore_3
       5: iconst_0
       6: istore        4
       8: aload_3
       9: invokeinterface #33,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      14: astore        5
      16: aload         5
      18: invokeinterface #39,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      23: ifeq          83
      26: aload         5
      28: invokeinterface #43,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      33: astore        6
      35: aload         6
      37: checkcast     #60                 // class java/lang/Character
      40: invokevirtual #64                 // Method java/lang/Character.charValue:()C
      43: istore        7
      45: iconst_0
      46: istore        8
      48: aload_1
      49: iload         7
      51: invokestatic  #113                // Method java/lang/Character.valueOf:(C)Ljava/lang/Character;
      54: invokevirtual #119                // Method java/util/HashSet.contains:(Ljava/lang/Object;)Z
      57: ifeq          78
      60: aload_2
      61: iload         7
      63: invokestatic  #113                // Method java/lang/Character.valueOf:(C)Ljava/lang/Character;
      66: invokevirtual #119                // Method java/util/HashSet.contains:(Ljava/lang/Object;)Z
      69: ifeq          78
      72: iload         7
      74: invokestatic  #113                // Method java/lang/Character.valueOf:(C)Ljava/lang/Character;
      77: areturn
      78: nop
      79: nop
      80: goto          16
      83: nop
      84: aconst_null
      85: areturn

  private static final int toPriorityPoint(char);
    Code:
       0: iload_0
       1: invokestatic  #188                // Method java/lang/Character.isUpperCase:(C)Z
       4: ifeq          14
       7: iload_0
       8: bipush        38
      10: isub
      11: goto          18
      14: iload_0
      15: bipush        96
      17: isub
      18: ireturn
}


/*
A number is called happy if it leads to 1 after a sequence of steps
wherein each step number is replaced by the sum of squares of its digit
that is if we start with Happy Number and keep replacing it with digits square sum, we reach 1.

Examples of Happy numbers are:- 1, 7, 10, 13, 19, 23, 28, 31, 32, 44, 49, 68, 70,...
*/

import java.util.*

fun squaredSum(n: Int): Int{
    var sum_of_square:Int = 0
    var c:Int = n
    while(c!=0){
        sum_of_square += c%10 * (c % 10)
        c /= 10
    }
    return sum_of_square
}

//defining a boolean function to check whether the number is happy number or not
fun Happynumber(number: Int): Boolean {
    var slow: Int
    var fast: Int

    fast = number
    slow = fast
    do {

        slow = squaredSum(slow)

        fast = squaredSum(squaredSum(fast))
    } while (slow != fast)

    return slow == 1
}

//Testing code
fun main(){
    val input = Scanner(System.`in`)

    println("Enter the number which you want to check")
    val n = input.nextInt()
    if (Happynumber(n))
        println("$n is a Happy number")
    else
        println("$n is not a happy number")
}

/*
Enter the number which you want to check
19
19 is a Happy number

Enter the number which you want to check
20
20 is not a happy number
*/

/*
Time complexity :- O(N)
Space complexity :- O(1), which is constant
 */

Compiled from "HappyNumber.kt"
public final class HappyNumberKt {
  public static final int squaredSum(int);
    Code:
       0: iconst_0
       1: istore_1
       2: iload_0
       3: istore_2
       4: iload_2
       5: ifeq          28
       8: iload_1
       9: iload_2
      10: bipush        10
      12: irem
      13: iload_2
      14: bipush        10
      16: irem
      17: imul
      18: iadd
      19: istore_1
      20: iload_2
      21: bipush        10
      23: idiv
      24: istore_2
      25: goto          4
      28: iload_1
      29: ireturn

  public static final boolean Happynumber(int);
    Code:
       0: iconst_0
       1: istore_1
       2: iconst_0
       3: istore_2
       4: iload_0
       5: istore_2
       6: iload_2
       7: istore_1
       8: iload_1
       9: invokestatic  #14                 // Method squaredSum:(I)I
      12: istore_1
      13: iload_2
      14: invokestatic  #14                 // Method squaredSum:(I)I
      17: invokestatic  #14                 // Method squaredSum:(I)I
      20: istore_2
      21: iload_1
      22: iload_2
      23: if_icmpne     8
      26: iload_1
      27: iconst_1
      28: if_icmpne     35
      31: iconst_1
      32: goto          36
      35: iconst_0
      36: ireturn

  public static final void main();
    Code:
       0: new           #21                 // class java/util/Scanner
       3: dup
       4: getstatic     #27                 // Field java/lang/System.in:Ljava/io/InputStream;
       7: invokespecial #31                 // Method java/util/Scanner."<init>":(Ljava/io/InputStream;)V
      10: astore_0
      11: ldc           #33                 // String Enter the number which you want to check
      13: getstatic     #37                 // Field java/lang/System.out:Ljava/io/PrintStream;
      16: swap
      17: invokevirtual #43                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
      20: aload_0
      21: invokevirtual #47                 // Method java/util/Scanner.nextInt:()I
      24: istore_1
      25: iload_1
      26: invokestatic  #49                 // Method Happynumber:(I)Z
      29: ifeq          61
      32: new           #51                 // class java/lang/StringBuilder
      35: dup
      36: invokespecial #53                 // Method java/lang/StringBuilder."<init>":()V
      39: iload_1
      40: invokevirtual #57                 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
      43: ldc           #59                 // String  is a Happy number
      45: invokevirtual #62                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      48: invokevirtual #66                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
      51: getstatic     #37                 // Field java/lang/System.out:Ljava/io/PrintStream;
      54: swap
      55: invokevirtual #43                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
      58: goto          87
      61: new           #51                 // class java/lang/StringBuilder
      64: dup
      65: invokespecial #53                 // Method java/lang/StringBuilder."<init>":()V
      68: iload_1
      69: invokevirtual #57                 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
      72: ldc           #68                 // String  is not a happy number
      74: invokevirtual #62                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      77: invokevirtual #66                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
      80: getstatic     #37                 // Field java/lang/System.out:Ljava/io/PrintStream;
      83: swap
      84: invokevirtual #43                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
      87: return

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