r/adventofcode • u/Billaloto • Jan 08 '19
Help Day 14 part 2 does not terminate
I did not think I was doing anything expensive in this recursion,
all tests pass ofc but the input does not terminate or take more than a night ^^
I must miss something in the algorithm or is carrying around such a string so expensive?..
3
Upvotes
2
u/Balise Jan 08 '19
Hint: it can happen that you hit the right string without the last characters being the string you are testing against, because you may add several characters at a time :)
2
u/jk2432 Jan 09 '19
I don't know Scala. Can you explain what you expect this to find?
if (newState.takeRight(recipesNbStr.length * 2).contains(recipesNbStr))FWIW, I used a
whileloop instead of recursion.2
u/GeneralYouri Jan 11 '19
You mentioned in comments that you still have a 13hr runtime. Here's a couple of things that may be contributing to such long runtime:
recipesNb.toStringevery iteration, when you can just stringify it once and store that. I'm doing some 15 million iterations; stringifying a number 15 million times doesn't sound like it'd be very fast to me.%operators to determine which new recipes to add. The%operator is expensive, and at 15 million iterations you're using it 90 million times. You can in fact rewrite this part without using this operator at all. Hint: You can replace the expensive modulo with some addition and subtraction, much cheaper.if (newState.takeRight(recipesNbStr.length * 2).contains(recipesNbStr)). But you can still reduce this further. Hint: what's the largest value possible forr1andr2, and thus their sum? And what does that mean for the number of recipes added per iteration, and thus the number of string checks you need to perform?.asDigityou're doing twice every iteration, but I don't know Scala so I don't know exactly what it does or what its runtime is. It looks like a form of typecasting, which is why I'm even listing it here, because these have the potential to be very expensive (see the first point about.toStringfor example). The.asDigithere may be fast or slow, I don't know.2
u/sim642 Jan 09 '19
Instead of takeRight you can just use indexOf starting from almost end, where the change happens, it avoids constructing new strings all the time. Also you don't need 2 times its length but exactly the length.
My Scala solution
2
u/askalski Jan 14 '19
The most likely issue is this line:
val newState = state + (r1 + r2)Assuming Scala strings are immutable (I'm not much familiar with Scala), this will make a copy of
stateeach time, which means that by the end of Part 2, the program will have copied many terabytes of data around. You should consider using a mutable string object such asStringBuilderwhich offers an append operation that avoids copying the entire string.