My solution works on testInput, but when I run it ...
# advent-of-code
s
My solution works on testInput, but when I run it on the actual input, I run out of memory. What should I do?
r
have a look at this thread, where today's problem is being discussed without providing actual solutions, which might give you some pointers: https://kotlinlang.slack.com/archives/C87V9MQFK/p1701767182803779
d
Please thread questions in the “Advent of Code Day N” thread
s
I am afraid I still haven't found anything.. The problem is that my code runs out of solution while adding seeds from the input, not in finding the minimum location or any other values..
in here
d
You should not try to brute force this
s
The part that I add value to the seed, exactly?
d
Yes the proper solution doesn’t check all seeds in the range
s
oh, okay. I'll try to find a way. Thanks
n
While I agree that brute-force is not the way to go, if you want to try it the trick is to generate the seeds on-the-fly via sequence or regular imperative loop. Then you only need to track the lowest result you've found, so you don't run out of memory.
s
So, something like this: For each value in sequence, calculate the location of that seed, store the location in a variable and if the next value has a lower location, store that instead, and return the location at the end, which is of course the min location value. Is this correct?
d
yes you could do that, it may take about 10 minutes to run depending on how fast your seed->location method is
s
Okayy, thank you very much. I'll try it right away.
c
Unlike other problems of this ilk you can get away with brute forcing this. Just make sure you're not storing lists in memory.
s
So, I implemented my program to get rid of the memory error. Just wondering, does changing this value boost the run time or not?
image.png
n
It should make it run faster since you're not commiting things to memory and recalling them again. But it will still be very slow.
s
okay, so you solved the part 2 already, right? How much time did your solution take?
I mean, if it's longer than 10 mins, I have a thing to do and, come back,,
d
79 micros
s
79 micro seconds??
n
I did a bruteforce solution but I suspect it was different than yours. I didn't start with the seeds. I started with the output. I tried 0, ran it backwards through the maps, and ended up with the starting seed is to arrive at 0. Then I checked whether that seed was in the startingSeed ranges. If not, I proceeded to 1, then 2, etc. until one matched. That took 10 seconds.
I then refactored to how all the cool kids are doing it, and it was 26ms. Not as fast as Dan, but enough to call it a day.
d
Yes, not using brute force
s
Wow, mine definitely looks like an infinite loop, compared to yours, haha. But I hope it doesn't end with a bug or wrong answer after waiting that long,
n
That's one reason not to carry on trying to get the naive bruteforce to work. Especially dealing with ranges, very easy for off-by-one errors. Bruteforce is really hard to debug, especially if you have to wait 10 minutes to see if you got it wrong!
d
Yes one of the guidelines is “every problem has a solution that completes in at most 15 seconds on ten-year-old hardware”
1
s
Haha you guys are right. But the problem is that I haven't learned a lot about data structures, algorithms or optimization methods yet. So, implementing any other solution is difficult for me.
r
regarding the memory setting you're mentioning here: https://kotlinlang.slack.com/archives/C87V9MQFK/p1701812401785649?thread_ts=1701809361.246379&cid=C87V9MQFK, I think that's only setting the max heap size for the IntelliJ IDE itself, not for any program you're running from your IDE, because that will start a new Java process (with its own memory settings)
n
Haha you guys are right. But the problem is that I haven't learned a lot about data structures, algorithms or optimization methods yet.
Don't let that stop you! My last computer class was in high school in 1995. And I'm a lawyer, not a programmer. You learn by doing (and asking for help... and reading wikipedia...)!
s
Hmm, so it's more useful for Android Studio since it deals with emulator and decomposition. But not as much for Intellij
r
@Safi K don't give up, even if you don't succeed at solving all the problems every day, it's all about learning and having fun. If that means at some point you get stuck and have to look at other people's solutions, don't feel bad about that. We all start somewhere, I've been doing AoC for a few times now and it's a struggle at times, but keep it fun for yourself! Don't feel the need to compete with others (I'm having fun even with my hacky solutions that are far from the top solutions 😁)
n
This is an insanely difficult year. Usually the first 10 days are way easier than this.
d
Yeah my response to this question is, thanks for the math homework 😄
😁 1
First it was wall of text crits you for 20 damage
s
Don't let that stop you! My last computer class was in high school in 1995. And I'm a lawyer, not a programmer. You learn by doing (and asking for help... and reading wikipedia...)!
That's true, thank you. I will try to start solving problems earlier tomorrow, so I'll have more time to find better solutions and optimizations. Right now, I really am in a hurry..
don't give up, even if you don't succeed at solving all the problems every day, it's all about learning and having fun..
Thank you, I'll try my best. I was not able to solve day three's second part because I misunderstood something, so I moved on to day 4 anyways, and now to day 5. Yeah, comparing to others is the worst. I do it sometimes but with IG models actually, than other programmers lol 😂 And it's my first year doing AoC.
c
Brute forcing mine takes about 5 minutes
s
Well, it's been about 20 mins and the program is still running. I think it's useless to wait more for it..
I should find a better solution now.
j
a tip for bruteforcing: have some periodic output to show the progress, that way you can estimate how much longer you need to wait. A friend of mine did today's problem the brute force way and waited one hour for the solution.
👍 1
n
Yes one of the guidelines is “every problem has a solution that completes in at most 15 seconds on ten-year-old hardware”
The funny thing is that the most compute-intensive problems are in the early years when 10 year old computers were ancient by today's standards. All that MD5 hashing... hard to come up with a shortcut for those. I got 2016 day 14 down to 2 seconds (cold start) using parallelism on my 2018-era Ryzen 5 CPU. In 2006, Intel had just released their first dual-core desktop offering. I can't imagine doing that many hashes on that in 15 seconds without getting awfully close to bare metal.
s
a tip for bruteforcing: have some periodic output to show the progress, that way you can estimate how much longer you need to wait. A friend of mine did today's problem the brute force way and waited one hour for the solution.
Thank you, I had to do it now haha.