package day19

import day19.Resource.CLAY
import day19.Resource.GEODE
import day19.Resource.NONE
import day19.Resource.OBSIDIAN
import day19.Resource.ORE
import kotlin.math.max
import readInput

val regex = """.+ (\d+).+ (\d+) .+ (\d+) .+ (\d+) .+ (\d+) .+ (\d+).+ (\d+) .*""".toRegex()

fun main() {
    val input = readInput("main/day19/Day19_test")
    println(part1(input))
    println(part2(input))
}

fun part1(input: List<String>): Int {
    val bluePrints = input.toBlueprints()
    return bluePrints.sumOf { it.id * it.maxGeodes(0) }
}

fun part2(input: List<String>): Long {
    return -1
}

enum class Resource {
    NONE, ORE, CLAY, OBSIDIAN, GEODE
}

fun List<String>.toBlueprints() = map { line ->
    regex.matchEntire(line)?.destructured?.let { (id, oreRobotOre, clayRoboTOre, obsidianRobotOre, obsidianRobotClay, geodeRobotOre, geodeRobotObsidian) ->
        BluePrint(
            id = id.toInt(),
            oreRobotTemplate = OreRobot(cost = listOf(Cost(ORE, oreRobotOre.toInt()))),
            clayRobotTemplate = ClayRobot(cost = listOf(Cost(ORE, clayRoboTOre.toInt()))),
            obsidianRobotTemplate = ObsidianRobot(cost = listOf(Cost(ORE, obsidianRobotOre.toInt()), Cost(CLAY, obsidianRobotClay.toInt()))),
            geodeRobotTemplate = GeodeRobot(cost = listOf(Cost(ORE, geodeRobotOre.toInt()), Cost(OBSIDIAN, geodeRobotObsidian.toInt()))),
        )
    } ?: error("PARSER ERROR")
}

data class Cost(val resource: Resource, val amount: Int)
data class BluePrint(
    val id: Int,
    val oreRobotTemplate: OreRobot,
    val clayRobotTemplate: ClayRobot,
    val obsidianRobotTemplate: ObsidianRobot,
    val geodeRobotTemplate: GeodeRobot
) {
    private val allTemplates = listOf(oreRobotTemplate, clayRobotTemplate, obsidianRobotTemplate, geodeRobotTemplate).reversed()

    fun maxGeodes(
        maxSoFar: Int,
        robots: List<Robot> = listOf(oreRobotTemplate.instance()),
        resources: MutableList<Resource> = mutableListOf(),
        timeLeft: Int = 24
    ): Int {
        if (timeLeft > 0) {
            val buildableRobots = buildableRobots(resources)
            buildableRobots.forEach { newRobot ->
                newRobot.cost.forEach { repeat(it.amount) { _ -> resources.remove(it.resource) } }
                return maxGeodes(
                    max(maxSoFar, resources.count { it == GEODE }),
                    robots + newRobot,
                    resources.filter { it != NONE }.toMutableList(),
                    timeLeft - 1
                )
            }
            robots.forEach { resources.add(it.resource) }
            return maxGeodes(max(maxSoFar, resources.count { it == GEODE }), robots, resources, timeLeft - 1)
        } else return maxSoFar
    }

    private fun buildableRobots(resources: List<Resource>): MutableList<Robot> {
        return allTemplates.filter {
            it.canBeBuiltFrom(resources)
        }.map {
            it.instance()
        }.toMutableList()
    }
}

sealed interface Robot {
    val resource: Resource
    val cost: List<Cost>
    fun canBeBuiltFrom(resources: List<Resource>): Boolean
    fun instance(): Robot
}

data class OreRobot(override val resource: Resource = ORE, override val cost: List<Cost>) : Robot {
    override fun canBeBuiltFrom(resources: List<Resource>): Boolean {
        return cost.all { cost ->
            resources.count {
                it == cost.resource
            } >= cost.amount
        }
    }

    override fun instance(): Robot {
        return copy()
    }
}

data class ClayRobot(override val resource: Resource = CLAY, override val cost: List<Cost>) : Robot {
    override fun canBeBuiltFrom(resources: List<Resource>): Boolean {
        return cost.all { cost ->
            resources.count {
                it == cost.resource
            } >= cost.amount
        }
    }

    override fun instance(): Robot {
        return copy()
    }
}

data class ObsidianRobot(override val resource: Resource = OBSIDIAN, override val cost: List<Cost>) : Robot {
    override fun canBeBuiltFrom(resources: List<Resource>): Boolean {
        return cost.all { cost ->
            resources.count {
                it == cost.resource
            } >= cost.amount
        }
    }

    override fun instance(): Robot {
        return copy()
    }
}

data class GeodeRobot(override val resource: Resource = GEODE, override val cost: List<Cost>) : Robot {
    override fun canBeBuiltFrom(resources: List<Resource>): Boolean {
        return cost.all { cost ->
            resources.count {
                it == cost.resource
            } >= cost.amount
        }
    }

    override fun instance(): Robot {
        return copy()
    }
}

//object NoOp : Robot {
//    override val resource: Resource
//        get() = NONE
//    override val cost: List<Cost>
//        get() = emptyList()
//
//    override fun canBeBuiltFrom(resources: List<Resource>): Boolean = true
//
//    override fun instance(): Robot {
//        return this
//    }
//}

Compiled from "day19.kt"
public final class day19.Day19Kt {
  private static final kotlin.text.Regex regex;

  public static final kotlin.text.Regex getRegex();
    Code:
       0: getstatic     #11                 // Field regex:Lkotlin/text/Regex;
       3: areturn

  public static final void main();
    Code:
       0: ldc           #15                 // String main/day19/Day19_test
       2: invokestatic  #21                 // Method UtilsKt.readInput:(Ljava/lang/String;)Ljava/util/List;
       5: astore_0
       6: aload_0
       7: invokestatic  #25                 // Method part1:(Ljava/util/List;)I
      10: istore_1
      11: getstatic     #31                 // Field java/lang/System.out:Ljava/io/PrintStream;
      14: iload_1
      15: invokevirtual #37                 // Method java/io/PrintStream.println:(I)V
      18: aload_0
      19: invokestatic  #41                 // Method part2:(Ljava/util/List;)J
      22: lstore_1
      23: getstatic     #31                 // Field java/lang/System.out:Ljava/io/PrintStream;
      26: lload_1
      27: invokevirtual #44                 // Method java/io/PrintStream.println:(J)V
      30: return

  public static final int part1(java.util.List<java.lang.String>);
    Code:
       0: aload_0
       1: ldc           #48                 // String input
       3: invokestatic  #54                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: invokestatic  #58                 // Method toBlueprints:(Ljava/util/List;)Ljava/util/List;
      10: astore_1
      11: aload_1
      12: checkcast     #60                 // class java/lang/Iterable
      15: astore_2
      16: iconst_0
      17: istore_3
      18: aload_2
      19: invokeinterface #64,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      24: astore        4
      26: aload         4
      28: invokeinterface #70,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      33: ifeq          87
      36: aload         4
      38: invokeinterface #74,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      43: astore        5
      45: iload_3
      46: aload         5
      48: checkcast     #76                 // class day19/BluePrint
      51: astore        6
      53: istore        8
      55: iconst_0
      56: istore        7
      58: aload         6
      60: invokevirtual #80                 // Method day19/BluePrint.getId:()I
      63: aload         6
      65: iconst_0
      66: aconst_null
      67: aconst_null
      68: iconst_0
      69: bipush        14
      71: aconst_null
      72: invokestatic  #84                 // Method day19/BluePrint.maxGeodes$default:(Lday19/BluePrint;ILjava/util/List;Ljava/util/List;IILjava/lang/Object;)I
      75: imul
      76: istore        9
      78: iload         8
      80: iload         9
      82: iadd
      83: istore_3
      84: goto          26
      87: iload_3
      88: ireturn

  public static final long part2(java.util.List<java.lang.String>);
    Code:
       0: aload_0
       1: ldc           #48                 // String input
       3: invokestatic  #54                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: ldc2_w        #93                 // long -1l
       9: lreturn

  public static final java.util.List<day19.BluePrint> toBlueprints(java.util.List<java.lang.String>);
    Code:
       0: aload_0
       1: ldc           #97                 // String <this>
       3: invokestatic  #54                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: checkcast     #60                 // class java/lang/Iterable
      10: astore_1
      11: iconst_0
      12: istore_2
      13: aload_1
      14: astore_3
      15: new           #99                 // class java/util/ArrayList
      18: dup
      19: aload_1
      20: bipush        10
      22: invokestatic  #105                // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
      25: invokespecial #108                // Method java/util/ArrayList."<init>":(I)V
      28: checkcast     #110                // class java/util/Collection
      31: astore        4
      33: iconst_0
      34: istore        5
      36: aload_3
      37: invokeinterface #64,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      42: astore        6
      44: aload         6
      46: invokeinterface #70,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      51: ifeq          472
      54: aload         6
      56: invokeinterface #74,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      61: astore        7
      63: aload         4
      65: aload         7
      67: checkcast     #112                // class java/lang/String
      70: astore        8
      72: astore        20
      74: iconst_0
      75: istore        9
      77: getstatic     #11                 // Field regex:Lkotlin/text/Regex;
      80: aload         8
      82: checkcast     #114                // class java/lang/CharSequence
      85: invokevirtual #120                // Method kotlin/text/Regex.matchEntire:(Ljava/lang/CharSequence;)Lkotlin/text/MatchResult;
      88: dup
      89: ifnull        446
      92: invokeinterface #126,  1          // InterfaceMethod kotlin/text/MatchResult.getDestructured:()Lkotlin/text/MatchResult$Destructured;
      97: dup
      98: ifnull        446
     101: astore        10
     103: iconst_0
     104: istore        11
     106: aload         10
     108: invokevirtual #132                // Method kotlin/text/MatchResult$Destructured.getMatch:()Lkotlin/text/MatchResult;
     111: invokeinterface #136,  1          // InterfaceMethod kotlin/text/MatchResult.getGroupValues:()Ljava/util/List;
     116: iconst_1
     117: invokeinterface #140,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     122: checkcast     #112                // class java/lang/String
     125: astore        12
     127: aload         10
     129: invokevirtual #132                // Method kotlin/text/MatchResult$Destructured.getMatch:()Lkotlin/text/MatchResult;
     132: invokeinterface #136,  1          // InterfaceMethod kotlin/text/MatchResult.getGroupValues:()Ljava/util/List;
     137: iconst_2
     138: invokeinterface #140,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     143: checkcast     #112                // class java/lang/String
     146: astore        13
     148: aload         10
     150: invokevirtual #132                // Method kotlin/text/MatchResult$Destructured.getMatch:()Lkotlin/text/MatchResult;
     153: invokeinterface #136,  1          // InterfaceMethod kotlin/text/MatchResult.getGroupValues:()Ljava/util/List;
     158: iconst_3
     159: invokeinterface #140,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     164: checkcast     #112                // class java/lang/String
     167: astore        14
     169: aload         10
     171: invokevirtual #132                // Method kotlin/text/MatchResult$Destructured.getMatch:()Lkotlin/text/MatchResult;
     174: invokeinterface #136,  1          // InterfaceMethod kotlin/text/MatchResult.getGroupValues:()Ljava/util/List;
     179: iconst_4
     180: invokeinterface #140,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     185: checkcast     #112                // class java/lang/String
     188: astore        15
     190: aload         10
     192: invokevirtual #132                // Method kotlin/text/MatchResult$Destructured.getMatch:()Lkotlin/text/MatchResult;
     195: invokeinterface #136,  1          // InterfaceMethod kotlin/text/MatchResult.getGroupValues:()Ljava/util/List;
     200: iconst_5
     201: invokeinterface #140,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     206: checkcast     #112                // class java/lang/String
     209: astore        16
     211: aload         10
     213: invokevirtual #132                // Method kotlin/text/MatchResult$Destructured.getMatch:()Lkotlin/text/MatchResult;
     216: invokeinterface #136,  1          // InterfaceMethod kotlin/text/MatchResult.getGroupValues:()Ljava/util/List;
     221: bipush        6
     223: invokeinterface #140,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     228: checkcast     #112                // class java/lang/String
     231: astore        17
     233: aload         10
     235: invokevirtual #132                // Method kotlin/text/MatchResult$Destructured.getMatch:()Lkotlin/text/MatchResult;
     238: invokeinterface #136,  1          // InterfaceMethod kotlin/text/MatchResult.getGroupValues:()Ljava/util/List;
     243: bipush        7
     245: invokeinterface #140,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     250: checkcast     #112                // class java/lang/String
     253: astore        18
     255: new           #76                 // class day19/BluePrint
     258: dup
     259: aload         12
     261: invokestatic  #146                // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
     264: new           #148                // class day19/OreRobot
     267: dup
     268: aconst_null
     269: new           #150                // class day19/Cost
     272: dup
     273: getstatic     #156                // Field day19/Resource.ORE:Lday19/Resource;
     276: aload         13
     278: invokestatic  #146                // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
     281: invokespecial #159                // Method day19/Cost."<init>":(Lday19/Resource;I)V
     284: invokestatic  #163                // Method kotlin/collections/CollectionsKt.listOf:(Ljava/lang/Object;)Ljava/util/List;
     287: iconst_1
     288: aconst_null
     289: invokespecial #166                // Method day19/OreRobot."<init>":(Lday19/Resource;Ljava/util/List;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
     292: new           #168                // class day19/ClayRobot
     295: dup
     296: aconst_null
     297: new           #150                // class day19/Cost
     300: dup
     301: getstatic     #156                // Field day19/Resource.ORE:Lday19/Resource;
     304: aload         14
     306: invokestatic  #146                // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
     309: invokespecial #159                // Method day19/Cost."<init>":(Lday19/Resource;I)V
     312: invokestatic  #163                // Method kotlin/collections/CollectionsKt.listOf:(Ljava/lang/Object;)Ljava/util/List;
     315: iconst_1
     316: aconst_null
     317: invokespecial #169                // Method day19/ClayRobot."<init>":(Lday19/Resource;Ljava/util/List;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
     320: new           #171                // class day19/ObsidianRobot
     323: dup
     324: aconst_null
     325: iconst_2
     326: anewarray     #150                // class day19/Cost
     329: astore        19
     331: aload         19
     333: iconst_0
     334: new           #150                // class day19/Cost
     337: dup
     338: getstatic     #156                // Field day19/Resource.ORE:Lday19/Resource;
     341: aload         15
     343: invokestatic  #146                // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
     346: invokespecial #159                // Method day19/Cost."<init>":(Lday19/Resource;I)V
     349: aastore
     350: aload         19
     352: iconst_1
     353: new           #150                // class day19/Cost
     356: dup
     357: getstatic     #174                // Field day19/Resource.CLAY:Lday19/Resource;
     360: aload         16
     362: invokestatic  #146                // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
     365: invokespecial #159                // Method day19/Cost."<init>":(Lday19/Resource;I)V
     368: aastore
     369: aload         19
     371: invokestatic  #177                // Method kotlin/collections/CollectionsKt.listOf:([Ljava/lang/Object;)Ljava/util/List;
     374: iconst_1
     375: aconst_null
     376: invokespecial #178                // Method day19/ObsidianRobot."<init>":(Lday19/Resource;Ljava/util/List;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
     379: new           #180                // class day19/GeodeRobot
     382: dup
     383: aconst_null
     384: iconst_2
     385: anewarray     #150                // class day19/Cost
     388: astore        19
     390: aload         19
     392: iconst_0
     393: new           #150                // class day19/Cost
     396: dup
     397: getstatic     #156                // Field day19/Resource.ORE:Lday19/Resource;
     400: aload         17
     402: invokestatic  #146                // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
     405: invokespecial #159                // Method day19/Cost."<init>":(Lday19/Resource;I)V
     408: aastore
     409: aload         19
     411: iconst_1
     412: new           #150                // class day19/Cost
     415: dup
     416: getstatic     #183                // Field day19/Resource.OBSIDIAN:Lday19/Resource;
     419: aload         18
     421: invokestatic  #146                // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
     424: invokespecial #159                // Method day19/Cost."<init>":(Lday19/Resource;I)V
     427: aastore
     428: aload         19
     430: invokestatic  #177                // Method kotlin/collections/CollectionsKt.listOf:([Ljava/lang/Object;)Ljava/util/List;
     433: iconst_1
     434: aconst_null
     435: invokespecial #184                // Method day19/GeodeRobot."<init>":(Lday19/Resource;Ljava/util/List;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
     438: invokespecial #187                // Method day19/BluePrint."<init>":(ILday19/OreRobot;Lday19/ClayRobot;Lday19/ObsidianRobot;Lday19/GeodeRobot;)V
     441: nop
     442: nop
     443: goto          460
     446: pop
     447: new           #189                // class java/lang/IllegalStateException
     450: dup
     451: ldc           #191                // String PARSER ERROR
     453: invokevirtual #195                // Method java/lang/Object.toString:()Ljava/lang/String;
     456: invokespecial #198                // Method java/lang/IllegalStateException."<init>":(Ljava/lang/String;)V
     459: athrow
     460: aload         20
     462: swap
     463: invokeinterface #202,  2          // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     468: pop
     469: goto          44
     472: aload         4
     474: checkcast     #91                 // class java/util/List
     477: nop
     478: areturn

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

  static {};
    Code:
       0: new           #116                // class kotlin/text/Regex
       3: dup
       4: ldc           #233                // String .+ (\\d+).+ (\\d+) .+ (\\d+) .+ (\\d+) .+ (\\d+) .+ (\\d+).+ (\\d+) .*
       6: invokespecial #234                // Method kotlin/text/Regex."<init>":(Ljava/lang/String;)V
       9: putstatic     #11                 // Field regex:Lkotlin/text/Regex;
      12: return
}


package year_2016.day12

import readInput

fun main() {
    val input = readInput("main/year_2016/day12/Day12")

    println(part1(input))
    println(part2(input))
}

val registers = mutableMapOf(
    "a" to 0,
    "b" to 0,
    "c" to 0,
    "d" to 0,
)

fun part1(input: List<String>): Int {

    var index = 0

    while (index < input.size) {
        val line = input[index]
        when (line.substring(0, 3)) {
            "cpy" -> {
                val (_, x, reg) = line.split(" ")
                if (x in listOf("a", "b", "c", "d"))
                    registers[reg] = registers[x]!!
                else
                    registers[reg] = x.toInt()
                index++
            }

            "inc" -> {
                val (_, reg) = line.split(" ")
                registers[reg] = registers[reg]!! + 1
                index++
            }

            "dec" -> {
                val (_, reg) = line.split(" ")
                registers[reg] = registers[reg]!! - 1
                index++
            }

            "jnz" -> {
                val (_, x, y) = line.split(" ")
                if (x.all { it.isDigit() }) {
                    if (x.toInt() != 0) index += y.toInt()
                } else {
                    if (registers[x] != 0)
                        index += y.toInt()
                    else index++
                }
            }
        }

    }
    return registers["a"]!!
}

fun part2(input: List<String>): Int {
    registers["c"] = 1
    return part1(input)
}

Compiled from "day16.kt"
public final class year_2016.day12.Day16Kt {
  private static final java.util.Map<java.lang.String, java.lang.Integer> registers;

  public static final void main();
    Code:
       0: ldc           #8                  // String main/year_2016/day12/Day12
       2: invokestatic  #14                 // Method UtilsKt.readInput:(Ljava/lang/String;)Ljava/util/List;
       5: astore_0
       6: aload_0
       7: invokestatic  #18                 // Method part1:(Ljava/util/List;)I
      10: istore_1
      11: getstatic     #24                 // Field java/lang/System.out:Ljava/io/PrintStream;
      14: iload_1
      15: invokevirtual #30                 // Method java/io/PrintStream.println:(I)V
      18: aload_0
      19: invokestatic  #33                 // Method part2:(Ljava/util/List;)I
      22: istore_1
      23: getstatic     #24                 // Field java/lang/System.out:Ljava/io/PrintStream;
      26: iload_1
      27: invokevirtual #30                 // Method java/io/PrintStream.println:(I)V
      30: return

  public static final java.util.Map<java.lang.String, java.lang.Integer> getRegisters();
    Code:
       0: getstatic     #43                 // Field registers:Ljava/util/Map;
       3: areturn

  public static final int part1(java.util.List<java.lang.String>);
    Code:
       0: aload_0
       1: ldc           #45                 // String input
       3: invokestatic  #51                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: iconst_0
       7: istore_1
       8: iload_1
       9: aload_0
      10: invokeinterface #57,  1           // InterfaceMethod java/util/List.size:()I
      15: if_icmpge     632
      18: aload_0
      19: iload_1
      20: invokeinterface #61,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      25: checkcast     #63                 // class java/lang/String
      28: astore_2
      29: aload_2
      30: iconst_0
      31: iconst_3
      32: invokevirtual #67                 // Method java/lang/String.substring:(II)Ljava/lang/String;
      35: dup
      36: ldc           #69                 // String substring(...)
      38: invokestatic  #72                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
      41: astore_3
      42: aload_3
      43: invokevirtual #75                 // Method java/lang/String.hashCode:()I
      46: lookupswitch  { // 4
                 98732: 112
                 99330: 88
                104414: 124
                105398: 100
               default: 629
          }
      88: aload_3
      89: ldc           #77                 // String dec
      91: invokevirtual #81                 // Method java/lang/String.equals:(Ljava/lang/Object;)Z
      94: ifne          368
      97: goto          8
     100: aload_3
     101: ldc           #83                 // String jnz
     103: invokevirtual #81                 // Method java/lang/String.equals:(Ljava/lang/Object;)Z
     106: ifne          447
     109: goto          8
     112: aload_3
     113: ldc           #85                 // String cpy
     115: invokevirtual #81                 // Method java/lang/String.equals:(Ljava/lang/Object;)Z
     118: ifne          136
     121: goto          8
     124: aload_3
     125: ldc           #87                 // String inc
     127: invokevirtual #81                 // Method java/lang/String.equals:(Ljava/lang/Object;)Z
     130: ifne          289
     133: goto          8
     136: aload_2
     137: checkcast     #89                 // class java/lang/CharSequence
     140: iconst_1
     141: anewarray     #63                 // class java/lang/String
     144: astore        5
     146: aload         5
     148: iconst_0
     149: ldc           #91                 // String
     151: aastore
     152: aload         5
     154: iconst_0
     155: iconst_0
     156: bipush        6
     158: aconst_null
     159: invokestatic  #97                 // Method kotlin/text/StringsKt.split$default:(Ljava/lang/CharSequence;[Ljava/lang/String;ZIILjava/lang/Object;)Ljava/util/List;
     162: astore        4
     164: aload         4
     166: iconst_1
     167: invokeinterface #61,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     172: checkcast     #63                 // class java/lang/String
     175: astore        5
     177: aload         4
     179: iconst_2
     180: invokeinterface #61,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     185: checkcast     #63                 // class java/lang/String
     188: astore        6
     190: iconst_4
     191: anewarray     #63                 // class java/lang/String
     194: astore        7
     196: aload         7
     198: iconst_0
     199: ldc           #99                 // String a
     201: aastore
     202: aload         7
     204: iconst_1
     205: ldc           #101                // String b
     207: aastore
     208: aload         7
     210: iconst_2
     211: ldc           #103                // String c
     213: aastore
     214: aload         7
     216: iconst_3
     217: ldc           #105                // String d
     219: aastore
     220: aload         7
     222: invokestatic  #111                // Method kotlin/collections/CollectionsKt.listOf:([Ljava/lang/Object;)Ljava/util/List;
     225: aload         5
     227: invokeinterface #114,  2          // InterfaceMethod java/util/List.contains:(Ljava/lang/Object;)Z
     232: ifeq          263
     235: getstatic     #43                 // Field registers:Ljava/util/Map;
     238: aload         6
     240: getstatic     #43                 // Field registers:Ljava/util/Map;
     243: aload         5
     245: invokeinterface #119,  2          // InterfaceMethod java/util/Map.get:(Ljava/lang/Object;)Ljava/lang/Object;
     250: dup
     251: invokestatic  #123                // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
     254: invokeinterface #127,  3          // InterfaceMethod java/util/Map.put:(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
     259: pop
     260: goto          283
     263: nop
     264: getstatic     #43                 // Field registers:Ljava/util/Map;
     267: aload         6
     269: aload         5
     271: invokestatic  #133                // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
     274: invokestatic  #137                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     277: invokeinterface #127,  3          // InterfaceMethod java/util/Map.put:(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
     282: pop
     283: iinc          1, 1
     286: goto          8
     289: aload_2
     290: checkcast     #89                 // class java/lang/CharSequence
     293: iconst_1
     294: anewarray     #63                 // class java/lang/String
     297: astore        5
     299: aload         5
     301: iconst_0
     302: ldc           #91                 // String
     304: aastore
     305: aload         5
     307: iconst_0
     308: iconst_0
     309: bipush        6
     311: aconst_null
     312: invokestatic  #97                 // Method kotlin/text/StringsKt.split$default:(Ljava/lang/CharSequence;[Ljava/lang/String;ZIILjava/lang/Object;)Ljava/util/List;
     315: iconst_1
     316: invokeinterface #61,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     321: checkcast     #63                 // class java/lang/String
     324: astore        5
     326: getstatic     #43                 // Field registers:Ljava/util/Map;
     329: aload         5
     331: getstatic     #43                 // Field registers:Ljava/util/Map;
     334: aload         5
     336: invokeinterface #119,  2          // InterfaceMethod java/util/Map.get:(Ljava/lang/Object;)Ljava/lang/Object;
     341: dup
     342: invokestatic  #123                // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
     345: checkcast     #139                // class java/lang/Number
     348: invokevirtual #142                // Method java/lang/Number.intValue:()I
     351: iconst_1
     352: iadd
     353: invokestatic  #137                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     356: invokeinterface #127,  3          // InterfaceMethod java/util/Map.put:(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
     361: pop
     362: iinc          1, 1
     365: goto          8
     368: aload_2
     369: checkcast     #89                 // class java/lang/CharSequence
     372: iconst_1
     373: anewarray     #63                 // class java/lang/String
     376: astore        5
     378: aload         5
     380: iconst_0
     381: ldc           #91                 // String
     383: aastore
     384: aload         5
     386: iconst_0
     387: iconst_0
     388: bipush        6
     390: aconst_null
     391: invokestatic  #97                 // Method kotlin/text/StringsKt.split$default:(Ljava/lang/CharSequence;[Ljava/lang/String;ZIILjava/lang/Object;)Ljava/util/List;
     394: iconst_1
     395: invokeinterface #61,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     400: checkcast     #63                 // class java/lang/String
     403: astore        5
     405: getstatic     #43                 // Field registers:Ljava/util/Map;
     408: aload         5
     410: getstatic     #43                 // Field registers:Ljava/util/Map;
     413: aload         5
     415: invokeinterface #119,  2          // InterfaceMethod java/util/Map.get:(Ljava/lang/Object;)Ljava/lang/Object;
     420: dup
     421: invokestatic  #123                // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
     424: checkcast     #139                // class java/lang/Number
     427: invokevirtual #142                // Method java/lang/Number.intValue:()I
     430: iconst_1
     431: isub
     432: invokestatic  #137                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     435: invokeinterface #127,  3          // InterfaceMethod java/util/Map.put:(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
     440: pop
     441: iinc          1, 1
     444: goto          8
     447: aload_2
     448: checkcast     #89                 // class java/lang/CharSequence
     451: iconst_1
     452: anewarray     #63                 // class java/lang/String
     455: astore        5
     457: aload         5
     459: iconst_0
     460: ldc           #91                 // String
     462: aastore
     463: aload         5
     465: iconst_0
     466: iconst_0
     467: bipush        6
     469: aconst_null
     470: invokestatic  #97                 // Method kotlin/text/StringsKt.split$default:(Ljava/lang/CharSequence;[Ljava/lang/String;ZIILjava/lang/Object;)Ljava/util/List;
     473: astore        4
     475: aload         4
     477: iconst_1
     478: invokeinterface #61,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     483: checkcast     #63                 // class java/lang/String
     486: astore        5
     488: aload         4
     490: iconst_2
     491: invokeinterface #61,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     496: checkcast     #63                 // class java/lang/String
     499: astore        6
     501: aload         5
     503: checkcast     #89                 // class java/lang/CharSequence
     506: astore        7
     508: iconst_0
     509: istore        8
     511: iconst_0
     512: istore        9
     514: iload         9
     516: aload         7
     518: invokeinterface #145,  1          // InterfaceMethod java/lang/CharSequence.length:()I
     523: if_icmpge     563
     526: aload         7
     528: iload         9
     530: invokeinterface #149,  2          // InterfaceMethod java/lang/CharSequence.charAt:(I)C
     535: istore        10
     537: iload         10
     539: istore        11
     541: iconst_0
     542: istore        12
     544: iload         11
     546: invokestatic  #155                // Method java/lang/Character.isDigit:(C)Z
     549: nop
     550: ifne          557
     553: iconst_0
     554: goto          564
     557: iinc          9, 1
     560: goto          514
     563: iconst_1
     564: ifeq          586
     567: aload         5
     569: invokestatic  #133                // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
     572: ifeq          8
     575: iload_1
     576: aload         6
     578: invokestatic  #133                // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
     581: iadd
     582: istore_1
     583: goto          8
     586: getstatic     #43                 // Field registers:Ljava/util/Map;
     589: aload         5
     591: invokeinterface #119,  2          // InterfaceMethod java/util/Map.get:(Ljava/lang/Object;)Ljava/lang/Object;
     596: checkcast     #129                // class java/lang/Integer
     599: dup
     600: ifnonnull     607
     603: pop
     604: goto          613
     607: invokevirtual #156                // Method java/lang/Integer.intValue:()I
     610: ifeq          624
     613: iload_1
     614: aload         6
     616: invokestatic  #133                // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
     619: iadd
     620: istore_1
     621: goto          8
     624: iload_1
     625: iinc          1, 1
     628: pop
     629: goto          8
     632: getstatic     #43                 // Field registers:Ljava/util/Map;
     635: ldc           #99                 // String a
     637: invokeinterface #119,  2          // InterfaceMethod java/util/Map.get:(Ljava/lang/Object;)Ljava/lang/Object;
     642: dup
     643: invokestatic  #123                // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
     646: checkcast     #139                // class java/lang/Number
     649: invokevirtual #142                // Method java/lang/Number.intValue:()I
     652: ireturn

  public static final int part2(java.util.List<java.lang.String>);
    Code:
       0: aload_0
       1: ldc           #45                 // String input
       3: invokestatic  #51                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: getstatic     #43                 // Field registers:Ljava/util/Map;
       9: ldc           #103                // String c
      11: iconst_1
      12: invokestatic  #137                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      15: invokeinterface #127,  3          // InterfaceMethod java/util/Map.put:(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
      20: pop
      21: aload_0
      22: invokestatic  #18                 // Method part1:(Ljava/util/List;)I
      25: ireturn

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

  static {};
    Code:
       0: iconst_4
       1: anewarray     #179                // class kotlin/Pair
       4: astore_0
       5: aload_0
       6: iconst_0
       7: ldc           #99                 // String a
       9: iconst_0
      10: invokestatic  #137                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      13: invokestatic  #185                // Method kotlin/TuplesKt.to:(Ljava/lang/Object;Ljava/lang/Object;)Lkotlin/Pair;
      16: aastore
      17: aload_0
      18: iconst_1
      19: ldc           #101                // String b
      21: iconst_0
      22: invokestatic  #137                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      25: invokestatic  #185                // Method kotlin/TuplesKt.to:(Ljava/lang/Object;Ljava/lang/Object;)Lkotlin/Pair;
      28: aastore
      29: aload_0
      30: iconst_2
      31: ldc           #103                // String c
      33: iconst_0
      34: invokestatic  #137                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      37: invokestatic  #185                // Method kotlin/TuplesKt.to:(Ljava/lang/Object;Ljava/lang/Object;)Lkotlin/Pair;
      40: aastore
      41: aload_0
      42: iconst_3
      43: ldc           #105                // String d
      45: iconst_0
      46: invokestatic  #137                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      49: invokestatic  #185                // Method kotlin/TuplesKt.to:(Ljava/lang/Object;Ljava/lang/Object;)Lkotlin/Pair;
      52: aastore
      53: aload_0
      54: invokestatic  #191                // Method kotlin/collections/MapsKt.mutableMapOf:([Lkotlin/Pair;)Ljava/util/Map;
      57: putstatic     #43                 // Field registers:Ljava/util/Map;
      60: return
}


package year_2016.day15

import readInput

val reg = """\b\d+\b""".toRegex()

fun main() {

    val input = readInput("main/year_2016/day15/Day15")

    println(part1(input))
    println(part2(input))
}

fun part1(input: List<String>): Int {
    return part2(input.dropLast(1))
}

fun part2(input: List<String>): Int {
    val discs = input.map(String::toDisc)
    var time = 0
    while (true) {
        if (discs.all {
                (it.layer + it.startingPosition + time) % it.positions == 0
            })
            return time
        time++
    }
}

data class Disc(
    val layer: Int,
    val positions: Int,
    val startingPosition: Int
)

fun String.toDisc(): Disc {
    val (layer, positions, _, startingPosition) = reg.findAll(this).toList()
    return Disc(layer.value.toInt(), positions.value.toInt(), startingPosition.value.toInt())

}

Compiled from "day15.kt"
public final class year_2016.day15.Day15Kt {
  private static final kotlin.text.Regex reg;

  public static final kotlin.text.Regex getReg();
    Code:
       0: getstatic     #11                 // Field reg:Lkotlin/text/Regex;
       3: areturn

  public static final void main();
    Code:
       0: ldc           #15                 // String main/year_2016/day15/Day15
       2: invokestatic  #21                 // Method UtilsKt.readInput:(Ljava/lang/String;)Ljava/util/List;
       5: astore_0
       6: aload_0
       7: invokestatic  #25                 // Method part1:(Ljava/util/List;)I
      10: istore_1
      11: getstatic     #31                 // Field java/lang/System.out:Ljava/io/PrintStream;
      14: iload_1
      15: invokevirtual #37                 // Method java/io/PrintStream.println:(I)V
      18: aload_0
      19: invokestatic  #40                 // Method part2:(Ljava/util/List;)I
      22: istore_1
      23: getstatic     #31                 // Field java/lang/System.out:Ljava/io/PrintStream;
      26: iload_1
      27: invokevirtual #37                 // Method java/io/PrintStream.println:(I)V
      30: return

  public static final int part1(java.util.List<java.lang.String>);
    Code:
       0: aload_0
       1: ldc           #44                 // String input
       3: invokestatic  #50                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: iconst_1
       8: invokestatic  #56                 // Method kotlin/collections/CollectionsKt.dropLast:(Ljava/util/List;I)Ljava/util/List;
      11: invokestatic  #40                 // Method part2:(Ljava/util/List;)I
      14: ireturn

  public static final int part2(java.util.List<java.lang.String>);
    Code:
       0: aload_0
       1: ldc           #44                 // String input
       3: invokestatic  #50                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: checkcast     #58                 // class java/lang/Iterable
      10: astore_2
      11: iconst_0
      12: istore_3
      13: aload_2
      14: astore        4
      16: new           #60                 // class java/util/ArrayList
      19: dup
      20: aload_2
      21: bipush        10
      23: invokestatic  #64                 // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
      26: invokespecial #67                 // Method java/util/ArrayList."<init>":(I)V
      29: checkcast     #69                 // class java/util/Collection
      32: astore        5
      34: iconst_0
      35: istore        6
      37: aload         4
      39: invokeinterface #73,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      44: astore        7
      46: aload         7
      48: invokeinterface #79,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      53: ifeq          96
      56: aload         7
      58: invokeinterface #83,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      63: astore        8
      65: aload         5
      67: aload         8
      69: checkcast     #85                 // class java/lang/String
      72: astore        9
      74: astore        11
      76: iconst_0
      77: istore        10
      79: aload         9
      81: invokestatic  #89                 // Method toDisc:(Ljava/lang/String;)Lyear_2016/day15/Disc;
      84: aload         11
      86: swap
      87: invokeinterface #93,  2           // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
      92: pop
      93: goto          46
      96: aload         5
      98: checkcast     #95                 // class java/util/List
     101: nop
     102: astore_1
     103: iconst_0
     104: istore_2
     105: nop
     106: aload_1
     107: checkcast     #58                 // class java/lang/Iterable
     110: astore_3
     111: iconst_0
     112: istore        4
     114: aload_3
     115: instanceof    #69                 // class java/util/Collection
     118: ifeq          137
     121: aload_3
     122: checkcast     #69                 // class java/util/Collection
     125: invokeinterface #98,  1           // InterfaceMethod java/util/Collection.isEmpty:()Z
     130: ifeq          137
     133: iconst_1
     134: goto          209
     137: aload_3
     138: invokeinterface #73,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     143: astore        5
     145: aload         5
     147: invokeinterface #79,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     152: ifeq          208
     155: aload         5
     157: invokeinterface #83,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     162: astore        6
     164: aload         6
     166: checkcast     #100                // class year_2016/day15/Disc
     169: astore        7
     171: iconst_0
     172: istore        8
     174: aload         7
     176: invokevirtual #104                // Method year_2016/day15/Disc.getLayer:()I
     179: aload         7
     181: invokevirtual #107                // Method year_2016/day15/Disc.getStartingPosition:()I
     184: iadd
     185: iload_2
     186: iadd
     187: aload         7
     189: invokevirtual #110                // Method year_2016/day15/Disc.getPositions:()I
     192: irem
     193: ifne          200
     196: iconst_1
     197: goto          201
     200: iconst_0
     201: ifne          145
     204: iconst_0
     205: goto          209
     208: iconst_1
     209: ifeq          214
     212: iload_2
     213: ireturn
     214: iload_2
     215: istore_3
     216: iload_3
     217: iconst_1
     218: iadd
     219: istore_2
     220: goto          105

  public static final year_2016.day15.Disc toDisc(java.lang.String);
    Code:
       0: aload_0
       1: ldc           #133                // String <this>
       3: invokestatic  #50                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: getstatic     #11                 // Field reg:Lkotlin/text/Regex;
       9: aload_0
      10: checkcast     #135                // class java/lang/CharSequence
      13: iconst_0
      14: iconst_2
      15: aconst_null
      16: invokestatic  #141                // Method kotlin/text/Regex.findAll$default:(Lkotlin/text/Regex;Ljava/lang/CharSequence;IILjava/lang/Object;)Lkotlin/sequences/Sequence;
      19: invokestatic  #147                // Method kotlin/sequences/SequencesKt.toList:(Lkotlin/sequences/Sequence;)Ljava/util/List;
      22: astore_1
      23: aload_1
      24: iconst_0
      25: invokeinterface #151,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      30: checkcast     #153                // class kotlin/text/MatchResult
      33: astore_2
      34: aload_1
      35: iconst_1
      36: invokeinterface #151,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      41: checkcast     #153                // class kotlin/text/MatchResult
      44: astore_3
      45: aload_1
      46: iconst_3
      47: invokeinterface #151,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      52: checkcast     #153                // class kotlin/text/MatchResult
      55: astore        4
      57: new           #100                // class year_2016/day15/Disc
      60: dup
      61: aload_2
      62: invokeinterface #157,  1          // InterfaceMethod kotlin/text/MatchResult.getValue:()Ljava/lang/String;
      67: invokestatic  #163                // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
      70: aload_3
      71: invokeinterface #157,  1          // InterfaceMethod kotlin/text/MatchResult.getValue:()Ljava/lang/String;
      76: invokestatic  #163                // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
      79: aload         4
      81: invokeinterface #157,  1          // InterfaceMethod kotlin/text/MatchResult.getValue:()Ljava/lang/String;
      86: invokestatic  #163                // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
      89: invokespecial #166                // Method year_2016/day15/Disc."<init>":(III)V
      92: areturn

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

  static {};
    Code:
       0: new           #137                // class kotlin/text/Regex
       3: dup
       4: ldc           #179                // String \\b\\d+\\b
       6: invokespecial #182                // Method kotlin/text/Regex."<init>":(Ljava/lang/String;)V
       9: putstatic     #11                 // Field reg:Lkotlin/text/Regex;
      12: return
}


package year_2016.day18

const val input: Row = ".^^.^^^..^.^..^.^^.^^^^.^^.^^...^..^...^^^..^^...^..^^^^^^..^.^^^..^.^^^^.^^^.^...^^^.^^.^^^.^.^^.^."

typealias Row = String

fun main() {
    println(part1())
    println(part2())

}

fun part1(): Int = generateSequence(input) { s -> s.nextRow() }.take(40).sumOf { it.count { c -> c == '.' } }

fun part2(): Int = generateSequence(input) { s -> s.nextRow() }.take(400000).sumOf { it.count { c -> c == '.' } }

fun Row.nextRow(): Row {
    return this.mapIndexed { index, c ->
        if (leftIsTrap(index) && centerIsTrap(index) && !rightIsTrap(index)) '^'
        else if (!leftIsTrap(index) && centerIsTrap(index) && rightIsTrap(index)) '^'
        else if (leftIsTrap(index) && !centerIsTrap(index) && !rightIsTrap(index)) '^'
        else if (!leftIsTrap(index) && !centerIsTrap(index) && rightIsTrap(index)) '^'
        else '.'
    }.joinToString("")
}

fun Row.leftIsTrap(i: Int) = i > 0 && this[i - 1] == '^'
fun Row.rightIsTrap(i: Int) = i < length - 1 && this[i + 1] == '^'
fun Row.centerIsTrap(i: Int) = this[i] == '^'

Compiled from "day18.kt"
public final class year_2016.day18.Day18Kt {
  public static final java.lang.String input;

  public static final void main();
    Code:
       0: invokestatic  #10                 // Method part1:()I
       3: istore_0
       4: getstatic     #16                 // Field java/lang/System.out:Ljava/io/PrintStream;
       7: iload_0
       8: invokevirtual #22                 // Method java/io/PrintStream.println:(I)V
      11: invokestatic  #25                 // Method part2:()I
      14: istore_0
      15: getstatic     #16                 // Field java/lang/System.out:Ljava/io/PrintStream;
      18: iload_0
      19: invokevirtual #22                 // Method java/io/PrintStream.println:(I)V
      22: return

  public static final int part1();
    Code:
       0: ldc           #27                 // String .^^.^^^..^.^..^.^^.^^^^.^^.^^...^..^...^^^..^^...^..^^^^^^..^.^^^..^.^^^^.^^^.^...^^^.^^.^^^.^.^^.^.
       2: invokedynamic #46,  0             // InvokeDynamic #0:invoke:()Lkotlin/jvm/functions/Function1;
       7: invokestatic  #52                 // Method kotlin/sequences/SequencesKt.generateSequence:(Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Lkotlin/sequences/Sequence;
      10: bipush        40
      12: invokestatic  #56                 // Method kotlin/sequences/SequencesKt.take:(Lkotlin/sequences/Sequence;I)Lkotlin/sequences/Sequence;
      15: astore_0
      16: iconst_0
      17: istore_1
      18: aload_0
      19: invokeinterface #62,  1           // InterfaceMethod kotlin/sequences/Sequence.iterator:()Ljava/util/Iterator;
      24: astore_2
      25: aload_2
      26: invokeinterface #68,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      31: ifeq          137
      34: aload_2
      35: invokeinterface #72,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      40: astore_3
      41: iload_1
      42: aload_3
      43: checkcast     #74                 // class java/lang/String
      46: astore        4
      48: istore        13
      50: iconst_0
      51: istore        5
      53: aload         4
      55: checkcast     #76                 // class java/lang/CharSequence
      58: astore        6
      60: iconst_0
      61: istore        7
      63: iconst_0
      64: istore        8
      66: iconst_0
      67: istore        9
      69: iload         9
      71: aload         6
      73: invokeinterface #79,  1           // InterfaceMethod java/lang/CharSequence.length:()I
      78: if_icmpge     123
      81: aload         6
      83: iload         9
      85: invokeinterface #83,  2           // InterfaceMethod java/lang/CharSequence.charAt:(I)C
      90: istore        10
      92: iload         10
      94: istore        11
      96: iconst_0
      97: istore        12
      99: iload         11
     101: bipush        46
     103: if_icmpne     110
     106: iconst_1
     107: goto          111
     110: iconst_0
     111: ifeq          117
     114: iinc          8, 1
     117: iinc          9, 1
     120: goto          69
     123: iload         8
     125: nop
     126: istore        14
     128: iload         13
     130: iload         14
     132: iadd
     133: istore_1
     134: goto          25
     137: iload_1
     138: ireturn

  public static final int part2();
    Code:
       0: ldc           #27                 // String .^^.^^^..^.^..^.^^.^^^^.^^.^^...^..^...^^^..^^...^..^^^^^^..^.^^^..^.^^^^.^^^.^...^^^.^^.^^^.^.^^.^.
       2: invokedynamic #100,  0            // InvokeDynamic #1:invoke:()Lkotlin/jvm/functions/Function1;
       7: invokestatic  #52                 // Method kotlin/sequences/SequencesKt.generateSequence:(Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Lkotlin/sequences/Sequence;
      10: ldc           #101                // int 400000
      12: invokestatic  #56                 // Method kotlin/sequences/SequencesKt.take:(Lkotlin/sequences/Sequence;I)Lkotlin/sequences/Sequence;
      15: astore_0
      16: iconst_0
      17: istore_1
      18: aload_0
      19: invokeinterface #62,  1           // InterfaceMethod kotlin/sequences/Sequence.iterator:()Ljava/util/Iterator;
      24: astore_2
      25: aload_2
      26: invokeinterface #68,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      31: ifeq          137
      34: aload_2
      35: invokeinterface #72,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      40: astore_3
      41: iload_1
      42: aload_3
      43: checkcast     #74                 // class java/lang/String
      46: astore        4
      48: istore        13
      50: iconst_0
      51: istore        5
      53: aload         4
      55: checkcast     #76                 // class java/lang/CharSequence
      58: astore        6
      60: iconst_0
      61: istore        7
      63: iconst_0
      64: istore        8
      66: iconst_0
      67: istore        9
      69: iload         9
      71: aload         6
      73: invokeinterface #79,  1           // InterfaceMethod java/lang/CharSequence.length:()I
      78: if_icmpge     123
      81: aload         6
      83: iload         9
      85: invokeinterface #83,  2           // InterfaceMethod java/lang/CharSequence.charAt:(I)C
      90: istore        10
      92: iload         10
      94: istore        11
      96: iconst_0
      97: istore        12
      99: iload         11
     101: bipush        46
     103: if_icmpne     110
     106: iconst_1
     107: goto          111
     110: iconst_0
     111: ifeq          117
     114: iinc          8, 1
     117: iinc          9, 1
     120: goto          69
     123: iload         8
     125: nop
     126: istore        14
     128: iload         13
     130: iload         14
     132: iadd
     133: istore_1
     134: goto          25
     137: iload_1
     138: ireturn

  public static final java.lang.String nextRow(java.lang.String);
    Code:
       0: aload_0
       1: ldc           #107                // String <this>
       3: invokestatic  #113                // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: checkcast     #76                 // class java/lang/CharSequence
      10: astore_1
      11: iconst_0
      12: istore_2
      13: aload_1
      14: astore_3
      15: new           #115                // class java/util/ArrayList
      18: dup
      19: aload_1
      20: invokeinterface #79,  1           // InterfaceMethod java/lang/CharSequence.length:()I
      25: invokespecial #118                // Method java/util/ArrayList."<init>":(I)V
      28: checkcast     #120                // class java/util/Collection
      31: astore        4
      33: iconst_0
      34: istore        5
      36: iconst_0
      37: istore        6
      39: iconst_0
      40: istore        7
      42: iload         7
      44: aload_3
      45: invokeinterface #79,  1           // InterfaceMethod java/lang/CharSequence.length:()I
      50: if_icmpge     229
      53: aload_3
      54: iload         7
      56: invokeinterface #83,  2           // InterfaceMethod java/lang/CharSequence.charAt:(I)C
      61: istore        8
      63: aload         4
      65: iload         6
      67: iinc          6, 1
      70: iload         8
      72: istore        9
      74: istore        10
      76: astore        12
      78: iconst_0
      79: istore        11
      81: aload_0
      82: iload         10
      84: invokestatic  #124                // Method leftIsTrap:(Ljava/lang/String;I)Z
      87: ifeq          113
      90: aload_0
      91: iload         10
      93: invokestatic  #127                // Method centerIsTrap:(Ljava/lang/String;I)Z
      96: ifeq          113
      99: aload_0
     100: iload         10
     102: invokestatic  #130                // Method rightIsTrap:(Ljava/lang/String;I)Z
     105: ifne          113
     108: bipush        94
     110: goto          211
     113: aload_0
     114: iload         10
     116: invokestatic  #124                // Method leftIsTrap:(Ljava/lang/String;I)Z
     119: ifne          145
     122: aload_0
     123: iload         10
     125: invokestatic  #127                // Method centerIsTrap:(Ljava/lang/String;I)Z
     128: ifeq          145
     131: aload_0
     132: iload         10
     134: invokestatic  #130                // Method rightIsTrap:(Ljava/lang/String;I)Z
     137: ifeq          145
     140: bipush        94
     142: goto          211
     145: aload_0
     146: iload         10
     148: invokestatic  #124                // Method leftIsTrap:(Ljava/lang/String;I)Z
     151: ifeq          177
     154: aload_0
     155: iload         10
     157: invokestatic  #127                // Method centerIsTrap:(Ljava/lang/String;I)Z
     160: ifne          177
     163: aload_0
     164: iload         10
     166: invokestatic  #130                // Method rightIsTrap:(Ljava/lang/String;I)Z
     169: ifne          177
     172: bipush        94
     174: goto          211
     177: aload_0
     178: iload         10
     180: invokestatic  #124                // Method leftIsTrap:(Ljava/lang/String;I)Z
     183: ifne          209
     186: aload_0
     187: iload         10
     189: invokestatic  #127                // Method centerIsTrap:(Ljava/lang/String;I)Z
     192: ifne          209
     195: aload_0
     196: iload         10
     198: invokestatic  #130                // Method rightIsTrap:(Ljava/lang/String;I)Z
     201: ifeq          209
     204: bipush        94
     206: goto          211
     209: bipush        46
     211: invokestatic  #136                // Method java/lang/Character.valueOf:(C)Ljava/lang/Character;
     214: aload         12
     216: swap
     217: invokeinterface #140,  2          // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     222: pop
     223: iinc          7, 1
     226: goto          42
     229: aload         4
     231: checkcast     #142                // class java/util/List
     234: nop
     235: checkcast     #144                // class java/lang/Iterable
     238: ldc           #146                // String
     240: checkcast     #76                 // class java/lang/CharSequence
     243: aconst_null
     244: aconst_null
     245: iconst_0
     246: aconst_null
     247: aconst_null
     248: bipush        62
     250: aconst_null
     251: invokestatic  #152                // Method kotlin/collections/CollectionsKt.joinToString$default:(Ljava/lang/Iterable;Ljava/lang/CharSequence;Ljava/lang/CharSequence;Ljava/lang/CharSequence;ILjava/lang/CharSequence;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Ljava/lang/String;
     254: areturn

  public static final boolean leftIsTrap(java.lang.String, int);
    Code:
       0: aload_0
       1: ldc           #107                // String <this>
       3: invokestatic  #113                // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: iload_1
       7: ifle          26
      10: aload_0
      11: iload_1
      12: iconst_1
      13: isub
      14: invokevirtual #164                // Method java/lang/String.charAt:(I)C
      17: bipush        94
      19: if_icmpne     26
      22: iconst_1
      23: goto          27
      26: iconst_0
      27: ireturn

  public static final boolean rightIsTrap(java.lang.String, int);
    Code:
       0: aload_0
       1: ldc           #107                // String <this>
       3: invokestatic  #113                // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: iload_1
       7: aload_0
       8: invokevirtual #167                // Method java/lang/String.length:()I
      11: iconst_1
      12: isub
      13: if_icmpge     32
      16: aload_0
      17: iload_1
      18: iconst_1
      19: iadd
      20: invokevirtual #164                // Method java/lang/String.charAt:(I)C
      23: bipush        94
      25: if_icmpne     32
      28: iconst_1
      29: goto          33
      32: iconst_0
      33: ireturn

  public static final boolean centerIsTrap(java.lang.String, int);
    Code:
       0: aload_0
       1: ldc           #107                // String <this>
       3: invokestatic  #113                // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: iload_1
       8: invokevirtual #164                // Method java/lang/String.charAt:(I)C
      11: bipush        94
      13: if_icmpne     20
      16: iconst_1
      17: goto          21
      20: iconst_0
      21: ireturn

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

  private static final java.lang.String part1$lambda$0(java.lang.String);
    Code:
       0: aload_0
       1: ldc           #176                // String s
       3: invokestatic  #113                // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: invokestatic  #178                // Method nextRow:(Ljava/lang/String;)Ljava/lang/String;
      10: areturn

  private static final java.lang.String part2$lambda$3(java.lang.String);
    Code:
       0: aload_0
       1: ldc           #176                // String s
       3: invokestatic  #113                // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: invokestatic  #178                // Method nextRow:(Ljava/lang/String;)Ljava/lang/String;
      10: areturn
}


package year_2018.day18

import Point
import findPattern
import readInput

typealias Forest = Map<Point, Char>

private const val TREE = '|'
private const val OPEN = '.'
private const val LUMBER_YARD = '#'

fun main() {
    val input = readInput("main/year_2018/day18/Day18")
    println(part1(input))
    println(part2(input))
}

fun part1(input: List<String>): Int {
    val forest = input.parse()
    return forest.resourceValueList(11).last()
}

fun part2(input: List<String>): Int {
    val stepCount = 1000000000
    val forest = input.parse()
    val resourceValueList = forest.resourceValueList()
    val (stepsBeforePattern, patternSize) = resourceValueList.findPattern()

    val indexInCycle = (stepCount - stepsBeforePattern) % patternSize
    return resourceValueList.drop(stepsBeforePattern)[indexInCycle]
}

fun Forest.step(): Forest = this.keys.associateWith { here ->
    when (this[here]) {
        OPEN -> if (this.countNeighborsOfKind(here, TREE) >= 3) TREE else OPEN
        TREE -> if (this.countNeighborsOfKind(here, LUMBER_YARD) >= 3) LUMBER_YARD else TREE
        LUMBER_YARD -> if (countNeighborsOfKind(here, LUMBER_YARD) > 0 && countNeighborsOfKind(here, TREE) > 0) LUMBER_YARD else OPEN
        else -> error("Something went wrong: $here, ${this[here]}")
    }
}

private fun Forest.resourceValue(): Int = count { it.value == LUMBER_YARD } * count { it.value == TREE }

fun Forest.resourceValueList(n: Int = 600): List<Int> = generateSequence(this) { it.step() }.take(n).map { it.resourceValue() }.toList()

fun Forest.countNeighborsOfKind(p: Point, c: Char) = p.allNeighbours().count { this[it] == c }

fun List<String>.parse(): Forest = mutableMapOf<Point, Char>().apply {
    forEachIndexed { y, line ->
        line.forEachIndexed { x, c ->
            this[Point(x, y)] = c
        }
    }
}

Compiled from "day18.kt"
public final class year_2018.day18.Day18Kt {
  private static final char TREE;

  private static final char OPEN;

  private static final char LUMBER_YARD;

  public static final void main();
    Code:
       0: ldc           #8                  // String main/year_2018/day18/Day18
       2: invokestatic  #14                 // Method UtilsKt.readInput:(Ljava/lang/String;)Ljava/util/List;
       5: astore_0
       6: aload_0
       7: invokestatic  #18                 // Method part1:(Ljava/util/List;)I
      10: istore_1
      11: getstatic     #24                 // Field java/lang/System.out:Ljava/io/PrintStream;
      14: iload_1
      15: invokevirtual #30                 // Method java/io/PrintStream.println:(I)V
      18: aload_0
      19: invokestatic  #33                 // Method part2:(Ljava/util/List;)I
      22: istore_1
      23: getstatic     #24                 // Field java/lang/System.out:Ljava/io/PrintStream;
      26: iload_1
      27: invokevirtual #30                 // Method java/io/PrintStream.println:(I)V
      30: return

  public static final int part1(java.util.List<java.lang.String>);
    Code:
       0: aload_0
       1: ldc           #38                 // String input
       3: invokestatic  #44                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: invokestatic  #48                 // Method parse:(Ljava/util/List;)Ljava/util/Map;
      10: astore_1
      11: aload_1
      12: bipush        11
      14: invokestatic  #52                 // Method resourceValueList:(Ljava/util/Map;I)Ljava/util/List;
      17: invokestatic  #58                 // Method kotlin/collections/CollectionsKt.last:(Ljava/util/List;)Ljava/lang/Object;
      20: checkcast     #60                 // class java/lang/Number
      23: invokevirtual #64                 // Method java/lang/Number.intValue:()I
      26: ireturn

  public static final int part2(java.util.List<java.lang.String>);
    Code:
       0: aload_0
       1: ldc           #38                 // String input
       3: invokestatic  #44                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: ldc           #67                 // int 1000000000
       8: istore_1
       9: aload_0
      10: invokestatic  #48                 // Method parse:(Ljava/util/List;)Ljava/util/Map;
      13: astore_2
      14: aload_2
      15: iconst_0
      16: iconst_1
      17: aconst_null
      18: invokestatic  #71                 // Method resourceValueList$default:(Ljava/util/Map;IILjava/lang/Object;)Ljava/util/List;
      21: astore_3
      22: aload_3
      23: iconst_0
      24: iconst_1
      25: aconst_null
      26: invokestatic  #75                 // Method UtilsKt.findPattern$default:(Ljava/util/List;IILjava/lang/Object;)Lkotlin/Pair;
      29: astore        4
      31: aload         4
      33: invokevirtual #81                 // Method kotlin/Pair.component1:()Ljava/lang/Object;
      36: checkcast     #60                 // class java/lang/Number
      39: invokevirtual #64                 // Method java/lang/Number.intValue:()I
      42: istore        5
      44: aload         4
      46: invokevirtual #84                 // Method kotlin/Pair.component2:()Ljava/lang/Object;
      49: checkcast     #60                 // class java/lang/Number
      52: invokevirtual #64                 // Method java/lang/Number.intValue:()I
      55: istore        6
      57: iload_1
      58: iload         5
      60: isub
      61: iload         6
      63: irem
      64: istore        7
      66: aload_3
      67: checkcast     #86                 // class java/lang/Iterable
      70: iload         5
      72: invokestatic  #90                 // Method kotlin/collections/CollectionsKt.drop:(Ljava/lang/Iterable;I)Ljava/util/List;
      75: iload         7
      77: invokeinterface #96,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      82: checkcast     #60                 // class java/lang/Number
      85: invokevirtual #64                 // Method java/lang/Number.intValue:()I
      88: ireturn

  public static final java.util.Map<Point, java.lang.Character> step(java.util.Map<Point, java.lang.Character>);
    Code:
       0: aload_0
       1: ldc           #106                // String <this>
       3: invokestatic  #44                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: invokeinterface #112,  1          // InterfaceMethod java/util/Map.keySet:()Ljava/util/Set;
      12: checkcast     #86                 // class java/lang/Iterable
      15: astore_1
      16: iconst_0
      17: istore_2
      18: new           #114                // class java/util/LinkedHashMap
      21: dup
      22: aload_1
      23: bipush        10
      25: invokestatic  #118                // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
      28: invokestatic  #124                // Method kotlin/collections/MapsKt.mapCapacity:(I)I
      31: bipush        16
      33: invokestatic  #130                // Method kotlin/ranges/RangesKt.coerceAtLeast:(II)I
      36: invokespecial #133                // Method java/util/LinkedHashMap."<init>":(I)V
      39: astore_3
      40: aload_1
      41: astore        4
      43: iconst_0
      44: istore        5
      46: aload         4
      48: invokeinterface #137,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      53: astore        6
      55: aload         6
      57: invokeinterface #143,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
      62: ifeq          317
      65: aload         6
      67: invokeinterface #146,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      72: astore        7
      74: aload_3
      75: checkcast     #108                // class java/util/Map
      78: aload         7
      80: aload         7
      82: checkcast     #148                // class Point
      85: astore        8
      87: astore        13
      89: astore        12
      91: iconst_0
      92: istore        9
      94: aload_0
      95: aload         8
      97: invokeinterface #151,  2          // InterfaceMethod java/util/Map.get:(Ljava/lang/Object;)Ljava/lang/Object;
     102: checkcast     #153                // class java/lang/Character
     105: astore        10
     107: aload         10
     109: bipush        46
     111: istore        11
     113: dup
     114: ifnonnull     121
     117: pop
     118: goto          151
     121: invokevirtual #157                // Method java/lang/Character.charValue:()C
     124: iload         11
     126: if_icmpne     151
     129: aload_0
     130: aload         8
     132: bipush        124
     134: invokestatic  #161                // Method countNeighborsOfKind:(Ljava/util/Map;LPoint;C)I
     137: iconst_3
     138: if_icmplt     146
     141: bipush        124
     143: goto          296
     146: bipush        46
     148: goto          296
     151: aload         10
     153: bipush        124
     155: istore        11
     157: dup
     158: ifnonnull     165
     161: pop
     162: goto          195
     165: invokevirtual #157                // Method java/lang/Character.charValue:()C
     168: iload         11
     170: if_icmpne     195
     173: aload_0
     174: aload         8
     176: bipush        35
     178: invokestatic  #161                // Method countNeighborsOfKind:(Ljava/util/Map;LPoint;C)I
     181: iconst_3
     182: if_icmplt     190
     185: bipush        35
     187: goto          296
     190: bipush        124
     192: goto          296
     195: aload         10
     197: bipush        35
     199: istore        11
     201: dup
     202: ifnonnull     209
     205: pop
     206: goto          249
     209: invokevirtual #157                // Method java/lang/Character.charValue:()C
     212: iload         11
     214: if_icmpne     249
     217: aload_0
     218: aload         8
     220: bipush        35
     222: invokestatic  #161                // Method countNeighborsOfKind:(Ljava/util/Map;LPoint;C)I
     225: ifle          244
     228: aload_0
     229: aload         8
     231: bipush        124
     233: invokestatic  #161                // Method countNeighborsOfKind:(Ljava/util/Map;LPoint;C)I
     236: ifle          244
     239: bipush        35
     241: goto          296
     244: bipush        46
     246: goto          296
     249: new           #163                // class java/lang/IllegalStateException
     252: dup
     253: new           #165                // class java/lang/StringBuilder
     256: dup
     257: invokespecial #167                // Method java/lang/StringBuilder."<init>":()V
     260: ldc           #169                // String Something went wrong:
     262: invokevirtual #173                // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     265: aload         8
     267: invokevirtual #176                // Method java/lang/StringBuilder.append:(Ljava/lang/Object;)Ljava/lang/StringBuilder;
     270: ldc           #178                // String ,
     272: invokevirtual #173                // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     275: aload_0
     276: aload         8
     278: invokeinterface #151,  2          // InterfaceMethod java/util/Map.get:(Ljava/lang/Object;)Ljava/lang/Object;
     283: invokevirtual #176                // Method java/lang/StringBuilder.append:(Ljava/lang/Object;)Ljava/lang/StringBuilder;
     286: invokevirtual #182                // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
     289: invokevirtual #183                // Method java/lang/Object.toString:()Ljava/lang/String;
     292: invokespecial #186                // Method java/lang/IllegalStateException."<init>":(Ljava/lang/String;)V
     295: athrow
     296: nop
     297: invokestatic  #190                // Method java/lang/Character.valueOf:(C)Ljava/lang/Character;
     300: astore        14
     302: aload         12
     304: aload         13
     306: aload         14
     308: invokeinterface #194,  3          // InterfaceMethod java/util/Map.put:(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
     313: pop
     314: goto          55
     317: aload_3
     318: checkcast     #108                // class java/util/Map
     321: nop
     322: areturn

  private static final int resourceValue(java.util.Map<Point, java.lang.Character>);
    Code:
       0: aload_0
       1: astore_1
       2: iconst_0
       3: istore_2
       4: aload_1
       5: invokeinterface #213,  1          // InterfaceMethod java/util/Map.isEmpty:()Z
      10: ifeq          17
      13: iconst_0
      14: goto          94
      17: iconst_0
      18: istore_3
      19: aload_1
      20: invokeinterface #216,  1          // InterfaceMethod java/util/Map.entrySet:()Ljava/util/Set;
      25: invokeinterface #219,  1          // InterfaceMethod java/util/Set.iterator:()Ljava/util/Iterator;
      30: astore        4
      32: aload         4
      34: invokeinterface #143,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
      39: ifeq          93
      42: aload         4
      44: invokeinterface #146,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      49: checkcast     #221                // class java/util/Map$Entry
      52: astore        5
      54: aload         5
      56: astore        6
      58: iconst_0
      59: istore        7
      61: aload         6
      63: invokeinterface #224,  1          // InterfaceMethod java/util/Map$Entry.getValue:()Ljava/lang/Object;
      68: checkcast     #153                // class java/lang/Character
      71: invokevirtual #157                // Method java/lang/Character.charValue:()C
      74: bipush        35
      76: if_icmpne     83
      79: iconst_1
      80: goto          84
      83: iconst_0
      84: ifeq          32
      87: iinc          3, 1
      90: goto          32
      93: iload_3
      94: aload_0
      95: astore_1
      96: istore        8
      98: iconst_0
      99: istore_2
     100: aload_1
     101: invokeinterface #213,  1          // InterfaceMethod java/util/Map.isEmpty:()Z
     106: ifeq          113
     109: iconst_0
     110: goto          190
     113: iconst_0
     114: istore_3
     115: aload_1
     116: invokeinterface #216,  1          // InterfaceMethod java/util/Map.entrySet:()Ljava/util/Set;
     121: invokeinterface #219,  1          // InterfaceMethod java/util/Set.iterator:()Ljava/util/Iterator;
     126: astore        4
     128: aload         4
     130: invokeinterface #143,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
     135: ifeq          189
     138: aload         4
     140: invokeinterface #146,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     145: checkcast     #221                // class java/util/Map$Entry
     148: astore        5
     150: aload         5
     152: astore        6
     154: iconst_0
     155: istore        7
     157: aload         6
     159: invokeinterface #224,  1          // InterfaceMethod java/util/Map$Entry.getValue:()Ljava/lang/Object;
     164: checkcast     #153                // class java/lang/Character
     167: invokevirtual #157                // Method java/lang/Character.charValue:()C
     170: bipush        124
     172: if_icmpne     179
     175: iconst_1
     176: goto          180
     179: iconst_0
     180: ifeq          128
     183: iinc          3, 1
     186: goto          128
     189: iload_3
     190: istore        9
     192: iload         8
     194: iload         9
     196: imul
     197: ireturn

  public static final java.util.List<java.lang.Integer> resourceValueList(java.util.Map<Point, java.lang.Character>, int);
    Code:
       0: aload_0
       1: ldc           #106                // String <this>
       3: invokestatic  #44                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: invokedynamic #251,  0            // InvokeDynamic #0:invoke:()Lkotlin/jvm/functions/Function1;
      12: invokestatic  #257                // Method kotlin/sequences/SequencesKt.generateSequence:(Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Lkotlin/sequences/Sequence;
      15: iload_1
      16: invokestatic  #261                // Method kotlin/sequences/SequencesKt.take:(Lkotlin/sequences/Sequence;I)Lkotlin/sequences/Sequence;
      19: invokedynamic #268,  0            // InvokeDynamic #1:invoke:()Lkotlin/jvm/functions/Function1;
      24: invokestatic  #272                // Method kotlin/sequences/SequencesKt.map:(Lkotlin/sequences/Sequence;Lkotlin/jvm/functions/Function1;)Lkotlin/sequences/Sequence;
      27: invokestatic  #276                // Method kotlin/sequences/SequencesKt.toList:(Lkotlin/sequences/Sequence;)Ljava/util/List;
      30: areturn

  public static java.util.List resourceValueList$default(java.util.Map, int, int, java.lang.Object);
    Code:
       0: iload_2
       1: iconst_1
       2: iand
       3: ifeq          10
       6: sipush        600
       9: istore_1
      10: aload_0
      11: iload_1
      12: invokestatic  #52                 // Method resourceValueList:(Ljava/util/Map;I)Ljava/util/List;
      15: areturn

  public static final int countNeighborsOfKind(java.util.Map<Point, java.lang.Character>, Point, char);
    Code:
       0: aload_0
       1: ldc           #106                // String <this>
       3: invokestatic  #44                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_1
       7: ldc_w         #281                // String p
      10: invokestatic  #44                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
      13: aload_1
      14: invokevirtual #284                // Method Point.allNeighbours:()Ljava/util/Set;
      17: checkcast     #86                 // class java/lang/Iterable
      20: astore_3
      21: iconst_0
      22: istore        4
      24: aload_3
      25: instanceof    #286                // class java/util/Collection
      28: ifeq          47
      31: aload_3
      32: checkcast     #286                // class java/util/Collection
      35: invokeinterface #287,  1          // InterfaceMethod java/util/Collection.isEmpty:()Z
      40: ifeq          47
      43: iconst_0
      44: goto          141
      47: iconst_0
      48: istore        5
      50: aload_3
      51: invokeinterface #137,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      56: astore        6
      58: aload         6
      60: invokeinterface #143,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
      65: ifeq          139
      68: aload         6
      70: invokeinterface #146,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      75: astore        7
      77: aload         7
      79: checkcast     #148                // class Point
      82: astore        8
      84: iconst_0
      85: istore        9
      87: aload_0
      88: aload         8
      90: invokeinterface #151,  2          // InterfaceMethod java/util/Map.get:(Ljava/lang/Object;)Ljava/lang/Object;
      95: checkcast     #153                // class java/lang/Character
      98: iload_2
      99: istore        10
     101: dup
     102: ifnonnull     109
     105: pop
     106: goto          121
     109: invokevirtual #157                // Method java/lang/Character.charValue:()C
     112: iload         10
     114: if_icmpne     121
     117: iconst_1
     118: goto          122
     121: iconst_0
     122: ifeq          58
     125: iinc          5, 1
     128: iload         5
     130: ifge          58
     133: invokestatic  #290                // Method kotlin/collections/CollectionsKt.throwCountOverflow:()V
     136: goto          58
     139: iload         5
     141: ireturn

  public static final java.util.Map<Point, java.lang.Character> parse(java.util.List<java.lang.String>);
    Code:
       0: aload_0
       1: ldc           #106                // String <this>
       3: invokestatic  #44                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: new           #114                // class java/util/LinkedHashMap
       9: dup
      10: invokespecial #296                // Method java/util/LinkedHashMap."<init>":()V
      13: checkcast     #108                // class java/util/Map
      16: astore_1
      17: aload_1
      18: astore_2
      19: iconst_0
      20: istore_3
      21: aload_0
      22: checkcast     #86                 // class java/lang/Iterable
      25: astore        4
      27: iconst_0
      28: istore        5
      30: iconst_0
      31: istore        6
      33: aload         4
      35: invokeinterface #137,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      40: astore        7
      42: aload         7
      44: invokeinterface #143,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
      49: ifeq          184
      52: aload         7
      54: invokeinterface #146,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      59: astore        8
      61: iload         6
      63: iinc          6, 1
      66: istore        9
      68: iload         9
      70: ifge          76
      73: invokestatic  #299                // Method kotlin/collections/CollectionsKt.throwIndexOverflow:()V
      76: iload         9
      78: aload         8
      80: checkcast     #301                // class java/lang/String
      83: astore        10
      85: istore        11
      87: iconst_0
      88: istore        12
      90: aload         10
      92: checkcast     #303                // class java/lang/CharSequence
      95: astore        13
      97: iconst_0
      98: istore        14
     100: iconst_0
     101: istore        15
     103: iconst_0
     104: istore        16
     106: iload         16
     108: aload         13
     110: invokeinterface #306,  1          // InterfaceMethod java/lang/CharSequence.length:()I
     115: if_icmpge     178
     118: aload         13
     120: iload         16
     122: invokeinterface #310,  2          // InterfaceMethod java/lang/CharSequence.charAt:(I)C
     127: istore        17
     129: iload         15
     131: iinc          15, 1
     134: iload         17
     136: istore        18
     138: istore        19
     140: iconst_0
     141: istore        20
     143: iload         18
     145: invokestatic  #190                // Method java/lang/Character.valueOf:(C)Ljava/lang/Character;
     148: astore        21
     150: aload_2
     151: new           #148                // class Point
     154: dup
     155: iload         19
     157: iload         11
     159: invokespecial #313                // Method Point."<init>":(II)V
     162: aload         21
     164: invokeinterface #194,  3          // InterfaceMethod java/util/Map.put:(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
     169: pop
     170: nop
     171: nop
     172: iinc          16, 1
     175: goto          106
     178: nop
     179: nop
     180: nop
     181: goto          42
     184: nop
     185: nop
     186: aload_1
     187: areturn

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

  private static final java.util.Map resourceValueList$lambda$3(java.util.Map);
    Code:
       0: aload_0
       1: ldc_w         #333                // String it
       4: invokestatic  #44                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       7: aload_0
       8: invokestatic  #335                // Method step:(Ljava/util/Map;)Ljava/util/Map;
      11: areturn

  private static final int resourceValueList$lambda$4(java.util.Map);
    Code:
       0: aload_0
       1: ldc_w         #333                // String it
       4: invokestatic  #44                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       7: aload_0
       8: invokestatic  #337                // Method resourceValue:(Ljava/util/Map;)I
      11: ireturn
}