-
I solved Day 16 (link to the problem) in Scryer-Prolog with what I thought was a very straight-forward implementation (link to the code). However processing an input of 110x110 characters results in almost 60 seconds of running time for part 1, people do similar algorithms in orders of milliseconds in other languages. This discussion is more of a call for guidance. What is going wrong here? Am I not choosing the right data structures, i.e. should I use for example assocs for whenever I want to check membership on seen states? Will that make such a big difference? Is Prolog not suitable for this kind of matrix manipulations? Am I writing faulty predicates that compute too much? Part 2 even consumes the full 64gbs of RAM of my machine without finishing. Quoting the Power of Prolog:
Maybe I'm falling in both categories? So perhaps this ticket can be seen as a call for review and critique on the code I wrote. If you take some minutes to read through the code, thanks in advance! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
One thing I noticed in the code you posted is: matrix_nth0(X-Y, Mat, E) :- nth0(Y, Mat, L), nth0(X, L, E). So, access to any element has linear expected time in the size of the matrix. If you instead, using May I suggest that you please post a program that compiles, with a description of what you did to run it, so that others can reproduce the issue? Thank you a lot! |
Beta Was this translation helpful? Give feedback.
One thing I noticed in the code you posted is:
So, access to any element has linear expected time in the size of the matrix. If you instead, using
library(assoc)
, represent the matrix as an AVL tree with keys of the formX-Y
, then you reduce this to logarithmic worst-case overhead for accessing any elementX-Y
in the matrix. That's not the O(1) worst-case access time we get from programming languages with fast array operations as built-ins (such as APL or J), but can improve the running time by many orders of magnitude, if this matrix access is the bottleneck in your use case.May I suggest that you please post a program…