import kotlin.io.path.Path
import kotlin.io.path.forEachLine

fun day1_part1(path: String) {
    var result = 0
    Path(path).forEachLine { line ->
        val numLine = line.filter { it.isDigit() }
        val num = when (numLine.length) {
            1 -> numLine + numLine
            2 -> numLine
            else -> numLine.removeRange(1, numLine.lastIndex)
        }.toIntOrNull()
        if (num != null) result += num
    }
    println(result)
}

fun day1_part2(path: String) {
    var result = 0
    val map = mapOf(
        "one" to "1",
        "two" to "2",
        "three" to "3",
        "four" to "4",
        "five" to "5",
        "six" to "6",
        "seven" to "7",
        "eight" to "8",
        "nine" to "9",
    )
    val substrings = map.flatMap { listOf(it.key, it.value) }
    Path(path).forEachLine { line ->
        val numLine = findAllOverlappingMatches(line, substrings).joinToString("") { map[it] ?: it }
        val num = when (numLine.length) {
            1 -> numLine + numLine
            2 -> numLine
            else -> numLine.removeRange(1, numLine.lastIndex)
        }.toIntOrNull()
        if (num != null) result += num
    }
    println(result)
}

fun findAllOverlappingMatches(input: String, substrings: List<String>): List<String> {
    val matches = mutableListOf<String>()

    for (i in input.indices) {
        for (j in i + 1..input.length) {
            val substring = input.substring(i, j)
            if (substrings.contains(substring)) {
                matches.add(substring)
            }
        }
    }

    return matches
}

Compiled from "Day1.kt"
public final class Day1Kt {
  public static final void day1_part1(java.lang.String);
    Code:
       0: aload_0
       1: ldc           #11                 // String path
       3: invokestatic  #17                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: iconst_0
       7: istore_1
       8: aload_0
       9: iconst_0
      10: anewarray     #19                 // class java/lang/String
      13: invokestatic  #25                 // Method java/nio/file/Paths.get:(Ljava/lang/String;[Ljava/lang/String;)Ljava/nio/file/Path;
      16: dup
      17: ldc           #27                 // String get(...)
      19: invokestatic  #30                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
      22: astore_2
      23: getstatic     #36                 // Field kotlin/text/Charsets.UTF_8:Ljava/nio/charset/Charset;
      26: aload_2
      27: swap
      28: invokestatic  #42                 // Method java/nio/file/Files.newBufferedReader:(Ljava/nio/file/Path;Ljava/nio/charset/Charset;)Ljava/io/BufferedReader;
      31: dup
      32: ldc           #44                 // String newBufferedReader(...)
      34: invokestatic  #30                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
      37: checkcast     #46                 // class java/io/Reader
      40: astore_3
      41: aload_3
      42: checkcast     #48                 // class java/io/BufferedReader
      45: checkcast     #50                 // class java/io/Closeable
      48: astore        4
      50: aconst_null
      51: astore        5
      53: nop
      54: aload         4
      56: checkcast     #48                 // class java/io/BufferedReader
      59: astore        6
      61: aload         6
      63: invokestatic  #56                 // Method kotlin/io/TextStreamsKt.lineSequence:(Ljava/io/BufferedReader;)Lkotlin/sequences/Sequence;
      66: astore        7
      68: aload         7
      70: astore        8
      72: aload         8
      74: invokeinterface #62,  1           // InterfaceMethod kotlin/sequences/Sequence.iterator:()Ljava/util/Iterator;
      79: astore        9
      81: aload         9
      83: invokeinterface #68,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      88: ifeq          318
      91: aload         9
      93: invokeinterface #72,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      98: checkcast     #19                 // class java/lang/String
     101: astore        10
     103: iconst_0
     104: istore        11
     106: aload         10
     108: astore        12
     110: iconst_0
     111: istore        13
     113: aload         12
     115: checkcast     #74                 // class java/lang/CharSequence
     118: astore        14
     120: new           #76                 // class java/lang/StringBuilder
     123: dup
     124: invokespecial #80                 // Method java/lang/StringBuilder."<init>":()V
     127: checkcast     #82                 // class java/lang/Appendable
     130: astore        15
     132: iconst_0
     133: istore        16
     135: iconst_0
     136: istore        17
     138: aload         14
     140: invokeinterface #86,  1           // InterfaceMethod java/lang/CharSequence.length:()I
     145: istore        18
     147: iload         17
     149: iload         18
     151: if_icmpge     197
     154: aload         14
     156: iload         17
     158: invokeinterface #90,  2           // InterfaceMethod java/lang/CharSequence.charAt:(I)C
     163: istore        19
     165: iload         19
     167: istore        20
     169: iconst_0
     170: istore        21
     172: iload         20
     174: invokestatic  #96                 // Method java/lang/Character.isDigit:(C)Z
     177: nop
     178: ifeq          191
     181: aload         15
     183: iload         19
     185: invokeinterface #100,  2          // InterfaceMethod java/lang/Appendable.append:(C)Ljava/lang/Appendable;
     190: pop
     191: iinc          17, 1
     194: goto          147
     197: aload         15
     199: checkcast     #76                 // class java/lang/StringBuilder
     202: invokevirtual #104                // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
     205: astore        22
     207: aload         22
     209: invokevirtual #105                // Method java/lang/String.length:()I
     212: tableswitch   { // 1 to 2
                     1: 236
                     2: 259
               default: 264
          }
     236: new           #76                 // class java/lang/StringBuilder
     239: dup
     240: invokespecial #80                 // Method java/lang/StringBuilder."<init>":()V
     243: aload         22
     245: invokevirtual #108                // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     248: aload         22
     250: invokevirtual #108                // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     253: invokevirtual #104                // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
     256: goto          296
     259: aload         22
     261: goto          296
     264: aload         22
     266: astore        14
     268: iconst_1
     269: istore        15
     271: aload         22
     273: checkcast     #74                 // class java/lang/CharSequence
     276: invokestatic  #114                // Method kotlin/text/StringsKt.getLastIndex:(Ljava/lang/CharSequence;)I
     279: istore        16
     281: aload         14
     283: checkcast     #74                 // class java/lang/CharSequence
     286: iload         15
     288: iload         16
     290: invokestatic  #118                // Method kotlin/text/StringsKt.removeRange:(Ljava/lang/CharSequence;II)Ljava/lang/CharSequence;
     293: invokevirtual #119                // Method java/lang/Object.toString:()Ljava/lang/String;
     296: invokestatic  #123                // Method kotlin/text/StringsKt.toIntOrNull:(Ljava/lang/String;)Ljava/lang/Integer;
     299: astore        12
     301: aload         12
     303: ifnull        314
     306: iload_1
     307: aload         12
     309: invokevirtual #128                // Method java/lang/Integer.intValue:()I
     312: iadd
     313: istore_1
     314: nop
     315: goto          81
     318: aload         4
     320: aload         5
     322: invokestatic  #134                // Method kotlin/io/CloseableKt.closeFinally:(Ljava/io/Closeable;Ljava/lang/Throwable;)V
     325: goto          349
     328: astore        23
     330: aload         23
     332: astore        5
     334: aload         23
     336: athrow
     337: astore        23
     339: aload         4
     341: aload         5
     343: invokestatic  #134                // Method kotlin/io/CloseableKt.closeFinally:(Ljava/io/Closeable;Ljava/lang/Throwable;)V
     346: aload         23
     348: athrow
     349: iload_1
     350: istore_2
     351: getstatic     #140                // Field java/lang/System.out:Ljava/io/PrintStream;
     354: iload_2
     355: invokevirtual #146                // Method java/io/PrintStream.println:(I)V
     358: return
    Exception table:
       from    to  target type
          53   318   328   Class java/lang/Throwable
          53   318   337   any
         328   337   337   any
         337   339   337   any

  public static final void day1_part2(java.lang.String);
    Code:
       0: aload_0
       1: ldc           #11                 // String path
       3: invokestatic  #17                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: iconst_0
       7: istore_1
       8: bipush        9
      10: anewarray     #171                // class kotlin/Pair
      13: astore_3
      14: aload_3
      15: iconst_0
      16: ldc           #173                // String one
      18: ldc           #175                // String 1
      20: invokestatic  #181                // Method kotlin/TuplesKt.to:(Ljava/lang/Object;Ljava/lang/Object;)Lkotlin/Pair;
      23: aastore
      24: aload_3
      25: iconst_1
      26: ldc           #183                // String two
      28: ldc           #185                // String 2
      30: invokestatic  #181                // Method kotlin/TuplesKt.to:(Ljava/lang/Object;Ljava/lang/Object;)Lkotlin/Pair;
      33: aastore
      34: aload_3
      35: iconst_2
      36: ldc           #187                // String three
      38: ldc           #189                // String 3
      40: invokestatic  #181                // Method kotlin/TuplesKt.to:(Ljava/lang/Object;Ljava/lang/Object;)Lkotlin/Pair;
      43: aastore
      44: aload_3
      45: iconst_3
      46: ldc           #191                // String four
      48: ldc           #193                // String 4
      50: invokestatic  #181                // Method kotlin/TuplesKt.to:(Ljava/lang/Object;Ljava/lang/Object;)Lkotlin/Pair;
      53: aastore
      54: aload_3
      55: iconst_4
      56: ldc           #195                // String five
      58: ldc           #197                // String 5
      60: invokestatic  #181                // Method kotlin/TuplesKt.to:(Ljava/lang/Object;Ljava/lang/Object;)Lkotlin/Pair;
      63: aastore
      64: aload_3
      65: iconst_5
      66: ldc           #199                // String six
      68: ldc           #201                // String 6
      70: invokestatic  #181                // Method kotlin/TuplesKt.to:(Ljava/lang/Object;Ljava/lang/Object;)Lkotlin/Pair;
      73: aastore
      74: aload_3
      75: bipush        6
      77: ldc           #203                // String seven
      79: ldc           #205                // String 7
      81: invokestatic  #181                // Method kotlin/TuplesKt.to:(Ljava/lang/Object;Ljava/lang/Object;)Lkotlin/Pair;
      84: aastore
      85: aload_3
      86: bipush        7
      88: ldc           #207                // String eight
      90: ldc           #209                // String 8
      92: invokestatic  #181                // Method kotlin/TuplesKt.to:(Ljava/lang/Object;Ljava/lang/Object;)Lkotlin/Pair;
      95: aastore
      96: aload_3
      97: bipush        8
      99: ldc           #211                // String nine
     101: ldc           #213                // String 9
     103: invokestatic  #181                // Method kotlin/TuplesKt.to:(Ljava/lang/Object;Ljava/lang/Object;)Lkotlin/Pair;
     106: aastore
     107: aload_3
     108: invokestatic  #219                // Method kotlin/collections/MapsKt.mapOf:([Lkotlin/Pair;)Ljava/util/Map;
     111: astore_2
     112: aload_2
     113: astore        4
     115: iconst_0
     116: istore        5
     118: aload         4
     120: astore        6
     122: new           #221                // class java/util/ArrayList
     125: dup
     126: invokespecial #222                // Method java/util/ArrayList."<init>":()V
     129: checkcast     #224                // class java/util/Collection
     132: astore        7
     134: iconst_0
     135: istore        8
     137: aload         6
     139: invokeinterface #230,  1          // InterfaceMethod java/util/Map.entrySet:()Ljava/util/Set;
     144: invokeinterface #233,  1          // InterfaceMethod java/util/Set.iterator:()Ljava/util/Iterator;
     149: astore        9
     151: aload         9
     153: invokeinterface #68,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     158: ifeq          229
     161: aload         9
     163: invokeinterface #72,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     168: checkcast     #235                // class java/util/Map$Entry
     171: astore        10
     173: aload         10
     175: astore        11
     177: iconst_0
     178: istore        12
     180: iconst_2
     181: anewarray     #19                 // class java/lang/String
     184: astore        13
     186: aload         13
     188: iconst_0
     189: aload         11
     191: invokeinterface #238,  1          // InterfaceMethod java/util/Map$Entry.getKey:()Ljava/lang/Object;
     196: aastore
     197: aload         13
     199: iconst_1
     200: aload         11
     202: invokeinterface #241,  1          // InterfaceMethod java/util/Map$Entry.getValue:()Ljava/lang/Object;
     207: aastore
     208: aload         13
     210: invokestatic  #247                // Method kotlin/collections/CollectionsKt.listOf:([Ljava/lang/Object;)Ljava/util/List;
     213: checkcast     #249                // class java/lang/Iterable
     216: astore        11
     218: aload         7
     220: aload         11
     222: invokestatic  #253                // Method kotlin/collections/CollectionsKt.addAll:(Ljava/util/Collection;Ljava/lang/Iterable;)Z
     225: pop
     226: goto          151
     229: aload         7
     231: checkcast     #255                // class java/util/List
     234: nop
     235: astore_3
     236: aload_0
     237: iconst_0
     238: anewarray     #19                 // class java/lang/String
     241: invokestatic  #25                 // Method java/nio/file/Paths.get:(Ljava/lang/String;[Ljava/lang/String;)Ljava/nio/file/Path;
     244: dup
     245: ldc           #27                 // String get(...)
     247: invokestatic  #30                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
     250: astore        4
     252: getstatic     #36                 // Field kotlin/text/Charsets.UTF_8:Ljava/nio/charset/Charset;
     255: aload         4
     257: swap
     258: invokestatic  #42                 // Method java/nio/file/Files.newBufferedReader:(Ljava/nio/file/Path;Ljava/nio/charset/Charset;)Ljava/io/BufferedReader;
     261: dup
     262: ldc           #44                 // String newBufferedReader(...)
     264: invokestatic  #30                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
     267: checkcast     #46                 // class java/io/Reader
     270: astore        6
     272: aload         6
     274: checkcast     #48                 // class java/io/BufferedReader
     277: checkcast     #50                 // class java/io/Closeable
     280: astore        7
     282: aconst_null
     283: astore        8
     285: nop
     286: aload         7
     288: checkcast     #48                 // class java/io/BufferedReader
     291: astore        9
     293: aload         9
     295: invokestatic  #56                 // Method kotlin/io/TextStreamsKt.lineSequence:(Ljava/io/BufferedReader;)Lkotlin/sequences/Sequence;
     298: astore        10
     300: aload         10
     302: astore        11
     304: aload         11
     306: invokeinterface #62,  1           // InterfaceMethod kotlin/sequences/Sequence.iterator:()Ljava/util/Iterator;
     311: astore        12
     313: aload         12
     315: invokeinterface #68,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     320: ifeq          482
     323: aload         12
     325: invokeinterface #72,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     330: checkcast     #19                 // class java/lang/String
     333: astore        13
     335: iconst_0
     336: istore        14
     338: aload         13
     340: aload_3
     341: invokestatic  #259                // Method findAllOverlappingMatches:(Ljava/lang/String;Ljava/util/List;)Ljava/util/List;
     344: checkcast     #249                // class java/lang/Iterable
     347: ldc_w         #261                // String
     350: checkcast     #74                 // class java/lang/CharSequence
     353: aconst_null
     354: aconst_null
     355: iconst_0
     356: aconst_null
     357: aload_2
     358: invokedynamic #281,  0            // InvokeDynamic #0:invoke:(Ljava/util/Map;)Lkotlin/jvm/functions/Function1;
     363: bipush        30
     365: aconst_null
     366: invokestatic  #285                // Method kotlin/collections/CollectionsKt.joinToString$default:(Ljava/lang/Iterable;Ljava/lang/CharSequence;Ljava/lang/CharSequence;Ljava/lang/CharSequence;ILjava/lang/CharSequence;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Ljava/lang/String;
     369: astore        15
     371: aload         15
     373: invokevirtual #105                // Method java/lang/String.length:()I
     376: tableswitch   { // 1 to 2
                     1: 400
                     2: 423
               default: 428
          }
     400: new           #76                 // class java/lang/StringBuilder
     403: dup
     404: invokespecial #80                 // Method java/lang/StringBuilder."<init>":()V
     407: aload         15
     409: invokevirtual #108                // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     412: aload         15
     414: invokevirtual #108                // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
     417: invokevirtual #104                // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
     420: goto          460
     423: aload         15
     425: goto          460
     428: aload         15
     430: astore        16
     432: iconst_1
     433: istore        17
     435: aload         15
     437: checkcast     #74                 // class java/lang/CharSequence
     440: invokestatic  #114                // Method kotlin/text/StringsKt.getLastIndex:(Ljava/lang/CharSequence;)I
     443: istore        18
     445: aload         16
     447: checkcast     #74                 // class java/lang/CharSequence
     450: iload         17
     452: iload         18
     454: invokestatic  #118                // Method kotlin/text/StringsKt.removeRange:(Ljava/lang/CharSequence;II)Ljava/lang/CharSequence;
     457: invokevirtual #119                // Method java/lang/Object.toString:()Ljava/lang/String;
     460: invokestatic  #123                // Method kotlin/text/StringsKt.toIntOrNull:(Ljava/lang/String;)Ljava/lang/Integer;
     463: astore        19
     465: aload         19
     467: ifnull        478
     470: iload_1
     471: aload         19
     473: invokevirtual #128                // Method java/lang/Integer.intValue:()I
     476: iadd
     477: istore_1
     478: nop
     479: goto          313
     482: aload         7
     484: aload         8
     486: invokestatic  #134                // Method kotlin/io/CloseableKt.closeFinally:(Ljava/io/Closeable;Ljava/lang/Throwable;)V
     489: goto          513
     492: astore        20
     494: aload         20
     496: astore        8
     498: aload         20
     500: athrow
     501: astore        20
     503: aload         7
     505: aload         8
     507: invokestatic  #134                // Method kotlin/io/CloseableKt.closeFinally:(Ljava/io/Closeable;Ljava/lang/Throwable;)V
     510: aload         20
     512: athrow
     513: iload_1
     514: istore        4
     516: getstatic     #140                // Field java/lang/System.out:Ljava/io/PrintStream;
     519: iload         4
     521: invokevirtual #146                // Method java/io/PrintStream.println:(I)V
     524: return
    Exception table:
       from    to  target type
         285   482   492   Class java/lang/Throwable
         285   482   501   any
         492   501   501   any
         501   503   501   any

  public static final java.util.List<java.lang.String> findAllOverlappingMatches(java.lang.String, java.util.List<java.lang.String>);
    Code:
       0: aload_0
       1: ldc_w         #304                // String input
       4: invokestatic  #17                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       7: aload_1
       8: ldc_w         #305                // String substrings
      11: invokestatic  #17                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
      14: new           #221                // class java/util/ArrayList
      17: dup
      18: invokespecial #222                // Method java/util/ArrayList."<init>":()V
      21: checkcast     #255                // class java/util/List
      24: astore_2
      25: iconst_0
      26: istore_3
      27: aload_0
      28: checkcast     #74                 // class java/lang/CharSequence
      31: invokeinterface #86,  1           // InterfaceMethod java/lang/CharSequence.length:()I
      36: istore        4
      38: iload_3
      39: iload         4
      41: if_icmpge     117
      44: iload_3
      45: iconst_1
      46: iadd
      47: istore        5
      49: aload_0
      50: invokevirtual #105                // Method java/lang/String.length:()I
      53: istore        6
      55: iload         5
      57: iload         6
      59: if_icmpgt     111
      62: aload_0
      63: iload_3
      64: iload         5
      66: invokevirtual #309                // Method java/lang/String.substring:(II)Ljava/lang/String;
      69: dup
      70: ldc_w         #311                // String substring(...)
      73: invokestatic  #30                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
      76: astore        7
      78: aload_1
      79: aload         7
      81: invokeinterface #315,  2          // InterfaceMethod java/util/List.contains:(Ljava/lang/Object;)Z
      86: ifeq          98
      89: aload_2
      90: aload         7
      92: invokeinterface #318,  2          // InterfaceMethod java/util/List.add:(Ljava/lang/Object;)Z
      97: pop
      98: iload         5
     100: iload         6
     102: if_icmpeq     111
     105: iinc          5, 1
     108: goto          62
     111: iinc          3, 1
     114: goto          38
     117: aload_2
     118: areturn

  private static final java.lang.CharSequence day1_part2$lambda$4$lambda$3(java.util.Map, java.lang.String);
    Code:
       0: aload_1
       1: ldc_w         #322                // String it
       4: invokestatic  #17                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       7: aload_0
       8: aload_1
       9: invokeinterface #324,  2          // InterfaceMethod java/util/Map.get:(Ljava/lang/Object;)Ljava/lang/Object;
      14: checkcast     #19                 // class java/lang/String
      17: dup
      18: ifnull        27
      21: checkcast     #74                 // class java/lang/CharSequence
      24: goto          32
      27: pop
      28: aload_1
      29: checkcast     #74                 // class java/lang/CharSequence
      32: areturn
}