package day11

import java.io.File

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

data class Octopus(
    var energy: Int,
    var flashed: Boolean = false
) {
    fun canFlash(): Boolean = energy > 9 && !flashed

    fun flash() {
        energy = 0
        flashed = true
    }

    fun reset() {
        if (flashed) {
            flashed = false
            energy = 0
        }
    }
}

fun List<List<Octopus>>.dump(): String {
    val octopuses = this
    return buildString {
        octopuses.forEach { row ->
            row.forEach { octopus ->
                append(octopus.energy)
            }
            appendLine()
        }
    }.trim()
}

fun List<List<Octopus>>.increase() {
    forEach { row ->
        row.forEach { octopus ->
            octopus.energy++
        }
    }
}

fun List<List<Octopus>>.increase(neighbors: List<Position>) {
    neighbors.forEach { neighbor ->
        get(neighbor.y)[neighbor.x].energy++
    }
}

fun List<List<Octopus>>.neighborsOf(x: Int, y: Int): List<Position> {
    val candidates = listOf(
        Position(x - 1, y - 1),
        Position(x, y - 1),
        Position(x + 1, y - 1),

        Position(x - 1, y),
        Position(x + 1, y),

        Position(x - 1, y + 1),
        Position(x, y + 1),
        Position(x + 1, y + 1),
    )

    return candidates.filter { candidate ->
        candidate.y in indices &&
        candidate.x in get(0).indices
    }
}

fun List<List<Octopus>>.flash(): Pair<Int, List<Position>> {
    val neighbors = mutableListOf<Position>()
    var flashes = 0

    indices.forEach { y ->
        get(y).indices.forEach { x ->
            val octopus = get(y)[x]
            if (octopus.canFlash()) {
                octopus.flash()
                flashes++
                neighbors.addAll(neighborsOf(x, y))
            }
        }
    }

    return Pair(flashes, neighbors)
}

fun List<List<Octopus>>.simulate(): Int {
    increase()

    var (flashes, neighbors) = flash()
    while (neighbors.isNotEmpty()) {
        increase(neighbors)
        val (additionalFlashes, newNeighbors) = flash()
        neighbors = newNeighbors
        flashes += additionalFlashes
    }

    reset()

    return flashes
}

fun List<List<Octopus>>.simulate(times: Int): Int {
    var flashes = 0

    repeat(times) {
        flashes += simulate()
    }

    return flashes
}

fun List<List<Octopus>>.reset() {
    forEach { row ->
        row.forEach { octopus ->
            octopus.reset()
        }
    }
}

fun List<List<Octopus>>.allFlashed(): Boolean {
    return all { row ->
        row.all { octopus -> octopus.energy == 0 }
    }
}

fun List<List<Octopus>>.simulateUntilAllFlashed(): Int {
    var step = 0
    while (!allFlashed()) {
        simulate()
        step++
    }
    return step
}

fun readOctopuses(fileName: String): List<List<Octopus>> {
    return readOctopusesFromString(File(fileName)
        .readText())
}

fun readOctopusesFromString(input: String): List<List<Octopus>> {
    return input
        .lines()
        .map { line ->
            line.toCharArray().map { value -> Octopus(value.digitToInt()) }
        }
}

fun part1() {
    val octopuses = readOctopuses("day11.txt")

    val flashes = octopuses.simulate(100)

    println("Flashes = $flashes")
}

fun part2() {
    val octopuses = readOctopuses("day11.txt")

    val steps = octopuses.simulateUntilAllFlashed()

    println("Steps = $steps")
}

fun main() {
    part2()
}

Compiled from "Day11.kt"
public final class day11.Day11Kt {
  public static final java.lang.String dump(java.util.List<? extends java.util.List<day11.Octopus>>);
    Code:
       0: aload_0
       1: ldc           #10                 // String <this>
       3: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: astore_1
       8: new           #18                 // class java/lang/StringBuilder
      11: dup
      12: invokespecial #22                 // Method java/lang/StringBuilder."<init>":()V
      15: astore_2
      16: aload_2
      17: astore_3
      18: iconst_0
      19: istore        4
      21: aload_1
      22: checkcast     #24                 // class java/lang/Iterable
      25: astore        5
      27: iconst_0
      28: istore        6
      30: aload         5
      32: invokeinterface #28,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      37: astore        7
      39: aload         7
      41: invokeinterface #34,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      46: ifeq          144
      49: aload         7
      51: invokeinterface #38,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      56: astore        8
      58: aload         8
      60: checkcast     #40                 // class java/util/List
      63: astore        9
      65: iconst_0
      66: istore        10
      68: aload         9
      70: checkcast     #24                 // class java/lang/Iterable
      73: astore        11
      75: iconst_0
      76: istore        12
      78: aload         11
      80: invokeinterface #28,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      85: astore        13
      87: aload         13
      89: invokeinterface #34,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      94: ifeq          131
      97: aload         13
      99: invokeinterface #38,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     104: astore        14
     106: aload         14
     108: checkcast     #42                 // class day11/Octopus
     111: astore        15
     113: iconst_0
     114: istore        16
     116: aload_3
     117: aload         15
     119: invokevirtual #46                 // Method day11/Octopus.getEnergy:()I
     122: invokevirtual #50                 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
     125: pop
     126: nop
     127: nop
     128: goto          87
     131: nop
     132: aload_3
     133: bipush        10
     135: invokevirtual #53                 // Method java/lang/StringBuilder.append:(C)Ljava/lang/StringBuilder;
     138: pop
     139: nop
     140: nop
     141: goto          39
     144: nop
     145: nop
     146: aload_2
     147: invokevirtual #57                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
     150: checkcast     #59                 // class java/lang/CharSequence
     153: invokestatic  #65                 // Method kotlin/text/StringsKt.trim:(Ljava/lang/CharSequence;)Ljava/lang/CharSequence;
     156: invokevirtual #66                 // Method java/lang/Object.toString:()Ljava/lang/String;
     159: areturn

  public static final void increase(java.util.List<? extends java.util.List<day11.Octopus>>);
    Code:
       0: aload_0
       1: ldc           #10                 // String <this>
       3: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: checkcast     #24                 // class java/lang/Iterable
      10: astore_1
      11: iconst_0
      12: istore_2
      13: aload_1
      14: invokeinterface #28,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      19: astore_3
      20: aload_3
      21: invokeinterface #34,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      26: ifeq          122
      29: aload_3
      30: invokeinterface #38,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      35: astore        4
      37: aload         4
      39: checkcast     #40                 // class java/util/List
      42: astore        5
      44: iconst_0
      45: istore        6
      47: aload         5
      49: checkcast     #24                 // class java/lang/Iterable
      52: astore        7
      54: iconst_0
      55: istore        8
      57: aload         7
      59: invokeinterface #28,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      64: astore        9
      66: aload         9
      68: invokeinterface #34,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      73: ifeq          116
      76: aload         9
      78: invokeinterface #38,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      83: astore        10
      85: aload         10
      87: checkcast     #42                 // class day11/Octopus
      90: astore        11
      92: iconst_0
      93: istore        12
      95: aload         11
      97: invokevirtual #46                 // Method day11/Octopus.getEnergy:()I
     100: istore        13
     102: aload         11
     104: iload         13
     106: iconst_1
     107: iadd
     108: invokevirtual #90                 // Method day11/Octopus.setEnergy:(I)V
     111: nop
     112: nop
     113: goto          66
     116: nop
     117: nop
     118: nop
     119: goto          20
     122: nop
     123: return

  public static final void increase(java.util.List<? extends java.util.List<day11.Octopus>>, java.util.List<day11.Position>);
    Code:
       0: aload_0
       1: ldc           #10                 // String <this>
       3: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_1
       7: ldc           #97                 // String neighbors
       9: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
      12: aload_1
      13: checkcast     #24                 // class java/lang/Iterable
      16: astore_2
      17: iconst_0
      18: istore_3
      19: aload_2
      20: invokeinterface #28,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      25: astore        4
      27: aload         4
      29: invokeinterface #34,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      34: ifeq          106
      37: aload         4
      39: invokeinterface #38,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      44: astore        5
      46: aload         5
      48: checkcast     #99                 // class day11/Position
      51: astore        6
      53: iconst_0
      54: istore        7
      56: aload_0
      57: aload         6
      59: invokevirtual #102                // Method day11/Position.getY:()I
      62: invokeinterface #106,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      67: checkcast     #40                 // class java/util/List
      70: aload         6
      72: invokevirtual #109                // Method day11/Position.getX:()I
      75: invokeinterface #106,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      80: checkcast     #42                 // class day11/Octopus
      83: astore        8
      85: aload         8
      87: invokevirtual #46                 // Method day11/Octopus.getEnergy:()I
      90: istore        9
      92: aload         8
      94: iload         9
      96: iconst_1
      97: iadd
      98: invokevirtual #90                 // Method day11/Octopus.setEnergy:(I)V
     101: nop
     102: nop
     103: goto          27
     106: nop
     107: return

  public static final java.util.List<day11.Position> neighborsOf(java.util.List<? extends java.util.List<day11.Octopus>>, int, int);
    Code:
       0: aload_0
       1: ldc           #10                 // String <this>
       3: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: bipush        8
       8: anewarray     #99                 // class day11/Position
      11: astore        4
      13: aload         4
      15: iconst_0
      16: new           #99                 // class day11/Position
      19: dup
      20: iload_1
      21: iconst_1
      22: isub
      23: iload_2
      24: iconst_1
      25: isub
      26: invokespecial #118                // Method day11/Position."<init>":(II)V
      29: aastore
      30: aload         4
      32: iconst_1
      33: new           #99                 // class day11/Position
      36: dup
      37: iload_1
      38: iload_2
      39: iconst_1
      40: isub
      41: invokespecial #118                // Method day11/Position."<init>":(II)V
      44: aastore
      45: aload         4
      47: iconst_2
      48: new           #99                 // class day11/Position
      51: dup
      52: iload_1
      53: iconst_1
      54: iadd
      55: iload_2
      56: iconst_1
      57: isub
      58: invokespecial #118                // Method day11/Position."<init>":(II)V
      61: aastore
      62: aload         4
      64: iconst_3
      65: new           #99                 // class day11/Position
      68: dup
      69: iload_1
      70: iconst_1
      71: isub
      72: iload_2
      73: invokespecial #118                // Method day11/Position."<init>":(II)V
      76: aastore
      77: aload         4
      79: iconst_4
      80: new           #99                 // class day11/Position
      83: dup
      84: iload_1
      85: iconst_1
      86: iadd
      87: iload_2
      88: invokespecial #118                // Method day11/Position."<init>":(II)V
      91: aastore
      92: aload         4
      94: iconst_5
      95: new           #99                 // class day11/Position
      98: dup
      99: iload_1
     100: iconst_1
     101: isub
     102: iload_2
     103: iconst_1
     104: iadd
     105: invokespecial #118                // Method day11/Position."<init>":(II)V
     108: aastore
     109: aload         4
     111: bipush        6
     113: new           #99                 // class day11/Position
     116: dup
     117: iload_1
     118: iload_2
     119: iconst_1
     120: iadd
     121: invokespecial #118                // Method day11/Position."<init>":(II)V
     124: aastore
     125: aload         4
     127: bipush        7
     129: new           #99                 // class day11/Position
     132: dup
     133: iload_1
     134: iconst_1
     135: iadd
     136: iload_2
     137: iconst_1
     138: iadd
     139: invokespecial #118                // Method day11/Position."<init>":(II)V
     142: aastore
     143: aload         4
     145: invokestatic  #124                // Method kotlin/collections/CollectionsKt.listOf:([Ljava/lang/Object;)Ljava/util/List;
     148: astore_3
     149: aload_3
     150: checkcast     #24                 // class java/lang/Iterable
     153: astore        4
     155: iconst_0
     156: istore        5
     158: aload         4
     160: astore        6
     162: new           #126                // class java/util/ArrayList
     165: dup
     166: invokespecial #127                // Method java/util/ArrayList."<init>":()V
     169: checkcast     #129                // class java/util/Collection
     172: astore        7
     174: iconst_0
     175: istore        8
     177: aload         6
     179: invokeinterface #28,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     184: astore        9
     186: aload         9
     188: invokeinterface #34,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     193: ifeq          328
     196: aload         9
     198: invokeinterface #38,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     203: astore        10
     205: aload         10
     207: checkcast     #99                 // class day11/Position
     210: astore        11
     212: iconst_0
     213: istore        12
     215: aload_0
     216: checkcast     #129                // class java/util/Collection
     219: invokeinterface #132,  1          // InterfaceMethod java/util/Collection.size:()I
     224: istore        13
     226: aload         11
     228: invokevirtual #102                // Method day11/Position.getY:()I
     231: istore        14
     233: iconst_0
     234: iload         14
     236: if_icmpgt     254
     239: iload         14
     241: iload         13
     243: if_icmpge     250
     246: iconst_1
     247: goto          255
     250: iconst_0
     251: goto          255
     254: iconst_0
     255: ifeq          311
     258: aload_0
     259: iconst_0
     260: invokeinterface #106,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     265: checkcast     #129                // class java/util/Collection
     268: invokeinterface #132,  1          // InterfaceMethod java/util/Collection.size:()I
     273: istore        13
     275: aload         11
     277: invokevirtual #109                // Method day11/Position.getX:()I
     280: istore        14
     282: iconst_0
     283: iload         14
     285: if_icmpgt     303
     288: iload         14
     290: iload         13
     292: if_icmpge     299
     295: iconst_1
     296: goto          304
     299: iconst_0
     300: goto          304
     303: iconst_0
     304: ifeq          311
     307: iconst_1
     308: goto          312
     311: iconst_0
     312: ifeq          186
     315: aload         7
     317: aload         10
     319: invokeinterface #136,  2          // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     324: pop
     325: goto          186
     328: aload         7
     330: checkcast     #40                 // class java/util/List
     333: nop
     334: areturn

  public static final kotlin.Pair<java.lang.Integer, java.util.List<day11.Position>> flash(java.util.List<? extends java.util.List<day11.Octopus>>);
    Code:
       0: aload_0
       1: ldc           #10                 // String <this>
       3: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: new           #126                // class java/util/ArrayList
       9: dup
      10: invokespecial #127                // Method java/util/ArrayList."<init>":()V
      13: checkcast     #40                 // class java/util/List
      16: astore_1
      17: iconst_0
      18: istore_2
      19: aload_0
      20: checkcast     #129                // class java/util/Collection
      23: invokestatic  #156                // Method kotlin/collections/CollectionsKt.getIndices:(Ljava/util/Collection;)Lkotlin/ranges/IntRange;
      26: checkcast     #24                 // class java/lang/Iterable
      29: astore_3
      30: iconst_0
      31: istore        4
      33: aload_3
      34: invokeinterface #28,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      39: astore        5
      41: aload         5
      43: invokeinterface #34,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      48: ifeq          195
      51: aload         5
      53: checkcast     #158                // class kotlin/collections/IntIterator
      56: invokevirtual #161                // Method kotlin/collections/IntIterator.nextInt:()I
      59: istore        6
      61: iload         6
      63: istore        7
      65: iconst_0
      66: istore        8
      68: aload_0
      69: iload         7
      71: invokeinterface #106,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      76: checkcast     #129                // class java/util/Collection
      79: invokestatic  #156                // Method kotlin/collections/CollectionsKt.getIndices:(Ljava/util/Collection;)Lkotlin/ranges/IntRange;
      82: checkcast     #24                 // class java/lang/Iterable
      85: astore        9
      87: iconst_0
      88: istore        10
      90: aload         9
      92: invokeinterface #28,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      97: astore        11
      99: aload         11
     101: invokeinterface #34,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     106: ifeq          189
     109: aload         11
     111: checkcast     #158                // class kotlin/collections/IntIterator
     114: invokevirtual #161                // Method kotlin/collections/IntIterator.nextInt:()I
     117: istore        12
     119: iload         12
     121: istore        13
     123: iconst_0
     124: istore        14
     126: aload_0
     127: iload         7
     129: invokeinterface #106,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     134: checkcast     #40                 // class java/util/List
     137: iload         13
     139: invokeinterface #106,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     144: checkcast     #42                 // class day11/Octopus
     147: astore        15
     149: aload         15
     151: invokevirtual #164                // Method day11/Octopus.canFlash:()Z
     154: ifeq          184
     157: aload         15
     159: invokevirtual #166                // Method day11/Octopus.flash:()V
     162: iload_2
     163: iconst_1
     164: iadd
     165: istore_2
     166: aload_1
     167: aload_0
     168: iload         13
     170: iload         7
     172: invokestatic  #168                // Method neighborsOf:(Ljava/util/List;II)Ljava/util/List;
     175: checkcast     #129                // class java/util/Collection
     178: invokeinterface #172,  2          // InterfaceMethod java/util/List.addAll:(Ljava/util/Collection;)Z
     183: pop
     184: nop
     185: nop
     186: goto          99
     189: nop
     190: nop
     191: nop
     192: goto          41
     195: nop
     196: new           #174                // class kotlin/Pair
     199: dup
     200: iload_2
     201: invokestatic  #180                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     204: aload_1
     205: invokespecial #183                // Method kotlin/Pair."<init>":(Ljava/lang/Object;Ljava/lang/Object;)V
     208: areturn

  public static final int simulate(java.util.List<? extends java.util.List<day11.Octopus>>);
    Code:
       0: aload_0
       1: ldc           #10                 // String <this>
       3: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: invokestatic  #192                // Method increase:(Ljava/util/List;)V
      10: aload_0
      11: invokestatic  #194                // Method flash:(Ljava/util/List;)Lkotlin/Pair;
      14: astore_1
      15: aload_1
      16: invokevirtual #197                // Method kotlin/Pair.component1:()Ljava/lang/Object;
      19: checkcast     #199                // class java/lang/Number
      22: invokevirtual #202                // Method java/lang/Number.intValue:()I
      25: istore_2
      26: aload_1
      27: invokevirtual #205                // Method kotlin/Pair.component2:()Ljava/lang/Object;
      30: checkcast     #40                 // class java/util/List
      33: astore_3
      34: aload_3
      35: checkcast     #129                // class java/util/Collection
      38: invokeinterface #208,  1          // InterfaceMethod java/util/Collection.isEmpty:()Z
      43: ifne          50
      46: iconst_1
      47: goto          51
      50: iconst_0
      51: ifeq          99
      54: aload_0
      55: aload_3
      56: invokestatic  #210                // Method increase:(Ljava/util/List;Ljava/util/List;)V
      59: aload_0
      60: invokestatic  #194                // Method flash:(Ljava/util/List;)Lkotlin/Pair;
      63: astore        4
      65: aload         4
      67: invokevirtual #197                // Method kotlin/Pair.component1:()Ljava/lang/Object;
      70: checkcast     #199                // class java/lang/Number
      73: invokevirtual #202                // Method java/lang/Number.intValue:()I
      76: istore        5
      78: aload         4
      80: invokevirtual #205                // Method kotlin/Pair.component2:()Ljava/lang/Object;
      83: checkcast     #40                 // class java/util/List
      86: astore        6
      88: aload         6
      90: astore_3
      91: iload_2
      92: iload         5
      94: iadd
      95: istore_2
      96: goto          34
      99: aload_0
     100: invokestatic  #213                // Method reset:(Ljava/util/List;)V
     103: iload_2
     104: ireturn

  public static final int simulate(java.util.List<? extends java.util.List<day11.Octopus>>, int);
    Code:
       0: aload_0
       1: ldc           #10                 // String <this>
       3: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: iconst_0
       7: istore_2
       8: iconst_0
       9: istore_3
      10: iload_3
      11: iload_1
      12: if_icmpge     35
      15: iload_3
      16: istore        4
      18: iconst_0
      19: istore        5
      21: iload_2
      22: aload_0
      23: invokestatic  #220                // Method simulate:(Ljava/util/List;)I
      26: iadd
      27: istore_2
      28: nop
      29: iinc          3, 1
      32: goto          10
      35: iload_2
      36: ireturn

  public static final void reset(java.util.List<? extends java.util.List<day11.Octopus>>);
    Code:
       0: aload_0
       1: ldc           #10                 // String <this>
       3: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: checkcast     #24                 // class java/lang/Iterable
      10: astore_1
      11: iconst_0
      12: istore_2
      13: aload_1
      14: invokeinterface #28,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      19: astore_3
      20: aload_3
      21: invokeinterface #34,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      26: ifeq          111
      29: aload_3
      30: invokeinterface #38,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      35: astore        4
      37: aload         4
      39: checkcast     #40                 // class java/util/List
      42: astore        5
      44: iconst_0
      45: istore        6
      47: aload         5
      49: checkcast     #24                 // class java/lang/Iterable
      52: astore        7
      54: iconst_0
      55: istore        8
      57: aload         7
      59: invokeinterface #28,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      64: astore        9
      66: aload         9
      68: invokeinterface #34,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      73: ifeq          105
      76: aload         9
      78: invokeinterface #38,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      83: astore        10
      85: aload         10
      87: checkcast     #42                 // class day11/Octopus
      90: astore        11
      92: iconst_0
      93: istore        12
      95: aload         11
      97: invokevirtual #225                // Method day11/Octopus.reset:()V
     100: nop
     101: nop
     102: goto          66
     105: nop
     106: nop
     107: nop
     108: goto          20
     111: nop
     112: return

  public static final boolean allFlashed(java.util.List<? extends java.util.List<day11.Octopus>>);
    Code:
       0: aload_0
       1: ldc           #10                 // String <this>
       3: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: checkcast     #24                 // class java/lang/Iterable
      10: astore_1
      11: iconst_0
      12: istore_2
      13: aload_1
      14: instanceof    #129                // class java/util/Collection
      17: ifeq          36
      20: aload_1
      21: checkcast     #129                // class java/util/Collection
      24: invokeinterface #208,  1          // InterfaceMethod java/util/Collection.isEmpty:()Z
      29: ifeq          36
      32: iconst_1
      33: goto          173
      36: aload_1
      37: invokeinterface #28,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      42: astore_3
      43: aload_3
      44: invokeinterface #34,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      49: ifeq          172
      52: aload_3
      53: invokeinterface #38,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      58: astore        4
      60: aload         4
      62: checkcast     #40                 // class java/util/List
      65: astore        5
      67: iconst_0
      68: istore        6
      70: aload         5
      72: checkcast     #24                 // class java/lang/Iterable
      75: astore        7
      77: iconst_0
      78: istore        8
      80: aload         7
      82: instanceof    #129                // class java/util/Collection
      85: ifeq          105
      88: aload         7
      90: checkcast     #129                // class java/util/Collection
      93: invokeinterface #208,  1          // InterfaceMethod java/util/Collection.isEmpty:()Z
      98: ifeq          105
     101: iconst_1
     102: goto          164
     105: aload         7
     107: invokeinterface #28,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     112: astore        9
     114: aload         9
     116: invokeinterface #34,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     121: ifeq          163
     124: aload         9
     126: invokeinterface #38,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     131: astore        10
     133: aload         10
     135: checkcast     #42                 // class day11/Octopus
     138: astore        11
     140: iconst_0
     141: istore        12
     143: aload         11
     145: invokevirtual #46                 // Method day11/Octopus.getEnergy:()I
     148: ifne          155
     151: iconst_1
     152: goto          156
     155: iconst_0
     156: ifne          114
     159: iconst_0
     160: goto          164
     163: iconst_1
     164: nop
     165: ifne          43
     168: iconst_0
     169: goto          173
     172: iconst_1
     173: ireturn

  public static final int simulateUntilAllFlashed(java.util.List<? extends java.util.List<day11.Octopus>>);
    Code:
       0: aload_0
       1: ldc           #10                 // String <this>
       3: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: iconst_0
       7: istore_1
       8: aload_0
       9: invokestatic  #239                // Method allFlashed:(Ljava/util/List;)Z
      12: ifne          26
      15: aload_0
      16: invokestatic  #220                // Method simulate:(Ljava/util/List;)I
      19: pop
      20: iinc          1, 1
      23: goto          8
      26: iload_1
      27: ireturn

  public static final java.util.List<java.util.List<day11.Octopus>> readOctopuses(java.lang.String);
    Code:
       0: aload_0
       1: ldc           #246                // String fileName
       3: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: new           #248                // class java/io/File
       9: dup
      10: aload_0
      11: invokespecial #251                // Method java/io/File."<init>":(Ljava/lang/String;)V
      14: aconst_null
      15: iconst_1
      16: aconst_null
      17: invokestatic  #257                // Method kotlin/io/FilesKt.readText$default:(Ljava/io/File;Ljava/nio/charset/Charset;ILjava/lang/Object;)Ljava/lang/String;
      20: invokestatic  #260                // Method readOctopusesFromString:(Ljava/lang/String;)Ljava/util/List;
      23: areturn

  public static final java.util.List<java.util.List<day11.Octopus>> readOctopusesFromString(java.lang.String);
    Code:
       0: aload_0
       1: ldc_w         #263                // String input
       4: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       7: aload_0
       8: checkcast     #59                 // class java/lang/CharSequence
      11: invokestatic  #267                // Method kotlin/text/StringsKt.lines:(Ljava/lang/CharSequence;)Ljava/util/List;
      14: checkcast     #24                 // class java/lang/Iterable
      17: astore_1
      18: nop
      19: iconst_0
      20: istore_2
      21: aload_1
      22: astore_3
      23: new           #126                // class java/util/ArrayList
      26: dup
      27: aload_1
      28: bipush        10
      30: invokestatic  #271                // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
      33: invokespecial #273                // Method java/util/ArrayList."<init>":(I)V
      36: checkcast     #129                // class java/util/Collection
      39: astore        4
      41: iconst_0
      42: istore        5
      44: aload_3
      45: invokeinterface #28,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      50: astore        6
      52: aload         6
      54: invokeinterface #34,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      59: ifeq          207
      62: aload         6
      64: invokeinterface #38,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      69: astore        7
      71: aload         4
      73: aload         7
      75: checkcast     #275                // class java/lang/String
      78: astore        8
      80: astore        21
      82: iconst_0
      83: istore        9
      85: aload         8
      87: invokevirtual #279                // Method java/lang/String.toCharArray:()[C
      90: dup
      91: ldc_w         #281                // String toCharArray(...)
      94: invokestatic  #284                // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
      97: astore        10
      99: nop
     100: iconst_0
     101: istore        11
     103: aload         10
     105: astore        12
     107: new           #126                // class java/util/ArrayList
     110: dup
     111: aload         10
     113: arraylength
     114: invokespecial #273                // Method java/util/ArrayList."<init>":(I)V
     117: checkcast     #129                // class java/util/Collection
     120: astore        13
     122: iconst_0
     123: istore        14
     125: iconst_0
     126: istore        15
     128: aload         12
     130: arraylength
     131: istore        16
     133: iload         15
     135: iload         16
     137: if_icmpge     188
     140: aload         12
     142: iload         15
     144: caload
     145: istore        17
     147: aload         13
     149: iload         17
     151: istore        18
     153: astore        19
     155: iconst_0
     156: istore        20
     158: new           #42                 // class day11/Octopus
     161: dup
     162: iload         18
     164: invokestatic  #290                // Method kotlin/text/CharsKt.digitToInt:(C)I
     167: iconst_0
     168: iconst_2
     169: aconst_null
     170: invokespecial #293                // Method day11/Octopus."<init>":(IZILkotlin/jvm/internal/DefaultConstructorMarker;)V
     173: aload         19
     175: swap
     176: invokeinterface #136,  2          // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     181: pop
     182: iinc          15, 1
     185: goto          133
     188: aload         13
     190: checkcast     #40                 // class java/util/List
     193: nop
     194: nop
     195: aload         21
     197: swap
     198: invokeinterface #136,  2          // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     203: pop
     204: goto          52
     207: aload         4
     209: checkcast     #40                 // class java/util/List
     212: nop
     213: areturn

  public static final void part1();
    Code:
       0: ldc_w         #308                // String day11.txt
       3: invokestatic  #310                // Method readOctopuses:(Ljava/lang/String;)Ljava/util/List;
       6: astore_0
       7: aload_0
       8: bipush        100
      10: invokestatic  #312                // Method simulate:(Ljava/util/List;I)I
      13: istore_1
      14: new           #18                 // class java/lang/StringBuilder
      17: dup
      18: invokespecial #22                 // Method java/lang/StringBuilder."<init>":()V
      21: ldc_w         #314                // String Flashes =
      24: invokevirtual #317                // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      27: iload_1
      28: invokevirtual #50                 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
      31: invokevirtual #57                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
      34: getstatic     #323                // Field java/lang/System.out:Ljava/io/PrintStream;
      37: swap
      38: invokevirtual #329                // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
      41: return

  public static final void part2();
    Code:
       0: ldc_w         #308                // String day11.txt
       3: invokestatic  #310                // Method readOctopuses:(Ljava/lang/String;)Ljava/util/List;
       6: astore_0
       7: aload_0
       8: invokestatic  #332                // Method simulateUntilAllFlashed:(Ljava/util/List;)I
      11: istore_1
      12: new           #18                 // class java/lang/StringBuilder
      15: dup
      16: invokespecial #22                 // Method java/lang/StringBuilder."<init>":()V
      19: ldc_w         #334                // String Steps =
      22: invokevirtual #317                // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      25: iload_1
      26: invokevirtual #50                 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
      29: invokevirtual #57                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
      32: getstatic     #323                // Field java/lang/System.out:Ljava/io/PrintStream;
      35: swap
      36: invokevirtual #329                // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
      39: return

  public static final void main();
    Code:
       0: invokestatic  #338                // Method part2:()V
       3: return

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


package day17

import kotlin.math.max

data class TargetArea(
    val xRange: IntRange,
    val yRange: IntRange
) {
    fun calculateResult(x: Int, y: Int): AreaResult {
        return when {
            y < yRange.minOrNull()!! -> AreaResult.MissBottom
            x > xRange.maxOrNull()!! -> AreaResult.MissRight
            xRange.contains(x) && yRange.contains(y) -> AreaResult.Hit
            else -> AreaResult.Ongoing
        }
    }
}

data class Velocity(
    val x: Int,
    val y: Int
) {
    fun simulate(): Velocity {
        val newX = if (x > 0) {
            x - 1
        } else if (x < 0) {
            x + 1
        } else {
            0
        }

        val newY = y - 1

        return Velocity(newX, newY)
    }
}

sealed interface AreaResult {
    object Hit : AreaResult
    object Ongoing : AreaResult
    object MissBottom : AreaResult
    object MissRight : AreaResult
}

sealed interface Result {
    data class Hit(
        val maxHeight: Int
    ) : Result

    object MissBottom : Result
    object MissRight : Result
}

fun AreaResult.toResult(
    maxHeight: Int
): Result {
    return when (this) {
        is AreaResult.Ongoing -> throw IllegalStateException("Translating ongoing area result")
        is AreaResult.Hit -> Result.Hit(maxHeight)
        is AreaResult.MissBottom -> Result.MissBottom
        is AreaResult.MissRight -> Result.MissRight
    }
}

fun launchProbe(initial: Velocity, target: TargetArea): Result {
    var x = initial.x
    var y = initial.y
    var velocity = initial
    var maxHeight = initial.y
    var areaResult: AreaResult = target.calculateResult(x, y)

    while (areaResult is AreaResult.Ongoing) {
        velocity = velocity.simulate()
        x += velocity.x
        y += velocity.y
        areaResult = target.calculateResult(x, y)
        maxHeight = max(maxHeight, y)
    }

    return areaResult.toResult(maxHeight)
}

data class VelocityResults(
    val maxHeight: Int,
    val count: Int
)

fun findAllProbeVelocities(
    target: TargetArea,
    maxPossibleHeight: Int = 20
): VelocityResults {
    val xRange = 1..target.xRange.last // Too large (on the left side)
    val yRange = target.yRange.first .. maxPossibleHeight
    var count = 0
    var maxHeight = Int.MIN_VALUE

    for (x in xRange) {
        for (y in yRange) {
            val result = launchProbe(
                Velocity(x, y),
                target
            )

            if (result is Result.Hit) {
                maxHeight = max(maxHeight, result.maxHeight)
                count++
            }
        }
    }

    if (maxHeight == Int.MIN_VALUE) {
        throw IllegalStateException("Could not find max height")
    }

    return VelocityResults(maxHeight, count)
}

fun part1() {
    val target = TargetArea(
        xRange = 153..199,
        yRange = -114..-75
    )

    val result = findAllProbeVelocities(
        target,
        maxPossibleHeight = 1500
    )

    println("Result: $result")
}

fun part2() {
    val target = TargetArea(
        xRange = 153..199,
        yRange = -114..-75
    )

    val result = findAllProbeVelocities(
        target,
        maxPossibleHeight = 1000
    )

    println("Result: $result")
}

fun main() {
    part2()
}

Compiled from "Day17.kt"
public final class day17.Day17Kt {
  public static final day17.Result toResult(day17.AreaResult, 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_0
       7: astore_2
       8: aload_2
       9: instanceof    #17                 // class day17/AreaResult$Ongoing
      12: ifeq          25
      15: new           #19                 // class java/lang/IllegalStateException
      18: dup
      19: ldc           #21                 // String Translating ongoing area result
      21: invokespecial #25                 // Method java/lang/IllegalStateException."<init>":(Ljava/lang/String;)V
      24: athrow
      25: aload_2
      26: instanceof    #27                 // class day17/AreaResult$Hit
      29: ifeq          46
      32: new           #29                 // class day17/Result$Hit
      35: dup
      36: iload_1
      37: invokespecial #32                 // Method day17/Result$Hit."<init>":(I)V
      40: checkcast     #34                 // class day17/Result
      43: goto          86
      46: aload_2
      47: instanceof    #36                 // class day17/AreaResult$MissBottom
      50: ifeq          62
      53: getstatic     #42                 // Field day17/Result$MissBottom.INSTANCE:Lday17/Result$MissBottom;
      56: checkcast     #34                 // class day17/Result
      59: goto          86
      62: aload_2
      63: instanceof    #44                 // class day17/AreaResult$MissRight
      66: ifeq          78
      69: getstatic     #49                 // Field day17/Result$MissRight.INSTANCE:Lday17/Result$MissRight;
      72: checkcast     #34                 // class day17/Result
      75: goto          86
      78: new           #51                 // class kotlin/NoWhenBranchMatchedException
      81: dup
      82: invokespecial #54                 // Method kotlin/NoWhenBranchMatchedException."<init>":()V
      85: athrow
      86: areturn

  public static final day17.Result launchProbe(day17.Velocity, day17.TargetArea);
    Code:
       0: aload_0
       1: ldc           #64                 // String initial
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_1
       7: ldc           #66                 // String target
       9: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
      12: aload_0
      13: invokevirtual #72                 // Method day17/Velocity.getX:()I
      16: istore_2
      17: aload_0
      18: invokevirtual #75                 // Method day17/Velocity.getY:()I
      21: istore_3
      22: aload_0
      23: astore        4
      25: aload_0
      26: invokevirtual #75                 // Method day17/Velocity.getY:()I
      29: istore        5
      31: aload_1
      32: iload_2
      33: iload_3
      34: invokevirtual #81                 // Method day17/TargetArea.calculateResult:(II)Lday17/AreaResult;
      37: astore        6
      39: aload         6
      41: instanceof    #17                 // class day17/AreaResult$Ongoing
      44: ifeq          89
      47: aload         4
      49: invokevirtual #85                 // Method day17/Velocity.simulate:()Lday17/Velocity;
      52: astore        4
      54: iload_2
      55: aload         4
      57: invokevirtual #72                 // Method day17/Velocity.getX:()I
      60: iadd
      61: istore_2
      62: iload_3
      63: aload         4
      65: invokevirtual #75                 // Method day17/Velocity.getY:()I
      68: iadd
      69: istore_3
      70: aload_1
      71: iload_2
      72: iload_3
      73: invokevirtual #81                 // Method day17/TargetArea.calculateResult:(II)Lday17/AreaResult;
      76: astore        6
      78: iload         5
      80: iload_3
      81: invokestatic  #91                 // Method java/lang/Math.max:(II)I
      84: istore        5
      86: goto          39
      89: aload         6
      91: iload         5
      93: invokestatic  #93                 // Method toResult:(Lday17/AreaResult;I)Lday17/Result;
      96: areturn

  public static final day17.VelocityResults findAllProbeVelocities(day17.TargetArea, int);
    Code:
       0: aload_0
       1: ldc           #66                 // String target
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: new           #103                // class kotlin/ranges/IntRange
       9: dup
      10: iconst_1
      11: aload_0
      12: invokevirtual #107                // Method day17/TargetArea.getXRange:()Lkotlin/ranges/IntRange;
      15: invokevirtual #110                // Method kotlin/ranges/IntRange.getLast:()I
      18: invokespecial #113                // Method kotlin/ranges/IntRange."<init>":(II)V
      21: astore_2
      22: new           #103                // class kotlin/ranges/IntRange
      25: dup
      26: aload_0
      27: invokevirtual #116                // Method day17/TargetArea.getYRange:()Lkotlin/ranges/IntRange;
      30: invokevirtual #119                // Method kotlin/ranges/IntRange.getFirst:()I
      33: iload_1
      34: invokespecial #113                // Method kotlin/ranges/IntRange."<init>":(II)V
      37: astore_3
      38: iconst_0
      39: istore        4
      41: ldc           #120                // int -2147483648
      43: istore        5
      45: aload_2
      46: invokevirtual #119                // Method kotlin/ranges/IntRange.getFirst:()I
      49: istore        6
      51: aload_2
      52: invokevirtual #110                // Method kotlin/ranges/IntRange.getLast:()I
      55: istore        7
      57: iload         6
      59: iload         7
      61: if_icmpgt     152
      64: aload_3
      65: invokevirtual #119                // Method kotlin/ranges/IntRange.getFirst:()I
      68: istore        8
      70: aload_3
      71: invokevirtual #110                // Method kotlin/ranges/IntRange.getLast:()I
      74: istore        9
      76: iload         8
      78: iload         9
      80: if_icmpgt     139
      83: new           #68                 // class day17/Velocity
      86: dup
      87: iload         6
      89: iload         8
      91: invokespecial #121                // Method day17/Velocity."<init>":(II)V
      94: aload_0
      95: invokestatic  #123                // Method launchProbe:(Lday17/Velocity;Lday17/TargetArea;)Lday17/Result;
      98: astore        10
     100: aload         10
     102: instanceof    #29                 // class day17/Result$Hit
     105: ifeq          126
     108: iload         5
     110: aload         10
     112: checkcast     #29                 // class day17/Result$Hit
     115: invokevirtual #126                // Method day17/Result$Hit.getMaxHeight:()I
     118: invokestatic  #91                 // Method java/lang/Math.max:(II)I
     121: istore        5
     123: iinc          4, 1
     126: iload         8
     128: iload         9
     130: if_icmpeq     139
     133: iinc          8, 1
     136: goto          83
     139: iload         6
     141: iload         7
     143: if_icmpeq     152
     146: iinc          6, 1
     149: goto          64
     152: iload         5
     154: ldc           #120                // int -2147483648
     156: if_icmpne     169
     159: new           #19                 // class java/lang/IllegalStateException
     162: dup
     163: ldc           #128                // String Could not find max height
     165: invokespecial #25                 // Method java/lang/IllegalStateException."<init>":(Ljava/lang/String;)V
     168: athrow
     169: new           #130                // class day17/VelocityResults
     172: dup
     173: iload         5
     175: iload         4
     177: invokespecial #131                // Method day17/VelocityResults."<init>":(II)V
     180: areturn

  public static day17.VelocityResults findAllProbeVelocities$default(day17.TargetArea, int, int, java.lang.Object);
    Code:
       0: iload_2
       1: iconst_2
       2: iand
       3: ifeq          9
       6: bipush        20
       8: istore_1
       9: aload_0
      10: iload_1
      11: invokestatic  #142                // Method findAllProbeVelocities:(Lday17/TargetArea;I)Lday17/VelocityResults;
      14: areturn

  public static final void part1();
    Code:
       0: new           #77                 // class day17/TargetArea
       3: dup
       4: new           #103                // class kotlin/ranges/IntRange
       7: dup
       8: sipush        153
      11: sipush        199
      14: invokespecial #113                // Method kotlin/ranges/IntRange."<init>":(II)V
      17: new           #103                // class kotlin/ranges/IntRange
      20: dup
      21: bipush        -114
      23: bipush        -75
      25: invokespecial #113                // Method kotlin/ranges/IntRange."<init>":(II)V
      28: invokespecial #146                // Method day17/TargetArea."<init>":(Lkotlin/ranges/IntRange;Lkotlin/ranges/IntRange;)V
      31: astore_0
      32: aload_0
      33: sipush        1500
      36: invokestatic  #142                // Method findAllProbeVelocities:(Lday17/TargetArea;I)Lday17/VelocityResults;
      39: astore_1
      40: new           #148                // class java/lang/StringBuilder
      43: dup
      44: invokespecial #149                // Method java/lang/StringBuilder."<init>":()V
      47: ldc           #151                // String Result:
      49: invokevirtual #155                // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      52: aload_1
      53: invokevirtual #158                // Method java/lang/StringBuilder.append:(Ljava/lang/Object;)Ljava/lang/StringBuilder;
      56: invokevirtual #162                // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
      59: getstatic     #168                // Field java/lang/System.out:Ljava/io/PrintStream;
      62: swap
      63: invokevirtual #174                // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
      66: return

  public static final void part2();
    Code:
       0: new           #77                 // class day17/TargetArea
       3: dup
       4: new           #103                // class kotlin/ranges/IntRange
       7: dup
       8: sipush        153
      11: sipush        199
      14: invokespecial #113                // Method kotlin/ranges/IntRange."<init>":(II)V
      17: new           #103                // class kotlin/ranges/IntRange
      20: dup
      21: bipush        -114
      23: bipush        -75
      25: invokespecial #113                // Method kotlin/ranges/IntRange."<init>":(II)V
      28: invokespecial #146                // Method day17/TargetArea."<init>":(Lkotlin/ranges/IntRange;Lkotlin/ranges/IntRange;)V
      31: astore_0
      32: aload_0
      33: sipush        1000
      36: invokestatic  #142                // Method findAllProbeVelocities:(Lday17/TargetArea;I)Lday17/VelocityResults;
      39: astore_1
      40: new           #148                // class java/lang/StringBuilder
      43: dup
      44: invokespecial #149                // Method java/lang/StringBuilder."<init>":()V
      47: ldc           #151                // String Result:
      49: invokevirtual #155                // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      52: aload_1
      53: invokevirtual #158                // Method java/lang/StringBuilder.append:(Ljava/lang/Object;)Ljava/lang/StringBuilder;
      56: invokevirtual #162                // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
      59: getstatic     #168                // Field java/lang/System.out:Ljava/io/PrintStream;
      62: swap
      63: invokevirtual #174                // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
      66: return

  public static final void main();
    Code:
       0: invokestatic  #179                // Method part2:()V
       3: return

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


package com.achawki.sequencetrainer.math

import kotlin.math.min
import kotlin.random.Random

interface Operator<T> {
    fun apply(t: T): Int
}

enum class BinaryOperator : Operator<Pair<Int, Int>> {
    PLUS {
        override fun apply(t: Pair<Int, Int>): Int {
            return t.first + t.second
        }
    },
    MINUS {
        override fun apply(t: Pair<Int, Int>): Int {
            return t.first - t.second
        }
    },
    TIMES {
        override fun apply(t: Pair<Int, Int>): Int {
            return t.first * t.second
        }
    },
    REMAINDER {
        override fun apply(t: Pair<Int, Int>): Int {
            return t.first % t.second
        }
    }
}

enum class UnaryOperator : Operator<Int> {
    SQUARE {
        override fun apply(t: Int): Int {
            return t * t
        }
    },
    DIGIT_SUM {
        override fun apply(t: Int): Int {
            if (t == 0) return 0
            val sign = if (t < 0) -1 else 1
            var digitSum = 0
            var n = t * sign
            while (n > 0) {
                digitSum += n % 10
                n /= 10
            }
            return digitSum * sign
        }
    }
}

enum class ListOperator : Operator<List<Int>> {
    SUM {
        override fun apply(t: List<Int>): Int {
            return t.sum()
        }
    }
}

class WeightedOperator<T>(val operator: Operator<T>, val weight: Int)

fun getRandomOperatorsFromWeightedOperators(
    availableOperators: List<WeightedOperator<*>>,
    operatorsToSelect: Int
): List<Operator<*>> {
    val operators = mutableListOf<Operator<*>>()
    val overallWeight = availableOperators.sumOf { it.weight }
    val filteredOperators = availableOperators.filter { weightedOperator -> weightedOperator.weight > 0 }
    while (operators.size < min(operatorsToSelect, filteredOperators.size)) {
        var currentWeight = 0
        val targetWeight = Random.nextDouble() * overallWeight
        for (operator in filteredOperators) {
            currentWeight += operator.weight
            if (currentWeight >= targetWeight) {
                if (!operators.contains(operator.operator)) {
                    operators.add(operator.operator)
                }
                break

            }
        }
    }
    return operators
}

Compiled from "Operator.kt"
public final class com.achawki.sequencetrainer.math.OperatorKt {
  public static final java.util.List<com.achawki.sequencetrainer.math.Operator<?>> getRandomOperatorsFromWeightedOperators(java.util.List<? extends com.achawki.sequencetrainer.math.WeightedOperator<?>>, int);
    Code:
       0: aload_0
       1: ldc           #10                 // String availableOperators
       3: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: new           #18                 // class java/util/ArrayList
       9: dup
      10: invokespecial #22                 // Method java/util/ArrayList."<init>":()V
      13: checkcast     #24                 // class java/util/List
      16: astore_2
      17: aload_0
      18: checkcast     #26                 // class java/lang/Iterable
      21: astore        4
      23: iconst_0
      24: istore        5
      26: aload         4
      28: invokeinterface #30,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      33: astore        6
      35: aload         6
      37: invokeinterface #36,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      42: ifeq          85
      45: aload         6
      47: invokeinterface #40,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      52: astore        7
      54: iload         5
      56: aload         7
      58: checkcast     #42                 // class com/achawki/sequencetrainer/math/WeightedOperator
      61: astore        8
      63: istore        14
      65: iconst_0
      66: istore        9
      68: aload         8
      70: invokevirtual #46                 // Method com/achawki/sequencetrainer/math/WeightedOperator.getWeight:()I
      73: istore        15
      75: iload         14
      77: iload         15
      79: iadd
      80: istore        5
      82: goto          35
      85: iload         5
      87: istore_3
      88: aload_0
      89: checkcast     #26                 // class java/lang/Iterable
      92: astore        5
      94: iconst_0
      95: istore        6
      97: aload         5
      99: astore        7
     101: new           #18                 // class java/util/ArrayList
     104: dup
     105: invokespecial #22                 // Method java/util/ArrayList."<init>":()V
     108: checkcast     #48                 // class java/util/Collection
     111: astore        8
     113: iconst_0
     114: istore        9
     116: aload         7
     118: invokeinterface #30,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     123: astore        10
     125: aload         10
     127: invokeinterface #36,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     132: ifeq          183
     135: aload         10
     137: invokeinterface #40,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     142: astore        11
     144: aload         11
     146: checkcast     #42                 // class com/achawki/sequencetrainer/math/WeightedOperator
     149: astore        12
     151: iconst_0
     152: istore        13
     154: aload         12
     156: invokevirtual #46                 // Method com/achawki/sequencetrainer/math/WeightedOperator.getWeight:()I
     159: ifle          166
     162: iconst_1
     163: goto          167
     166: iconst_0
     167: ifeq          125
     170: aload         8
     172: aload         11
     174: invokeinterface #52,  2           // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     179: pop
     180: goto          125
     183: aload         8
     185: checkcast     #24                 // class java/util/List
     188: nop
     189: astore        4
     191: aload_2
     192: invokeinterface #55,  1           // InterfaceMethod java/util/List.size:()I
     197: iload_1
     198: aload         4
     200: invokeinterface #55,  1           // InterfaceMethod java/util/List.size:()I
     205: invokestatic  #61                 // Method java/lang/Math.min:(II)I
     208: if_icmpge     304
     211: iconst_0
     212: istore        5
     214: getstatic     #67                 // Field kotlin/random/Random.Default:Lkotlin/random/Random$Default;
     217: invokevirtual #73                 // Method kotlin/random/Random$Default.nextDouble:()D
     220: iload_3
     221: i2d
     222: dmul
     223: dstore        6
     225: aload         4
     227: invokeinterface #74,  1           // InterfaceMethod java/util/List.iterator:()Ljava/util/Iterator;
     232: astore        8
     234: aload         8
     236: invokeinterface #36,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     241: ifeq          191
     244: aload         8
     246: invokeinterface #40,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     251: checkcast     #42                 // class com/achawki/sequencetrainer/math/WeightedOperator
     254: astore        9
     256: iload         5
     258: aload         9
     260: invokevirtual #46                 // Method com/achawki/sequencetrainer/math/WeightedOperator.getWeight:()I
     263: iadd
     264: istore        5
     266: iload         5
     268: i2d
     269: dload         6
     271: dcmpl
     272: iflt          234
     275: aload_2
     276: aload         9
     278: invokevirtual #78                 // Method com/achawki/sequencetrainer/math/WeightedOperator.getOperator:()Lcom/achawki/sequencetrainer/math/Operator;
     281: invokeinterface #81,  2           // InterfaceMethod java/util/List.contains:(Ljava/lang/Object;)Z
     286: ifne          301
     289: aload_2
     290: aload         9
     292: invokevirtual #78                 // Method com/achawki/sequencetrainer/math/WeightedOperator.getOperator:()Lcom/achawki/sequencetrainer/math/Operator;
     295: invokeinterface #82,  2           // InterfaceMethod java/util/List.add:(Ljava/lang/Object;)Z
     300: pop
     301: goto          191
     304: aload_2
     305: areturn
}

Compiled from "Operator.kt"
public interface com.achawki.sequencetrainer.math.Operator<T> {
  public abstract int apply(T);
}


package func.prog.java.recursion

fun main(args: Array<String>) {
    println("abc".toList().head())

    println(List(1) { 'a' }.mkString())
    println(('a'..'z').asSequence().toList().mkString())

    println("-- fold left --")
    println("Sum of [1..10] : " + sum2((1..10).toList()))
    println(List(1) { 'a' }.mkString2())
    println(('a'..'z').asSequence().toList().mkString2())

    println("-- that's all folds! --")
}

fun <T> List<T>.head(): T {
    require(isNotEmpty()) { "`head` of an empty list" }
    return first()
}

fun <T> List<T>.tail(): List<T> {
    require(isNotEmpty()) { "`tail` of an empty list" }
    return drop(1)
}

fun sum(xs: List<Int>): Int =
        if (xs.isEmpty()) 0
        else xs.head() + sum(xs.tail())

fun sum2(xs: List<Int>): Int = xs.foldLeft(0) { a, b -> a + b }

fun sumCoRec(xs: List<Int>): Int {
    tailrec fun helper(ns: List<Int>, acc: Int): Int =
            if (ns.isEmpty()) acc
            else helper(ns.tail(), ns.head() + acc)

    return helper(xs, 0)
}

fun <T> List<T>.mkString(
        prefix: String = "[",
        delim: String = ",",
        suffix: String = "]"): String {
    tailrec fun helper(xs: List<T>, acc: String): String =
            when {
                xs.isEmpty() -> acc
                xs.tail().isEmpty() -> acc + xs.head()
                else -> helper(xs.tail(), acc + xs.head() + delim)
            }

    return prefix + helper(this, "") + suffix
}

fun <T> List<T>.mkString2(
        prefix: String = "[",
        delim: String = ",",
        suffix: String = "]"): String {
    val s = foldLeft("") { acc, elem ->
        if (acc.isEmpty()) elem.toString()
        else acc + delim + elem.toString()
    }

    return prefix + s + suffix
}

/**
 * z -> zero value aka seed
 */
fun <T, U> List<T>.foldLeft(z: U, f: (U, T) -> U): U {
    tailrec fun helper(xs: List<T>, acc: U): U = when {
        xs.isEmpty() -> acc
        else -> helper(xs.tail(), f(acc, xs.head()))
    }

    return helper(this, z)
}

fun <T, U> List<T>.foldRight(z: U, f: (T, U) -> U): U =
        reversed().foldLeft(z) { u, t -> f(t, u) }

Compiled from "ListExtn.kt"
public final class func.prog.java.recursion.ListExtnKt {
  public static final void main(java.lang.String[]);
    Code:
       0: aload_0
       1: ldc           #9                  // String args
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: ldc           #17                 // String abc
       8: checkcast     #19                 // class java/lang/CharSequence
      11: invokestatic  #25                 // Method kotlin/text/StringsKt.toList:(Ljava/lang/CharSequence;)Ljava/util/List;
      14: invokestatic  #29                 // Method head:(Ljava/util/List;)Ljava/lang/Object;
      17: checkcast     #31                 // class java/lang/Character
      20: invokevirtual #35                 // Method java/lang/Character.charValue:()C
      23: istore_1
      24: getstatic     #41                 // Field java/lang/System.out:Ljava/io/PrintStream;
      27: iload_1
      28: invokevirtual #47                 // Method java/io/PrintStream.println:(C)V
      31: iconst_1
      32: istore_1
      33: new           #49                 // class java/util/ArrayList
      36: dup
      37: iload_1
      38: invokespecial #53                 // Method java/util/ArrayList."<init>":(I)V
      41: astore_2
      42: iconst_0
      43: istore_3
      44: iload_3
      45: iload_1
      46: if_icmpge     80
      49: iload_3
      50: istore        4
      52: aload_2
      53: iload         4
      55: istore        5
      57: astore        7
      59: iconst_0
      60: istore        6
      62: bipush        97
      64: invokestatic  #57                 // Method java/lang/Character.valueOf:(C)Ljava/lang/Character;
      67: aload         7
      69: swap
      70: invokevirtual #61                 // Method java/util/ArrayList.add:(Ljava/lang/Object;)Z
      73: pop
      74: iinc          3, 1
      77: goto          44
      80: aload_2
      81: checkcast     #63                 // class java/util/List
      84: aconst_null
      85: aconst_null
      86: aconst_null
      87: bipush        7
      89: aconst_null
      90: invokestatic  #67                 // Method mkString$default:(Ljava/util/List;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;ILjava/lang/Object;)Ljava/lang/String;
      93: getstatic     #41                 // Field java/lang/System.out:Ljava/io/PrintStream;
      96: swap
      97: invokevirtual #70                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
     100: new           #72                 // class kotlin/ranges/CharRange
     103: dup
     104: bipush        97
     106: bipush        122
     108: invokespecial #75                 // Method kotlin/ranges/CharRange."<init>":(CC)V
     111: checkcast     #77                 // class java/lang/Iterable
     114: invokestatic  #83                 // Method kotlin/collections/CollectionsKt.asSequence:(Ljava/lang/Iterable;)Lkotlin/sequences/Sequence;
     117: invokestatic  #88                 // Method kotlin/sequences/SequencesKt.toList:(Lkotlin/sequences/Sequence;)Ljava/util/List;
     120: aconst_null
     121: aconst_null
     122: aconst_null
     123: bipush        7
     125: aconst_null
     126: invokestatic  #67                 // Method mkString$default:(Ljava/util/List;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;ILjava/lang/Object;)Ljava/lang/String;
     129: getstatic     #41                 // Field java/lang/System.out:Ljava/io/PrintStream;
     132: swap
     133: invokevirtual #70                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
     136: ldc           #90                 // String -- fold left --
     138: getstatic     #41                 // Field java/lang/System.out:Ljava/io/PrintStream;
     141: swap
     142: invokevirtual #70                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
     145: new           #92                 // class java/lang/StringBuilder
     148: dup
     149: invokespecial #95                 // Method java/lang/StringBuilder."<init>":()V
     152: ldc           #97                 // String Sum of [1..10] :
     154: invokevirtual #101                // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     157: new           #103                // class kotlin/ranges/IntRange
     160: dup
     161: iconst_1
     162: bipush        10
     164: invokespecial #106                // Method kotlin/ranges/IntRange."<init>":(II)V
     167: checkcast     #77                 // class java/lang/Iterable
     170: invokestatic  #109                // Method kotlin/collections/CollectionsKt.toList:(Ljava/lang/Iterable;)Ljava/util/List;
     173: invokestatic  #113                // Method sum2:(Ljava/util/List;)I
     176: invokevirtual #116                // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
     179: invokevirtual #120                // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
     182: getstatic     #41                 // Field java/lang/System.out:Ljava/io/PrintStream;
     185: swap
     186: invokevirtual #70                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
     189: iconst_1
     190: istore_1
     191: new           #49                 // class java/util/ArrayList
     194: dup
     195: iload_1
     196: invokespecial #53                 // Method java/util/ArrayList."<init>":(I)V
     199: astore_2
     200: iconst_0
     201: istore_3
     202: iload_3
     203: iload_1
     204: if_icmpge     238
     207: iload_3
     208: istore        4
     210: aload_2
     211: iload         4
     213: istore        5
     215: astore        7
     217: iconst_0
     218: istore        6
     220: bipush        97
     222: invokestatic  #57                 // Method java/lang/Character.valueOf:(C)Ljava/lang/Character;
     225: aload         7
     227: swap
     228: invokevirtual #61                 // Method java/util/ArrayList.add:(Ljava/lang/Object;)Z
     231: pop
     232: iinc          3, 1
     235: goto          202
     238: aload_2
     239: checkcast     #63                 // class java/util/List
     242: aconst_null
     243: aconst_null
     244: aconst_null
     245: bipush        7
     247: aconst_null
     248: invokestatic  #123                // Method mkString2$default:(Ljava/util/List;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;ILjava/lang/Object;)Ljava/lang/String;
     251: getstatic     #41                 // Field java/lang/System.out:Ljava/io/PrintStream;
     254: swap
     255: invokevirtual #70                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
     258: new           #72                 // class kotlin/ranges/CharRange
     261: dup
     262: bipush        97
     264: bipush        122
     266: invokespecial #75                 // Method kotlin/ranges/CharRange."<init>":(CC)V
     269: checkcast     #77                 // class java/lang/Iterable
     272: invokestatic  #83                 // Method kotlin/collections/CollectionsKt.asSequence:(Ljava/lang/Iterable;)Lkotlin/sequences/Sequence;
     275: invokestatic  #88                 // Method kotlin/sequences/SequencesKt.toList:(Lkotlin/sequences/Sequence;)Ljava/util/List;
     278: aconst_null
     279: aconst_null
     280: aconst_null
     281: bipush        7
     283: aconst_null
     284: invokestatic  #123                // Method mkString2$default:(Ljava/util/List;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;ILjava/lang/Object;)Ljava/lang/String;
     287: getstatic     #41                 // Field java/lang/System.out:Ljava/io/PrintStream;
     290: swap
     291: invokevirtual #70                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
     294: ldc           #125                // String -- that\'s all folds! --
     296: getstatic     #41                 // Field java/lang/System.out:Ljava/io/PrintStream;
     299: swap
     300: invokevirtual #70                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
     303: return

  public static final <T> T head(java.util.List<? extends T>);
    Code:
       0: aload_0
       1: ldc           #133                // String <this>
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: checkcast     #135                // class java/util/Collection
      10: invokeinterface #139,  1          // InterfaceMethod java/util/Collection.isEmpty:()Z
      15: ifne          22
      18: iconst_1
      19: goto          23
      22: iconst_0
      23: ifne          43
      26: iconst_0
      27: istore_1
      28: ldc           #141                // String `head` of an empty list
      30: astore_1
      31: new           #143                // class java/lang/IllegalArgumentException
      34: dup
      35: aload_1
      36: invokevirtual #144                // Method java/lang/Object.toString:()Ljava/lang/String;
      39: invokespecial #147                // Method java/lang/IllegalArgumentException."<init>":(Ljava/lang/String;)V
      42: athrow
      43: aload_0
      44: invokestatic  #150                // Method kotlin/collections/CollectionsKt.first:(Ljava/util/List;)Ljava/lang/Object;
      47: areturn

  public static final <T> java.util.List<T> tail(java.util.List<? extends T>);
    Code:
       0: aload_0
       1: ldc           #133                // String <this>
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: checkcast     #135                // class java/util/Collection
      10: invokeinterface #139,  1          // InterfaceMethod java/util/Collection.isEmpty:()Z
      15: ifne          22
      18: iconst_1
      19: goto          23
      22: iconst_0
      23: ifne          43
      26: iconst_0
      27: istore_1
      28: ldc           #158                // String `tail` of an empty list
      30: astore_1
      31: new           #143                // class java/lang/IllegalArgumentException
      34: dup
      35: aload_1
      36: invokevirtual #144                // Method java/lang/Object.toString:()Ljava/lang/String;
      39: invokespecial #147                // Method java/lang/IllegalArgumentException."<init>":(Ljava/lang/String;)V
      42: athrow
      43: aload_0
      44: checkcast     #77                 // class java/lang/Iterable
      47: iconst_1
      48: invokestatic  #162                // Method kotlin/collections/CollectionsKt.drop:(Ljava/lang/Iterable;I)Ljava/util/List;
      51: areturn

  public static final int sum(java.util.List<java.lang.Integer>);
    Code:
       0: aload_0
       1: ldc           #168                // String xs
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: invokeinterface #169,  1          // InterfaceMethod java/util/List.isEmpty:()Z
      12: ifeq          19
      15: iconst_0
      16: goto          37
      19: aload_0
      20: invokestatic  #29                 // Method head:(Ljava/util/List;)Ljava/lang/Object;
      23: checkcast     #171                // class java/lang/Number
      26: invokevirtual #175                // Method java/lang/Number.intValue:()I
      29: aload_0
      30: invokestatic  #177                // Method tail:(Ljava/util/List;)Ljava/util/List;
      33: invokestatic  #179                // Method sum:(Ljava/util/List;)I
      36: iadd
      37: ireturn

  public static final int sum2(java.util.List<java.lang.Integer>);
    Code:
       0: aload_0
       1: ldc           #168                // String xs
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: iconst_0
       8: invokestatic  #184                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      11: invokedynamic #204,  0            // InvokeDynamic #0:invoke:()Lkotlin/jvm/functions/Function2;
      16: invokestatic  #208                // Method foldLeft:(Ljava/util/List;Ljava/lang/Object;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object;
      19: checkcast     #171                // class java/lang/Number
      22: invokevirtual #175                // Method java/lang/Number.intValue:()I
      25: ireturn

  public static final int sumCoRec(java.util.List<java.lang.Integer>);
    Code:
       0: aload_0
       1: ldc           #168                // String xs
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: iconst_0
       8: invokestatic  #213                // Method sumCoRec$helper:(Ljava/util/List;I)I
      11: ireturn

  public static final <T> java.lang.String mkString(java.util.List<? extends T>, java.lang.String, java.lang.String, java.lang.String);
    Code:
       0: aload_0
       1: ldc           #133                // String <this>
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_1
       7: ldc           #218                // String prefix
       9: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
      12: aload_2
      13: ldc           #220                // String delim
      15: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
      18: aload_3
      19: ldc           #222                // String suffix
      21: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
      24: new           #92                 // class java/lang/StringBuilder
      27: dup
      28: invokespecial #95                 // Method java/lang/StringBuilder."<init>":()V
      31: aload_1
      32: invokevirtual #101                // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      35: aload_2
      36: aload_0
      37: ldc           #224                // String
      39: invokestatic  #228                // Method mkString$helper$5:(Ljava/lang/String;Ljava/util/List;Ljava/lang/String;)Ljava/lang/String;
      42: invokevirtual #101                // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      45: aload_3
      46: invokevirtual #101                // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      49: invokevirtual #120                // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
      52: areturn

  public static java.lang.String mkString$default(java.util.List, java.lang.String, java.lang.String, java.lang.String, int, java.lang.Object);
    Code:
       0: iload         4
       2: iconst_1
       3: iand
       4: ifeq          10
       7: ldc           #232                // String [
       9: astore_1
      10: iload         4
      12: iconst_2
      13: iand
      14: ifeq          20
      17: ldc           #234                // String ,
      19: astore_2
      20: iload         4
      22: iconst_4
      23: iand
      24: ifeq          30
      27: ldc           #236                // String ]
      29: astore_3
      30: aload_0
      31: aload_1
      32: aload_2
      33: aload_3
      34: invokestatic  #238                // Method mkString:(Ljava/util/List;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;
      37: areturn

  public static final <T> java.lang.String mkString2(java.util.List<? extends T>, java.lang.String, java.lang.String, java.lang.String);
    Code:
       0: aload_0
       1: ldc           #133                // String <this>
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_1
       7: ldc           #218                // String prefix
       9: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
      12: aload_2
      13: ldc           #220                // String delim
      15: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
      18: aload_3
      19: ldc           #222                // String suffix
      21: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
      24: aload_0
      25: ldc           #224                // String
      27: aload_2
      28: invokedynamic #249,  0            // InvokeDynamic #1:invoke:(Ljava/lang/String;)Lkotlin/jvm/functions/Function2;
      33: invokestatic  #208                // Method foldLeft:(Ljava/util/List;Ljava/lang/Object;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object;
      36: checkcast     #251                // class java/lang/String
      39: astore        4
      41: new           #92                 // class java/lang/StringBuilder
      44: dup
      45: invokespecial #95                 // Method java/lang/StringBuilder."<init>":()V
      48: aload_1
      49: invokevirtual #101                // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      52: aload         4
      54: invokevirtual #101                // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      57: aload_3
      58: invokevirtual #101                // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      61: invokevirtual #120                // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
      64: areturn

  public static java.lang.String mkString2$default(java.util.List, java.lang.String, java.lang.String, java.lang.String, int, java.lang.Object);
    Code:
       0: iload         4
       2: iconst_1
       3: iand
       4: ifeq          10
       7: ldc           #232                // String [
       9: astore_1
      10: iload         4
      12: iconst_2
      13: iand
      14: ifeq          20
      17: ldc           #234                // String ,
      19: astore_2
      20: iload         4
      22: iconst_4
      23: iand
      24: ifeq          30
      27: ldc           #236                // String ]
      29: astore_3
      30: aload_0
      31: aload_1
      32: aload_2
      33: aload_3
      34: invokestatic  #255                // Method mkString2:(Ljava/util/List;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;
      37: areturn

  public static final <T, U> U foldLeft(java.util.List<? extends T>, U, kotlin.jvm.functions.Function2<? super U, ? super T, ? extends U>);
    Code:
       0: aload_0
       1: ldc           #133                // String <this>
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_2
       7: ldc_w         #258                // String f
      10: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
      13: aload_2
      14: aload_0
      15: aload_1
      16: invokestatic  #262                // Method foldLeft$helper$7:(Lkotlin/jvm/functions/Function2;Ljava/util/List;Ljava/lang/Object;)Ljava/lang/Object;
      19: areturn

  public static final <T, U> U foldRight(java.util.List<? extends T>, U, kotlin.jvm.functions.Function2<? super T, ? super U, ? extends U>);
    Code:
       0: aload_0
       1: ldc           #133                // String <this>
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_2
       7: ldc_w         #258                // String f
      10: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
      13: aload_0
      14: checkcast     #77                 // class java/lang/Iterable
      17: invokestatic  #271                // Method kotlin/collections/CollectionsKt.reversed:(Ljava/lang/Iterable;)Ljava/util/List;
      20: aload_1
      21: aload_2
      22: invokedynamic #279,  0            // InvokeDynamic #2:invoke:(Lkotlin/jvm/functions/Function2;)Lkotlin/jvm/functions/Function2;
      27: invokestatic  #208                // Method foldLeft:(Ljava/util/List;Ljava/lang/Object;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object;
      30: areturn

  private static final int sum2$lambda$4(int, int);
    Code:
       0: iload_0
       1: iload_1
       2: iadd
       3: ireturn

  private static final int sumCoRec$helper(java.util.List<java.lang.Integer>, int);
    Code:
       0: aload_0
       1: invokeinterface #169,  1          // InterfaceMethod java/util/List.isEmpty:()Z
       6: ifeq          13
       9: iload_1
      10: goto          38
      13: aload_0
      14: invokestatic  #177                // Method tail:(Ljava/util/List;)Ljava/util/List;
      17: astore_2
      18: aload_0
      19: invokestatic  #29                 // Method head:(Ljava/util/List;)Ljava/lang/Object;
      22: checkcast     #171                // class java/lang/Number
      25: invokevirtual #175                // Method java/lang/Number.intValue:()I
      28: iload_1
      29: iadd
      30: istore_3
      31: aload_2
      32: astore_0
      33: iload_3
      34: istore_1
      35: goto          0
      38: ireturn

  private static final <T> java.lang.String mkString$helper$5(java.lang.String, java.util.List<? extends T>, java.lang.String);
    Code:
       0: nop
       1: aload_1
       2: invokeinterface #169,  1          // InterfaceMethod java/util/List.isEmpty:()Z
       7: ifeq          14
      10: aload_2
      11: goto          90
      14: aload_1
      15: invokestatic  #177                // Method tail:(Ljava/util/List;)Ljava/util/List;
      18: invokeinterface #169,  1          // InterfaceMethod java/util/List.isEmpty:()Z
      23: ifeq          50
      26: new           #92                 // class java/lang/StringBuilder
      29: dup
      30: invokespecial #95                 // Method java/lang/StringBuilder."<init>":()V
      33: aload_2
      34: invokevirtual #101                // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      37: aload_1
      38: invokestatic  #29                 // Method head:(Ljava/util/List;)Ljava/lang/Object;
      41: invokevirtual #289                // Method java/lang/StringBuilder.append:(Ljava/lang/Object;)Ljava/lang/StringBuilder;
      44: invokevirtual #120                // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
      47: goto          90
      50: aload_1
      51: invokestatic  #177                // Method tail:(Ljava/util/List;)Ljava/util/List;
      54: astore_3
      55: new           #92                 // class java/lang/StringBuilder
      58: dup
      59: invokespecial #95                 // Method java/lang/StringBuilder."<init>":()V
      62: aload_2
      63: invokevirtual #101                // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      66: aload_1
      67: invokestatic  #29                 // Method head:(Ljava/util/List;)Ljava/lang/Object;
      70: invokevirtual #289                // Method java/lang/StringBuilder.append:(Ljava/lang/Object;)Ljava/lang/StringBuilder;
      73: aload_0
      74: invokevirtual #101                // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      77: invokevirtual #120                // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
      80: astore        4
      82: aload_3
      83: astore_1
      84: aload         4
      86: astore_2
      87: goto          0
      90: areturn

  private static final java.lang.String mkString2$lambda$6(java.lang.String, java.lang.String, java.lang.Object);
    Code:
       0: aload_1
       1: ldc_w         #291                // String acc
       4: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       7: aload_1
       8: checkcast     #19                 // class java/lang/CharSequence
      11: invokeinterface #294,  1          // InterfaceMethod java/lang/CharSequence.length:()I
      16: ifne          23
      19: iconst_1
      20: goto          24
      23: iconst_0
      24: ifeq          34
      27: aload_2
      28: invokestatic  #297                // Method java/lang/String.valueOf:(Ljava/lang/Object;)Ljava/lang/String;
      31: goto          56
      34: new           #92                 // class java/lang/StringBuilder
      37: dup
      38: invokespecial #95                 // Method java/lang/StringBuilder."<init>":()V
      41: aload_1
      42: invokevirtual #101                // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      45: aload_0
      46: invokevirtual #101                // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      49: aload_2
      50: invokevirtual #289                // Method java/lang/StringBuilder.append:(Ljava/lang/Object;)Ljava/lang/StringBuilder;
      53: invokevirtual #120                // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
      56: areturn

  private static final <T, U> U foldLeft$helper$7(kotlin.jvm.functions.Function2<? super U, ? super T, ? extends U>, java.util.List<? extends T>, U);
    Code:
       0: nop
       1: aload_1
       2: invokeinterface #169,  1          // InterfaceMethod java/util/List.isEmpty:()Z
       7: ifeq          14
      10: aload_2
      11: goto          40
      14: aload_1
      15: invokestatic  #177                // Method tail:(Ljava/util/List;)Ljava/util/List;
      18: astore_3
      19: aload_0
      20: aload_2
      21: aload_1
      22: invokestatic  #29                 // Method head:(Ljava/util/List;)Ljava/lang/Object;
      25: invokeinterface #303,  3          // InterfaceMethod kotlin/jvm/functions/Function2.invoke:(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
      30: astore        4
      32: aload_3
      33: astore_1
      34: aload         4
      36: astore_2
      37: goto          0
      40: areturn

  private static final java.lang.Object foldRight$lambda$8(kotlin.jvm.functions.Function2, java.lang.Object, java.lang.Object);
    Code:
       0: aload_0
       1: aload_2
       2: aload_1
       3: invokeinterface #303,  3          // InterfaceMethod kotlin/jvm/functions/Function2.invoke:(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
       8: areturn
}