package daily

fun main() {
    val solution = Solution309()
    //10
    val i8 = solution.maxProfit(intArrayOf(8,6,4,3,3,2,3,5,8,3,8,2,6))
    //13
    val i7 = solution.maxProfit(intArrayOf(1,7,2,4,11))
    //6
    val i4 = solution.maxProfit(intArrayOf(3, 3, 5, 0, 0, 3, 1, 4))
    //3
    val i = solution.maxProfit(intArrayOf(1, 2, 3, 0, 2))
    //3
    val i2 = solution.maxProfit(intArrayOf(1, 4, 2))
    //1
    val i3 = solution.maxProfit(intArrayOf(2, 1, 2, 0, 1))
    //6
    val i5 = solution.maxProfit(intArrayOf(1, 4, 2, 7))
    //10
    val i6 = solution.maxProfit(intArrayOf(1, 2, 7, 4, 11))
}

/**
 * 买卖股票的最佳时期（含冰冻期）
 */
class Solution309 {
    fun maxProfit(prices: IntArray): Int {
        if(prices.size < 2) {
            return 0
        }

        if(prices.size == 2) {
            return if(prices[1] - prices[0] > 0) prices[1] - prices[0] else 0
        }

        var result = 0
        var hold = false
        var freeze = false
        var buy = 0
        var i = 0
        while (i < prices.size - 3) {
            if(freeze) {
                freeze = false
                i++
                continue
            }
            //空仓，观望：明天跌 或 明天涨但后天有抄底的机会
            if(!hold && (downTomorrow(prices,i)
                    || dltTomorrow(prices, i) <= dltTomorrow(prices,i+2) && dltTomorrow(prices,i) + dltTomorrow(prices,i+1) <= 0)) {
                i++
                continue
            }

            //空仓，买
            if(!hold) {
                buy = prices[i++]
                hold = true
                continue
            }

            //持仓，卖：明天跌而且后天没涨回来 或 明天涨但后天有抄底的机会
            if(hold && (
                            downTomorrow(prices,i) && dltTomorrow(prices,i) + dltTomorrow(prices,i+1) <= 0
                                    || upTomorrow(prices,i) && dltTomorrow(prices, i) <= dltTomorrow(prices,i+2) && dltTomorrow(prices,i) + dltTomorrow(prices,i+1) <= 0
                            )) {
                hold = false
                freeze = true
                result += prices[i++] - buy
                continue
            }

            //观望
            i++
        }

        //最后三天的决策
        //持仓，只能卖一次，最高那天卖
        if(hold) {
            result += Math.max(prices[prices.size - 3], Math.max(prices[prices.size - 2],prices[prices.size - 1])) - buy
        } else if(freeze) {
            //空仓冰冻期 Math.max(第三天-第二天，0)
            result += Math.max(dltTomorrow(prices,prices.size-2),0)
        } else {
            //空仓，Math.max(第二天-第一天，第三天 - 前两天中低的一天，0）
            val best = prices[prices.size - 1] - Math.min(prices[prices.size - 2], prices[prices.size - 3])
            result += Math.max(prices[prices.size-2] - prices[prices.size - 3],Math.max(best,0))
        }
        return result
    }

    private fun dltTomorrow(prices: IntArray, i: Int) = prices[i + 1] - prices[i]

    private fun upTomorrow(prices: IntArray, i: Int): Boolean {
        return prices[i + 1] > prices[i]
    }

    private fun downTomorrow(prices: IntArray, i: Int): Boolean {
        return prices[i + 1] <= prices[i]
    }
}

Compiled from "LeetCode309.kt"
public final class daily.LeetCode309Kt {
  public static final void main();
    Code:
       0: new           #8                  // class daily/Solution309
       3: dup
       4: invokespecial #11                 // Method daily/Solution309."<init>":()V
       7: astore_0
       8: aload_0
       9: bipush        13
      11: newarray       int
      13: astore_2
      14: aload_2
      15: iconst_0
      16: bipush        8
      18: iastore
      19: aload_2
      20: iconst_1
      21: bipush        6
      23: iastore
      24: aload_2
      25: iconst_2
      26: iconst_4
      27: iastore
      28: aload_2
      29: iconst_3
      30: iconst_3
      31: iastore
      32: aload_2
      33: iconst_4
      34: iconst_3
      35: iastore
      36: aload_2
      37: iconst_5
      38: iconst_2
      39: iastore
      40: aload_2
      41: bipush        6
      43: iconst_3
      44: iastore
      45: aload_2
      46: bipush        7
      48: iconst_5
      49: iastore
      50: aload_2
      51: bipush        8
      53: bipush        8
      55: iastore
      56: aload_2
      57: bipush        9
      59: iconst_3
      60: iastore
      61: aload_2
      62: bipush        10
      64: bipush        8
      66: iastore
      67: aload_2
      68: bipush        11
      70: iconst_2
      71: iastore
      72: aload_2
      73: bipush        12
      75: bipush        6
      77: iastore
      78: aload_2
      79: invokevirtual #15                 // Method daily/Solution309.maxProfit:([I)I
      82: istore_1
      83: aload_0
      84: iconst_5
      85: newarray       int
      87: astore_3
      88: aload_3
      89: iconst_0
      90: iconst_1
      91: iastore
      92: aload_3
      93: iconst_1
      94: bipush        7
      96: iastore
      97: aload_3
      98: iconst_2
      99: iconst_2
     100: iastore
     101: aload_3
     102: iconst_3
     103: iconst_4
     104: iastore
     105: aload_3
     106: iconst_4
     107: bipush        11
     109: iastore
     110: aload_3
     111: invokevirtual #15                 // Method daily/Solution309.maxProfit:([I)I
     114: istore_2
     115: aload_0
     116: bipush        8
     118: newarray       int
     120: astore        4
     122: aload         4
     124: iconst_0
     125: iconst_3
     126: iastore
     127: aload         4
     129: iconst_1
     130: iconst_3
     131: iastore
     132: aload         4
     134: iconst_2
     135: iconst_5
     136: iastore
     137: aload         4
     139: iconst_3
     140: iconst_0
     141: iastore
     142: aload         4
     144: iconst_4
     145: iconst_0
     146: iastore
     147: aload         4
     149: iconst_5
     150: iconst_3
     151: iastore
     152: aload         4
     154: bipush        6
     156: iconst_1
     157: iastore
     158: aload         4
     160: bipush        7
     162: iconst_4
     163: iastore
     164: aload         4
     166: invokevirtual #15                 // Method daily/Solution309.maxProfit:([I)I
     169: istore_3
     170: aload_0
     171: iconst_5
     172: newarray       int
     174: astore        5
     176: aload         5
     178: iconst_0
     179: iconst_1
     180: iastore
     181: aload         5
     183: iconst_1
     184: iconst_2
     185: iastore
     186: aload         5
     188: iconst_2
     189: iconst_3
     190: iastore
     191: aload         5
     193: iconst_3
     194: iconst_0
     195: iastore
     196: aload         5
     198: iconst_4
     199: iconst_2
     200: iastore
     201: aload         5
     203: invokevirtual #15                 // Method daily/Solution309.maxProfit:([I)I
     206: istore        4
     208: aload_0
     209: iconst_3
     210: newarray       int
     212: astore        6
     214: aload         6
     216: iconst_0
     217: iconst_1
     218: iastore
     219: aload         6
     221: iconst_1
     222: iconst_4
     223: iastore
     224: aload         6
     226: iconst_2
     227: iconst_2
     228: iastore
     229: aload         6
     231: invokevirtual #15                 // Method daily/Solution309.maxProfit:([I)I
     234: istore        5
     236: aload_0
     237: iconst_5
     238: newarray       int
     240: astore        7
     242: aload         7
     244: iconst_0
     245: iconst_2
     246: iastore
     247: aload         7
     249: iconst_1
     250: iconst_1
     251: iastore
     252: aload         7
     254: iconst_2
     255: iconst_2
     256: iastore
     257: aload         7
     259: iconst_3
     260: iconst_0
     261: iastore
     262: aload         7
     264: iconst_4
     265: iconst_1
     266: iastore
     267: aload         7
     269: invokevirtual #15                 // Method daily/Solution309.maxProfit:([I)I
     272: istore        6
     274: aload_0
     275: iconst_4
     276: newarray       int
     278: astore        8
     280: aload         8
     282: iconst_0
     283: iconst_1
     284: iastore
     285: aload         8
     287: iconst_1
     288: iconst_4
     289: iastore
     290: aload         8
     292: iconst_2
     293: iconst_2
     294: iastore
     295: aload         8
     297: iconst_3
     298: bipush        7
     300: iastore
     301: aload         8
     303: invokevirtual #15                 // Method daily/Solution309.maxProfit:([I)I
     306: istore        7
     308: aload_0
     309: iconst_5
     310: newarray       int
     312: astore        9
     314: aload         9
     316: iconst_0
     317: iconst_1
     318: iastore
     319: aload         9
     321: iconst_1
     322: iconst_2
     323: iastore
     324: aload         9
     326: iconst_2
     327: bipush        7
     329: iastore
     330: aload         9
     332: iconst_3
     333: iconst_4
     334: iastore
     335: aload         9
     337: iconst_4
     338: bipush        11
     340: iastore
     341: aload         9
     343: invokevirtual #15                 // Method daily/Solution309.maxProfit:([I)I
     346: istore        8
     348: return

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


package letcode10

fun main() {
    val solution = Solution10()
    //false
    val match9 = solution.isMatch("aaa", "aaaa")
    //true
    val match8 = solution.isMatch("ab", ".*..")
    //true
    val match7 = solution.isMatch("aab", "c*a*b*")
    //false
    val match = solution.isMatch("aa", "a")
    //true
    val match1 = solution.isMatch("aa", "a*")
    //true
    val match4 = solution.isMatch("aaa", ".*")
    //true
    val match5 = solution.isMatch("aba", ".*")
    //true
    val match2 = solution.isMatch("aab", "c*a*b")
    //false
    val match3 = solution.isMatch("mississippi", "mis*is*p*.")
    //false
    val match6 = solution.isMatch("a", ".*..a*")
}

/**
 * 含元字符.和*（.前字符匹配1~n次；*前字符匹配0~n次）的正则表达式匹配
 */
class Solution10 {
    fun isMatch(s: String, p: String): Boolean {
        return isMatch(s, s.length - 1, p, p.length - 1)
    }

    fun isMatch(s: String, endS: Int, p: String, endP: Int): Boolean {
        if (endS < 0 && canBeEmpty(p, endP)) return true
        if (endS < 0 || endP < 0) return false
        return p[endP] != '*' && (p[endP] == '.' || s[endS] == p[endP]) && isMatch(s, endS - 1, p, endP - 1)
                || p[endP] == '*' && endP > 0 && isMatchStar(s, endS, p, endP)
    }

    fun isMatchStar(s: String, endS: Int, p: String, endP: Int): Boolean {
        if (endS < 0 && canBeEmpty(p, endP)) return true
        if (endS < 0 || endP <= 0) return false
        //匹配*前一个字符 或是匹配*前第两个字符
        return (p[endP - 1] == '.' || s[endS] == p[endP - 1]) && isMatchStar(s, endS - 1, p, endP)
                || endP > 1 && isMatch(s, endS, p, endP - 2)
    }

    /**
     * 可以是空串
     */
    private fun canBeEmpty(p: String, endP: Int): Boolean {
        var flag = false
        for (i in endP downTo 0) {
            if (flag && p[i] != '*')
                flag = false
            else if (!flag && p[i] == '*')
                flag = true
            else if (!flag && p[i] != '*')
                return false
        }
        return true
    }

}

Compiled from "LeetCode10.kt"
public final class letcode10.LeetCode10Kt {
  public static final void main();
    Code:
       0: new           #8                  // class letcode10/Solution10
       3: dup
       4: invokespecial #11                 // Method letcode10/Solution10."<init>":()V
       7: astore_0
       8: aload_0
       9: ldc           #13                 // String aaa
      11: ldc           #15                 // String aaaa
      13: invokevirtual #19                 // Method letcode10/Solution10.isMatch:(Ljava/lang/String;Ljava/lang/String;)Z
      16: istore_1
      17: aload_0
      18: ldc           #21                 // String ab
      20: ldc           #23                 // String .*..
      22: invokevirtual #19                 // Method letcode10/Solution10.isMatch:(Ljava/lang/String;Ljava/lang/String;)Z
      25: istore_2
      26: aload_0
      27: ldc           #25                 // String aab
      29: ldc           #27                 // String c*a*b*
      31: invokevirtual #19                 // Method letcode10/Solution10.isMatch:(Ljava/lang/String;Ljava/lang/String;)Z
      34: istore_3
      35: aload_0
      36: ldc           #29                 // String aa
      38: ldc           #31                 // String a
      40: invokevirtual #19                 // Method letcode10/Solution10.isMatch:(Ljava/lang/String;Ljava/lang/String;)Z
      43: istore        4
      45: aload_0
      46: ldc           #29                 // String aa
      48: ldc           #33                 // String a*
      50: invokevirtual #19                 // Method letcode10/Solution10.isMatch:(Ljava/lang/String;Ljava/lang/String;)Z
      53: istore        5
      55: aload_0
      56: ldc           #13                 // String aaa
      58: ldc           #35                 // String .*
      60: invokevirtual #19                 // Method letcode10/Solution10.isMatch:(Ljava/lang/String;Ljava/lang/String;)Z
      63: istore        6
      65: aload_0
      66: ldc           #37                 // String aba
      68: ldc           #35                 // String .*
      70: invokevirtual #19                 // Method letcode10/Solution10.isMatch:(Ljava/lang/String;Ljava/lang/String;)Z
      73: istore        7
      75: aload_0
      76: ldc           #25                 // String aab
      78: ldc           #39                 // String c*a*b
      80: invokevirtual #19                 // Method letcode10/Solution10.isMatch:(Ljava/lang/String;Ljava/lang/String;)Z
      83: istore        8
      85: aload_0
      86: ldc           #41                 // String mississippi
      88: ldc           #43                 // String mis*is*p*.
      90: invokevirtual #19                 // Method letcode10/Solution10.isMatch:(Ljava/lang/String;Ljava/lang/String;)Z
      93: istore        9
      95: aload_0
      96: ldc           #31                 // String a
      98: ldc           #45                 // String .*..a*
     100: invokevirtual #19                 // Method letcode10/Solution10.isMatch:(Ljava/lang/String;Ljava/lang/String;)Z
     103: istore        10
     105: return

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


package com.mantono.lingress

fun median(c: Collection<Number>): Double = c.asSequence().median()
fun mean(c: Collection<Number>): Double = c.map { it.toDouble() }.average()
fun range(c: Collection<Number>): Double = c.asSequence().range()
fun variance(c: Collection<Number>): Double = c.asSequence().variance()
fun standardDeviation(c: Collection<Number>): Double = Math.sqrt(variance(c))

fun Sequence<Number>.median(): Double
{
	val size = this.asSequence().count()

	val evenSize = size % 2 == 0
	val elementsToDrop: Int = when(evenSize)
	{
		true -> ((size/2.0) - 1).toInt()
		false -> size/2
	}

	return this.map { it.toDouble() }
			.sorted()
			.drop(elementsToDrop)
			.take(if(evenSize) 2 else 1)
			.average()
}

fun Sequence<Number>.range(): Double
{
	val min: Double = map { it.toDouble() }.min() ?: 0.0
	val max: Double = map { it.toDouble() }.max() ?: 0.0
	return max - min
}

fun Sequence<Number>.variance(): Double
{
	val doubleSequence: Sequence<Double> = map { it.toDouble() }
	val mean = doubleSequence.average()

	val sum = doubleSequence
			.map { it - mean }
			.map { Math.pow(it, 2.0) }
			.sum()

	return sum/count()
}

fun Sequence<Number>.standardDeviation(): Double = Math.sqrt(variance())

Compiled from "Spread.kt"
public final class com.mantono.lingress.SpreadKt {
  public static final double median(java.util.Collection<? extends java.lang.Number>);
    Code:
       0: aload_0
       1: ldc           #10                 // String c
       3: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: checkcast     #18                 // class java/lang/Iterable
      10: invokestatic  #24                 // Method kotlin/collections/CollectionsKt.asSequence:(Ljava/lang/Iterable;)Lkotlin/sequences/Sequence;
      13: invokestatic  #27                 // Method median:(Lkotlin/sequences/Sequence;)D
      16: dreturn

  public static final double mean(java.util.Collection<? extends java.lang.Number>);
    Code:
       0: aload_0
       1: ldc           #10                 // String c
       3: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: checkcast     #18                 // class java/lang/Iterable
      10: astore_1
      11: iconst_0
      12: istore_2
      13: aload_1
      14: astore_3
      15: new           #31                 // class java/util/ArrayList
      18: dup
      19: aload_1
      20: bipush        10
      22: invokestatic  #35                 // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
      25: invokespecial #39                 // Method java/util/ArrayList."<init>":(I)V
      28: checkcast     #41                 // class java/util/Collection
      31: astore        4
      33: iconst_0
      34: istore        5
      36: aload_3
      37: invokeinterface #45,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      42: astore        6
      44: aload         6
      46: invokeinterface #51,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      51: ifeq          97
      54: aload         6
      56: invokeinterface #55,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      61: astore        7
      63: aload         4
      65: aload         7
      67: checkcast     #57                 // class java/lang/Number
      70: astore        8
      72: astore        10
      74: iconst_0
      75: istore        9
      77: aload         8
      79: invokevirtual #61                 // Method java/lang/Number.doubleValue:()D
      82: invokestatic  #67                 // Method java/lang/Double.valueOf:(D)Ljava/lang/Double;
      85: aload         10
      87: swap
      88: invokeinterface #71,  2           // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
      93: pop
      94: goto          44
      97: aload         4
      99: checkcast     #73                 // class java/util/List
     102: nop
     103: checkcast     #18                 // class java/lang/Iterable
     106: invokestatic  #77                 // Method kotlin/collections/CollectionsKt.averageOfDouble:(Ljava/lang/Iterable;)D
     109: dreturn

  public static final double range(java.util.Collection<? extends java.lang.Number>);
    Code:
       0: aload_0
       1: ldc           #10                 // String c
       3: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: checkcast     #18                 // class java/lang/Iterable
      10: invokestatic  #24                 // Method kotlin/collections/CollectionsKt.asSequence:(Ljava/lang/Iterable;)Lkotlin/sequences/Sequence;
      13: invokestatic  #92                 // Method range:(Lkotlin/sequences/Sequence;)D
      16: dreturn

  public static final double variance(java.util.Collection<? extends java.lang.Number>);
    Code:
       0: aload_0
       1: ldc           #10                 // String c
       3: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: checkcast     #18                 // class java/lang/Iterable
      10: invokestatic  #24                 // Method kotlin/collections/CollectionsKt.asSequence:(Ljava/lang/Iterable;)Lkotlin/sequences/Sequence;
      13: invokestatic  #95                 // Method variance:(Lkotlin/sequences/Sequence;)D
      16: dreturn

  public static final double standardDeviation(java.util.Collection<? extends java.lang.Number>);
    Code:
       0: aload_0
       1: ldc           #10                 // String c
       3: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: invokestatic  #98                 // Method variance:(Ljava/util/Collection;)D
      10: invokestatic  #104                // Method java/lang/Math.sqrt:(D)D
      13: dreturn

  public static final double median(kotlin.sequences.Sequence<? extends java.lang.Number>);
    Code:
       0: aload_0
       1: ldc           #107                // String <this>
       3: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: invokestatic  #113                // Method kotlin/sequences/SequencesKt.count:(Lkotlin/sequences/Sequence;)I
      10: istore_1
      11: iload_1
      12: iconst_2
      13: irem
      14: ifne          21
      17: iconst_1
      18: goto          22
      21: iconst_0
      22: istore_2
      23: iload_2
      24: istore        4
      26: iload         4
      28: iconst_1
      29: if_icmpne     45
      32: iload_1
      33: i2d
      34: ldc2_w        #114                // double 2.0d
      37: ddiv
      38: iconst_1
      39: i2d
      40: dsub
      41: d2i
      42: goto          64
      45: iload         4
      47: ifne          56
      50: iload_1
      51: iconst_2
      52: idiv
      53: goto          64
      56: new           #117                // class kotlin/NoWhenBranchMatchedException
      59: dup
      60: invokespecial #120                // Method kotlin/NoWhenBranchMatchedException."<init>":()V
      63: athrow
      64: istore_3
      65: aload_0
      66: invokedynamic #140,  0            // InvokeDynamic #0:invoke:()Lkotlin/jvm/functions/Function1;
      71: invokestatic  #144                // Method kotlin/sequences/SequencesKt.map:(Lkotlin/sequences/Sequence;Lkotlin/jvm/functions/Function1;)Lkotlin/sequences/Sequence;
      74: invokestatic  #148                // Method kotlin/sequences/SequencesKt.sorted:(Lkotlin/sequences/Sequence;)Lkotlin/sequences/Sequence;
      77: iload_3
      78: invokestatic  #152                // Method kotlin/sequences/SequencesKt.drop:(Lkotlin/sequences/Sequence;I)Lkotlin/sequences/Sequence;
      81: iload_2
      82: ifeq          89
      85: iconst_2
      86: goto          90
      89: iconst_1
      90: invokestatic  #155                // Method kotlin/sequences/SequencesKt.take:(Lkotlin/sequences/Sequence;I)Lkotlin/sequences/Sequence;
      93: invokestatic  #157                // Method kotlin/sequences/SequencesKt.averageOfDouble:(Lkotlin/sequences/Sequence;)D
      96: dreturn

  public static final double range(kotlin.sequences.Sequence<? extends java.lang.Number>);
    Code:
       0: aload_0
       1: ldc           #107                // String <this>
       3: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: invokedynamic #170,  0            // InvokeDynamic #1:invoke:()Lkotlin/jvm/functions/Function1;
      12: invokestatic  #144                // Method kotlin/sequences/SequencesKt.map:(Lkotlin/sequences/Sequence;Lkotlin/jvm/functions/Function1;)Lkotlin/sequences/Sequence;
      15: invokestatic  #173                // Method kotlin/sequences/SequencesKt.minOrThrow:(Lkotlin/sequences/Sequence;)D
      18: dstore_1
      19: aload_0
      20: invokedynamic #178,  0            // InvokeDynamic #2:invoke:()Lkotlin/jvm/functions/Function1;
      25: invokestatic  #144                // Method kotlin/sequences/SequencesKt.map:(Lkotlin/sequences/Sequence;Lkotlin/jvm/functions/Function1;)Lkotlin/sequences/Sequence;
      28: invokestatic  #181                // Method kotlin/sequences/SequencesKt.maxOrThrow:(Lkotlin/sequences/Sequence;)D
      31: dstore_3
      32: dload_3
      33: dload_1
      34: dsub
      35: dreturn

  public static final double variance(kotlin.sequences.Sequence<? extends java.lang.Number>);
    Code:
       0: aload_0
       1: ldc           #107                // String <this>
       3: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: invokedynamic #190,  0            // InvokeDynamic #3:invoke:()Lkotlin/jvm/functions/Function1;
      12: invokestatic  #144                // Method kotlin/sequences/SequencesKt.map:(Lkotlin/sequences/Sequence;Lkotlin/jvm/functions/Function1;)Lkotlin/sequences/Sequence;
      15: astore_1
      16: aload_1
      17: invokestatic  #157                // Method kotlin/sequences/SequencesKt.averageOfDouble:(Lkotlin/sequences/Sequence;)D
      20: dstore_2
      21: aload_1
      22: dload_2
      23: invokedynamic #200,  0            // InvokeDynamic #4:invoke:(D)Lkotlin/jvm/functions/Function1;
      28: invokestatic  #144                // Method kotlin/sequences/SequencesKt.map:(Lkotlin/sequences/Sequence;Lkotlin/jvm/functions/Function1;)Lkotlin/sequences/Sequence;
      31: invokedynamic #205,  0            // InvokeDynamic #5:invoke:()Lkotlin/jvm/functions/Function1;
      36: invokestatic  #144                // Method kotlin/sequences/SequencesKt.map:(Lkotlin/sequences/Sequence;Lkotlin/jvm/functions/Function1;)Lkotlin/sequences/Sequence;
      39: invokestatic  #208                // Method kotlin/sequences/SequencesKt.sumOfDouble:(Lkotlin/sequences/Sequence;)D
      42: dstore        4
      44: dload         4
      46: aload_0
      47: invokestatic  #113                // Method kotlin/sequences/SequencesKt.count:(Lkotlin/sequences/Sequence;)I
      50: i2d
      51: ddiv
      52: dreturn

  public static final double standardDeviation(kotlin.sequences.Sequence<? extends java.lang.Number>);
    Code:
       0: aload_0
       1: ldc           #107                // String <this>
       3: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: invokestatic  #95                 // Method variance:(Lkotlin/sequences/Sequence;)D
      10: invokestatic  #104                // Method java/lang/Math.sqrt:(D)D
      13: dreturn

  private static final double median$lambda$1(java.lang.Number);
    Code:
       0: aload_0
       1: ldc           #213                // String it
       3: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: invokevirtual #61                 // Method java/lang/Number.doubleValue:()D
      10: dreturn

  private static final double range$lambda$2(java.lang.Number);
    Code:
       0: aload_0
       1: ldc           #213                // String it
       3: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: invokevirtual #61                 // Method java/lang/Number.doubleValue:()D
      10: dreturn

  private static final double range$lambda$3(java.lang.Number);
    Code:
       0: aload_0
       1: ldc           #213                // String it
       3: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: invokevirtual #61                 // Method java/lang/Number.doubleValue:()D
      10: dreturn

  private static final double variance$lambda$4(java.lang.Number);
    Code:
       0: aload_0
       1: ldc           #213                // String it
       3: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: invokevirtual #61                 // Method java/lang/Number.doubleValue:()D
      10: dreturn

  private static final double variance$lambda$5(double, double);
    Code:
       0: dload_2
       1: dload_0
       2: dsub
       3: dreturn

  private static final double variance$lambda$6(double);
    Code:
       0: dload_0
       1: ldc2_w        #114                // double 2.0d
       4: invokestatic  #217                // Method java/lang/Math.pow:(DD)D
       7: dreturn
}


package com.mantono.lingress

data class RegressionLine(val m: Double, val b: Double)
{
	fun y(x: Double): Double = m*x+b
	fun x(y: Double): Double = (y-b)/m
	operator fun get(x: Double): Double = y(x)
	override fun toString(): String = "y = $m * x + $b"
}

data class Point(val x: Double, val y: Double)
{
	constructor(independent: Number, dependent: Number): this(independent.toDouble(), dependent.toDouble())
}

fun residual(line: RegressionLine, point: Point): Double
{
	val predictedY = line[point.x]
	val actualY = point.y
	return actualY - predictedY
}

fun sumOfSquaredErrors(line: RegressionLine, data: Collection<Point>): Double
{
	return data.asSequence()
			.map {squaredError(line, it)}
			.sum()
}

fun squaredError(line: RegressionLine, p: Point): Double = Math.pow(residual(line, p), 2.0)

fun rootMeanSquareError(line: RegressionLine, data: Collection<Point>): Double
{
	if(data.isEmpty())
		return Double.NaN
	if(data.size == 1)
		return 0.0

	val squaredErr = sumOfSquaredErrors(line, data) / (data.size - 1)
	return Math.sqrt(squaredErr)
}

fun totalVariationInY(data: Collection<Point>): Double
{
	val avg = data.asSequence()
			.map { it.y }
			.average()

	return data
			.asSequence()
			.map { it.y - avg }
			.map { Math.pow(it, 2.0) }
			.sum()
}

inline fun squared(n: Double): Double = Math.pow(n, 2.0)

fun regressionLineFrom(c: Collection<Point>): RegressionLine
{
	val xMean = c.asSequence()
			.map { it.x }
			.average()

	val yMean = c.asSequence()
			.map { it.y }
			.average()

	val xyMean = c.asSequence()
			.map { it.x * it.y }
			.average()

	val xSquaredMean = c.asSequence()
			.map {squared(it.x)}
			.average()

	val m: Double = (xMean * yMean - (xyMean)) / (Math.pow(xMean, 2.0) - xSquaredMean)
	val b: Double = yMean - (m * xMean)

	return RegressionLine(m, b)
}

Compiled from "Regression.kt"
public final class com.mantono.lingress.RegressionKt {
  public static final double residual(com.mantono.lingress.RegressionLine, com.mantono.lingress.Point);
    Code:
       0: aload_0
       1: ldc           #9                  // String line
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_1
       7: ldc           #17                 // String point
       9: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
      12: aload_0
      13: aload_1
      14: invokevirtual #23                 // Method com/mantono/lingress/Point.getX:()D
      17: invokevirtual #29                 // Method com/mantono/lingress/RegressionLine.get:(D)D
      20: dstore_2
      21: aload_1
      22: invokevirtual #32                 // Method com/mantono/lingress/Point.getY:()D
      25: dstore        4
      27: dload         4
      29: dload_2
      30: dsub
      31: dreturn

  public static final double sumOfSquaredErrors(com.mantono.lingress.RegressionLine, java.util.Collection<com.mantono.lingress.Point>);
    Code:
       0: aload_0
       1: ldc           #9                  // String line
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_1
       7: ldc           #42                 // String data
       9: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
      12: aload_1
      13: checkcast     #44                 // class java/lang/Iterable
      16: invokestatic  #50                 // Method kotlin/collections/CollectionsKt.asSequence:(Ljava/lang/Iterable;)Lkotlin/sequences/Sequence;
      19: aload_0
      20: invokedynamic #69,  0             // InvokeDynamic #0:invoke:(Lcom/mantono/lingress/RegressionLine;)Lkotlin/jvm/functions/Function1;
      25: invokestatic  #75                 // Method kotlin/sequences/SequencesKt.map:(Lkotlin/sequences/Sequence;Lkotlin/jvm/functions/Function1;)Lkotlin/sequences/Sequence;
      28: invokestatic  #79                 // Method kotlin/sequences/SequencesKt.sumOfDouble:(Lkotlin/sequences/Sequence;)D
      31: dreturn

  public static final double squaredError(com.mantono.lingress.RegressionLine, com.mantono.lingress.Point);
    Code:
       0: aload_0
       1: ldc           #9                  // String line
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_1
       7: ldc           #83                 // String p
       9: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
      12: aload_0
      13: aload_1
      14: invokestatic  #85                 // Method residual:(Lcom/mantono/lingress/RegressionLine;Lcom/mantono/lingress/Point;)D
      17: ldc2_w        #86                 // double 2.0d
      20: invokestatic  #93                 // Method java/lang/Math.pow:(DD)D
      23: dreturn

  public static final double rootMeanSquareError(com.mantono.lingress.RegressionLine, java.util.Collection<com.mantono.lingress.Point>);
    Code:
       0: aload_0
       1: ldc           #9                  // String line
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_1
       7: ldc           #42                 // String data
       9: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
      12: aload_1
      13: invokeinterface #100,  1          // InterfaceMethod java/util/Collection.isEmpty:()Z
      18: ifeq          25
      21: ldc2_w        #101                // double NaNd
      24: dreturn
      25: aload_1
      26: invokeinterface #106,  1          // InterfaceMethod java/util/Collection.size:()I
      31: iconst_1
      32: if_icmpne     37
      35: dconst_0
      36: dreturn
      37: aload_0
      38: aload_1
      39: invokestatic  #108                // Method sumOfSquaredErrors:(Lcom/mantono/lingress/RegressionLine;Ljava/util/Collection;)D
      42: aload_1
      43: invokeinterface #106,  1          // InterfaceMethod java/util/Collection.size:()I
      48: iconst_1
      49: isub
      50: i2d
      51: ddiv
      52: dstore_2
      53: dload_2
      54: invokestatic  #111                // Method java/lang/Math.sqrt:(D)D
      57: dreturn

  public static final double totalVariationInY(java.util.Collection<com.mantono.lingress.Point>);
    Code:
       0: aload_0
       1: ldc           #42                 // String data
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: checkcast     #44                 // class java/lang/Iterable
      10: invokestatic  #50                 // Method kotlin/collections/CollectionsKt.asSequence:(Ljava/lang/Iterable;)Lkotlin/sequences/Sequence;
      13: invokedynamic #123,  0            // InvokeDynamic #1:invoke:()Lkotlin/jvm/functions/Function1;
      18: invokestatic  #75                 // Method kotlin/sequences/SequencesKt.map:(Lkotlin/sequences/Sequence;Lkotlin/jvm/functions/Function1;)Lkotlin/sequences/Sequence;
      21: invokestatic  #126                // Method kotlin/sequences/SequencesKt.averageOfDouble:(Lkotlin/sequences/Sequence;)D
      24: dstore_1
      25: aload_0
      26: checkcast     #44                 // class java/lang/Iterable
      29: invokestatic  #50                 // Method kotlin/collections/CollectionsKt.asSequence:(Ljava/lang/Iterable;)Lkotlin/sequences/Sequence;
      32: dload_1
      33: invokedynamic #134,  0            // InvokeDynamic #2:invoke:(D)Lkotlin/jvm/functions/Function1;
      38: invokestatic  #75                 // Method kotlin/sequences/SequencesKt.map:(Lkotlin/sequences/Sequence;Lkotlin/jvm/functions/Function1;)Lkotlin/sequences/Sequence;
      41: invokedynamic #141,  0            // InvokeDynamic #3:invoke:()Lkotlin/jvm/functions/Function1;
      46: invokestatic  #75                 // Method kotlin/sequences/SequencesKt.map:(Lkotlin/sequences/Sequence;Lkotlin/jvm/functions/Function1;)Lkotlin/sequences/Sequence;
      49: invokestatic  #79                 // Method kotlin/sequences/SequencesKt.sumOfDouble:(Lkotlin/sequences/Sequence;)D
      52: dreturn

  public static final double squared(double);
    Code:
       0: iconst_0
       1: istore_2
       2: dload_0
       3: ldc2_w        #86                 // double 2.0d
       6: invokestatic  #93                 // Method java/lang/Math.pow:(DD)D
       9: dreturn

  public static final com.mantono.lingress.RegressionLine regressionLineFrom(java.util.Collection<com.mantono.lingress.Point>);
    Code:
       0: aload_0
       1: ldc           #151                // String c
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: checkcast     #44                 // class java/lang/Iterable
      10: invokestatic  #50                 // Method kotlin/collections/CollectionsKt.asSequence:(Ljava/lang/Iterable;)Lkotlin/sequences/Sequence;
      13: invokedynamic #156,  0            // InvokeDynamic #4:invoke:()Lkotlin/jvm/functions/Function1;
      18: invokestatic  #75                 // Method kotlin/sequences/SequencesKt.map:(Lkotlin/sequences/Sequence;Lkotlin/jvm/functions/Function1;)Lkotlin/sequences/Sequence;
      21: invokestatic  #126                // Method kotlin/sequences/SequencesKt.averageOfDouble:(Lkotlin/sequences/Sequence;)D
      24: dstore_1
      25: aload_0
      26: checkcast     #44                 // class java/lang/Iterable
      29: invokestatic  #50                 // Method kotlin/collections/CollectionsKt.asSequence:(Ljava/lang/Iterable;)Lkotlin/sequences/Sequence;
      32: invokedynamic #161,  0            // InvokeDynamic #5:invoke:()Lkotlin/jvm/functions/Function1;
      37: invokestatic  #75                 // Method kotlin/sequences/SequencesKt.map:(Lkotlin/sequences/Sequence;Lkotlin/jvm/functions/Function1;)Lkotlin/sequences/Sequence;
      40: invokestatic  #126                // Method kotlin/sequences/SequencesKt.averageOfDouble:(Lkotlin/sequences/Sequence;)D
      43: dstore_3
      44: aload_0
      45: checkcast     #44                 // class java/lang/Iterable
      48: invokestatic  #50                 // Method kotlin/collections/CollectionsKt.asSequence:(Ljava/lang/Iterable;)Lkotlin/sequences/Sequence;
      51: invokedynamic #166,  0            // InvokeDynamic #6:invoke:()Lkotlin/jvm/functions/Function1;
      56: invokestatic  #75                 // Method kotlin/sequences/SequencesKt.map:(Lkotlin/sequences/Sequence;Lkotlin/jvm/functions/Function1;)Lkotlin/sequences/Sequence;
      59: invokestatic  #126                // Method kotlin/sequences/SequencesKt.averageOfDouble:(Lkotlin/sequences/Sequence;)D
      62: dstore        5
      64: aload_0
      65: checkcast     #44                 // class java/lang/Iterable
      68: invokestatic  #50                 // Method kotlin/collections/CollectionsKt.asSequence:(Ljava/lang/Iterable;)Lkotlin/sequences/Sequence;
      71: invokedynamic #171,  0            // InvokeDynamic #7:invoke:()Lkotlin/jvm/functions/Function1;
      76: invokestatic  #75                 // Method kotlin/sequences/SequencesKt.map:(Lkotlin/sequences/Sequence;Lkotlin/jvm/functions/Function1;)Lkotlin/sequences/Sequence;
      79: invokestatic  #126                // Method kotlin/sequences/SequencesKt.averageOfDouble:(Lkotlin/sequences/Sequence;)D
      82: dstore        7
      84: dload_1
      85: dload_3
      86: dmul
      87: dload         5
      89: dsub
      90: dload_1
      91: ldc2_w        #86                 // double 2.0d
      94: invokestatic  #93                 // Method java/lang/Math.pow:(DD)D
      97: dload         7
      99: dsub
     100: ddiv
     101: dstore        9
     103: dload_3
     104: dload         9
     106: dload_1
     107: dmul
     108: dsub
     109: dstore        11
     111: new           #25                 // class com/mantono/lingress/RegressionLine
     114: dup
     115: dload         9
     117: dload         11
     119: invokespecial #175                // Method com/mantono/lingress/RegressionLine."<init>":(DD)V
     122: areturn

  private static final double sumOfSquaredErrors$lambda$0(com.mantono.lingress.RegressionLine, com.mantono.lingress.Point);
    Code:
       0: aload_1
       1: ldc           #183                // String it
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: aload_1
       8: invokestatic  #185                // Method squaredError:(Lcom/mantono/lingress/RegressionLine;Lcom/mantono/lingress/Point;)D
      11: dreturn

  private static final double totalVariationInY$lambda$1(com.mantono.lingress.Point);
    Code:
       0: aload_0
       1: ldc           #183                // String it
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: invokevirtual #32                 // Method com/mantono/lingress/Point.getY:()D
      10: dreturn

  private static final double totalVariationInY$lambda$2(double, com.mantono.lingress.Point);
    Code:
       0: aload_2
       1: ldc           #183                // String it
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_2
       7: invokevirtual #32                 // Method com/mantono/lingress/Point.getY:()D
      10: dload_0
      11: dsub
      12: dreturn

  private static final double totalVariationInY$lambda$3(double);
    Code:
       0: dload_0
       1: ldc2_w        #86                 // double 2.0d
       4: invokestatic  #93                 // Method java/lang/Math.pow:(DD)D
       7: dreturn

  private static final double regressionLineFrom$lambda$4(com.mantono.lingress.Point);
    Code:
       0: aload_0
       1: ldc           #183                // String it
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: invokevirtual #23                 // Method com/mantono/lingress/Point.getX:()D
      10: dreturn

  private static final double regressionLineFrom$lambda$5(com.mantono.lingress.Point);
    Code:
       0: aload_0
       1: ldc           #183                // String it
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: invokevirtual #32                 // Method com/mantono/lingress/Point.getY:()D
      10: dreturn

  private static final double regressionLineFrom$lambda$6(com.mantono.lingress.Point);
    Code:
       0: aload_0
       1: ldc           #183                // String it
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: invokevirtual #23                 // Method com/mantono/lingress/Point.getX:()D
      10: aload_0
      11: invokevirtual #32                 // Method com/mantono/lingress/Point.getY:()D
      14: dmul
      15: dreturn

  private static final double regressionLineFrom$lambda$7(com.mantono.lingress.Point);
    Code:
       0: aload_0
       1: ldc           #183                // String it
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: invokevirtual #23                 // Method com/mantono/lingress/Point.getX:()D
      10: dstore_1
      11: iconst_0
      12: istore_3
      13: dload_1
      14: ldc2_w        #86                 // double 2.0d
      17: invokestatic  #93                 // Method java/lang/Math.pow:(DD)D
      20: dreturn
}


package uk.co.skipoles.eventmodeller.visualisation.render

import java.awt.Graphics2D
import java.awt.geom.Rectangle2D

internal fun Graphics2D.drawCentredMultiLineString(
    s: String,
    x: Int,
    y: Int,
    width: Int,
    height: Int
) {
  val lines = divideIntoLines("", s, width, areaForString(this))
  val linesThatFit = mostLinesThatFitHeight(lines, height)

  val totalHeight = linesThatFit.sumOf { it.area.height }.toInt()
  var yPosition = y + ((height - totalHeight) / 2)
  linesThatFit.forEach {
    drawString(it.s, (x + ((width - it.area.width.toInt()) / 2)), yPosition)
    yPosition += it.area.height.toInt()
  }
}

private data class LineWithArea(val s: String, val area: Rectangle2D)

private fun divideIntoLines(
    current: String,
    remainder: String,
    maxWidth: Int,
    findArea: (String) -> Rectangle2D
): List<LineWithArea> {
  if (remainder.isEmpty()) return listOf(LineWithArea(current, findArea(current)))

  val nextWord = remainder.takeWhile { it != ' ' }
  val line = if (current.isEmpty()) nextWord else "$current $nextWord"
  val lineArea = findArea(line)
  return if (lineArea.width < (maxWidth - 2)) { // Line is shorter than max width
    divideIntoLines(line, remainder.drop(nextWord.length + 1), maxWidth, findArea)
  } else if (current.isEmpty()) { // Line is longer than max width and is a single word
    val longestSubstring = longestSubstringThatFitsWidth(line, maxWidth, findArea)
    listOf(LineWithArea(longestSubstring, findArea(longestSubstring))) +
        divideIntoLines("", remainder.drop(longestSubstring.length), maxWidth, findArea)
  } else { // Line is longer than max width, so needs a line break
    listOf(LineWithArea(current, findArea(current))) +
        divideIntoLines("", remainder.trim(), maxWidth, findArea)
  }
}

private fun longestSubstringThatFitsWidth(
    word: String,
    maxWidth: Int,
    findArea: (String) -> Rectangle2D
) =
    word
        .fold(Pair("", false)) { res, ch ->
          val (s, state) = res
          if (state) res
          else {
            val test = s + ch
            val testArea = findArea(test)
            if (testArea.width < (maxWidth - 2)) Pair(test, false) else Pair(s, true)
          }
        }
        .first

private fun mostLinesThatFitHeight(lines: List<LineWithArea>, maxHeight: Int) =
    lines
        .fold(Pair(listOf<LineWithArea>(), false)) { result, line ->
          val (validLines, done) = result
          if (done) result
          else {
            val newHeight = validLines.sumOf { it.area.height } + line.area.height
            if (newHeight < (maxHeight - 2)) Pair(validLines + line, false)
            else Pair(validLines, true)
          }
        }
        .first

private fun areaForString(graphics: Graphics2D): (String) -> Rectangle2D =
    fun(s: String) = graphics.font.getStringBounds(s, graphics.fontRenderContext)

Compiled from "CenteredMultiLineStringRenderer.kt"
public final class uk.co.skipoles.eventmodeller.visualisation.render.CenteredMultiLineStringRendererKt {
  public static final void drawCentredMultiLineString(java.awt.Graphics2D, java.lang.String, int, int, int, int);
    Code:
       0: aload_0
       1: ldc           #9                  // String <this>
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_1
       7: ldc           #17                 // String s
       9: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
      12: ldc           #19                 // String
      14: aload_1
      15: iload         4
      17: aload_0
      18: invokestatic  #23                 // Method areaForString:(Ljava/awt/Graphics2D;)Lkotlin/jvm/functions/Function1;
      21: invokestatic  #27                 // Method divideIntoLines:(Ljava/lang/String;Ljava/lang/String;ILkotlin/jvm/functions/Function1;)Ljava/util/List;
      24: astore        6
      26: aload         6
      28: iload         5
      30: invokestatic  #31                 // Method mostLinesThatFitHeight:(Ljava/util/List;I)Ljava/util/List;
      33: astore        7
      35: aload         7
      37: checkcast     #33                 // class java/lang/Iterable
      40: astore        9
      42: dconst_0
      43: dstore        10
      45: aload         9
      47: invokeinterface #37,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      52: astore        12
      54: aload         12
      56: invokeinterface #43,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      61: ifeq          107
      64: aload         12
      66: invokeinterface #47,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      71: astore        13
      73: dload         10
      75: aload         13
      77: checkcast     #49                 // class uk/co/skipoles/eventmodeller/visualisation/render/LineWithArea
      80: astore        14
      82: dstore        16
      84: iconst_0
      85: istore        15
      87: aload         14
      89: invokevirtual #53                 // Method uk/co/skipoles/eventmodeller/visualisation/render/LineWithArea.getArea:()Ljava/awt/geom/Rectangle2D;
      92: invokevirtual #59                 // Method java/awt/geom/Rectangle2D.getHeight:()D
      95: dstore        18
      97: dload         16
      99: dload         18
     101: dadd
     102: dstore        10
     104: goto          54
     107: dload         10
     109: d2i
     110: istore        8
     112: iconst_0
     113: istore        9
     115: iload_3
     116: iload         5
     118: iload         8
     120: isub
     121: iconst_2
     122: idiv
     123: iadd
     124: istore        9
     126: aload         7
     128: checkcast     #33                 // class java/lang/Iterable
     131: astore        10
     133: iconst_0
     134: istore        11
     136: aload         10
     138: invokeinterface #37,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     143: astore        12
     145: aload         12
     147: invokeinterface #43,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     152: ifeq          220
     155: aload         12
     157: invokeinterface #47,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     162: astore        13
     164: aload         13
     166: checkcast     #49                 // class uk/co/skipoles/eventmodeller/visualisation/render/LineWithArea
     169: astore        14
     171: iconst_0
     172: istore        15
     174: aload_0
     175: aload         14
     177: invokevirtual #63                 // Method uk/co/skipoles/eventmodeller/visualisation/render/LineWithArea.getS:()Ljava/lang/String;
     180: iload_2
     181: iload         4
     183: aload         14
     185: invokevirtual #53                 // Method uk/co/skipoles/eventmodeller/visualisation/render/LineWithArea.getArea:()Ljava/awt/geom/Rectangle2D;
     188: invokevirtual #66                 // Method java/awt/geom/Rectangle2D.getWidth:()D
     191: d2i
     192: isub
     193: iconst_2
     194: idiv
     195: iadd
     196: iload         9
     198: invokevirtual #72                 // Method java/awt/Graphics2D.drawString:(Ljava/lang/String;II)V
     201: iload         9
     203: aload         14
     205: invokevirtual #53                 // Method uk/co/skipoles/eventmodeller/visualisation/render/LineWithArea.getArea:()Ljava/awt/geom/Rectangle2D;
     208: invokevirtual #59                 // Method java/awt/geom/Rectangle2D.getHeight:()D
     211: d2i
     212: iadd
     213: istore        9
     215: nop
     216: nop
     217: goto          145
     220: nop
     221: return

  private static final java.util.List<uk.co.skipoles.eventmodeller.visualisation.render.LineWithArea> divideIntoLines(java.lang.String, java.lang.String, int, kotlin.jvm.functions.Function1<? super java.lang.String, ? extends java.awt.geom.Rectangle2D>);
    Code:
       0: aload_1
       1: checkcast     #101                // class java/lang/CharSequence
       4: invokeinterface #105,  1          // InterfaceMethod java/lang/CharSequence.length:()I
       9: ifne          16
      12: iconst_1
      13: goto          17
      16: iconst_0
      17: ifeq          42
      20: new           #49                 // class uk/co/skipoles/eventmodeller/visualisation/render/LineWithArea
      23: dup
      24: aload_0
      25: aload_3
      26: aload_0
      27: invokeinterface #111,  2          // InterfaceMethod kotlin/jvm/functions/Function1.invoke:(Ljava/lang/Object;)Ljava/lang/Object;
      32: checkcast     #55                 // class java/awt/geom/Rectangle2D
      35: invokespecial #115                // Method uk/co/skipoles/eventmodeller/visualisation/render/LineWithArea."<init>":(Ljava/lang/String;Ljava/awt/geom/Rectangle2D;)V
      38: invokestatic  #121                // Method kotlin/collections/CollectionsKt.listOf:(Ljava/lang/Object;)Ljava/util/List;
      41: areturn
      42: aload_1
      43: astore        5
      45: iconst_0
      46: istore        6
      48: iconst_0
      49: istore        7
      51: aload         5
      53: invokevirtual #122                // Method java/lang/String.length:()I
      56: istore        8
      58: iload         7
      60: iload         8
      62: if_icmpge     123
      65: aload         5
      67: iload         7
      69: invokevirtual #126                // Method java/lang/String.charAt:(I)C
      72: istore        9
      74: iconst_0
      75: istore        10
      77: iload         9
      79: bipush        32
      81: if_icmpeq     88
      84: iconst_1
      85: goto          89
      88: iconst_0
      89: ifne          117
      92: aload         5
      94: astore        9
      96: iconst_0
      97: istore        10
      99: aload         9
     101: iload         10
     103: iload         7
     105: invokevirtual #130                // Method java/lang/String.substring:(II)Ljava/lang/String;
     108: dup
     109: ldc           #132                // String substring(...)
     111: invokestatic  #135                // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
     114: goto          125
     117: iinc          7, 1
     120: goto          58
     123: aload         5
     125: astore        4
     127: aload_0
     128: checkcast     #101                // class java/lang/CharSequence
     131: invokeinterface #105,  1          // InterfaceMethod java/lang/CharSequence.length:()I
     136: ifne          143
     139: iconst_1
     140: goto          144
     143: iconst_0
     144: ifeq          152
     147: aload         4
     149: goto          176
     152: new           #137                // class java/lang/StringBuilder
     155: dup
     156: invokespecial #140                // Method java/lang/StringBuilder."<init>":()V
     159: aload_0
     160: invokevirtual #144                // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     163: bipush        32
     165: invokevirtual #147                // Method java/lang/StringBuilder.append:(C)Ljava/lang/StringBuilder;
     168: aload         4
     170: invokevirtual #144                // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     173: invokevirtual #150                // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
     176: astore        5
     178: aload_3
     179: aload         5
     181: invokeinterface #111,  2          // InterfaceMethod kotlin/jvm/functions/Function1.invoke:(Ljava/lang/Object;)Ljava/lang/Object;
     186: checkcast     #55                 // class java/awt/geom/Rectangle2D
     189: astore        6
     191: aload         6
     193: invokevirtual #66                 // Method java/awt/geom/Rectangle2D.getWidth:()D
     196: iload_2
     197: iconst_2
     198: isub
     199: i2d
     200: dcmpg
     201: ifge          225
     204: aload         5
     206: aload_1
     207: aload         4
     209: invokevirtual #122                // Method java/lang/String.length:()I
     212: iconst_1
     213: iadd
     214: invokestatic  #156                // Method kotlin/text/StringsKt.drop:(Ljava/lang/String;I)Ljava/lang/String;
     217: iload_2
     218: aload_3
     219: invokestatic  #27                 // Method divideIntoLines:(Ljava/lang/String;Ljava/lang/String;ILkotlin/jvm/functions/Function1;)Ljava/util/List;
     222: goto          352
     225: aload_0
     226: checkcast     #101                // class java/lang/CharSequence
     229: invokeinterface #105,  1          // InterfaceMethod java/lang/CharSequence.length:()I
     234: ifne          241
     237: iconst_1
     238: goto          242
     241: iconst_0
     242: ifeq          305
     245: aload         5
     247: iload_2
     248: aload_3
     249: invokestatic  #160                // Method longestSubstringThatFitsWidth:(Ljava/lang/String;ILkotlin/jvm/functions/Function1;)Ljava/lang/String;
     252: astore        7
     254: new           #49                 // class uk/co/skipoles/eventmodeller/visualisation/render/LineWithArea
     257: dup
     258: aload         7
     260: aload_3
     261: aload         7
     263: invokeinterface #111,  2          // InterfaceMethod kotlin/jvm/functions/Function1.invoke:(Ljava/lang/Object;)Ljava/lang/Object;
     268: checkcast     #55                 // class java/awt/geom/Rectangle2D
     271: invokespecial #115                // Method uk/co/skipoles/eventmodeller/visualisation/render/LineWithArea."<init>":(Ljava/lang/String;Ljava/awt/geom/Rectangle2D;)V
     274: invokestatic  #121                // Method kotlin/collections/CollectionsKt.listOf:(Ljava/lang/Object;)Ljava/util/List;
     277: checkcast     #162                // class java/util/Collection
     280: ldc           #19                 // String
     282: aload_1
     283: aload         7
     285: invokevirtual #122                // Method java/lang/String.length:()I
     288: invokestatic  #156                // Method kotlin/text/StringsKt.drop:(Ljava/lang/String;I)Ljava/lang/String;
     291: iload_2
     292: aload_3
     293: invokestatic  #27                 // Method divideIntoLines:(Ljava/lang/String;Ljava/lang/String;ILkotlin/jvm/functions/Function1;)Ljava/util/List;
     296: checkcast     #33                 // class java/lang/Iterable
     299: invokestatic  #166                // Method kotlin/collections/CollectionsKt.plus:(Ljava/util/Collection;Ljava/lang/Iterable;)Ljava/util/List;
     302: goto          352
     305: new           #49                 // class uk/co/skipoles/eventmodeller/visualisation/render/LineWithArea
     308: dup
     309: aload_0
     310: aload_3
     311: aload_0
     312: invokeinterface #111,  2          // InterfaceMethod kotlin/jvm/functions/Function1.invoke:(Ljava/lang/Object;)Ljava/lang/Object;
     317: checkcast     #55                 // class java/awt/geom/Rectangle2D
     320: invokespecial #115                // Method uk/co/skipoles/eventmodeller/visualisation/render/LineWithArea."<init>":(Ljava/lang/String;Ljava/awt/geom/Rectangle2D;)V
     323: invokestatic  #121                // Method kotlin/collections/CollectionsKt.listOf:(Ljava/lang/Object;)Ljava/util/List;
     326: checkcast     #162                // class java/util/Collection
     329: ldc           #19                 // String
     331: aload_1
     332: checkcast     #101                // class java/lang/CharSequence
     335: invokestatic  #170                // Method kotlin/text/StringsKt.trim:(Ljava/lang/CharSequence;)Ljava/lang/CharSequence;
     338: invokevirtual #171                // Method java/lang/Object.toString:()Ljava/lang/String;
     341: iload_2
     342: aload_3
     343: invokestatic  #27                 // Method divideIntoLines:(Ljava/lang/String;Ljava/lang/String;ILkotlin/jvm/functions/Function1;)Ljava/util/List;
     346: checkcast     #33                 // class java/lang/Iterable
     349: invokestatic  #166                // Method kotlin/collections/CollectionsKt.plus:(Ljava/util/Collection;Ljava/lang/Iterable;)Ljava/util/List;
     352: areturn

  private static final java.lang.String longestSubstringThatFitsWidth(java.lang.String, int, kotlin.jvm.functions.Function1<? super java.lang.String, ? extends java.awt.geom.Rectangle2D>);
    Code:
       0: aload_0
       1: checkcast     #101                // class java/lang/CharSequence
       4: astore_3
       5: new           #189                // class kotlin/Pair
       8: dup
       9: ldc           #19                 // String
      11: iconst_0
      12: invokestatic  #195                // Method java/lang/Boolean.valueOf:(Z)Ljava/lang/Boolean;
      15: invokespecial #198                // Method kotlin/Pair."<init>":(Ljava/lang/Object;Ljava/lang/Object;)V
      18: astore        4
      20: iconst_0
      21: istore        5
      23: aload         4
      25: astore        6
      27: iconst_0
      28: istore        7
      30: iload         7
      32: aload_3
      33: invokeinterface #105,  1          // InterfaceMethod java/lang/CharSequence.length:()I
      38: if_icmpge     181
      41: aload_3
      42: iload         7
      44: invokeinterface #199,  2          // InterfaceMethod java/lang/CharSequence.charAt:(I)C
      49: istore        8
      51: aload         6
      53: iload         8
      55: istore        9
      57: astore        10
      59: iconst_0
      60: istore        11
      62: aload         10
      64: invokevirtual #202                // Method kotlin/Pair.component1:()Ljava/lang/Object;
      67: checkcast     #96                 // class java/lang/String
      70: astore        12
      72: aload         10
      74: invokevirtual #205                // Method kotlin/Pair.component2:()Ljava/lang/Object;
      77: checkcast     #191                // class java/lang/Boolean
      80: invokevirtual #208                // Method java/lang/Boolean.booleanValue:()Z
      83: istore        13
      85: iload         13
      87: ifeq          95
      90: aload         10
      92: goto          172
      95: new           #137                // class java/lang/StringBuilder
      98: dup
      99: invokespecial #140                // Method java/lang/StringBuilder."<init>":()V
     102: aload         12
     104: invokevirtual #144                // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     107: iload         9
     109: invokevirtual #147                // Method java/lang/StringBuilder.append:(C)Ljava/lang/StringBuilder;
     112: invokevirtual #150                // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
     115: astore        14
     117: aload_2
     118: aload         14
     120: invokeinterface #111,  2          // InterfaceMethod kotlin/jvm/functions/Function1.invoke:(Ljava/lang/Object;)Ljava/lang/Object;
     125: checkcast     #55                 // class java/awt/geom/Rectangle2D
     128: astore        15
     130: aload         15
     132: invokevirtual #66                 // Method java/awt/geom/Rectangle2D.getWidth:()D
     135: iload_1
     136: iconst_2
     137: isub
     138: i2d
     139: dcmpg
     140: ifge          159
     143: new           #189                // class kotlin/Pair
     146: dup
     147: aload         14
     149: iconst_0
     150: invokestatic  #195                // Method java/lang/Boolean.valueOf:(Z)Ljava/lang/Boolean;
     153: invokespecial #198                // Method kotlin/Pair."<init>":(Ljava/lang/Object;Ljava/lang/Object;)V
     156: goto          172
     159: new           #189                // class kotlin/Pair
     162: dup
     163: aload         12
     165: iconst_1
     166: invokestatic  #195                // Method java/lang/Boolean.valueOf:(Z)Ljava/lang/Boolean;
     169: invokespecial #198                // Method kotlin/Pair."<init>":(Ljava/lang/Object;Ljava/lang/Object;)V
     172: nop
     173: astore        6
     175: iinc          7, 1
     178: goto          30
     181: aload         6
     183: invokevirtual #211                // Method kotlin/Pair.getFirst:()Ljava/lang/Object;
     186: checkcast     #96                 // class java/lang/String
     189: areturn

  private static final java.util.List<uk.co.skipoles.eventmodeller.visualisation.render.LineWithArea> mostLinesThatFitHeight(java.util.List<uk.co.skipoles.eventmodeller.visualisation.render.LineWithArea>, int);
    Code:
       0: aload_0
       1: checkcast     #33                 // class java/lang/Iterable
       4: astore_2
       5: new           #189                // class kotlin/Pair
       8: dup
       9: invokestatic  #230                // Method kotlin/collections/CollectionsKt.emptyList:()Ljava/util/List;
      12: iconst_0
      13: invokestatic  #195                // Method java/lang/Boolean.valueOf:(Z)Ljava/lang/Boolean;
      16: invokespecial #198                // Method kotlin/Pair."<init>":(Ljava/lang/Object;Ljava/lang/Object;)V
      19: astore_3
      20: iconst_0
      21: istore        4
      23: aload_3
      24: astore        5
      26: aload_2
      27: invokeinterface #37,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      32: astore        6
      34: aload         6
      36: invokeinterface #43,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      41: ifeq          238
      44: aload         6
      46: invokeinterface #47,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      51: astore        7
      53: aload         5
      55: aload         7
      57: checkcast     #49                 // class uk/co/skipoles/eventmodeller/visualisation/render/LineWithArea
      60: astore        8
      62: astore        9
      64: iconst_0
      65: istore        10
      67: aload         9
      69: invokevirtual #202                // Method kotlin/Pair.component1:()Ljava/lang/Object;
      72: checkcast     #98                 // class java/util/List
      75: astore        11
      77: aload         9
      79: invokevirtual #205                // Method kotlin/Pair.component2:()Ljava/lang/Object;
      82: checkcast     #191                // class java/lang/Boolean
      85: invokevirtual #208                // Method java/lang/Boolean.booleanValue:()Z
      88: istore        12
      90: iload         12
      92: ifeq          100
      95: aload         9
      97: goto          232
     100: aload         11
     102: checkcast     #33                 // class java/lang/Iterable
     105: astore        13
     107: dconst_0
     108: dstore        14
     110: aload         13
     112: invokeinterface #37,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     117: astore        16
     119: aload         16
     121: invokeinterface #43,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     126: ifeq          172
     129: aload         16
     131: invokeinterface #47,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     136: astore        17
     138: dload         14
     140: aload         17
     142: checkcast     #49                 // class uk/co/skipoles/eventmodeller/visualisation/render/LineWithArea
     145: astore        18
     147: dstore        19
     149: iconst_0
     150: istore        21
     152: aload         18
     154: invokevirtual #53                 // Method uk/co/skipoles/eventmodeller/visualisation/render/LineWithArea.getArea:()Ljava/awt/geom/Rectangle2D;
     157: invokevirtual #59                 // Method java/awt/geom/Rectangle2D.getHeight:()D
     160: dstore        22
     162: dload         19
     164: dload         22
     166: dadd
     167: dstore        14
     169: goto          119
     172: dload         14
     174: aload         8
     176: invokevirtual #53                 // Method uk/co/skipoles/eventmodeller/visualisation/render/LineWithArea.getArea:()Ljava/awt/geom/Rectangle2D;
     179: invokevirtual #59                 // Method java/awt/geom/Rectangle2D.getHeight:()D
     182: dadd
     183: dstore        24
     185: dload         24
     187: iload_1
     188: iconst_2
     189: isub
     190: i2d
     191: dcmpg
     192: ifge          219
     195: new           #189                // class kotlin/Pair
     198: dup
     199: aload         11
     201: checkcast     #162                // class java/util/Collection
     204: aload         8
     206: invokestatic  #233                // Method kotlin/collections/CollectionsKt.plus:(Ljava/util/Collection;Ljava/lang/Object;)Ljava/util/List;
     209: iconst_0
     210: invokestatic  #195                // Method java/lang/Boolean.valueOf:(Z)Ljava/lang/Boolean;
     213: invokespecial #198                // Method kotlin/Pair."<init>":(Ljava/lang/Object;Ljava/lang/Object;)V
     216: goto          232
     219: new           #189                // class kotlin/Pair
     222: dup
     223: aload         11
     225: iconst_1
     226: invokestatic  #195                // Method java/lang/Boolean.valueOf:(Z)Ljava/lang/Boolean;
     229: invokespecial #198                // Method kotlin/Pair."<init>":(Ljava/lang/Object;Ljava/lang/Object;)V
     232: nop
     233: astore        5
     235: goto          34
     238: aload         5
     240: invokevirtual #211                // Method kotlin/Pair.getFirst:()Ljava/lang/Object;
     243: checkcast     #98                 // class java/util/List
     246: areturn

  private static final kotlin.jvm.functions.Function1<java.lang.String, java.awt.geom.Rectangle2D> areaForString(java.awt.Graphics2D);
    Code:
       0: aload_0
       1: invokedynamic #259,  0            // InvokeDynamic #0:invoke:(Ljava/awt/Graphics2D;)Lkotlin/jvm/functions/Function1;
       6: areturn

  private static final java.awt.geom.Rectangle2D areaForString$lambda$6(java.awt.Graphics2D, java.lang.String);
    Code:
       0: aload_0
       1: invokevirtual #264                // Method java/awt/Graphics2D.getFont:()Ljava/awt/Font;
       4: aload_1
       5: aload_0
       6: invokevirtual #268                // Method java/awt/Graphics2D.getFontRenderContext:()Ljava/awt/font/FontRenderContext;
       9: invokevirtual #274                // Method java/awt/Font.getStringBounds:(Ljava/lang/String;Ljava/awt/font/FontRenderContext;)Ljava/awt/geom/Rectangle2D;
      12: areturn
}


package org.fundamentals.fp.euler

/**
 * Largest prime factor
 * https://projecteuler.net/problem=3
 *
 * The prime factors of 13195 are 5, 7, 13 and 29.
 *
 * What is the largest prime factor of the number 600851475143 ?
 *
 * Scenario 13195
 *
 * Given primeFactor
 * When 13195
 * Then 29 [5, 7, 13, 29]
 *
 * Scenario 600851475143
 *
 * Given primeFactor
 * When 600851475143
 * Then ?
 */
fun KotlinSolution03(limit : Long) : Long {

    return limit.primeFactors().max()!!
}

/**
 * Checks if this number is a multiple of the provided number _(eg. this % other == 0)_.
 *
 * _Reference: [http://mathworld.wolfram.com/Multiple.html]_
 *
 * @return true if the receiver is a multiple of the provided number
 */
infix fun Long.isMultipleOf(other: Long) = this % other == 0L

/**
 * Returns the square root of the receiver _(eg. from Math.sqrt)_.
 *
 * _Reference: [http://mathworld.wolfram.com/SquareRoot.html]_
 *
 * @return the square root of the receiver
 */
fun Double.squareRoot() = Math.sqrt(this)

/**
 * @see [Double.squareRoot]
 */
fun Double.sqrt() = this.squareRoot()

/**
 * Returns the integer part of the receiver _(eg. removes the decimal part)_.
 *
 * Example: `10.25 becomes 10`
 *
 * _Reference: [http://mathworld.wolfram.com/FloorFunction.html]_
 *
 * @return the integer part of the receiver
 */
fun Double.floor() = this.toLong()

/**
 * @see [Double.squareRoot]
 */
fun Long.sqrt() = this.toDouble().sqrt()

/**
 * Checks if the receiver number is a prime number _(eg. only divisors are 1 and itself)_.
 *
 * _Reference: [http://mathworld.wolfram.com/PrimeNumber.html]_
 *
 * @return true if the receiver number is a prime
 */
fun Long.isPrime() = this > 1 && (2..this.sqrt().floor()).all { !(this isMultipleOf it) }

/**
 * Returns a List containing all prime factors of the receiver numbers _(eg. factorization of receiver into its consituent primes)_.
 *
 * _Reference: [http://mathworld.wolfram.com/PrimeFactor.html]_
 *
 * @return a List containing all prime factors of the receiver number
 */
fun Long.primeFactors(): List<Long> = if (isPrime()) listOf(this) else {
    val nextPrimeFactor = (2..this.sqrt().floor()).find { this isMultipleOf it && it.isPrime() }
    if (nextPrimeFactor == null) emptyList()
    else listOf(nextPrimeFactor) + (this / nextPrimeFactor).primeFactors()
}

Compiled from "EulerProblem03.kt"
public final class org.fundamentals.fp.euler.EulerProblem03Kt {
  public static final long KotlinSolution03(long);
    Code:
       0: lload_0
       1: invokestatic  #10                 // Method primeFactors:(J)Ljava/util/List;
       4: checkcast     #12                 // class java/lang/Iterable
       7: invokestatic  #18                 // Method kotlin/collections/CollectionsKt.maxOrThrow:(Ljava/lang/Iterable;)Ljava/lang/Comparable;
      10: dup
      11: invokestatic  #24                 // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
      14: checkcast     #26                 // class java/lang/Number
      17: invokevirtual #30                 // Method java/lang/Number.longValue:()J
      20: lreturn

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

  public static final double squareRoot(double);
    Code:
       0: dload_0
       1: invokestatic  #43                 // Method java/lang/Math.sqrt:(D)D
       4: dreturn

  public static final double sqrt(double);
    Code:
       0: dload_0
       1: invokestatic  #47                 // Method squareRoot:(D)D
       4: dreturn

  public static final long floor(double);
    Code:
       0: dload_0
       1: d2l
       2: lreturn

  public static final double sqrt(long);
    Code:
       0: lload_0
       1: l2d
       2: invokestatic  #53                 // Method sqrt:(D)D
       5: dreturn

  public static final boolean isPrime(long);
    Code:
       0: lload_0
       1: lconst_1
       2: lcmp
       3: ifle          115
       6: new           #57                 // class kotlin/ranges/LongRange
       9: dup
      10: iconst_2
      11: i2l
      12: lload_0
      13: invokestatic  #59                 // Method sqrt:(J)D
      16: invokestatic  #61                 // Method floor:(D)J
      19: invokespecial #65                 // Method kotlin/ranges/LongRange."<init>":(JJ)V
      22: checkcast     #12                 // class java/lang/Iterable
      25: astore_2
      26: iconst_0
      27: istore_3
      28: aload_2
      29: instanceof    #67                 // class java/util/Collection
      32: ifeq          51
      35: aload_2
      36: checkcast     #67                 // class java/util/Collection
      39: invokeinterface #71,  1           // InterfaceMethod java/util/Collection.isEmpty:()Z
      44: ifeq          51
      47: iconst_1
      48: goto          108
      51: aload_2
      52: invokeinterface #75,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      57: astore        4
      59: aload         4
      61: invokeinterface #80,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      66: ifeq          107
      69: aload         4
      71: checkcast     #82                 // class kotlin/collections/LongIterator
      74: invokevirtual #85                 // Method kotlin/collections/LongIterator.nextLong:()J
      77: lstore        5
      79: lload         5
      81: lstore        7
      83: iconst_0
      84: istore        9
      86: lload_0
      87: lload         7
      89: invokestatic  #87                 // Method isMultipleOf:(JJ)Z
      92: ifne          99
      95: iconst_1
      96: goto          100
      99: iconst_0
     100: ifne          59
     103: iconst_0
     104: goto          108
     107: iconst_1
     108: ifeq          115
     111: iconst_1
     112: goto          116
     115: iconst_0
     116: ireturn

  public static final java.util.List<java.lang.Long> primeFactors(long);
    Code:
       0: lload_0
       1: invokestatic  #99                 // Method isPrime:(J)Z
       4: ifeq          17
       7: lload_0
       8: invokestatic  #105                // Method java/lang/Long.valueOf:(J)Ljava/lang/Long;
      11: invokestatic  #109                // Method kotlin/collections/CollectionsKt.listOf:(Ljava/lang/Object;)Ljava/util/List;
      14: goto          144
      17: new           #57                 // class kotlin/ranges/LongRange
      20: dup
      21: iconst_2
      22: i2l
      23: lload_0
      24: invokestatic  #59                 // Method sqrt:(J)D
      27: invokestatic  #61                 // Method floor:(D)J
      30: invokespecial #65                 // Method kotlin/ranges/LongRange."<init>":(JJ)V
      33: checkcast     #12                 // class java/lang/Iterable
      36: astore_3
      37: aload_3
      38: invokeinterface #75,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      43: astore        4
      45: aload         4
      47: invokeinterface #80,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      52: ifeq          107
      55: aload         4
      57: invokeinterface #113,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      62: astore        5
      64: aload         5
      66: checkcast     #26                 // class java/lang/Number
      69: invokevirtual #30                 // Method java/lang/Number.longValue:()J
      72: lstore        6
      74: iconst_0
      75: istore        8
      77: lload_0
      78: lload         6
      80: invokestatic  #87                 // Method isMultipleOf:(JJ)Z
      83: ifeq          98
      86: lload         6
      88: invokestatic  #99                 // Method isPrime:(J)Z
      91: ifeq          98
      94: iconst_1
      95: goto          99
      98: iconst_0
      99: ifeq          45
     102: aload         5
     104: goto          108
     107: aconst_null
     108: checkcast     #101                // class java/lang/Long
     111: astore_2
     112: aload_2
     113: ifnonnull     122
     116: invokestatic  #117                // Method kotlin/collections/CollectionsKt.emptyList:()Ljava/util/List;
     119: goto          144
     122: aload_2
     123: invokestatic  #109                // Method kotlin/collections/CollectionsKt.listOf:(Ljava/lang/Object;)Ljava/util/List;
     126: checkcast     #67                 // class java/util/Collection
     129: lload_0
     130: aload_2
     131: invokevirtual #118                // Method java/lang/Long.longValue:()J
     134: ldiv
     135: invokestatic  #10                 // Method primeFactors:(J)Ljava/util/List;
     138: checkcast     #12                 // class java/lang/Iterable
     141: invokestatic  #122                // Method kotlin/collections/CollectionsKt.plus:(Ljava/util/Collection;Ljava/lang/Iterable;)Ljava/util/List;
     144: areturn
}


package main.algorithms.searching

import java.util.*
import kotlin.collections.*
import kotlin.ranges.*
import kotlin.text.*

// Complete the whatFlavors function below.
fun whatFlavors(menu: Array<Int>, money: Int): Array<Int>? {

    val sortedMenu = menu.clone()

    Arrays.sort(sortedMenu)

    for ( i in 0 until sortedMenu.size) {
        val compliment = money - sortedMenu[i]
        val location = Arrays.binarySearch(sortedMenu, i+1, sortedMenu.size, compliment)

        if(location >=0 && location < sortedMenu.size && sortedMenu[location] == compliment) {
            return getIndicesFromValue(menu, sortedMenu[i], compliment)
        }
    }
    return null
}

fun getIndicesFromValue(menu: Array<Int>, value1: Int, value2: Int): Array<Int> {
    val index1 = indexOf(menu,value1, -1)
    val index2 = indexOf(menu,value2,index1)
    val indices:Array<Int> = Array(2){0}
    indices[0] = Math.min(index1,index2)
    indices[1] = Math.max(index1,index2)
    return indices
}

fun indexOf(array:Array<Int>, value:Int, excludeIndex:Int) :Int{
    for( i in 0 until array.size) {
        if(array[i] == value && i != excludeIndex) {
            return i
        }
    }
    return -1
}

fun main(args: Array<String>) {
    val scan = Scanner(System.`in`)

    val t = scan.nextLine().trim().toInt()

    for (tItr in 1..t) {
        val money = scan.nextLine().trim().toInt()

        val n = scan.nextLine().trim().toInt()

        val cost = scan.nextLine().trim().split(" ").map{ it.trim().toInt() }.toTypedArray()

        (whatFlavors(cost,money)?.map { print("${it+1} ") })
        println()
    }
}

Compiled from "IceCreamParlour.kt"
public final class main.algorithms.searching.IceCreamParlourKt {
  public static final java.lang.Integer[] whatFlavors(java.lang.Integer[], int);
    Code:
       0: aload_0
       1: ldc           #10                 // String menu
       3: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: invokevirtual #20                 // Method java/lang/Object.clone:()Ljava/lang/Object;
      10: checkcast     #22                 // class "[Ljava/lang/Integer;"
      13: astore_2
      14: aload_2
      15: invokestatic  #28                 // Method java/util/Arrays.sort:([Ljava/lang/Object;)V
      18: iconst_0
      19: istore_3
      20: aload_2
      21: arraylength
      22: istore        4
      24: iload_3
      25: iload         4
      27: if_icmpge     99
      30: iload_1
      31: aload_2
      32: iload_3
      33: aaload
      34: invokevirtual #34                 // Method java/lang/Integer.intValue:()I
      37: isub
      38: istore        5
      40: aload_2
      41: iload_3
      42: iconst_1
      43: iadd
      44: aload_2
      45: arraylength
      46: iload         5
      48: invokestatic  #38                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      51: invokestatic  #42                 // Method java/util/Arrays.binarySearch:([Ljava/lang/Object;IILjava/lang/Object;)I
      54: istore        6
      56: iload         6
      58: iflt          93
      61: iload         6
      63: aload_2
      64: arraylength
      65: if_icmpge     93
      68: aload_2
      69: iload         6
      71: aaload
      72: invokevirtual #34                 // Method java/lang/Integer.intValue:()I
      75: iload         5
      77: if_icmpne     93
      80: aload_0
      81: aload_2
      82: iload_3
      83: aaload
      84: invokevirtual #34                 // Method java/lang/Integer.intValue:()I
      87: iload         5
      89: invokestatic  #46                 // Method getIndicesFromValue:([Ljava/lang/Integer;II)[Ljava/lang/Integer;
      92: areturn
      93: iinc          3, 1
      96: goto          24
      99: aconst_null
     100: areturn

  public static final java.lang.Integer[] getIndicesFromValue(java.lang.Integer[], int, int);
    Code:
       0: aload_0
       1: ldc           #10                 // String menu
       3: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: iload_1
       8: iconst_m1
       9: invokestatic  #56                 // Method indexOf:([Ljava/lang/Integer;II)I
      12: istore_3
      13: aload_0
      14: iload_2
      15: iload_3
      16: invokestatic  #56                 // Method indexOf:([Ljava/lang/Integer;II)I
      19: istore        4
      21: iconst_0
      22: istore        6
      24: iconst_2
      25: anewarray     #30                 // class java/lang/Integer
      28: astore        7
      30: iload         6
      32: iconst_2
      33: if_icmpge     55
      36: iload         6
      38: istore        8
      40: aload         7
      42: iload         8
      44: iconst_0
      45: invokestatic  #38                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      48: aastore
      49: iinc          6, 1
      52: goto          30
      55: aload         7
      57: astore        5
      59: aload         5
      61: iconst_0
      62: iload_3
      63: iload         4
      65: invokestatic  #62                 // Method java/lang/Math.min:(II)I
      68: invokestatic  #38                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      71: aastore
      72: aload         5
      74: iconst_1
      75: iload_3
      76: iload         4
      78: invokestatic  #65                 // Method java/lang/Math.max:(II)I
      81: invokestatic  #38                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      84: aastore
      85: aload         5
      87: areturn

  public static final int indexOf(java.lang.Integer[], int, int);
    Code:
       0: aload_0
       1: ldc           #72                 // String array
       3: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: iconst_0
       7: istore_3
       8: aload_0
       9: arraylength
      10: istore        4
      12: iload_3
      13: iload         4
      15: if_icmpge     41
      18: aload_0
      19: iload_3
      20: aaload
      21: invokevirtual #34                 // Method java/lang/Integer.intValue:()I
      24: iload_1
      25: if_icmpne     35
      28: iload_3
      29: iload_2
      30: if_icmpeq     35
      33: iload_3
      34: ireturn
      35: iinc          3, 1
      38: goto          12
      41: iconst_m1
      42: ireturn

  public static final void main(java.lang.String[]);
    Code:
       0: aload_0
       1: ldc           #78                 // String args
       3: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: new           #80                 // class java/util/Scanner
       9: dup
      10: getstatic     #86                 // Field java/lang/System.in:Ljava/io/InputStream;
      13: invokespecial #90                 // Method java/util/Scanner."<init>":(Ljava/io/InputStream;)V
      16: astore_1
      17: aload_1
      18: invokevirtual #94                 // Method java/util/Scanner.nextLine:()Ljava/lang/String;
      21: dup
      22: ldc           #96                 // String nextLine(...)
      24: invokestatic  #99                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
      27: checkcast     #101                // class java/lang/CharSequence
      30: invokestatic  #107                // Method kotlin/text/StringsKt.trim:(Ljava/lang/CharSequence;)Ljava/lang/CharSequence;
      33: invokevirtual #110                // Method java/lang/Object.toString:()Ljava/lang/String;
      36: invokestatic  #114                // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
      39: istore_2
      40: iconst_1
      41: istore_3
      42: iload_3
      43: iload_2
      44: if_icmpgt     434
      47: aload_1
      48: invokevirtual #94                 // Method java/util/Scanner.nextLine:()Ljava/lang/String;
      51: dup
      52: ldc           #96                 // String nextLine(...)
      54: invokestatic  #99                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
      57: checkcast     #101                // class java/lang/CharSequence
      60: invokestatic  #107                // Method kotlin/text/StringsKt.trim:(Ljava/lang/CharSequence;)Ljava/lang/CharSequence;
      63: invokevirtual #110                // Method java/lang/Object.toString:()Ljava/lang/String;
      66: invokestatic  #114                // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
      69: istore        4
      71: aload_1
      72: invokevirtual #94                 // Method java/util/Scanner.nextLine:()Ljava/lang/String;
      75: dup
      76: ldc           #96                 // String nextLine(...)
      78: invokestatic  #99                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
      81: checkcast     #101                // class java/lang/CharSequence
      84: invokestatic  #107                // Method kotlin/text/StringsKt.trim:(Ljava/lang/CharSequence;)Ljava/lang/CharSequence;
      87: invokevirtual #110                // Method java/lang/Object.toString:()Ljava/lang/String;
      90: invokestatic  #114                // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
      93: istore        5
      95: aload_1
      96: invokevirtual #94                 // Method java/util/Scanner.nextLine:()Ljava/lang/String;
      99: dup
     100: ldc           #96                 // String nextLine(...)
     102: invokestatic  #99                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
     105: checkcast     #101                // class java/lang/CharSequence
     108: invokestatic  #107                // Method kotlin/text/StringsKt.trim:(Ljava/lang/CharSequence;)Ljava/lang/CharSequence;
     111: invokevirtual #110                // Method java/lang/Object.toString:()Ljava/lang/String;
     114: checkcast     #101                // class java/lang/CharSequence
     117: iconst_1
     118: anewarray     #116                // class java/lang/String
     121: astore        7
     123: aload         7
     125: iconst_0
     126: ldc           #118                // String
     128: aastore
     129: aload         7
     131: iconst_0
     132: iconst_0
     133: bipush        6
     135: aconst_null
     136: invokestatic  #122                // Method kotlin/text/StringsKt.split$default:(Ljava/lang/CharSequence;[Ljava/lang/String;ZIILjava/lang/Object;)Ljava/util/List;
     139: checkcast     #124                // class java/lang/Iterable
     142: astore        7
     144: iconst_0
     145: istore        8
     147: aload         7
     149: astore        9
     151: new           #126                // class java/util/ArrayList
     154: dup
     155: aload         7
     157: bipush        10
     159: invokestatic  #132                // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
     162: invokespecial #135                // Method java/util/ArrayList."<init>":(I)V
     165: checkcast     #137                // class java/util/Collection
     168: astore        10
     170: iconst_0
     171: istore        11
     173: aload         9
     175: invokeinterface #141,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     180: astore        12
     182: aload         12
     184: invokeinterface #147,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
     189: ifeq          246
     192: aload         12
     194: invokeinterface #150,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     199: astore        13
     201: aload         10
     203: aload         13
     205: checkcast     #116                // class java/lang/String
     208: astore        14
     210: astore        18
     212: iconst_0
     213: istore        15
     215: nop
     216: aload         14
     218: checkcast     #101                // class java/lang/CharSequence
     221: invokestatic  #107                // Method kotlin/text/StringsKt.trim:(Ljava/lang/CharSequence;)Ljava/lang/CharSequence;
     224: invokevirtual #110                // Method java/lang/Object.toString:()Ljava/lang/String;
     227: invokestatic  #114                // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
     230: nop
     231: invokestatic  #38                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     234: aload         18
     236: swap
     237: invokeinterface #154,  2          // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     242: pop
     243: goto          182
     246: aload         10
     248: checkcast     #156                // class java/util/List
     251: nop
     252: checkcast     #137                // class java/util/Collection
     255: astore        7
     257: nop
     258: iconst_0
     259: istore        8
     261: aload         7
     263: astore        9
     265: aload         9
     267: iconst_0
     268: anewarray     #30                 // class java/lang/Integer
     271: invokeinterface #160,  2          // InterfaceMethod java/util/Collection.toArray:([Ljava/lang/Object;)[Ljava/lang/Object;
     276: checkcast     #22                 // class "[Ljava/lang/Integer;"
     279: astore        6
     281: aload         6
     283: iload         4
     285: invokestatic  #162                // Method whatFlavors:([Ljava/lang/Integer;I)[Ljava/lang/Integer;
     288: dup
     289: ifnull        415
     292: astore        8
     294: iconst_0
     295: istore        9
     297: aload         8
     299: astore        10
     301: new           #126                // class java/util/ArrayList
     304: dup
     305: aload         8
     307: arraylength
     308: invokespecial #135                // Method java/util/ArrayList."<init>":(I)V
     311: checkcast     #137                // class java/util/Collection
     314: astore        11
     316: iconst_0
     317: istore        12
     319: iconst_0
     320: istore        13
     322: aload         10
     324: arraylength
     325: istore        14
     327: iload         13
     329: iload         14
     331: if_icmpge     405
     334: aload         10
     336: iload         13
     338: aaload
     339: astore        15
     341: aload         11
     343: aload         15
     345: checkcast     #164                // class java/lang/Number
     348: invokevirtual #165                // Method java/lang/Number.intValue:()I
     351: istore        16
     353: astore        18
     355: iconst_0
     356: istore        17
     358: new           #167                // class java/lang/StringBuilder
     361: dup
     362: invokespecial #170                // Method java/lang/StringBuilder."<init>":()V
     365: iload         16
     367: iconst_1
     368: iadd
     369: invokevirtual #174                // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
     372: bipush        32
     374: invokevirtual #177                // Method java/lang/StringBuilder.append:(C)Ljava/lang/StringBuilder;
     377: invokevirtual #178                // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
     380: getstatic     #182                // Field java/lang/System.out:Ljava/io/PrintStream;
     383: swap
     384: invokevirtual #188                // Method java/io/PrintStream.print:(Ljava/lang/Object;)V
     387: nop
     388: aload         18
     390: getstatic     #194                // Field kotlin/Unit.INSTANCE:Lkotlin/Unit;
     393: invokeinterface #154,  2          // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     398: pop
     399: iinc          13, 1
     402: goto          327
     405: aload         11
     407: checkcast     #156                // class java/util/List
     410: pop
     411: nop
     412: goto          417
     415: pop
     416: nop
     417: getstatic     #182                // Field java/lang/System.out:Ljava/io/PrintStream;
     420: invokevirtual #197                // Method java/io/PrintStream.println:()V
     423: iload_3
     424: iload_2
     425: if_icmpeq     434
     428: iinc          3, 1
     431: goto          47
     434: return
}