Skip to content

Poor performance results in a seamingly simple algorithm (Advent of Code 2023, Day 16). Help please! 🙏 #2235

Answered by triska
jasagredo asked this question in Q&A
Discussion options

You must be logged in to vote

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 library(assoc), represent the matrix as an AVL tree with keys of the form X-Y, then you reduce this to logarithmic worst-case overhead for accessing any element X-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…

Replies: 1 comment 5 replies

Comment options

You must be logged in to vote
5 replies
@UWN
Comment options

@jasagredo
Comment options

@jasagredo
Comment options

@triska
Comment options

@jasagredo
Comment options

Answer selected by jasagredo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants