Because if you tell people not to do something, a certain portion of them will want to do it just to prove you wrong. (That's definitely why I used recursion in my solution to your input. 😂 )
haskell doesn't use a call stack -- all function calls are on the heap. So you can still blow the heap, but that's usually a lot bigger than a call stack.
I adapted my earlier recursion + memoization solution to no longer use recursion. This version still uses memoization, but recursion is replaced with a queue. I got held up for a long time because I had a subtle error in my queue implementation that caused it to occasionally lose a queued value. This would not be an issue if JS had a proper set of standard data structures (Map and Set are nice, but not enough).
Trimming the output to the appropriate format (last 10 non-zero digits) was surprisingly difficult. I used a regular expression + manual string trimming because I am lazy.
This code finds the solution 3142155264 in about 2.5 seconds. It could probably be optimized a lot by replacing the .forEach() and .filter() calls with manual loops, but that would be more effort than its worth considering this code is already plenty fast.
Just for fun, the function can return results for arbitrary joltage spans. For this problem, the span is fixed at 3.
The function runs in O(n) time and O(1) space. (Technically it's O(m) space, where m is the maximum joltage span. Since m is fixed for this problem, it's O(1) relative to the length of the input data.)
The function uses recursion. (But all of the recursion is done in tail calls, so SBCL optimizes away the call stack.)
It takes 580 ms to run in its VM on a 1.9GHz Intel i7-8665U. (Loading the data from file takes 350 ms, and extracting the result digits takes 200 ms.)
Edit: I guess to count this as using O(1) space, I have to ignore the fact that it uses bignums for the totals.
Edit 2: I made some small tweaks to the bignum arithmetic and dropped the runtime from 4.8 seconds to 580 milliseconds. Mostly, that was to stop adding bignums to zero.
My solution isn't recursive, but it uses itertools.combinations, and I expected the worst. 3.87s, so before the heat death of the universe which is all I ask if my AOC solutions.
As of writing this comment, my copy/paste of the input file into vim is still running. Use wget folks.
For your input! It's not just "try every combination" which would definitely not work. I spent a lot of time trying to decide what math/data structure was best before just going "well let's try the lazy way" and lucky the runtime isn't bad at all.
Summary: it breaks up the sorted list into subgroups by detecting all the 3 jolt differences and then runs combinations for the adapters between the min and max in each group. The answer is the product of each subgroup.
I'm sad that the numbers get too big for my bash command line solution (also, I hard-coded the combinations that occurred in my puzzle input, so I'd have to be smarter and deal with 2-jumps and groups larger than 5 consecutive, neither of which I had to do for my own input).
3142155264 Well, I still solved it with recursion and a cache, plus some chunked processing.
Of course your input stack overflowed, but I could "fix" that by simply chunking the processing, starting at the back (higher values). This means worst case I need to recurse chunk_size times, which is easily tweakable. Currently it sits at 10000, but I found no significant difference between 100, 1000 and 10000.
My Rust solution runs in 4.2s on my i7-6600k (release config), including file reads and parts 1 and 2.
Code is here, but I am still learning Rust, so do not use it as a style guide. If you want to run it, wget the input file to more.txt
Having to figure out last nonzero digits in a large number with a lot of trailing zeroes reminded me of this Project Euler problem. Except that 50k digits is nothing compared to the beast you have to handle there...
8
u/[deleted] Dec 10 '20
https://i.imgur.com/GzMcrOH.jpg
6
u/djavaman Dec 10 '20
"takes the fun out of the optimisation"
memoization is a perfectly valid optimization. If you calced something once, don't calc it again. DRY.
2
u/geckothegeek42 Dec 10 '20
Ooh, you snuck in some 2 wide gaps, I like it
60ms on my laptop (3us for their input)
Whats the answer? (DM if you want), I dont think what i get is correct but I don't see why its wrong
2
u/S4x0Ph0ny Dec 10 '20
So that was the actually hardest part about your challenge for me, maybe you could've asked for the first 10 digits ;)
To be fair I was being an idiot at first trying to right shift based on the trailing zeroes.
2
u/mstksg Dec 10 '20
My recursive solution crunches through it in 1.1s :)
2
u/compdog Dec 11 '20
JavaScript (Node.JS)
I adapted my earlier recursion + memoization solution to no longer use recursion. This version still uses memoization, but recursion is replaced with a queue. I got held up for a long time because I had a subtle error in my queue implementation that caused it to occasionally lose a queued value. This would not be an issue if JS had a proper set of standard data structures (Map and Set are nice, but not enough).
Trimming the output to the appropriate format (last 10 non-zero digits) was surprisingly difficult. I used a regular expression + manual string trimming because I am lazy.
This code finds the solution 3142155264 in about 2.5 seconds. It could probably be optimized a lot by replacing the .forEach() and .filter() calls with manual loops, but that would be more effort than its worth considering this code is already plenty fast.
1
u/fzmad Dec 10 '20
3142155264
1
u/ephemient Dec 10 '20 edited Apr 24 '24
This space intentionally left blank.
1
u/phil_g Dec 10 '20 edited Dec 11 '20
I get 3142155264.
Here's a link to my code (in Common Lisp). Fun facts:
Edit: I guess to count this as using O(1) space, I have to ignore the fact that it uses bignums for the totals.
Edit 2: I made some small tweaks to the bignum arithmetic and dropped the runtime from 4.8 seconds to 580 milliseconds. Mostly, that was to stop adding bignums to zero.
1
u/dan_144 Dec 10 '20
My solution isn't recursive, but it uses itertools.combinations, and I expected the worst. 3.87s, so before the heat death of the universe which is all I ask if my AOC solutions.
As of writing this comment, my copy/paste of the input file into vim is still running. Use wget folks.
1
u/stickdude90 Dec 10 '20
Javascript and very large numbers don't mix (but it took less than a second to get there).
Answer: Infinity
1
u/Marcus316 Dec 10 '20
I'm sad that the numbers get too big for my bash command line solution (also, I hard-coded the combinations that occurred in my puzzle input, so I'd have to be smarter and deal with 2-jumps and groups larger than 5 consecutive, neither of which I had to do for my own input).
1
u/catt0 Dec 10 '20
3142155264 Well, I still solved it with recursion and a cache, plus some chunked processing.
Of course your input stack overflowed, but I could "fix" that by simply chunking the processing, starting at the back (higher values). This means worst case I need to recurse chunk_size times, which is easily tweakable. Currently it sits at 10000, but I found no significant difference between 100, 1000 and 10000.
My Rust solution runs in 4.2s on my i7-6600k (release config), including file reads and parts 1 and 2.
Code is here, but I am still learning Rust, so do not use it as a style guide. If you want to run it, wget the input file to more.txt
1
u/MichalMarsalek Dec 10 '20 edited Dec 10 '20
Having to figure out last nonzero digits in a large number with a lot of trailing zeroes reminded me of this Project Euler problem. Except that 50k digits is nothing compared to the beast you have to handle there...
0
u/[deleted] Dec 10 '20
[deleted] — view deleted comment