package net.sinceat.aoc2020.day12

import kotlin.math.abs

fun main() {
    println(readInstructions("testinput.txt").calculateManhattanDistancePart1())
    println(readInstructions("input.txt").calculateManhattanDistancePart1())

    println(readInstructions("testinput.txt").calculateManhattanDistancePart2())
    println(readInstructions("input.txt").calculateManhattanDistancePart2())
}

fun List<Instruction>.calculateManhattanDistancePart1(): Int {
    var facing = 90
    var xDistance = 0
    var yDistance = 0
    for (instruction in this) {
        when (instruction) {
            is Forward -> when (facing) {
                0 -> yDistance += instruction.distance
                90 -> xDistance += instruction.distance
                180 -> yDistance -= instruction.distance
                270 -> xDistance -= instruction.distance
            }
            is TurnLeft -> {
                val rawFacing = facing - instruction.degrees
                facing = if (rawFacing < 0) {
                    rawFacing + 360
                } else rawFacing
            }
            is TurnRight -> {
                val rawFacing = facing + instruction.degrees
                facing = if (rawFacing >= 360) {
                    rawFacing % 360
                } else rawFacing
            }
            is North -> yDistance += instruction.distance
            is East -> xDistance += instruction.distance
            is South -> yDistance -= instruction.distance
            is West -> xDistance -= instruction.distance
        }
    }
    return abs(xDistance) + abs(yDistance)
}

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

fun Point.north(amount: Int) = Point(x, y + amount)
fun Point.east(amount: Int) = Point(x + amount, y)
fun Point.south(amount: Int) = Point(x, y - amount)
fun Point.west(amount: Int) = Point(x - amount, y)
fun Point.rotateLeft() = Point(-y, x)
fun Point.rotateRight() = Point(y, -x)

fun List<Instruction>.calculateManhattanDistancePart2(): Int {
    var waypoint = Point(10, 1)
    var xDistance = 0
    var yDistance = 0
    for (instruction in this) {
        when (instruction) {
            is Forward -> {
                xDistance += waypoint.x * instruction.distance
                yDistance += waypoint.y * instruction.distance
            }
            is TurnLeft -> repeat(instruction.degrees / 90) {
                waypoint = waypoint.rotateLeft()
            }
            is TurnRight -> repeat(instruction.degrees / 90) {
                waypoint = waypoint.rotateRight()
            }
            is North -> waypoint = waypoint.north(instruction.distance)
            is East -> waypoint = waypoint.east(instruction.distance)
            is South -> waypoint = waypoint.south(instruction.distance)
            is West -> waypoint = waypoint.west(instruction.distance)
        }
    }
    return abs(xDistance) + abs(yDistance)
}

sealed class Instruction

class Forward(val distance: Int) : Instruction()

class TurnLeft(val degrees: Int) : Instruction()

class TurnRight(val degrees: Int) : Instruction()

class North(val distance: Int) : Instruction()

class East(val distance: Int) : Instruction()

class South(val distance: Int) : Instruction()

class West(val distance: Int) : Instruction()

fun readInstructions(fileName: String): List<Instruction> {
    return ClassLoader.getSystemResourceAsStream(fileName)!!.bufferedReader().useLines { lines ->
        lines.filter(String::isNotBlank).map { line ->
            val instructionType = line[0]
            val instructionArgument = line.removePrefix(instructionType.toString()).toInt()
            when (instructionType) {
                'F' -> Forward(instructionArgument)
                'L' -> TurnLeft(instructionArgument)
                'R' -> TurnRight(instructionArgument)
                'N' -> North(instructionArgument)
                'E' -> East(instructionArgument)
                'S' -> South(instructionArgument)
                'W' -> West(instructionArgument)
                else -> TODO("unhandled instruction type '$instructionType'")
            }
        }.toList()
    }
}

Compiled from "App.kt"
final class net.sinceat.aoc2020.day12.AppKt$readInstructions$1$1 extends kotlin.jvm.internal.FunctionReferenceImpl implements kotlin.jvm.functions.Function1<java.lang.String, java.lang.Boolean> {
  public static final net.sinceat.aoc2020.day12.AppKt$readInstructions$1$1 INSTANCE;

  net.sinceat.aoc2020.day12.AppKt$readInstructions$1$1();
    Code:
       0: aload_0
       1: iconst_1
       2: ldc           #11                 // class kotlin/text/StringsKt
       4: ldc           #13                 // String isNotBlank
       6: ldc           #15                 // String isNotBlank(Ljava/lang/CharSequence;)Z
       8: iconst_1
       9: invokespecial #18                 // Method kotlin/jvm/internal/FunctionReferenceImpl."<init>":(ILjava/lang/Class;Ljava/lang/String;Ljava/lang/String;I)V
      12: return

  public final java.lang.Boolean invoke(java.lang.String);
    Code:
       0: aload_1
       1: ldc           #24                 // String p0
       3: invokestatic  #30                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_1
       7: checkcast     #32                 // class java/lang/CharSequence
      10: invokestatic  #36                 // Method kotlin/text/StringsKt.isBlank:(Ljava/lang/CharSequence;)Z
      13: ifne          20
      16: iconst_1
      17: goto          21
      20: iconst_0
      21: invokestatic  #42                 // Method java/lang/Boolean.valueOf:(Z)Ljava/lang/Boolean;
      24: areturn

  public java.lang.Object invoke(java.lang.Object);
    Code:
       0: aload_0
       1: aload_1
       2: checkcast     #46                 // class java/lang/String
       5: invokevirtual #48                 // Method invoke:(Ljava/lang/String;)Ljava/lang/Boolean;
       8: areturn

  static {};
    Code:
       0: new           #2                  // class net/sinceat/aoc2020/day12/AppKt$readInstructions$1$1
       3: dup
       4: invokespecial #53                 // Method "<init>":()V
       7: putstatic     #56                 // Field INSTANCE:Lnet/sinceat/aoc2020/day12/AppKt$readInstructions$1$1;
      10: return
}

Compiled from "App.kt"
public final class net.sinceat.aoc2020.day12.AppKt {
  public static final void main();
    Code:
       0: ldc           #8                  // String testinput.txt
       2: invokestatic  #12                 // Method readInstructions:(Ljava/lang/String;)Ljava/util/List;
       5: invokestatic  #16                 // Method calculateManhattanDistancePart1:(Ljava/util/List;)I
       8: istore_0
       9: getstatic     #22                 // Field java/lang/System.out:Ljava/io/PrintStream;
      12: iload_0
      13: invokevirtual #28                 // Method java/io/PrintStream.println:(I)V
      16: ldc           #30                 // String input.txt
      18: invokestatic  #12                 // Method readInstructions:(Ljava/lang/String;)Ljava/util/List;
      21: invokestatic  #16                 // Method calculateManhattanDistancePart1:(Ljava/util/List;)I
      24: istore_0
      25: getstatic     #22                 // Field java/lang/System.out:Ljava/io/PrintStream;
      28: iload_0
      29: invokevirtual #28                 // Method java/io/PrintStream.println:(I)V
      32: ldc           #8                  // String testinput.txt
      34: invokestatic  #12                 // Method readInstructions:(Ljava/lang/String;)Ljava/util/List;
      37: invokestatic  #33                 // Method calculateManhattanDistancePart2:(Ljava/util/List;)I
      40: istore_0
      41: getstatic     #22                 // Field java/lang/System.out:Ljava/io/PrintStream;
      44: iload_0
      45: invokevirtual #28                 // Method java/io/PrintStream.println:(I)V
      48: ldc           #30                 // String input.txt
      50: invokestatic  #12                 // Method readInstructions:(Ljava/lang/String;)Ljava/util/List;
      53: invokestatic  #33                 // Method calculateManhattanDistancePart2:(Ljava/util/List;)I
      56: istore_0
      57: getstatic     #22                 // Field java/lang/System.out:Ljava/io/PrintStream;
      60: iload_0
      61: invokevirtual #28                 // Method java/io/PrintStream.println:(I)V
      64: return

  public static final int calculateManhattanDistancePart1(java.util.List<? extends net.sinceat.aoc2020.day12.Instruction>);
    Code:
       0: aload_0
       1: ldc           #37                 // String <this>
       3: invokestatic  #43                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: bipush        90
       8: istore_1
       9: iconst_0
      10: istore_2
      11: iconst_0
      12: istore_3
      13: aload_0
      14: invokeinterface #49,  1           // InterfaceMethod java/util/List.iterator:()Ljava/util/Iterator;
      19: astore        4
      21: aload         4
      23: invokeinterface #55,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      28: ifeq          335
      31: aload         4
      33: invokeinterface #59,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      38: checkcast     #61                 // class net/sinceat/aoc2020/day12/Instruction
      41: astore        5
      43: aload         5
      45: astore        6
      47: aload         6
      49: instanceof    #63                 // class net/sinceat/aoc2020/day12/Forward
      52: ifeq          156
      55: iload_1
      56: lookupswitch  { // 4
                     0: 100
                    90: 114
                   180: 128
                   270: 142
               default: 153
          }
     100: iload_3
     101: aload         5
     103: checkcast     #63                 // class net/sinceat/aoc2020/day12/Forward
     106: invokevirtual #67                 // Method net/sinceat/aoc2020/day12/Forward.getDistance:()I
     109: iadd
     110: istore_3
     111: goto          21
     114: iload_2
     115: aload         5
     117: checkcast     #63                 // class net/sinceat/aoc2020/day12/Forward
     120: invokevirtual #67                 // Method net/sinceat/aoc2020/day12/Forward.getDistance:()I
     123: iadd
     124: istore_2
     125: goto          21
     128: iload_3
     129: aload         5
     131: checkcast     #63                 // class net/sinceat/aoc2020/day12/Forward
     134: invokevirtual #67                 // Method net/sinceat/aoc2020/day12/Forward.getDistance:()I
     137: isub
     138: istore_3
     139: goto          21
     142: iload_2
     143: aload         5
     145: checkcast     #63                 // class net/sinceat/aoc2020/day12/Forward
     148: invokevirtual #67                 // Method net/sinceat/aoc2020/day12/Forward.getDistance:()I
     151: isub
     152: istore_2
     153: goto          21
     156: aload         6
     158: instanceof    #69                 // class net/sinceat/aoc2020/day12/TurnLeft
     161: ifeq          196
     164: iload_1
     165: aload         5
     167: checkcast     #69                 // class net/sinceat/aoc2020/day12/TurnLeft
     170: invokevirtual #72                 // Method net/sinceat/aoc2020/day12/TurnLeft.getDegrees:()I
     173: isub
     174: istore        7
     176: iload         7
     178: ifge          190
     181: iload         7
     183: sipush        360
     186: iadd
     187: goto          192
     190: iload         7
     192: istore_1
     193: goto          21
     196: aload         6
     198: instanceof    #74                 // class net/sinceat/aoc2020/day12/TurnRight
     201: ifeq          239
     204: iload_1
     205: aload         5
     207: checkcast     #74                 // class net/sinceat/aoc2020/day12/TurnRight
     210: invokevirtual #75                 // Method net/sinceat/aoc2020/day12/TurnRight.getDegrees:()I
     213: iadd
     214: istore        7
     216: iload         7
     218: sipush        360
     221: if_icmplt     233
     224: iload         7
     226: sipush        360
     229: irem
     230: goto          235
     233: iload         7
     235: istore_1
     236: goto          21
     239: aload         6
     241: instanceof    #77                 // class net/sinceat/aoc2020/day12/North
     244: ifeq          261
     247: iload_3
     248: aload         5
     250: checkcast     #77                 // class net/sinceat/aoc2020/day12/North
     253: invokevirtual #78                 // Method net/sinceat/aoc2020/day12/North.getDistance:()I
     256: iadd
     257: istore_3
     258: goto          21
     261: aload         6
     263: instanceof    #80                 // class net/sinceat/aoc2020/day12/East
     266: ifeq          283
     269: iload_2
     270: aload         5
     272: checkcast     #80                 // class net/sinceat/aoc2020/day12/East
     275: invokevirtual #81                 // Method net/sinceat/aoc2020/day12/East.getDistance:()I
     278: iadd
     279: istore_2
     280: goto          21
     283: aload         6
     285: instanceof    #83                 // class net/sinceat/aoc2020/day12/South
     288: ifeq          305
     291: iload_3
     292: aload         5
     294: checkcast     #83                 // class net/sinceat/aoc2020/day12/South
     297: invokevirtual #84                 // Method net/sinceat/aoc2020/day12/South.getDistance:()I
     300: isub
     301: istore_3
     302: goto          21
     305: aload         6
     307: instanceof    #86                 // class net/sinceat/aoc2020/day12/West
     310: ifeq          327
     313: iload_2
     314: aload         5
     316: checkcast     #86                 // class net/sinceat/aoc2020/day12/West
     319: invokevirtual #87                 // Method net/sinceat/aoc2020/day12/West.getDistance:()I
     322: isub
     323: istore_2
     324: goto          21
     327: new           #89                 // class kotlin/NoWhenBranchMatchedException
     330: dup
     331: invokespecial #92                 // Method kotlin/NoWhenBranchMatchedException."<init>":()V
     334: athrow
     335: iload_2
     336: invokestatic  #98                 // Method java/lang/Math.abs:(I)I
     339: iload_3
     340: invokestatic  #98                 // Method java/lang/Math.abs:(I)I
     343: iadd
     344: ireturn

  public static final net.sinceat.aoc2020.day12.Point north(net.sinceat.aoc2020.day12.Point, int);
    Code:
       0: aload_0
       1: ldc           #37                 // String <this>
       3: invokestatic  #43                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: new           #111                // class net/sinceat/aoc2020/day12/Point
       9: dup
      10: aload_0
      11: invokevirtual #114                // Method net/sinceat/aoc2020/day12/Point.getX:()I
      14: aload_0
      15: invokevirtual #117                // Method net/sinceat/aoc2020/day12/Point.getY:()I
      18: iload_1
      19: iadd
      20: invokespecial #120                // Method net/sinceat/aoc2020/day12/Point."<init>":(II)V
      23: areturn

  public static final net.sinceat.aoc2020.day12.Point east(net.sinceat.aoc2020.day12.Point, int);
    Code:
       0: aload_0
       1: ldc           #37                 // String <this>
       3: invokestatic  #43                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: new           #111                // class net/sinceat/aoc2020/day12/Point
       9: dup
      10: aload_0
      11: invokevirtual #114                // Method net/sinceat/aoc2020/day12/Point.getX:()I
      14: iload_1
      15: iadd
      16: aload_0
      17: invokevirtual #117                // Method net/sinceat/aoc2020/day12/Point.getY:()I
      20: invokespecial #120                // Method net/sinceat/aoc2020/day12/Point."<init>":(II)V
      23: areturn

  public static final net.sinceat.aoc2020.day12.Point south(net.sinceat.aoc2020.day12.Point, int);
    Code:
       0: aload_0
       1: ldc           #37                 // String <this>
       3: invokestatic  #43                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: new           #111                // class net/sinceat/aoc2020/day12/Point
       9: dup
      10: aload_0
      11: invokevirtual #114                // Method net/sinceat/aoc2020/day12/Point.getX:()I
      14: aload_0
      15: invokevirtual #117                // Method net/sinceat/aoc2020/day12/Point.getY:()I
      18: iload_1
      19: isub
      20: invokespecial #120                // Method net/sinceat/aoc2020/day12/Point."<init>":(II)V
      23: areturn

  public static final net.sinceat.aoc2020.day12.Point west(net.sinceat.aoc2020.day12.Point, int);
    Code:
       0: aload_0
       1: ldc           #37                 // String <this>
       3: invokestatic  #43                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: new           #111                // class net/sinceat/aoc2020/day12/Point
       9: dup
      10: aload_0
      11: invokevirtual #114                // Method net/sinceat/aoc2020/day12/Point.getX:()I
      14: iload_1
      15: isub
      16: aload_0
      17: invokevirtual #117                // Method net/sinceat/aoc2020/day12/Point.getY:()I
      20: invokespecial #120                // Method net/sinceat/aoc2020/day12/Point."<init>":(II)V
      23: areturn

  public static final net.sinceat.aoc2020.day12.Point rotateLeft(net.sinceat.aoc2020.day12.Point);
    Code:
       0: aload_0
       1: ldc           #37                 // String <this>
       3: invokestatic  #43                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: new           #111                // class net/sinceat/aoc2020/day12/Point
       9: dup
      10: aload_0
      11: invokevirtual #117                // Method net/sinceat/aoc2020/day12/Point.getY:()I
      14: ineg
      15: aload_0
      16: invokevirtual #114                // Method net/sinceat/aoc2020/day12/Point.getX:()I
      19: invokespecial #120                // Method net/sinceat/aoc2020/day12/Point."<init>":(II)V
      22: areturn

  public static final net.sinceat.aoc2020.day12.Point rotateRight(net.sinceat.aoc2020.day12.Point);
    Code:
       0: aload_0
       1: ldc           #37                 // String <this>
       3: invokestatic  #43                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: new           #111                // class net/sinceat/aoc2020/day12/Point
       9: dup
      10: aload_0
      11: invokevirtual #117                // Method net/sinceat/aoc2020/day12/Point.getY:()I
      14: aload_0
      15: invokevirtual #114                // Method net/sinceat/aoc2020/day12/Point.getX:()I
      18: ineg
      19: invokespecial #120                // Method net/sinceat/aoc2020/day12/Point."<init>":(II)V
      22: areturn

  public static final int calculateManhattanDistancePart2(java.util.List<? extends net.sinceat.aoc2020.day12.Instruction>);
    Code:
       0: aload_0
       1: ldc           #37                 // String <this>
       3: invokestatic  #43                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aconst_null
       7: astore_1
       8: new           #111                // class net/sinceat/aoc2020/day12/Point
      11: dup
      12: bipush        10
      14: iconst_1
      15: invokespecial #120                // Method net/sinceat/aoc2020/day12/Point."<init>":(II)V
      18: astore_1
      19: iconst_0
      20: istore_2
      21: iconst_0
      22: istore_3
      23: aload_0
      24: invokeinterface #49,  1           // InterfaceMethod java/util/List.iterator:()Ljava/util/Iterator;
      29: astore        4
      31: aload         4
      33: invokeinterface #55,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      38: ifeq          304
      41: aload         4
      43: invokeinterface #59,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      48: checkcast     #61                 // class net/sinceat/aoc2020/day12/Instruction
      51: astore        5
      53: aload         5
      55: astore        6
      57: aload         6
      59: instanceof    #63                 // class net/sinceat/aoc2020/day12/Forward
      62: ifeq          100
      65: iload_2
      66: aload_1
      67: invokevirtual #114                // Method net/sinceat/aoc2020/day12/Point.getX:()I
      70: aload         5
      72: checkcast     #63                 // class net/sinceat/aoc2020/day12/Forward
      75: invokevirtual #67                 // Method net/sinceat/aoc2020/day12/Forward.getDistance:()I
      78: imul
      79: iadd
      80: istore_2
      81: iload_3
      82: aload_1
      83: invokevirtual #117                // Method net/sinceat/aoc2020/day12/Point.getY:()I
      86: aload         5
      88: checkcast     #63                 // class net/sinceat/aoc2020/day12/Forward
      91: invokevirtual #67                 // Method net/sinceat/aoc2020/day12/Forward.getDistance:()I
      94: imul
      95: iadd
      96: istore_3
      97: goto          31
     100: aload         6
     102: instanceof    #69                 // class net/sinceat/aoc2020/day12/TurnLeft
     105: ifeq          150
     108: aload         5
     110: checkcast     #69                 // class net/sinceat/aoc2020/day12/TurnLeft
     113: invokevirtual #72                 // Method net/sinceat/aoc2020/day12/TurnLeft.getDegrees:()I
     116: bipush        90
     118: idiv
     119: istore        7
     121: iconst_0
     122: istore        8
     124: iload         8
     126: iload         7
     128: if_icmpge     31
     131: iload         8
     133: istore        9
     135: iconst_0
     136: istore        10
     138: aload_1
     139: invokestatic  #136                // Method rotateLeft:(Lnet/sinceat/aoc2020/day12/Point;)Lnet/sinceat/aoc2020/day12/Point;
     142: astore_1
     143: nop
     144: iinc          8, 1
     147: goto          124
     150: aload         6
     152: instanceof    #74                 // class net/sinceat/aoc2020/day12/TurnRight
     155: ifeq          200
     158: aload         5
     160: checkcast     #74                 // class net/sinceat/aoc2020/day12/TurnRight
     163: invokevirtual #75                 // Method net/sinceat/aoc2020/day12/TurnRight.getDegrees:()I
     166: bipush        90
     168: idiv
     169: istore        7
     171: iconst_0
     172: istore        8
     174: iload         8
     176: iload         7
     178: if_icmpge     31
     181: iload         8
     183: istore        9
     185: iconst_0
     186: istore        10
     188: aload_1
     189: invokestatic  #138                // Method rotateRight:(Lnet/sinceat/aoc2020/day12/Point;)Lnet/sinceat/aoc2020/day12/Point;
     192: astore_1
     193: nop
     194: iinc          8, 1
     197: goto          174
     200: aload         6
     202: instanceof    #77                 // class net/sinceat/aoc2020/day12/North
     205: ifeq          224
     208: aload_1
     209: aload         5
     211: checkcast     #77                 // class net/sinceat/aoc2020/day12/North
     214: invokevirtual #78                 // Method net/sinceat/aoc2020/day12/North.getDistance:()I
     217: invokestatic  #140                // Method north:(Lnet/sinceat/aoc2020/day12/Point;I)Lnet/sinceat/aoc2020/day12/Point;
     220: astore_1
     221: goto          31
     224: aload         6
     226: instanceof    #80                 // class net/sinceat/aoc2020/day12/East
     229: ifeq          248
     232: aload_1
     233: aload         5
     235: checkcast     #80                 // class net/sinceat/aoc2020/day12/East
     238: invokevirtual #81                 // Method net/sinceat/aoc2020/day12/East.getDistance:()I
     241: invokestatic  #142                // Method east:(Lnet/sinceat/aoc2020/day12/Point;I)Lnet/sinceat/aoc2020/day12/Point;
     244: astore_1
     245: goto          31
     248: aload         6
     250: instanceof    #83                 // class net/sinceat/aoc2020/day12/South
     253: ifeq          272
     256: aload_1
     257: aload         5
     259: checkcast     #83                 // class net/sinceat/aoc2020/day12/South
     262: invokevirtual #84                 // Method net/sinceat/aoc2020/day12/South.getDistance:()I
     265: invokestatic  #144                // Method south:(Lnet/sinceat/aoc2020/day12/Point;I)Lnet/sinceat/aoc2020/day12/Point;
     268: astore_1
     269: goto          31
     272: aload         6
     274: instanceof    #86                 // class net/sinceat/aoc2020/day12/West
     277: ifeq          296
     280: aload_1
     281: aload         5
     283: checkcast     #86                 // class net/sinceat/aoc2020/day12/West
     286: invokevirtual #87                 // Method net/sinceat/aoc2020/day12/West.getDistance:()I
     289: invokestatic  #146                // Method west:(Lnet/sinceat/aoc2020/day12/Point;I)Lnet/sinceat/aoc2020/day12/Point;
     292: astore_1
     293: goto          31
     296: new           #89                 // class kotlin/NoWhenBranchMatchedException
     299: dup
     300: invokespecial #92                 // Method kotlin/NoWhenBranchMatchedException."<init>":()V
     303: athrow
     304: iload_2
     305: invokestatic  #98                 // Method java/lang/Math.abs:(I)I
     308: iload_3
     309: invokestatic  #98                 // Method java/lang/Math.abs:(I)I
     312: iadd
     313: ireturn

  public static final java.util.List<net.sinceat.aoc2020.day12.Instruction> readInstructions(java.lang.String);
    Code:
       0: aload_0
       1: ldc           #157                // String fileName
       3: invokestatic  #43                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: invokestatic  #163                // Method java/lang/ClassLoader.getSystemResourceAsStream:(Ljava/lang/String;)Ljava/io/InputStream;
      10: dup
      11: invokestatic  #167                // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
      14: astore_1
      15: getstatic     #173                // Field kotlin/text/Charsets.UTF_8:Ljava/nio/charset/Charset;
      18: astore_2
      19: new           #175                // class java/io/InputStreamReader
      22: dup
      23: aload_1
      24: aload_2
      25: invokespecial #178                // Method java/io/InputStreamReader."<init>":(Ljava/io/InputStream;Ljava/nio/charset/Charset;)V
      28: checkcast     #180                // class java/io/Reader
      31: astore_3
      32: sipush        8192
      35: istore        4
      37: aload_3
      38: instanceof    #182                // class java/io/BufferedReader
      41: ifeq          51
      44: aload_3
      45: checkcast     #182                // class java/io/BufferedReader
      48: goto          61
      51: new           #182                // class java/io/BufferedReader
      54: dup
      55: aload_3
      56: iload         4
      58: invokespecial #185                // Method java/io/BufferedReader."<init>":(Ljava/io/Reader;I)V
      61: checkcast     #180                // class java/io/Reader
      64: astore_1
      65: nop
      66: iconst_0
      67: istore_2
      68: aload_1
      69: astore_3
      70: sipush        8192
      73: istore        4
      75: aload_3
      76: instanceof    #182                // class java/io/BufferedReader
      79: ifeq          89
      82: aload_3
      83: checkcast     #182                // class java/io/BufferedReader
      86: goto          99
      89: new           #182                // class java/io/BufferedReader
      92: dup
      93: aload_3
      94: iload         4
      96: invokespecial #185                // Method java/io/BufferedReader."<init>":(Ljava/io/Reader;I)V
      99: checkcast     #187                // class java/io/Closeable
     102: astore_3
     103: aconst_null
     104: astore        4
     106: nop
     107: aload_3
     108: checkcast     #182                // class java/io/BufferedReader
     111: astore        5
     113: iconst_0
     114: istore        6
     116: aload         5
     118: invokestatic  #193                // Method kotlin/io/TextStreamsKt.lineSequence:(Ljava/io/BufferedReader;)Lkotlin/sequences/Sequence;
     121: astore        7
     123: iconst_0
     124: istore        8
     126: aload         7
     128: getstatic     #199                // Field net/sinceat/aoc2020/day12/AppKt$readInstructions$1$1.INSTANCE:Lnet/sinceat/aoc2020/day12/AppKt$readInstructions$1$1;
     131: checkcast     #201                // class kotlin/jvm/functions/Function1
     134: invokestatic  #207                // Method kotlin/sequences/SequencesKt.filter:(Lkotlin/sequences/Sequence;Lkotlin/jvm/functions/Function1;)Lkotlin/sequences/Sequence;
     137: invokedynamic #226,  0            // InvokeDynamic #0:invoke:()Lkotlin/jvm/functions/Function1;
     142: invokestatic  #229                // Method kotlin/sequences/SequencesKt.map:(Lkotlin/sequences/Sequence;Lkotlin/jvm/functions/Function1;)Lkotlin/sequences/Sequence;
     145: invokestatic  #233                // Method kotlin/sequences/SequencesKt.toList:(Lkotlin/sequences/Sequence;)Ljava/util/List;
     148: astore        5
     150: aload_3
     151: aload         4
     153: invokestatic  #239                // Method kotlin/io/CloseableKt.closeFinally:(Ljava/io/Closeable;Ljava/lang/Throwable;)V
     156: aload         5
     158: goto          181
     161: astore        6
     163: aload         6
     165: astore        4
     167: aload         6
     169: athrow
     170: astore        6
     172: aload_3
     173: aload         4
     175: invokestatic  #239                // Method kotlin/io/CloseableKt.closeFinally:(Ljava/io/Closeable;Ljava/lang/Throwable;)V
     178: aload         6
     180: athrow
     181: nop
     182: areturn
    Exception table:
       from    to  target type
         106   150   161   Class java/lang/Throwable
         106   150   170   any
         161   170   170   any
         170   172   170   any

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

  private static final net.sinceat.aoc2020.day12.Instruction readInstructions$lambda$3$lambda$2(java.lang.String);
    Code:
       0: aload_0
       1: ldc_w         #264                // String line
       4: invokestatic  #43                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       7: aload_0
       8: iconst_0
       9: invokevirtual #268                // Method java/lang/String.charAt:(I)C
      12: istore_1
      13: aload_0
      14: iload_1
      15: invokestatic  #272                // Method java/lang/String.valueOf:(C)Ljava/lang/String;
      18: checkcast     #274                // class java/lang/CharSequence
      21: invokestatic  #280                // Method kotlin/text/StringsKt.removePrefix:(Ljava/lang/String;Ljava/lang/CharSequence;)Ljava/lang/String;
      24: invokestatic  #286                // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
      27: istore_2
      28: iload_1
      29: lookupswitch  { // 7
                    69: 152
                    70: 96
                    76: 110
                    78: 138
                    82: 124
                    83: 166
                    87: 180
               default: 194
          }
      96: new           #63                 // class net/sinceat/aoc2020/day12/Forward
      99: dup
     100: iload_2
     101: invokespecial #288                // Method net/sinceat/aoc2020/day12/Forward."<init>":(I)V
     104: checkcast     #61                 // class net/sinceat/aoc2020/day12/Instruction
     107: goto          248
     110: new           #69                 // class net/sinceat/aoc2020/day12/TurnLeft
     113: dup
     114: iload_2
     115: invokespecial #289                // Method net/sinceat/aoc2020/day12/TurnLeft."<init>":(I)V
     118: checkcast     #61                 // class net/sinceat/aoc2020/day12/Instruction
     121: goto          248
     124: new           #74                 // class net/sinceat/aoc2020/day12/TurnRight
     127: dup
     128: iload_2
     129: invokespecial #290                // Method net/sinceat/aoc2020/day12/TurnRight."<init>":(I)V
     132: checkcast     #61                 // class net/sinceat/aoc2020/day12/Instruction
     135: goto          248
     138: new           #77                 // class net/sinceat/aoc2020/day12/North
     141: dup
     142: iload_2
     143: invokespecial #291                // Method net/sinceat/aoc2020/day12/North."<init>":(I)V
     146: checkcast     #61                 // class net/sinceat/aoc2020/day12/Instruction
     149: goto          248
     152: new           #80                 // class net/sinceat/aoc2020/day12/East
     155: dup
     156: iload_2
     157: invokespecial #292                // Method net/sinceat/aoc2020/day12/East."<init>":(I)V
     160: checkcast     #61                 // class net/sinceat/aoc2020/day12/Instruction
     163: goto          248
     166: new           #83                 // class net/sinceat/aoc2020/day12/South
     169: dup
     170: iload_2
     171: invokespecial #293                // Method net/sinceat/aoc2020/day12/South."<init>":(I)V
     174: checkcast     #61                 // class net/sinceat/aoc2020/day12/Instruction
     177: goto          248
     180: new           #86                 // class net/sinceat/aoc2020/day12/West
     183: dup
     184: iload_2
     185: invokespecial #294                // Method net/sinceat/aoc2020/day12/West."<init>":(I)V
     188: checkcast     #61                 // class net/sinceat/aoc2020/day12/Instruction
     191: goto          248
     194: new           #296                // class java/lang/StringBuilder
     197: dup
     198: invokespecial #297                // Method java/lang/StringBuilder."<init>":()V
     201: ldc_w         #299                // String unhandled instruction type \'
     204: invokevirtual #303                // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     207: iload_1
     208: invokevirtual #306                // Method java/lang/StringBuilder.append:(C)Ljava/lang/StringBuilder;
     211: bipush        39
     213: invokevirtual #306                // Method java/lang/StringBuilder.append:(C)Ljava/lang/StringBuilder;
     216: invokevirtual #310                // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
     219: astore_3
     220: new           #312                // class kotlin/NotImplementedError
     223: dup
     224: new           #296                // class java/lang/StringBuilder
     227: dup
     228: invokespecial #297                // Method java/lang/StringBuilder."<init>":()V
     231: ldc_w         #314                // String An operation is not implemented:
     234: invokevirtual #303                // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     237: aload_3
     238: invokevirtual #303                // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     241: invokevirtual #310                // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
     244: invokespecial #317                // Method kotlin/NotImplementedError."<init>":(Ljava/lang/String;)V
     247: athrow
     248: areturn
}


package net.sinceat.aoc2020.day15

fun main() {
    println("1, 3, 2, ... = ${theElvesGamePart1(listOf(1, 3, 2)).nth(2020)}")
    println("2, 1, 3, ... = ${theElvesGamePart1(listOf(2, 1, 3)).nth(2020)}")
    println("1, 2, 3, ... = ${theElvesGamePart1(listOf(1, 2, 3)).nth(2020)}")
    println("2, 3, 1, ... = ${theElvesGamePart1(listOf(2, 3, 1)).nth(2020)}")
    println("3, 2, 1, ... = ${theElvesGamePart1(listOf(3, 2, 1)).nth(2020)}")
    println("3, 1, 2, ... = ${theElvesGamePart1(listOf(3, 1, 2)).nth(2020)}")

    println("5,2,8,16,18,0,1, ... = ${theElvesGamePart1(listOf(5, 2, 8, 16, 18, 0, 1)).nth(2020)}")
    println("5,2,8,16,18,0,1, ... = ${theElvesGamePart1(listOf(5, 2, 8, 16, 18, 0, 1)).nth(30000000)}")
}

fun theElvesGamePart1(startingNumbers: List<Int>) = sequence {
    val memory = mutableMapOf<Int, Int>()
    startingNumbers.subList(0, startingNumbers.size - 1).forEachIndexed { index, number ->
        yield(number)
        memory[number] = index + 1
    }
    var lastSpokenNumber = startingNumbers.last()
    var turn = startingNumbers.size
    while (true) {
        yield(lastSpokenNumber)
        val nextSpokenNumber = memory[lastSpokenNumber]?.let { turn - it } ?: 0
        memory[lastSpokenNumber] = turn
        turn++
        lastSpokenNumber = nextSpokenNumber
    }
}

fun Sequence<Int>.nth(n: Int): Int {
    return drop(n - 1).first()
}

Compiled from "App.kt"
public final class net.sinceat.aoc2020.day15.AppKt {
  public static final void main();
    Code:
       0: new           #8                  // class java/lang/StringBuilder
       3: dup
       4: invokespecial #11                 // Method java/lang/StringBuilder."<init>":()V
       7: ldc           #13                 // String 1, 3, 2, ... =
       9: invokevirtual #17                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      12: iconst_3
      13: anewarray     #19                 // class java/lang/Integer
      16: astore_0
      17: aload_0
      18: iconst_0
      19: iconst_1
      20: invokestatic  #23                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      23: aastore
      24: aload_0
      25: iconst_1
      26: iconst_3
      27: invokestatic  #23                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      30: aastore
      31: aload_0
      32: iconst_2
      33: iconst_2
      34: invokestatic  #23                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      37: aastore
      38: aload_0
      39: invokestatic  #29                 // Method kotlin/collections/CollectionsKt.listOf:([Ljava/lang/Object;)Ljava/util/List;
      42: invokestatic  #33                 // Method theElvesGamePart1:(Ljava/util/List;)Lkotlin/sequences/Sequence;
      45: sipush        2020
      48: invokestatic  #37                 // Method nth:(Lkotlin/sequences/Sequence;I)I
      51: invokevirtual #40                 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
      54: invokevirtual #44                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
      57: getstatic     #50                 // Field java/lang/System.out:Ljava/io/PrintStream;
      60: swap
      61: invokevirtual #56                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
      64: new           #8                  // class java/lang/StringBuilder
      67: dup
      68: invokespecial #11                 // Method java/lang/StringBuilder."<init>":()V
      71: ldc           #58                 // String 2, 1, 3, ... =
      73: invokevirtual #17                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      76: iconst_3
      77: anewarray     #19                 // class java/lang/Integer
      80: astore_0
      81: aload_0
      82: iconst_0
      83: iconst_2
      84: invokestatic  #23                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      87: aastore
      88: aload_0
      89: iconst_1
      90: iconst_1
      91: invokestatic  #23                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      94: aastore
      95: aload_0
      96: iconst_2
      97: iconst_3
      98: invokestatic  #23                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     101: aastore
     102: aload_0
     103: invokestatic  #29                 // Method kotlin/collections/CollectionsKt.listOf:([Ljava/lang/Object;)Ljava/util/List;
     106: invokestatic  #33                 // Method theElvesGamePart1:(Ljava/util/List;)Lkotlin/sequences/Sequence;
     109: sipush        2020
     112: invokestatic  #37                 // Method nth:(Lkotlin/sequences/Sequence;I)I
     115: invokevirtual #40                 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
     118: invokevirtual #44                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
     121: getstatic     #50                 // Field java/lang/System.out:Ljava/io/PrintStream;
     124: swap
     125: invokevirtual #56                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
     128: new           #8                  // class java/lang/StringBuilder
     131: dup
     132: invokespecial #11                 // Method java/lang/StringBuilder."<init>":()V
     135: ldc           #60                 // String 1, 2, 3, ... =
     137: invokevirtual #17                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     140: iconst_3
     141: anewarray     #19                 // class java/lang/Integer
     144: astore_0
     145: aload_0
     146: iconst_0
     147: iconst_1
     148: invokestatic  #23                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     151: aastore
     152: aload_0
     153: iconst_1
     154: iconst_2
     155: invokestatic  #23                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     158: aastore
     159: aload_0
     160: iconst_2
     161: iconst_3
     162: invokestatic  #23                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     165: aastore
     166: aload_0
     167: invokestatic  #29                 // Method kotlin/collections/CollectionsKt.listOf:([Ljava/lang/Object;)Ljava/util/List;
     170: invokestatic  #33                 // Method theElvesGamePart1:(Ljava/util/List;)Lkotlin/sequences/Sequence;
     173: sipush        2020
     176: invokestatic  #37                 // Method nth:(Lkotlin/sequences/Sequence;I)I
     179: invokevirtual #40                 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
     182: invokevirtual #44                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
     185: getstatic     #50                 // Field java/lang/System.out:Ljava/io/PrintStream;
     188: swap
     189: invokevirtual #56                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
     192: new           #8                  // class java/lang/StringBuilder
     195: dup
     196: invokespecial #11                 // Method java/lang/StringBuilder."<init>":()V
     199: ldc           #62                 // String 2, 3, 1, ... =
     201: invokevirtual #17                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     204: iconst_3
     205: anewarray     #19                 // class java/lang/Integer
     208: astore_0
     209: aload_0
     210: iconst_0
     211: iconst_2
     212: invokestatic  #23                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     215: aastore
     216: aload_0
     217: iconst_1
     218: iconst_3
     219: invokestatic  #23                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     222: aastore
     223: aload_0
     224: iconst_2
     225: iconst_1
     226: invokestatic  #23                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     229: aastore
     230: aload_0
     231: invokestatic  #29                 // Method kotlin/collections/CollectionsKt.listOf:([Ljava/lang/Object;)Ljava/util/List;
     234: invokestatic  #33                 // Method theElvesGamePart1:(Ljava/util/List;)Lkotlin/sequences/Sequence;
     237: sipush        2020
     240: invokestatic  #37                 // Method nth:(Lkotlin/sequences/Sequence;I)I
     243: invokevirtual #40                 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
     246: invokevirtual #44                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
     249: getstatic     #50                 // Field java/lang/System.out:Ljava/io/PrintStream;
     252: swap
     253: invokevirtual #56                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
     256: new           #8                  // class java/lang/StringBuilder
     259: dup
     260: invokespecial #11                 // Method java/lang/StringBuilder."<init>":()V
     263: ldc           #64                 // String 3, 2, 1, ... =
     265: invokevirtual #17                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     268: iconst_3
     269: anewarray     #19                 // class java/lang/Integer
     272: astore_0
     273: aload_0
     274: iconst_0
     275: iconst_3
     276: invokestatic  #23                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     279: aastore
     280: aload_0
     281: iconst_1
     282: iconst_2
     283: invokestatic  #23                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     286: aastore
     287: aload_0
     288: iconst_2
     289: iconst_1
     290: invokestatic  #23                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     293: aastore
     294: aload_0
     295: invokestatic  #29                 // Method kotlin/collections/CollectionsKt.listOf:([Ljava/lang/Object;)Ljava/util/List;
     298: invokestatic  #33                 // Method theElvesGamePart1:(Ljava/util/List;)Lkotlin/sequences/Sequence;
     301: sipush        2020
     304: invokestatic  #37                 // Method nth:(Lkotlin/sequences/Sequence;I)I
     307: invokevirtual #40                 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
     310: invokevirtual #44                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
     313: getstatic     #50                 // Field java/lang/System.out:Ljava/io/PrintStream;
     316: swap
     317: invokevirtual #56                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
     320: new           #8                  // class java/lang/StringBuilder
     323: dup
     324: invokespecial #11                 // Method java/lang/StringBuilder."<init>":()V
     327: ldc           #66                 // String 3, 1, 2, ... =
     329: invokevirtual #17                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     332: iconst_3
     333: anewarray     #19                 // class java/lang/Integer
     336: astore_0
     337: aload_0
     338: iconst_0
     339: iconst_3
     340: invokestatic  #23                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     343: aastore
     344: aload_0
     345: iconst_1
     346: iconst_1
     347: invokestatic  #23                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     350: aastore
     351: aload_0
     352: iconst_2
     353: iconst_2
     354: invokestatic  #23                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     357: aastore
     358: aload_0
     359: invokestatic  #29                 // Method kotlin/collections/CollectionsKt.listOf:([Ljava/lang/Object;)Ljava/util/List;
     362: invokestatic  #33                 // Method theElvesGamePart1:(Ljava/util/List;)Lkotlin/sequences/Sequence;
     365: sipush        2020
     368: invokestatic  #37                 // Method nth:(Lkotlin/sequences/Sequence;I)I
     371: invokevirtual #40                 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
     374: invokevirtual #44                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
     377: getstatic     #50                 // Field java/lang/System.out:Ljava/io/PrintStream;
     380: swap
     381: invokevirtual #56                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
     384: new           #8                  // class java/lang/StringBuilder
     387: dup
     388: invokespecial #11                 // Method java/lang/StringBuilder."<init>":()V
     391: ldc           #68                 // String 5,2,8,16,18,0,1, ... =
     393: invokevirtual #17                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     396: bipush        7
     398: anewarray     #19                 // class java/lang/Integer
     401: astore_0
     402: aload_0
     403: iconst_0
     404: iconst_5
     405: invokestatic  #23                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     408: aastore
     409: aload_0
     410: iconst_1
     411: iconst_2
     412: invokestatic  #23                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     415: aastore
     416: aload_0
     417: iconst_2
     418: bipush        8
     420: invokestatic  #23                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     423: aastore
     424: aload_0
     425: iconst_3
     426: bipush        16
     428: invokestatic  #23                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     431: aastore
     432: aload_0
     433: iconst_4
     434: bipush        18
     436: invokestatic  #23                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     439: aastore
     440: aload_0
     441: iconst_5
     442: iconst_0
     443: invokestatic  #23                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     446: aastore
     447: aload_0
     448: bipush        6
     450: iconst_1
     451: invokestatic  #23                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     454: aastore
     455: aload_0
     456: invokestatic  #29                 // Method kotlin/collections/CollectionsKt.listOf:([Ljava/lang/Object;)Ljava/util/List;
     459: invokestatic  #33                 // Method theElvesGamePart1:(Ljava/util/List;)Lkotlin/sequences/Sequence;
     462: sipush        2020
     465: invokestatic  #37                 // Method nth:(Lkotlin/sequences/Sequence;I)I
     468: invokevirtual #40                 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
     471: invokevirtual #44                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
     474: getstatic     #50                 // Field java/lang/System.out:Ljava/io/PrintStream;
     477: swap
     478: invokevirtual #56                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
     481: new           #8                  // class java/lang/StringBuilder
     484: dup
     485: invokespecial #11                 // Method java/lang/StringBuilder."<init>":()V
     488: ldc           #68                 // String 5,2,8,16,18,0,1, ... =
     490: invokevirtual #17                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     493: bipush        7
     495: anewarray     #19                 // class java/lang/Integer
     498: astore_0
     499: aload_0
     500: iconst_0
     501: iconst_5
     502: invokestatic  #23                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     505: aastore
     506: aload_0
     507: iconst_1
     508: iconst_2
     509: invokestatic  #23                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     512: aastore
     513: aload_0
     514: iconst_2
     515: bipush        8
     517: invokestatic  #23                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     520: aastore
     521: aload_0
     522: iconst_3
     523: bipush        16
     525: invokestatic  #23                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     528: aastore
     529: aload_0
     530: iconst_4
     531: bipush        18
     533: invokestatic  #23                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     536: aastore
     537: aload_0
     538: iconst_5
     539: iconst_0
     540: invokestatic  #23                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     543: aastore
     544: aload_0
     545: bipush        6
     547: iconst_1
     548: invokestatic  #23                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     551: aastore
     552: aload_0
     553: invokestatic  #29                 // Method kotlin/collections/CollectionsKt.listOf:([Ljava/lang/Object;)Ljava/util/List;
     556: invokestatic  #33                 // Method theElvesGamePart1:(Ljava/util/List;)Lkotlin/sequences/Sequence;
     559: ldc           #69                 // int 30000000
     561: invokestatic  #37                 // Method nth:(Lkotlin/sequences/Sequence;I)I
     564: invokevirtual #40                 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
     567: invokevirtual #44                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
     570: getstatic     #50                 // Field java/lang/System.out:Ljava/io/PrintStream;
     573: swap
     574: invokevirtual #56                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
     577: return

  public static final kotlin.sequences.Sequence<java.lang.Integer> theElvesGamePart1(java.util.List<java.lang.Integer>);
    Code:
       0: aload_0
       1: ldc           #73                 // String startingNumbers
       3: invokestatic  #79                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: new           #81                 // class net/sinceat/aoc2020/day15/AppKt$theElvesGamePart1$1
       9: dup
      10: aload_0
      11: aconst_null
      12: invokespecial #84                 // Method net/sinceat/aoc2020/day15/AppKt$theElvesGamePart1$1."<init>":(Ljava/util/List;Lkotlin/coroutines/Continuation;)V
      15: checkcast     #86                 // class kotlin/jvm/functions/Function2
      18: invokestatic  #92                 // Method kotlin/sequences/SequencesKt.sequence:(Lkotlin/jvm/functions/Function2;)Lkotlin/sequences/Sequence;
      21: areturn

  public static final int nth(kotlin.sequences.Sequence<java.lang.Integer>, int);
    Code:
       0: aload_0
       1: ldc           #96                 // String <this>
       3: invokestatic  #79                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: iload_1
       8: iconst_1
       9: isub
      10: invokestatic  #100                // Method kotlin/sequences/SequencesKt.drop:(Lkotlin/sequences/Sequence;I)Lkotlin/sequences/Sequence;
      13: invokestatic  #104                // Method kotlin/sequences/SequencesKt.first:(Lkotlin/sequences/Sequence;)Ljava/lang/Object;
      16: checkcast     #106                // class java/lang/Number
      19: invokevirtual #110                // Method java/lang/Number.intValue:()I
      22: ireturn

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

Compiled from "App.kt"
final class net.sinceat.aoc2020.day15.AppKt$theElvesGamePart1$1 extends kotlin.coroutines.jvm.internal.RestrictedSuspendLambda implements kotlin.jvm.functions.Function2<kotlin.sequences.SequenceScope<? super java.lang.Integer>, kotlin.coroutines.Continuation<? super kotlin.Unit>, java.lang.Object> {
  java.lang.Object L$1;

  java.lang.Object L$2;

  int I$0;

  int I$1;

  int I$2;

  int label;

  private java.lang.Object L$0;

  final java.util.List<java.lang.Integer> $startingNumbers;

  net.sinceat.aoc2020.day15.AppKt$theElvesGamePart1$1(java.util.List<java.lang.Integer>, kotlin.coroutines.Continuation<? super net.sinceat.aoc2020.day15.AppKt$theElvesGamePart1$1>);
    Code:
       0: aload_0
       1: aload_1
       2: putfield      #14                 // Field $startingNumbers:Ljava/util/List;
       5: aload_0
       6: iconst_2
       7: aload_2
       8: invokespecial #17                 // Method kotlin/coroutines/jvm/internal/RestrictedSuspendLambda."<init>":(ILkotlin/coroutines/Continuation;)V
      11: return

  public final java.lang.Object invokeSuspend(java.lang.Object);
    Code:
       0: invokestatic  #58                 // Method kotlin/coroutines/intrinsics/IntrinsicsKt.getCOROUTINE_SUSPENDED:()Ljava/lang/Object;
       3: astore        14
       5: aload_0
       6: getfield      #61                 // Field label:I
       9: tableswitch   { // 0 to 2
                     0: 36
                     1: 209
                     2: 380
               default: 509
          }
      36: aload_1
      37: invokestatic  #67                 // Method kotlin/ResultKt.throwOnFailure:(Ljava/lang/Object;)V
      40: aload_0
      41: getfield      #69                 // Field L$0:Ljava/lang/Object;
      44: checkcast     #71                 // class kotlin/sequences/SequenceScope
      47: astore_2
      48: new           #73                 // class java/util/LinkedHashMap
      51: dup
      52: invokespecial #76                 // Method java/util/LinkedHashMap."<init>":()V
      55: checkcast     #78                 // class java/util/Map
      58: astore_3
      59: aload_0
      60: getfield      #14                 // Field $startingNumbers:Ljava/util/List;
      63: iconst_0
      64: aload_0
      65: getfield      #14                 // Field $startingNumbers:Ljava/util/List;
      68: invokeinterface #84,  1           // InterfaceMethod java/util/List.size:()I
      73: iconst_1
      74: isub
      75: invokeinterface #88,  3           // InterfaceMethod java/util/List.subList:(II)Ljava/util/List;
      80: checkcast     #90                 // class java/lang/Iterable
      83: astore        4
      85: iconst_0
      86: istore        5
      88: iconst_0
      89: istore        6
      91: aload         4
      93: invokeinterface #94,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      98: astore        7
     100: aload         7
     102: invokeinterface #100,  1          // InterfaceMethod java/util/Iterator.hasNext:()Z
     107: ifeq          291
     110: aload         7
     112: invokeinterface #103,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     117: astore        8
     119: iload         6
     121: iinc          6, 1
     124: istore        9
     126: iload         9
     128: ifge          134
     131: invokestatic  #108                // Method kotlin/collections/CollectionsKt.throwIndexOverflow:()V
     134: iload         9
     136: aload         8
     138: checkcast     #110                // class java/lang/Number
     141: invokevirtual #113                // Method java/lang/Number.intValue:()I
     144: istore        10
     146: istore        11
     148: iconst_0
     149: istore        12
     151: aload_2
     152: iload         10
     154: invokestatic  #119                // Method kotlin/coroutines/jvm/internal/Boxing.boxInt:(I)Ljava/lang/Integer;
     157: aload_0
     158: aload_0
     159: aload_2
     160: putfield      #69                 // Field L$0:Ljava/lang/Object;
     163: aload_0
     164: aload_3
     165: putfield      #121                // Field L$1:Ljava/lang/Object;
     168: aload_0
     169: aload         7
     171: putfield      #123                // Field L$2:Ljava/lang/Object;
     174: aload_0
     175: iload         6
     177: putfield      #125                // Field I$0:I
     180: aload_0
     181: iload         10
     183: putfield      #127                // Field I$1:I
     186: aload_0
     187: iload         11
     189: putfield      #129                // Field I$2:I
     192: aload_0
     193: iconst_1
     194: putfield      #61                 // Field label:I
     197: invokevirtual #133                // Method kotlin/sequences/SequenceScope.yield:(Ljava/lang/Object;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
     200: dup
     201: aload         14
     203: if_acmpne     263
     206: aload         14
     208: areturn
     209: iconst_0
     210: istore        5
     212: iconst_0
     213: istore        12
     215: aload_0
     216: getfield      #129                // Field I$2:I
     219: istore        11
     221: aload_0
     222: getfield      #127                // Field I$1:I
     225: istore        10
     227: aload_0
     228: getfield      #125                // Field I$0:I
     231: istore        6
     233: aload_0
     234: getfield      #123                // Field L$2:Ljava/lang/Object;
     237: checkcast     #96                 // class java/util/Iterator
     240: astore        7
     242: aload_0
     243: getfield      #121                // Field L$1:Ljava/lang/Object;
     246: checkcast     #78                 // class java/util/Map
     249: astore_3
     250: aload_0
     251: getfield      #69                 // Field L$0:Ljava/lang/Object;
     254: checkcast     #71                 // class kotlin/sequences/SequenceScope
     257: astore_2
     258: aload_1
     259: invokestatic  #67                 // Method kotlin/ResultKt.throwOnFailure:(Ljava/lang/Object;)V
     262: aload_1
     263: pop
     264: iload         10
     266: invokestatic  #119                // Method kotlin/coroutines/jvm/internal/Boxing.boxInt:(I)Ljava/lang/Integer;
     269: astore        13
     271: aload_3
     272: aload         13
     274: iload         11
     276: iconst_1
     277: iadd
     278: invokestatic  #119                // Method kotlin/coroutines/jvm/internal/Boxing.boxInt:(I)Ljava/lang/Integer;
     281: invokeinterface #137,  3          // InterfaceMethod java/util/Map.put:(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
     286: pop
     287: nop
     288: goto          100
     291: nop
     292: aload_0
     293: getfield      #14                 // Field $startingNumbers:Ljava/util/List;
     296: invokestatic  #141                // Method kotlin/collections/CollectionsKt.last:(Ljava/util/List;)Ljava/lang/Object;
     299: checkcast     #110                // class java/lang/Number
     302: invokevirtual #113                // Method java/lang/Number.intValue:()I
     305: istore        4
     307: new           #143                // class kotlin/jvm/internal/Ref$IntRef
     310: dup
     311: invokespecial #144                // Method kotlin/jvm/internal/Ref$IntRef."<init>":()V
     314: astore        5
     316: aload         5
     318: aload_0
     319: getfield      #14                 // Field $startingNumbers:Ljava/util/List;
     322: invokeinterface #84,  1           // InterfaceMethod java/util/List.size:()I
     327: putfield      #147                // Field kotlin/jvm/internal/Ref$IntRef.element:I
     330: nop
     331: aload_2
     332: iload         4
     334: invokestatic  #119                // Method kotlin/coroutines/jvm/internal/Boxing.boxInt:(I)Ljava/lang/Integer;
     337: aload_0
     338: checkcast     #149                // class kotlin/coroutines/Continuation
     341: aload_0
     342: aload_2
     343: putfield      #69                 // Field L$0:Ljava/lang/Object;
     346: aload_0
     347: aload_3
     348: putfield      #121                // Field L$1:Ljava/lang/Object;
     351: aload_0
     352: aload         5
     354: putfield      #123                // Field L$2:Ljava/lang/Object;
     357: aload_0
     358: iload         4
     360: putfield      #125                // Field I$0:I
     363: aload_0
     364: iconst_2
     365: putfield      #61                 // Field label:I
     368: invokevirtual #133                // Method kotlin/sequences/SequenceScope.yield:(Ljava/lang/Object;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
     371: dup
     372: aload         14
     374: if_acmpne     416
     377: aload         14
     379: areturn
     380: aload_0
     381: getfield      #125                // Field I$0:I
     384: istore        4
     386: aload_0
     387: getfield      #123                // Field L$2:Ljava/lang/Object;
     390: checkcast     #143                // class kotlin/jvm/internal/Ref$IntRef
     393: astore        5
     395: aload_0
     396: getfield      #121                // Field L$1:Ljava/lang/Object;
     399: checkcast     #78                 // class java/util/Map
     402: astore_3
     403: aload_0
     404: getfield      #69                 // Field L$0:Ljava/lang/Object;
     407: checkcast     #71                 // class kotlin/sequences/SequenceScope
     410: astore_2
     411: aload_1
     412: invokestatic  #67                 // Method kotlin/ResultKt.throwOnFailure:(Ljava/lang/Object;)V
     415: aload_1
     416: pop
     417: aload_3
     418: iload         4
     420: invokestatic  #119                // Method kotlin/coroutines/jvm/internal/Boxing.boxInt:(I)Ljava/lang/Integer;
     423: invokeinterface #152,  2          // InterfaceMethod java/util/Map.get:(Ljava/lang/Object;)Ljava/lang/Object;
     428: checkcast     #154                // class java/lang/Integer
     431: dup
     432: ifnull        458
     435: checkcast     #110                // class java/lang/Number
     438: invokevirtual #113                // Method java/lang/Number.intValue:()I
     441: istore        10
     443: iconst_0
     444: istore        11
     446: aload         5
     448: getfield      #147                // Field kotlin/jvm/internal/Ref$IntRef.element:I
     451: iload         10
     453: isub
     454: nop
     455: goto          460
     458: pop
     459: iconst_0
     460: istore        6
     462: iload         4
     464: invokestatic  #119                // Method kotlin/coroutines/jvm/internal/Boxing.boxInt:(I)Ljava/lang/Integer;
     467: astore        8
     469: aload_3
     470: aload         8
     472: aload         5
     474: getfield      #147                // Field kotlin/jvm/internal/Ref$IntRef.element:I
     477: invokestatic  #119                // Method kotlin/coroutines/jvm/internal/Boxing.boxInt:(I)Ljava/lang/Integer;
     480: invokeinterface #137,  3          // InterfaceMethod java/util/Map.put:(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
     485: pop
     486: aload         5
     488: getfield      #147                // Field kotlin/jvm/internal/Ref$IntRef.element:I
     491: istore        7
     493: aload         5
     495: iload         7
     497: iconst_1
     498: iadd
     499: putfield      #147                // Field kotlin/jvm/internal/Ref$IntRef.element:I
     502: iload         6
     504: istore        4
     506: goto          330
     509: new           #156                // class java/lang/IllegalStateException
     512: dup
     513: ldc           #158                // String call to \'resume\' before \'invoke\' with coroutine
     515: invokespecial #161                // Method java/lang/IllegalStateException."<init>":(Ljava/lang/String;)V
     518: athrow

  public final kotlin.coroutines.Continuation<kotlin.Unit> create(java.lang.Object, kotlin.coroutines.Continuation<?>);
    Code:
       0: new           #2                  // class net/sinceat/aoc2020/day15/AppKt$theElvesGamePart1$1
       3: dup
       4: aload_0
       5: getfield      #14                 // Field $startingNumbers:Ljava/util/List;
       8: aload_2
       9: invokespecial #180                // Method "<init>":(Ljava/util/List;Lkotlin/coroutines/Continuation;)V
      12: astore_3
      13: aload_3
      14: aload_1
      15: putfield      #69                 // Field L$0:Ljava/lang/Object;
      18: aload_3
      19: checkcast     #149                // class kotlin/coroutines/Continuation
      22: areturn

  public final java.lang.Object invoke(kotlin.sequences.SequenceScope<? super java.lang.Integer>, kotlin.coroutines.Continuation<? super kotlin.Unit>);
    Code:
       0: aload_0
       1: aload_1
       2: aload_2
       3: invokevirtual #186                // Method create:(Ljava/lang/Object;Lkotlin/coroutines/Continuation;)Lkotlin/coroutines/Continuation;
       6: checkcast     #2                  // class net/sinceat/aoc2020/day15/AppKt$theElvesGamePart1$1
       9: getstatic     #192                // Field kotlin/Unit.INSTANCE:Lkotlin/Unit;
      12: invokevirtual #194                // Method invokeSuspend:(Ljava/lang/Object;)Ljava/lang/Object;
      15: areturn

  public java.lang.Object invoke(java.lang.Object, java.lang.Object);
    Code:
       0: aload_0
       1: aload_1
       2: checkcast     #71                 // class kotlin/sequences/SequenceScope
       5: aload_2
       6: checkcast     #149                // class kotlin/coroutines/Continuation
       9: invokevirtual #198                // Method invoke:(Lkotlin/sequences/SequenceScope;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
      12: areturn
}