-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay14.kt
36 lines (32 loc) · 1.32 KB
/
Day14.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package aoc2018.day14
fun generateRecipes(max: Int): String {
val scoreList = mutableListOf(3, 7)
var index1 = 0
var index2 = 1
repeat(max + 10) {
val recipesToAdd = (scoreList[index1] + scoreList[index2]).toString().map { Character.getNumericValue(it) }
scoreList.addAll(recipesToAdd)
index1 = (index1 + 1 + scoreList[index1]) % scoreList.size
index2 = (index2 + 1 + scoreList[index2]) % scoreList.size
}
return scoreList.subList(max, max + 10).joinToString("")
}
fun generateRecipesPart2(pattern: Int): Int {
val recipeList = mutableListOf(3, 7)
var index1 = 0
var index2 = 1
repeat(pattern * 17) {
val recipesToAdd = (recipeList[index1] + recipeList[index2]).toString().map { Character.getNumericValue(it) }
recipeList.addAll(recipesToAdd)
index1 = (index1 + 1 + recipeList[index1]) % recipeList.size
index2 = (index2 + 1 + recipeList[index2]) % recipeList.size
}
val recipeString = recipeList.joinToString("")
return recipeString.indicesOf(pattern.toString())
}
fun String?.indicesOf(substr: String, ignoreCase: Boolean = true): Int {
return this?.let {
val regex = if (ignoreCase) Regex(substr, RegexOption.IGNORE_CASE) else Regex(substr)
regex.findAll(this).map { it.range.first }.toList()[0]
} ?: -1
}