Another tailrec solution for the books. I had some issue with how long this took. After a few frustrating minutes, I figured out that .takeLast() was not as efficient as the .get() or array access notation.
tailrec
.takeLast()
.get()
private fun answer1(input: Int): Any? { val elf1 = 0 val elf2 = 1 val recipes = mutableListOf(3, 7) return findRecipes(2, elf1, elf2, recipes, input) } private tailrec fun findRecipes( n: Int, elf1: Int, elf2: Int, recipes: MutableList<Int>, limit: Int ): String { if (n > limit + 10) { return recipes.drop(limit).take(10).joinToString("") } val re1 = recipes[elf1] val re2 = recipes[elf2] val newRecipes = when { re1 + re2 > 9 -> mutableListOf(1, (re1 + re2) % 10) else -> mutableListOf((re1 + re2) % 10) } val nextN = n + newRecipes.count() recipes.addAll(newRecipes) return findRecipes( nextN, (elf1 + re1 + 1) % nextN, (elf2 + re2 + 1) % nextN, recipes, limit ) }
This one threw me, since the solution was so much farther out than I expected...
private fun answer2(input: Int): Any? { val elf1 = 0 val elf2 = 1 val recipes = mutableListOf(3, 7) return findRecipes2( 2, elf1, elf2, recipes, input.toString().map { it.toDigit() }) } private tailrec fun findRecipes2( n: Int, elf1: Int, elf2: Int, recipes: MutableList<Int>, check: List<Int> ): Int { val ccount = check.count() if ((n > ccount) && (n - ccount until n).map { recipes[it] } == check) { return n - ccount } else if ((n > ccount) && (n - ccount until n).map { recipes[it - 1] } == check) { return n - ccount - 1 } val re1 = recipes[elf1] val re2 = recipes[elf2] val newRecipes = when { re1 + re2 > 9 -> mutableListOf(1, (re1 + re2) % 10) else -> mutableListOf((re1 + re2) % 10) } val nextN = n + newRecipes.count() recipes.addAll(newRecipes) return findRecipes2( nextN, (elf1 + re1 + 1) % nextN, (elf2 + re2 + 1) % nextN, recipes, check ) }
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Kotlin Solution
Part 1
Another
tailrec
solution for the books. I had some issue with how long this took. After a few frustrating minutes, I figured out that.takeLast()
was not as efficient as the.get()
or array access notation.Part 2
This one threw me, since the solution was so much farther out than I expected...