r/adventofcode • u/joeld • Dec 13 '18
Help [2018 Day 9 Part 2] Racket solutions all too slow.
I'm currently stuck on Day 9 part two. I did one reeeaally ugly attempt using mutation. Then I tried a nice elegant FP solution with no mutation. My part 1 answer is correct in both cases, but running the code to solve for part two is so slow that basically never finishes.
After some searching and reading, it seems as though there might be some hope in using zippers, the immutable/FP answer to linked lists (concept). There is a Racket library implementing zippers but I am struggling to understand its use from the docs, including how I would remove a value from a zipper (as when removing a marble from the circle).
So here's where I'm at:
- Is the concept of zippers really likely to yield any speed improvements over what I have so far?
- Assuming zippers are worth looking into, does anyone have some cohesive code examples demonstrating their use (using the Racket
zipperspackage or some other implementation)? - Conversely, is Racket just too slow and inefficient for this type of problem?
[Update: I did finally grok the zipper concept, I did implement it by hand thanks to some light explaining by /u/permetz and I got Part 2 to finish in about 7.5 seconds. Here's my final product. Thanks all who chimed in!]
2
u/permetz Dec 13 '18 edited Dec 14 '18
First, zippers for lists are trivial to implement by hand. You don’t need a package for them in a lisp dialect. Second, I used zippers for my OCaml version of the code and they provided more than sufficient speedup; they’re a good option for a functional implementation.
1
u/clintm Dec 13 '18
This is probably better asked, or at least crossposted to r/racket.
1
u/Etsap1 Dec 13 '18
Your code comment here scares me: In this variation, the circle is stored as a list. The “current” marble is always the first item in the list.
Are you sure this isn't doing a tremendous amount of memory manipulation to move around the list items in physical memory? Not sure of the underlying implementation in this language and what it will actually do, but if execution time is the problem that's what I'd look at. I had mine simply create a new list with each pass (with some unfortunately ulgy boundary condition logic for when the scoring marble has to wrap around for the minus 7). This was cheap because I never had to move anything except the last 7 items, and appending a single item to the end of a list is always fast.
2
u/misiakw Dec 13 '18
My approach was to implement this circle structure, not to symulate it on array. Ive created object for marble with pointer to the righthand and lefthand marble. Then i created first marble pionting to itselt. All the problem from this point was just to jump throught pointesr rught and left, breaking one connections and inserting new ones. Ive written this in c# and got quite fast code.