package net.sinceat.aoc2020.day14

import kotlin.properties.Delegates

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

    println(part2InstructionProcessor(readInstructions("testinput2.txt")))
    println(part2InstructionProcessor(readInstructions("input.txt")))
}

fun part1InstructionProcessor(instructions: List<Instruction>): Long {
    var currentMask: Mask by Delegates.notNull()
    val memory = mutableMapOf<Long, Long>()
    for (instruction in instructions) {
        when (instruction) {
            is SetMask -> currentMask = instruction.mask
            is WriteMemory -> memory[instruction.address] = currentMask.apply(instruction.value)
        }
    }
    return memory.values.sum()
}

fun part2InstructionProcessor(instructions: List<Instruction>): Long {
    var currentMask: Mask by Delegates.notNull()
    val memory = mutableMapOf<Long, Long>()
    for (instruction in instructions) {
        when (instruction) {
            is SetMask -> currentMask = instruction.mask
            is WriteMemory -> {
                val addresses = currentMask.addressMasks(instruction.address)
                addresses.forEach { address ->
                    memory[address] = instruction.value
                }
            }
        }
    }
    return memory.values.sum()
}

class Mask(private val value: String) {

    private val orMask: Long = value.replace('X', '0').toLong(2)
    private val andMask: Long = value.replace('X', '1').toLong(2)

    fun apply(value: Long): Long {
        return value and andMask or orMask
    }

    fun addressMasks(address: Long): List<Long> {
        return maskAddress(address.toString(2).padStart(36, '0'), value).map { it.toLong(2) }.toList()
    }

    companion object {
        private fun maskAddress(address: String, mask: String): Sequence<String> {
            if (mask.isEmpty() || address.length != mask.length) {
                return emptySequence()
            }
            val maskHead = mask.first()
            val addressHead = address.first()
            val maskTail = mask.substring(1 until mask.length)
            val addressTail = address.substring(1 until address.length)
            if (maskTail.isEmpty()) {
                return sequence {
                    when (maskHead) {
                        '0' -> yield("$addressHead")
                        '1' -> yield("1")
                        'X' -> {
                            yield("0")
                            yield("1")
                        }
                    }
                }
            }
            return sequence {
                when (maskHead) {
                    '0' -> yieldAll(maskAddress(addressTail, maskTail).map { "$addressHead$it" })
                    '1' -> yieldAll(maskAddress(addressTail, maskTail).map { "1$it" })
                    'X' -> {
                        val tails = maskAddress(addressTail, maskTail).toList()
                        yieldAll(tails.map { "0$it" })
                        yieldAll(tails.map { "1$it" })
                    }
                }
            }
        }
    }
}

sealed class Instruction

data class SetMask(val mask: Mask) : Instruction()

data class WriteMemory(val address: Long, val value: Long) : Instruction()

fun readInstructions(fileName: String): List<Instruction> {
    return ClassLoader.getSystemResourceAsStream(fileName)!!.bufferedReader().useLines { lines ->
        lines.filter(String::isNotBlank).map { line -> line.split("=").map(String::trim) }.map { (left, right) ->
            if (left == "mask") {
                return@map SetMask(Mask(right))
            }
            WriteMemory(left.removePrefix("mem[").removeSuffix("]").toLong(), right.toLong())
        }.toList()
    }
}

Compiled from "App.kt"
final class net.sinceat.aoc2020.day14.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.day14.AppKt$readInstructions$1$1 INSTANCE;

  net.sinceat.aoc2020.day14.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/day14/AppKt$readInstructions$1$1
       3: dup
       4: invokespecial #53                 // Method "<init>":()V
       7: putstatic     #56                 // Field INSTANCE:Lnet/sinceat/aoc2020/day14/AppKt$readInstructions$1$1;
      10: return
}

Compiled from "App.kt"
public final class net.sinceat.aoc2020.day14.AppKt {
  static final kotlin.reflect.KProperty<java.lang.Object>[] $$delegatedProperties;

  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 part1InstructionProcessor:(Ljava/util/List;)J
       8: lstore_0
       9: getstatic     #22                 // Field java/lang/System.out:Ljava/io/PrintStream;
      12: lload_0
      13: invokevirtual #28                 // Method java/io/PrintStream.println:(J)V
      16: ldc           #30                 // String input.txt
      18: invokestatic  #12                 // Method readInstructions:(Ljava/lang/String;)Ljava/util/List;
      21: invokestatic  #16                 // Method part1InstructionProcessor:(Ljava/util/List;)J
      24: lstore_0
      25: getstatic     #22                 // Field java/lang/System.out:Ljava/io/PrintStream;
      28: lload_0
      29: invokevirtual #28                 // Method java/io/PrintStream.println:(J)V
      32: ldc           #32                 // String testinput2.txt
      34: invokestatic  #12                 // Method readInstructions:(Ljava/lang/String;)Ljava/util/List;
      37: invokestatic  #35                 // Method part2InstructionProcessor:(Ljava/util/List;)J
      40: lstore_0
      41: getstatic     #22                 // Field java/lang/System.out:Ljava/io/PrintStream;
      44: lload_0
      45: invokevirtual #28                 // Method java/io/PrintStream.println:(J)V
      48: ldc           #30                 // String input.txt
      50: invokestatic  #12                 // Method readInstructions:(Ljava/lang/String;)Ljava/util/List;
      53: invokestatic  #35                 // Method part2InstructionProcessor:(Ljava/util/List;)J
      56: lstore_0
      57: getstatic     #22                 // Field java/lang/System.out:Ljava/io/PrintStream;
      60: lload_0
      61: invokevirtual #28                 // Method java/io/PrintStream.println:(J)V
      64: return

  public static final long part1InstructionProcessor(java.util.List<? extends net.sinceat.aoc2020.day14.Instruction>);
    Code:
       0: aload_0
       1: ldc           #39                 // String instructions
       3: invokestatic  #45                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: getstatic     #51                 // Field kotlin/properties/Delegates.INSTANCE:Lkotlin/properties/Delegates;
       9: invokevirtual #55                 // Method kotlin/properties/Delegates.notNull:()Lkotlin/properties/ReadWriteProperty;
      12: astore_1
      13: new           #57                 // class java/util/LinkedHashMap
      16: dup
      17: invokespecial #60                 // Method java/util/LinkedHashMap."<init>":()V
      20: checkcast     #62                 // class java/util/Map
      23: astore_2
      24: aload_0
      25: invokeinterface #68,  1           // InterfaceMethod java/util/List.iterator:()Ljava/util/Iterator;
      30: astore_3
      31: aload_3
      32: invokeinterface #74,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      37: ifeq          133
      40: aload_3
      41: invokeinterface #78,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      46: checkcast     #80                 // class net/sinceat/aoc2020/day14/Instruction
      49: astore        4
      51: aload         4
      53: astore        5
      55: aload         5
      57: instanceof    #82                 // class net/sinceat/aoc2020/day14/SetMask
      60: ifeq          78
      63: aload_1
      64: aload         4
      66: checkcast     #82                 // class net/sinceat/aoc2020/day14/SetMask
      69: invokevirtual #86                 // Method net/sinceat/aoc2020/day14/SetMask.getMask:()Lnet/sinceat/aoc2020/day14/Mask;
      72: invokestatic  #90                 // Method part1InstructionProcessor$lambda$1:(Lkotlin/properties/ReadWriteProperty;Lnet/sinceat/aoc2020/day14/Mask;)V
      75: goto          31
      78: aload         5
      80: instanceof    #92                 // class net/sinceat/aoc2020/day14/WriteMemory
      83: ifeq          125
      86: aload_2
      87: aload         4
      89: checkcast     #92                 // class net/sinceat/aoc2020/day14/WriteMemory
      92: invokevirtual #96                 // Method net/sinceat/aoc2020/day14/WriteMemory.getAddress:()J
      95: invokestatic  #102                // Method java/lang/Long.valueOf:(J)Ljava/lang/Long;
      98: aload_1
      99: invokestatic  #106                // Method part1InstructionProcessor$lambda$0:(Lkotlin/properties/ReadWriteProperty;)Lnet/sinceat/aoc2020/day14/Mask;
     102: aload         4
     104: checkcast     #92                 // class net/sinceat/aoc2020/day14/WriteMemory
     107: invokevirtual #109                // Method net/sinceat/aoc2020/day14/WriteMemory.getValue:()J
     110: invokevirtual #115                // Method net/sinceat/aoc2020/day14/Mask.apply:(J)J
     113: invokestatic  #102                // Method java/lang/Long.valueOf:(J)Ljava/lang/Long;
     116: invokeinterface #119,  3          // InterfaceMethod java/util/Map.put:(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
     121: pop
     122: goto          31
     125: new           #121                // class kotlin/NoWhenBranchMatchedException
     128: dup
     129: invokespecial #122                // Method kotlin/NoWhenBranchMatchedException."<init>":()V
     132: athrow
     133: aload_2
     134: invokeinterface #126,  1          // InterfaceMethod java/util/Map.values:()Ljava/util/Collection;
     139: checkcast     #128                // class java/lang/Iterable
     142: invokestatic  #134                // Method kotlin/collections/CollectionsKt.sumOfLong:(Ljava/lang/Iterable;)J
     145: lreturn

  public static final long part2InstructionProcessor(java.util.List<? extends net.sinceat.aoc2020.day14.Instruction>);
    Code:
       0: aload_0
       1: ldc           #39                 // String instructions
       3: invokestatic  #45                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: getstatic     #51                 // Field kotlin/properties/Delegates.INSTANCE:Lkotlin/properties/Delegates;
       9: invokevirtual #55                 // Method kotlin/properties/Delegates.notNull:()Lkotlin/properties/ReadWriteProperty;
      12: astore_1
      13: new           #57                 // class java/util/LinkedHashMap
      16: dup
      17: invokespecial #60                 // Method java/util/LinkedHashMap."<init>":()V
      20: checkcast     #62                 // class java/util/Map
      23: astore_2
      24: aload_0
      25: invokeinterface #68,  1           // InterfaceMethod java/util/List.iterator:()Ljava/util/Iterator;
      30: astore_3
      31: aload_3
      32: invokeinterface #74,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      37: ifeq          198
      40: aload_3
      41: invokeinterface #78,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      46: checkcast     #80                 // class net/sinceat/aoc2020/day14/Instruction
      49: astore        4
      51: aload         4
      53: astore        5
      55: aload         5
      57: instanceof    #82                 // class net/sinceat/aoc2020/day14/SetMask
      60: ifeq          78
      63: aload_1
      64: aload         4
      66: checkcast     #82                 // class net/sinceat/aoc2020/day14/SetMask
      69: invokevirtual #86                 // Method net/sinceat/aoc2020/day14/SetMask.getMask:()Lnet/sinceat/aoc2020/day14/Mask;
      72: invokestatic  #146                // Method part2InstructionProcessor$lambda$3:(Lkotlin/properties/ReadWriteProperty;Lnet/sinceat/aoc2020/day14/Mask;)V
      75: goto          31
      78: aload         5
      80: instanceof    #92                 // class net/sinceat/aoc2020/day14/WriteMemory
      83: ifeq          190
      86: aload_1
      87: invokestatic  #149                // Method part2InstructionProcessor$lambda$2:(Lkotlin/properties/ReadWriteProperty;)Lnet/sinceat/aoc2020/day14/Mask;
      90: aload         4
      92: checkcast     #92                 // class net/sinceat/aoc2020/day14/WriteMemory
      95: invokevirtual #96                 // Method net/sinceat/aoc2020/day14/WriteMemory.getAddress:()J
      98: invokevirtual #153                // Method net/sinceat/aoc2020/day14/Mask.addressMasks:(J)Ljava/util/List;
     101: astore        6
     103: aload         6
     105: checkcast     #128                // class java/lang/Iterable
     108: astore        7
     110: iconst_0
     111: istore        8
     113: aload         7
     115: invokeinterface #154,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     120: astore        9
     122: aload         9
     124: invokeinterface #74,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     129: ifeq          186
     132: aload         9
     134: invokeinterface #78,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     139: astore        10
     141: aload         10
     143: checkcast     #156                // class java/lang/Number
     146: invokevirtual #159                // Method java/lang/Number.longValue:()J
     149: lstore        11
     151: iconst_0
     152: istore        13
     154: lload         11
     156: invokestatic  #102                // Method java/lang/Long.valueOf:(J)Ljava/lang/Long;
     159: astore        14
     161: aload_2
     162: aload         14
     164: aload         4
     166: checkcast     #92                 // class net/sinceat/aoc2020/day14/WriteMemory
     169: invokevirtual #109                // Method net/sinceat/aoc2020/day14/WriteMemory.getValue:()J
     172: invokestatic  #102                // Method java/lang/Long.valueOf:(J)Ljava/lang/Long;
     175: invokeinterface #119,  3          // InterfaceMethod java/util/Map.put:(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
     180: pop
     181: nop
     182: nop
     183: goto          122
     186: nop
     187: goto          31
     190: new           #121                // class kotlin/NoWhenBranchMatchedException
     193: dup
     194: invokespecial #122                // Method kotlin/NoWhenBranchMatchedException."<init>":()V
     197: athrow
     198: aload_2
     199: invokeinterface #126,  1          // InterfaceMethod java/util/Map.values:()Ljava/util/Collection;
     204: checkcast     #128                // class java/lang/Iterable
     207: invokestatic  #134                // Method kotlin/collections/CollectionsKt.sumOfLong:(Ljava/lang/Iterable;)J
     210: lreturn

  public static final java.util.List<net.sinceat.aoc2020.day14.Instruction> readInstructions(java.lang.String);
    Code:
       0: aload_0
       1: ldc           #174                // String fileName
       3: invokestatic  #45                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: invokestatic  #180                // Method java/lang/ClassLoader.getSystemResourceAsStream:(Ljava/lang/String;)Ljava/io/InputStream;
      10: dup
      11: invokestatic  #184                // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
      14: astore_1
      15: getstatic     #190                // Field kotlin/text/Charsets.UTF_8:Ljava/nio/charset/Charset;
      18: astore_2
      19: new           #192                // class java/io/InputStreamReader
      22: dup
      23: aload_1
      24: aload_2
      25: invokespecial #195                // Method java/io/InputStreamReader."<init>":(Ljava/io/InputStream;Ljava/nio/charset/Charset;)V
      28: checkcast     #197                // class java/io/Reader
      31: astore_3
      32: sipush        8192
      35: istore        4
      37: aload_3
      38: instanceof    #199                // class java/io/BufferedReader
      41: ifeq          51
      44: aload_3
      45: checkcast     #199                // class java/io/BufferedReader
      48: goto          61
      51: new           #199                // class java/io/BufferedReader
      54: dup
      55: aload_3
      56: iload         4
      58: invokespecial #202                // Method java/io/BufferedReader."<init>":(Ljava/io/Reader;I)V
      61: checkcast     #197                // 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    #199                // class java/io/BufferedReader
      79: ifeq          89
      82: aload_3
      83: checkcast     #199                // class java/io/BufferedReader
      86: goto          99
      89: new           #199                // class java/io/BufferedReader
      92: dup
      93: aload_3
      94: iload         4
      96: invokespecial #202                // Method java/io/BufferedReader."<init>":(Ljava/io/Reader;I)V
      99: checkcast     #204                // class java/io/Closeable
     102: astore_3
     103: aconst_null
     104: astore        4
     106: nop
     107: aload_3
     108: checkcast     #199                // class java/io/BufferedReader
     111: astore        5
     113: iconst_0
     114: istore        6
     116: aload         5
     118: invokestatic  #210                // 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     #215                // Field net/sinceat/aoc2020/day14/AppKt$readInstructions$1$1.INSTANCE:Lnet/sinceat/aoc2020/day14/AppKt$readInstructions$1$1;
     131: checkcast     #217                // class kotlin/jvm/functions/Function1
     134: invokestatic  #223                // Method kotlin/sequences/SequencesKt.filter:(Lkotlin/sequences/Sequence;Lkotlin/jvm/functions/Function1;)Lkotlin/sequences/Sequence;
     137: invokedynamic #241,  0            // InvokeDynamic #0:invoke:()Lkotlin/jvm/functions/Function1;
     142: invokestatic  #244                // Method kotlin/sequences/SequencesKt.map:(Lkotlin/sequences/Sequence;Lkotlin/jvm/functions/Function1;)Lkotlin/sequences/Sequence;
     145: invokedynamic #251,  0            // InvokeDynamic #1:invoke:()Lkotlin/jvm/functions/Function1;
     150: invokestatic  #244                // Method kotlin/sequences/SequencesKt.map:(Lkotlin/sequences/Sequence;Lkotlin/jvm/functions/Function1;)Lkotlin/sequences/Sequence;
     153: invokestatic  #255                // Method kotlin/sequences/SequencesKt.toList:(Lkotlin/sequences/Sequence;)Ljava/util/List;
     156: astore        5
     158: aload_3
     159: aload         4
     161: invokestatic  #261                // Method kotlin/io/CloseableKt.closeFinally:(Ljava/io/Closeable;Ljava/lang/Throwable;)V
     164: aload         5
     166: goto          189
     169: astore        6
     171: aload         6
     173: astore        4
     175: aload         6
     177: athrow
     178: astore        6
     180: aload_3
     181: aload         4
     183: invokestatic  #261                // Method kotlin/io/CloseableKt.closeFinally:(Ljava/io/Closeable;Ljava/lang/Throwable;)V
     186: aload         6
     188: athrow
     189: nop
     190: areturn
    Exception table:
       from    to  target type
         106   158   169   Class java/lang/Throwable
         106   158   178   any
         169   178   178   any
         178   180   178   any

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

  private static final net.sinceat.aoc2020.day14.Mask part1InstructionProcessor$lambda$0(kotlin.properties.ReadWriteProperty<java.lang.Object, net.sinceat.aoc2020.day14.Mask>);
    Code:
       0: aload_0
       1: aconst_null
       2: getstatic     #289                // Field $$delegatedProperties:[Lkotlin/reflect/KProperty;
       5: iconst_0
       6: aaload
       7: invokeinterface #292,  3          // InterfaceMethod kotlin/properties/ReadWriteProperty.getValue:(Ljava/lang/Object;Lkotlin/reflect/KProperty;)Ljava/lang/Object;
      12: checkcast     #111                // class net/sinceat/aoc2020/day14/Mask
      15: areturn

  private static final void part1InstructionProcessor$lambda$1(kotlin.properties.ReadWriteProperty<java.lang.Object, net.sinceat.aoc2020.day14.Mask>, net.sinceat.aoc2020.day14.Mask);
    Code:
       0: aload_0
       1: aconst_null
       2: getstatic     #289                // Field $$delegatedProperties:[Lkotlin/reflect/KProperty;
       5: iconst_0
       6: aaload
       7: aload_1
       8: invokeinterface #298,  4          // InterfaceMethod kotlin/properties/ReadWriteProperty.setValue:(Ljava/lang/Object;Lkotlin/reflect/KProperty;Ljava/lang/Object;)V
      13: return

  private static final net.sinceat.aoc2020.day14.Mask part2InstructionProcessor$lambda$2(kotlin.properties.ReadWriteProperty<java.lang.Object, net.sinceat.aoc2020.day14.Mask>);
    Code:
       0: aload_0
       1: aconst_null
       2: getstatic     #289                // Field $$delegatedProperties:[Lkotlin/reflect/KProperty;
       5: iconst_1
       6: aaload
       7: invokeinterface #292,  3          // InterfaceMethod kotlin/properties/ReadWriteProperty.getValue:(Ljava/lang/Object;Lkotlin/reflect/KProperty;)Ljava/lang/Object;
      12: checkcast     #111                // class net/sinceat/aoc2020/day14/Mask
      15: areturn

  private static final void part2InstructionProcessor$lambda$3(kotlin.properties.ReadWriteProperty<java.lang.Object, net.sinceat.aoc2020.day14.Mask>, net.sinceat.aoc2020.day14.Mask);
    Code:
       0: aload_0
       1: aconst_null
       2: getstatic     #289                // Field $$delegatedProperties:[Lkotlin/reflect/KProperty;
       5: iconst_1
       6: aaload
       7: aload_1
       8: invokeinterface #298,  4          // InterfaceMethod kotlin/properties/ReadWriteProperty.setValue:(Ljava/lang/Object;Lkotlin/reflect/KProperty;Ljava/lang/Object;)V
      13: return

  private static final java.util.List readInstructions$lambda$7$lambda$5(java.lang.String);
    Code:
       0: aload_0
       1: ldc_w         #302                // String line
       4: invokestatic  #45                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       7: aload_0
       8: checkcast     #304                // class java/lang/CharSequence
      11: iconst_1
      12: anewarray     #273                // class java/lang/String
      15: astore_1
      16: aload_1
      17: iconst_0
      18: ldc_w         #306                // String =
      21: aastore
      22: aload_1
      23: iconst_0
      24: iconst_0
      25: bipush        6
      27: aconst_null
      28: invokestatic  #312                // Method kotlin/text/StringsKt.split$default:(Ljava/lang/CharSequence;[Ljava/lang/String;ZIILjava/lang/Object;)Ljava/util/List;
      31: checkcast     #128                // class java/lang/Iterable
      34: astore_1
      35: iconst_0
      36: istore_2
      37: aload_1
      38: astore_3
      39: new           #314                // class java/util/ArrayList
      42: dup
      43: aload_1
      44: bipush        10
      46: invokestatic  #318                // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
      49: invokespecial #321                // Method java/util/ArrayList."<init>":(I)V
      52: checkcast     #323                // class java/util/Collection
      55: astore        4
      57: iconst_0
      58: istore        5
      60: aload_3
      61: invokeinterface #154,  1          // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      66: astore        6
      68: aload         6
      70: invokeinterface #74,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      75: ifeq          125
      78: aload         6
      80: invokeinterface #78,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      85: astore        7
      87: aload         4
      89: aload         7
      91: checkcast     #273                // class java/lang/String
      94: astore        8
      96: astore        10
      98: iconst_0
      99: istore        9
     101: aload         8
     103: checkcast     #304                // class java/lang/CharSequence
     106: invokestatic  #327                // Method kotlin/text/StringsKt.trim:(Ljava/lang/CharSequence;)Ljava/lang/CharSequence;
     109: invokevirtual #331                // Method java/lang/Object.toString:()Ljava/lang/String;
     112: nop
     113: aload         10
     115: swap
     116: invokeinterface #335,  2          // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     121: pop
     122: goto          68
     125: aload         4
     127: checkcast     #64                 // class java/util/List
     130: nop
     131: areturn

  private static final net.sinceat.aoc2020.day14.Instruction readInstructions$lambda$7$lambda$6(java.util.List);
    Code:
       0: aload_0
       1: ldc_w         #346                // String <destruct>
       4: invokestatic  #45                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       7: aload_0
       8: iconst_0
       9: invokeinterface #350,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      14: checkcast     #273                // class java/lang/String
      17: astore_1
      18: aload_0
      19: iconst_1
      20: invokeinterface #350,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      25: checkcast     #273                // class java/lang/String
      28: astore_2
      29: aload_1
      30: ldc_w         #352                // String mask
      33: invokestatic  #356                // Method kotlin/jvm/internal/Intrinsics.areEqual:(Ljava/lang/Object;Ljava/lang/Object;)Z
      36: ifeq          58
      39: new           #82                 // class net/sinceat/aoc2020/day14/SetMask
      42: dup
      43: new           #111                // class net/sinceat/aoc2020/day14/Mask
      46: dup
      47: aload_2
      48: invokespecial #359                // Method net/sinceat/aoc2020/day14/Mask."<init>":(Ljava/lang/String;)V
      51: invokespecial #362                // Method net/sinceat/aoc2020/day14/SetMask."<init>":(Lnet/sinceat/aoc2020/day14/Mask;)V
      54: checkcast     #80                 // class net/sinceat/aoc2020/day14/Instruction
      57: areturn
      58: new           #92                 // class net/sinceat/aoc2020/day14/WriteMemory
      61: dup
      62: aload_1
      63: ldc_w         #364                // String mem[
      66: checkcast     #304                // class java/lang/CharSequence
      69: invokestatic  #368                // Method kotlin/text/StringsKt.removePrefix:(Ljava/lang/String;Ljava/lang/CharSequence;)Ljava/lang/String;
      72: ldc_w         #370                // String ]
      75: checkcast     #304                // class java/lang/CharSequence
      78: invokestatic  #373                // Method kotlin/text/StringsKt.removeSuffix:(Ljava/lang/String;Ljava/lang/CharSequence;)Ljava/lang/String;
      81: invokestatic  #377                // Method java/lang/Long.parseLong:(Ljava/lang/String;)J
      84: aload_2
      85: invokestatic  #377                // Method java/lang/Long.parseLong:(Ljava/lang/String;)J
      88: invokespecial #380                // Method net/sinceat/aoc2020/day14/WriteMemory."<init>":(JJ)V
      91: checkcast     #80                 // class net/sinceat/aoc2020/day14/Instruction
      94: areturn

  static {};
    Code:
       0: iconst_2
       1: anewarray     #385                // class kotlin/reflect/KProperty
       4: astore_0
       5: aload_0
       6: iconst_0
       7: new           #387                // class kotlin/jvm/internal/MutablePropertyReference0Impl
      10: dup
      11: ldc           #2                  // class net/sinceat/aoc2020/day14/AppKt
      13: ldc_w         #389                // String currentMask
      16: ldc_w         #391                // String <v#0>
      19: iconst_1
      20: invokespecial #394                // Method kotlin/jvm/internal/MutablePropertyReference0Impl."<init>":(Ljava/lang/Class;Ljava/lang/String;Ljava/lang/String;I)V
      23: checkcast     #396                // class kotlin/jvm/internal/MutablePropertyReference0
      26: invokestatic  #402                // Method kotlin/jvm/internal/Reflection.mutableProperty0:(Lkotlin/jvm/internal/MutablePropertyReference0;)Lkotlin/reflect/KMutableProperty0;
      29: aastore
      30: aload_0
      31: iconst_1
      32: new           #387                // class kotlin/jvm/internal/MutablePropertyReference0Impl
      35: dup
      36: ldc           #2                  // class net/sinceat/aoc2020/day14/AppKt
      38: ldc_w         #389                // String currentMask
      41: ldc_w         #404                // String <v#1>
      44: iconst_1
      45: invokespecial #394                // Method kotlin/jvm/internal/MutablePropertyReference0Impl."<init>":(Ljava/lang/Class;Ljava/lang/String;Ljava/lang/String;I)V
      48: checkcast     #396                // class kotlin/jvm/internal/MutablePropertyReference0
      51: invokestatic  #402                // Method kotlin/jvm/internal/Reflection.mutableProperty0:(Lkotlin/jvm/internal/MutablePropertyReference0;)Lkotlin/reflect/KMutableProperty0;
      54: aastore
      55: aload_0
      56: putstatic     #289                // Field $$delegatedProperties:[Lkotlin/reflect/KProperty;
      59: return
}


package net.sinceat.aoc2020.day13

fun main() {
    run {
        val (currentTime, busIds) = readInputPart1("testinput.txt")
        val (busId, nextDeparture) = busIds.nextDepartures(currentTime).minByOrNull { (_, nextDeparture) ->
            nextDeparture
        }!!
        println("$busId x ($nextDeparture - $currentTime) = ${busId * (nextDeparture - currentTime)}")
    }
    run {
        val (currentTime, busIds) = readInputPart1("input.txt")
        val (busId, nextDeparture) = busIds.nextDepartures(currentTime).minByOrNull { (_, nextDeparture) ->
            nextDeparture
        }!!
        println("$busId x ($nextDeparture - $currentTime) = ${busId * (nextDeparture - currentTime)}")
        println()
    }
    run {
        println(readInputPart2("testinput.txt").findFirstConsecutiveDepartures())
    }
    run {
        println(readInputPart2("input.txt").findFirstConsecutiveDepartures())
    }
}

fun List<Pair<Int, Int>>.findFirstConsecutiveDepartures(): Long {
    var time = 0L
    var stepSize = 1L
    for ((busId, offset) in this) {
        while ((time + offset) % busId != 0L) {
            time += stepSize
        }
        stepSize *= busId
    }
    return time
}

fun List<Int>.nextDepartures(currentTime: Int): Map<Int, Int> {
    return associate { busId ->
        Pair(
            busId,
            (currentTime / busId) * busId + busId
        )
    }
}

fun readInputPart1(fileName: String): Pair<Int, List<Int>> {
    val lines = ClassLoader.getSystemResourceAsStream(fileName)!!.bufferedReader().readLines()
    val currentTime = lines[0].toInt()
    val busIds = lines[1].split(",").mapNotNull(String::toIntOrNull)
    return Pair(currentTime, busIds)
}

fun readInputPart2(fileName: String): List<Pair<Int, Int>> {
    val lines = ClassLoader.getSystemResourceAsStream(fileName)!!.bufferedReader().readLines()
    return lines[1].split(",").map(String::toIntOrNull).mapIndexed { index, busId ->
        if (busId == null) {
            return@mapIndexed null
        }
        Pair(busId, index)
    }.filterNotNull()
}

Compiled from "App.kt"
public final class net.sinceat.aoc2020.day13.AppKt {
  public static final void main();
    Code:
       0: iconst_0
       1: istore_0
       2: ldc           #8                  // String testinput.txt
       4: invokestatic  #12                 // Method readInputPart1:(Ljava/lang/String;)Lkotlin/Pair;
       7: astore_1
       8: aload_1
       9: invokevirtual #18                 // Method kotlin/Pair.component1:()Ljava/lang/Object;
      12: checkcast     #20                 // class java/lang/Number
      15: invokevirtual #24                 // Method java/lang/Number.intValue:()I
      18: istore_2
      19: aload_1
      20: invokevirtual #27                 // Method kotlin/Pair.component2:()Ljava/lang/Object;
      23: checkcast     #29                 // class java/util/List
      26: astore_3
      27: aload_3
      28: iload_2
      29: invokestatic  #33                 // Method nextDepartures:(Ljava/util/List;I)Ljava/util/Map;
      32: invokeinterface #39,  1           // InterfaceMethod java/util/Map.entrySet:()Ljava/util/Set;
      37: checkcast     #41                 // class java/lang/Iterable
      40: astore        4
      42: aload         4
      44: invokeinterface #45,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      49: astore        5
      51: aload         5
      53: invokeinterface #51,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      58: ifne          65
      61: aconst_null
      62: goto          183
      65: aload         5
      67: invokeinterface #54,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      72: astore        6
      74: aload         5
      76: invokeinterface #51,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      81: ifne          89
      84: aload         6
      86: goto          183
      89: aload         6
      91: checkcast     #56                 // class java/util/Map$Entry
      94: astore        7
      96: iconst_0
      97: istore        8
      99: aload         7
     101: invokeinterface #59,  1           // InterfaceMethod java/util/Map$Entry.getValue:()Ljava/lang/Object;
     106: checkcast     #20                 // class java/lang/Number
     109: invokevirtual #24                 // Method java/lang/Number.intValue:()I
     112: istore        9
     114: iload         9
     116: istore        7
     118: aload         5
     120: invokeinterface #54,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     125: astore        8
     127: aload         8
     129: checkcast     #56                 // class java/util/Map$Entry
     132: astore        9
     134: iconst_0
     135: istore        10
     137: aload         9
     139: invokeinterface #59,  1           // InterfaceMethod java/util/Map$Entry.getValue:()Ljava/lang/Object;
     144: checkcast     #20                 // class java/lang/Number
     147: invokevirtual #24                 // Method java/lang/Number.intValue:()I
     150: istore        11
     152: iload         11
     154: istore        9
     156: iload         7
     158: iload         9
     160: if_icmple     171
     163: aload         8
     165: astore        6
     167: iload         9
     169: istore        7
     171: aload         5
     173: invokeinterface #51,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     178: ifne          118
     181: aload         6
     183: checkcast     #56                 // class java/util/Map$Entry
     186: dup
     187: invokestatic  #65                 // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
     190: astore        12
     192: aload         12
     194: invokeinterface #68,  1           // InterfaceMethod java/util/Map$Entry.getKey:()Ljava/lang/Object;
     199: checkcast     #20                 // class java/lang/Number
     202: invokevirtual #24                 // Method java/lang/Number.intValue:()I
     205: istore        13
     207: aload         12
     209: invokeinterface #59,  1           // InterfaceMethod java/util/Map$Entry.getValue:()Ljava/lang/Object;
     214: checkcast     #20                 // class java/lang/Number
     217: invokevirtual #24                 // Method java/lang/Number.intValue:()I
     220: istore        4
     222: new           #70                 // class java/lang/StringBuilder
     225: dup
     226: invokespecial #73                 // Method java/lang/StringBuilder."<init>":()V
     229: iload         13
     231: invokevirtual #77                 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
     234: ldc           #79                 // String  x (
     236: invokevirtual #82                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     239: iload         4
     241: invokevirtual #77                 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
     244: ldc           #84                 // String  -
     246: invokevirtual #82                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     249: iload_2
     250: invokevirtual #77                 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
     253: ldc           #86                 // String ) =
     255: invokevirtual #82                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     258: iload         13
     260: iload         4
     262: iload_2
     263: isub
     264: imul
     265: invokevirtual #77                 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
     268: invokevirtual #90                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
     271: getstatic     #96                 // Field java/lang/System.out:Ljava/io/PrintStream;
     274: swap
     275: invokevirtual #101                // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
     278: nop
     279: nop
     280: iconst_0
     281: istore_0
     282: ldc           #103                // String input.txt
     284: invokestatic  #12                 // Method readInputPart1:(Ljava/lang/String;)Lkotlin/Pair;
     287: astore_1
     288: aload_1
     289: invokevirtual #18                 // Method kotlin/Pair.component1:()Ljava/lang/Object;
     292: checkcast     #20                 // class java/lang/Number
     295: invokevirtual #24                 // Method java/lang/Number.intValue:()I
     298: istore_2
     299: aload_1
     300: invokevirtual #27                 // Method kotlin/Pair.component2:()Ljava/lang/Object;
     303: checkcast     #29                 // class java/util/List
     306: astore_3
     307: aload_3
     308: iload_2
     309: invokestatic  #33                 // Method nextDepartures:(Ljava/util/List;I)Ljava/util/Map;
     312: invokeinterface #39,  1           // InterfaceMethod java/util/Map.entrySet:()Ljava/util/Set;
     317: checkcast     #41                 // class java/lang/Iterable
     320: astore        4
     322: aload         4
     324: invokeinterface #45,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     329: astore        5
     331: aload         5
     333: invokeinterface #51,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     338: ifne          345
     341: aconst_null
     342: goto          463
     345: aload         5
     347: invokeinterface #54,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     352: astore        6
     354: aload         5
     356: invokeinterface #51,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     361: ifne          369
     364: aload         6
     366: goto          463
     369: aload         6
     371: checkcast     #56                 // class java/util/Map$Entry
     374: astore        7
     376: iconst_0
     377: istore        8
     379: aload         7
     381: invokeinterface #59,  1           // InterfaceMethod java/util/Map$Entry.getValue:()Ljava/lang/Object;
     386: checkcast     #20                 // class java/lang/Number
     389: invokevirtual #24                 // Method java/lang/Number.intValue:()I
     392: istore        9
     394: iload         9
     396: istore        7
     398: aload         5
     400: invokeinterface #54,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     405: astore        8
     407: aload         8
     409: checkcast     #56                 // class java/util/Map$Entry
     412: astore        9
     414: iconst_0
     415: istore        10
     417: aload         9
     419: invokeinterface #59,  1           // InterfaceMethod java/util/Map$Entry.getValue:()Ljava/lang/Object;
     424: checkcast     #20                 // class java/lang/Number
     427: invokevirtual #24                 // Method java/lang/Number.intValue:()I
     430: istore        11
     432: iload         11
     434: istore        9
     436: iload         7
     438: iload         9
     440: if_icmple     451
     443: aload         8
     445: astore        6
     447: iload         9
     449: istore        7
     451: aload         5
     453: invokeinterface #51,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     458: ifne          398
     461: aload         6
     463: checkcast     #56                 // class java/util/Map$Entry
     466: dup
     467: invokestatic  #65                 // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
     470: astore        12
     472: aload         12
     474: invokeinterface #68,  1           // InterfaceMethod java/util/Map$Entry.getKey:()Ljava/lang/Object;
     479: checkcast     #20                 // class java/lang/Number
     482: invokevirtual #24                 // Method java/lang/Number.intValue:()I
     485: istore        13
     487: aload         12
     489: invokeinterface #59,  1           // InterfaceMethod java/util/Map$Entry.getValue:()Ljava/lang/Object;
     494: checkcast     #20                 // class java/lang/Number
     497: invokevirtual #24                 // Method java/lang/Number.intValue:()I
     500: istore        4
     502: new           #70                 // class java/lang/StringBuilder
     505: dup
     506: invokespecial #73                 // Method java/lang/StringBuilder."<init>":()V
     509: iload         13
     511: invokevirtual #77                 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
     514: ldc           #79                 // String  x (
     516: invokevirtual #82                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     519: iload         4
     521: invokevirtual #77                 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
     524: ldc           #84                 // String  -
     526: invokevirtual #82                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     529: iload_2
     530: invokevirtual #77                 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
     533: ldc           #86                 // String ) =
     535: invokevirtual #82                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     538: iload         13
     540: iload         4
     542: iload_2
     543: isub
     544: imul
     545: invokevirtual #77                 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
     548: invokevirtual #90                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
     551: getstatic     #96                 // Field java/lang/System.out:Ljava/io/PrintStream;
     554: swap
     555: invokevirtual #101                // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
     558: getstatic     #96                 // Field java/lang/System.out:Ljava/io/PrintStream;
     561: invokevirtual #105                // Method java/io/PrintStream.println:()V
     564: nop
     565: nop
     566: iconst_0
     567: istore_0
     568: ldc           #8                  // String testinput.txt
     570: invokestatic  #109                // Method readInputPart2:(Ljava/lang/String;)Ljava/util/List;
     573: invokestatic  #113                // Method findFirstConsecutiveDepartures:(Ljava/util/List;)J
     576: lstore_1
     577: getstatic     #96                 // Field java/lang/System.out:Ljava/io/PrintStream;
     580: lload_1
     581: invokevirtual #116                // Method java/io/PrintStream.println:(J)V
     584: nop
     585: nop
     586: iconst_0
     587: istore_0
     588: ldc           #103                // String input.txt
     590: invokestatic  #109                // Method readInputPart2:(Ljava/lang/String;)Ljava/util/List;
     593: invokestatic  #113                // Method findFirstConsecutiveDepartures:(Ljava/util/List;)J
     596: lstore_1
     597: getstatic     #96                 // Field java/lang/System.out:Ljava/io/PrintStream;
     600: lload_1
     601: invokevirtual #116                // Method java/io/PrintStream.println:(J)V
     604: nop
     605: nop
     606: return

  public static final long findFirstConsecutiveDepartures(java.util.List<kotlin.Pair<java.lang.Integer, java.lang.Integer>>);
    Code:
       0: aload_0
       1: ldc           #132                // String <this>
       3: invokestatic  #136                // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: lconst_0
       7: lstore_1
       8: lconst_1
       9: lstore_3
      10: aload_0
      11: invokeinterface #137,  1          // InterfaceMethod java/util/List.iterator:()Ljava/util/Iterator;
      16: astore        5
      18: aload         5
      20: invokeinterface #51,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      25: ifeq          96
      28: aload         5
      30: invokeinterface #54,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      35: checkcast     #14                 // class kotlin/Pair
      38: astore        6
      40: aload         6
      42: invokevirtual #18                 // Method kotlin/Pair.component1:()Ljava/lang/Object;
      45: checkcast     #20                 // class java/lang/Number
      48: invokevirtual #24                 // Method java/lang/Number.intValue:()I
      51: istore        7
      53: aload         6
      55: invokevirtual #27                 // Method kotlin/Pair.component2:()Ljava/lang/Object;
      58: checkcast     #20                 // class java/lang/Number
      61: invokevirtual #24                 // Method java/lang/Number.intValue:()I
      64: istore        8
      66: lload_1
      67: iload         8
      69: i2l
      70: ladd
      71: iload         7
      73: i2l
      74: lrem
      75: lconst_0
      76: lcmp
      77: ifeq          87
      80: lload_1
      81: lload_3
      82: ladd
      83: lstore_1
      84: goto          66
      87: lload_3
      88: iload         7
      90: i2l
      91: lmul
      92: lstore_3
      93: goto          18
      96: lload_1
      97: lreturn

  public static final java.util.Map<java.lang.Integer, java.lang.Integer> nextDepartures(java.util.List<java.lang.Integer>, int);
    Code:
       0: aload_0
       1: ldc           #132                // String <this>
       3: invokestatic  #136                // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: checkcast     #41                 // class java/lang/Iterable
      10: astore_2
      11: iconst_0
      12: istore_3
      13: aload_2
      14: bipush        10
      16: invokestatic  #149                // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
      19: invokestatic  #155                // Method kotlin/collections/MapsKt.mapCapacity:(I)I
      22: bipush        16
      24: invokestatic  #161                // Method kotlin/ranges/RangesKt.coerceAtLeast:(II)I
      27: istore        4
      29: aload_2
      30: astore        5
      32: new           #163                // class java/util/LinkedHashMap
      35: dup
      36: iload         4
      38: invokespecial #166                // Method java/util/LinkedHashMap."<init>":(I)V
      41: checkcast     #35                 // class java/util/Map
      44: astore        6
      46: iconst_0
      47: istore        7
      49: aload         5
      51: invokeinterface #45,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      56: astore        8
      58: aload         8
      60: invokeinterface #51,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      65: ifeq          143
      68: aload         8
      70: invokeinterface #54,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      75: astore        9
      77: aload         6
      79: astore        10
      81: aload         9
      83: checkcast     #20                 // class java/lang/Number
      86: invokevirtual #24                 // Method java/lang/Number.intValue:()I
      89: istore        11
      91: iconst_0
      92: istore        12
      94: new           #14                 // class kotlin/Pair
      97: dup
      98: iload         11
     100: invokestatic  #172                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     103: iload_1
     104: iload         11
     106: idiv
     107: iload         11
     109: imul
     110: iload         11
     112: iadd
     113: invokestatic  #172                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     116: invokespecial #175                // Method kotlin/Pair."<init>":(Ljava/lang/Object;Ljava/lang/Object;)V
     119: nop
     120: astore        11
     122: aload         10
     124: aload         11
     126: invokevirtual #178                // Method kotlin/Pair.getFirst:()Ljava/lang/Object;
     129: aload         11
     131: invokevirtual #181                // Method kotlin/Pair.getSecond:()Ljava/lang/Object;
     134: invokeinterface #185,  3          // InterfaceMethod java/util/Map.put:(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
     139: pop
     140: goto          58
     143: aload         6
     145: nop
     146: areturn

  public static final kotlin.Pair<java.lang.Integer, java.util.List<java.lang.Integer>> readInputPart1(java.lang.String);
    Code:
       0: aload_0
       1: ldc           #200                // String fileName
       3: invokestatic  #136                // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: invokestatic  #206                // Method java/lang/ClassLoader.getSystemResourceAsStream:(Ljava/lang/String;)Ljava/io/InputStream;
      10: dup
      11: invokestatic  #65                 // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
      14: astore_2
      15: getstatic     #212                // Field kotlin/text/Charsets.UTF_8:Ljava/nio/charset/Charset;
      18: astore_3
      19: new           #214                // class java/io/InputStreamReader
      22: dup
      23: aload_2
      24: aload_3
      25: invokespecial #217                // Method java/io/InputStreamReader."<init>":(Ljava/io/InputStream;Ljava/nio/charset/Charset;)V
      28: checkcast     #219                // class java/io/Reader
      31: astore        4
      33: sipush        8192
      36: istore        5
      38: aload         4
      40: instanceof    #221                // class java/io/BufferedReader
      43: ifeq          54
      46: aload         4
      48: checkcast     #221                // class java/io/BufferedReader
      51: goto          65
      54: new           #221                // class java/io/BufferedReader
      57: dup
      58: aload         4
      60: iload         5
      62: invokespecial #224                // Method java/io/BufferedReader."<init>":(Ljava/io/Reader;I)V
      65: checkcast     #219                // class java/io/Reader
      68: invokestatic  #230                // Method kotlin/io/TextStreamsKt.readLines:(Ljava/io/Reader;)Ljava/util/List;
      71: astore_1
      72: aload_1
      73: iconst_0
      74: invokeinterface #234,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      79: checkcast     #236                // class java/lang/String
      82: invokestatic  #240                // Method java/lang/Integer.parseInt:(Ljava/lang/String;)I
      85: istore_2
      86: aload_1
      87: iconst_1
      88: invokeinterface #234,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      93: checkcast     #242                // class java/lang/CharSequence
      96: iconst_1
      97: anewarray     #236                // class java/lang/String
     100: astore        4
     102: aload         4
     104: iconst_0
     105: ldc           #244                // String ,
     107: aastore
     108: aload         4
     110: iconst_0
     111: iconst_0
     112: bipush        6
     114: aconst_null
     115: invokestatic  #250                // Method kotlin/text/StringsKt.split$default:(Ljava/lang/CharSequence;[Ljava/lang/String;ZIILjava/lang/Object;)Ljava/util/List;
     118: checkcast     #41                 // class java/lang/Iterable
     121: astore        4
     123: iconst_0
     124: istore        5
     126: aload         4
     128: astore        6
     130: new           #252                // class java/util/ArrayList
     133: dup
     134: invokespecial #253                // Method java/util/ArrayList."<init>":()V
     137: checkcast     #255                // class java/util/Collection
     140: astore        7
     142: iconst_0
     143: istore        8
     145: aload         6
     147: astore        9
     149: iconst_0
     150: istore        10
     152: aload         9
     154: invokeinterface #45,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     159: astore        11
     161: aload         11
     163: invokeinterface #51,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     168: ifeq          229
     171: aload         11
     173: invokeinterface #54,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     178: astore        12
     180: aload         12
     182: astore        13
     184: iconst_0
     185: istore        14
     187: aload         13
     189: checkcast     #236                // class java/lang/String
     192: astore        15
     194: iconst_0
     195: istore        16
     197: aload         15
     199: invokestatic  #259                // Method kotlin/text/StringsKt.toIntOrNull:(Ljava/lang/String;)Ljava/lang/Integer;
     202: dup
     203: ifnull        224
     206: astore        17
     208: iconst_0
     209: istore        18
     211: aload         7
     213: aload         17
     215: invokeinterface #263,  2          // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     220: pop
     221: goto          225
     224: pop
     225: nop
     226: goto          161
     229: nop
     230: aload         7
     232: checkcast     #29                 // class java/util/List
     235: nop
     236: astore_3
     237: new           #14                 // class kotlin/Pair
     240: dup
     241: iload_2
     242: invokestatic  #172                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     245: aload_3
     246: invokespecial #175                // Method kotlin/Pair."<init>":(Ljava/lang/Object;Ljava/lang/Object;)V
     249: areturn

  public static final java.util.List<kotlin.Pair<java.lang.Integer, java.lang.Integer>> readInputPart2(java.lang.String);
    Code:
       0: aload_0
       1: ldc           #200                // String fileName
       3: invokestatic  #136                // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: invokestatic  #206                // Method java/lang/ClassLoader.getSystemResourceAsStream:(Ljava/lang/String;)Ljava/io/InputStream;
      10: dup
      11: invokestatic  #65                 // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
      14: astore_2
      15: getstatic     #212                // Field kotlin/text/Charsets.UTF_8:Ljava/nio/charset/Charset;
      18: astore_3
      19: new           #214                // class java/io/InputStreamReader
      22: dup
      23: aload_2
      24: aload_3
      25: invokespecial #217                // Method java/io/InputStreamReader."<init>":(Ljava/io/InputStream;Ljava/nio/charset/Charset;)V
      28: checkcast     #219                // class java/io/Reader
      31: astore        4
      33: sipush        8192
      36: istore        5
      38: aload         4
      40: instanceof    #221                // class java/io/BufferedReader
      43: ifeq          54
      46: aload         4
      48: checkcast     #221                // class java/io/BufferedReader
      51: goto          65
      54: new           #221                // class java/io/BufferedReader
      57: dup
      58: aload         4
      60: iload         5
      62: invokespecial #224                // Method java/io/BufferedReader."<init>":(Ljava/io/Reader;I)V
      65: checkcast     #219                // class java/io/Reader
      68: invokestatic  #230                // Method kotlin/io/TextStreamsKt.readLines:(Ljava/io/Reader;)Ljava/util/List;
      71: astore_1
      72: aload_1
      73: iconst_1
      74: invokeinterface #234,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      79: checkcast     #242                // class java/lang/CharSequence
      82: iconst_1
      83: anewarray     #236                // class java/lang/String
      86: astore_2
      87: aload_2
      88: iconst_0
      89: ldc           #244                // String ,
      91: aastore
      92: aload_2
      93: iconst_0
      94: iconst_0
      95: bipush        6
      97: aconst_null
      98: invokestatic  #250                // Method kotlin/text/StringsKt.split$default:(Ljava/lang/CharSequence;[Ljava/lang/String;ZIILjava/lang/Object;)Ljava/util/List;
     101: checkcast     #41                 // class java/lang/Iterable
     104: astore_2
     105: iconst_0
     106: istore_3
     107: aload_2
     108: astore        4
     110: new           #252                // class java/util/ArrayList
     113: dup
     114: aload_2
     115: bipush        10
     117: invokestatic  #149                // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
     120: invokespecial #284                // Method java/util/ArrayList."<init>":(I)V
     123: checkcast     #255                // class java/util/Collection
     126: astore        5
     128: iconst_0
     129: istore        6
     131: aload         4
     133: invokeinterface #45,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     138: astore        7
     140: aload         7
     142: invokeinterface #51,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     147: ifeq          190
     150: aload         7
     152: invokeinterface #54,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     157: astore        8
     159: aload         5
     161: aload         8
     163: checkcast     #236                // class java/lang/String
     166: astore        9
     168: astore        14
     170: iconst_0
     171: istore        10
     173: aload         9
     175: invokestatic  #259                // Method kotlin/text/StringsKt.toIntOrNull:(Ljava/lang/String;)Ljava/lang/Integer;
     178: aload         14
     180: swap
     181: invokeinterface #263,  2          // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     186: pop
     187: goto          140
     190: aload         5
     192: checkcast     #29                 // class java/util/List
     195: nop
     196: checkcast     #41                 // class java/lang/Iterable
     199: astore_2
     200: nop
     201: iconst_0
     202: istore_3
     203: aload_2
     204: astore        4
     206: new           #252                // class java/util/ArrayList
     209: dup
     210: aload_2
     211: bipush        10
     213: invokestatic  #149                // Method kotlin/collections/CollectionsKt.collectionSizeOrDefault:(Ljava/lang/Iterable;I)I
     216: invokespecial #284                // Method java/util/ArrayList."<init>":(I)V
     219: checkcast     #255                // class java/util/Collection
     222: astore        5
     224: iconst_0
     225: istore        6
     227: iconst_0
     228: istore        7
     230: aload         4
     232: invokeinterface #45,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     237: astore        8
     239: aload         8
     241: invokeinterface #51,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     246: ifeq          326
     249: aload         8
     251: invokeinterface #54,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     256: astore        9
     258: aload         5
     260: iload         7
     262: iinc          7, 1
     265: istore        10
     267: iload         10
     269: ifge          275
     272: invokestatic  #287                // Method kotlin/collections/CollectionsKt.throwIndexOverflow:()V
     275: iload         10
     277: aload         9
     279: checkcast     #168                // class java/lang/Integer
     282: astore        11
     284: istore        12
     286: astore        14
     288: iconst_0
     289: istore        13
     291: aload         11
     293: ifnonnull     300
     296: aconst_null
     297: goto          314
     300: new           #14                 // class kotlin/Pair
     303: dup
     304: aload         11
     306: iload         12
     308: invokestatic  #172                // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     311: invokespecial #175                // Method kotlin/Pair."<init>":(Ljava/lang/Object;Ljava/lang/Object;)V
     314: aload         14
     316: swap
     317: invokeinterface #263,  2          // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     322: pop
     323: goto          239
     326: aload         5
     328: checkcast     #29                 // class java/util/List
     331: nop
     332: checkcast     #41                 // class java/lang/Iterable
     335: invokestatic  #291                // Method kotlin/collections/CollectionsKt.filterNotNull:(Ljava/lang/Iterable;)Ljava/util/List;
     338: areturn

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