Advent of Code 2019 - Day 13

If I remove all the sleeps, my own program took about 550ms on my input while drawing, and and about 450ms without drawing. I do handle all inputs and outputs individually and with message passing as well. I’m not sure they should be that expensive.

Looking at the source code for the computer though: advent/lib/2019/computer.ex at master · cblavier/advent · GitHub

This little bit is:

          if new_output do
            if output_pid, do: send(output_pid, {:input, new_output})
            outputs ++ [new_output]
          else
            outputs
          end

You end up appending more and more output as the game moves forwards because you accumulate it.
You’ll drastically speed up the program if you do [new_output|outputs] there and reverse the result when you return the list of outputs at the end of the program.

This endless suffixing is probably explaining all the time lost to rewriting and GCing terms. Reversing once at the end will ensure O(1) adding within the tight loop and a single rewrite once the program is done.

7 Likes

Wow you are good :slight_smile:

Just changed this tiny piece of code and runtime changed from 7sec to 0.7sec
Great lesson today, thanks again!

:heart:

Still I would like to understand more about :

This endless suffixing is probably explaining all the time lost to rewriting and GCing terms. Reversing once at the end will ensure O(1) adding within the tight loop and a single rewrite once the program is done.

How so is this so different to append or prepend to a list? I would think it’s just about adding a pointer to a chained list, but I think I’m lacking knowledge about how things actually work under the hood

It would only be about adding a pointer — if data were mutable. But this is the Erlang VM and data is immutable. If you want someone who holds an older reference to data not to see it change, your only option is to rewrite the whole linked list.

Note that this isn’t the case with prepending since a new list head can point to any existing tail without that tail having to change. That’s why the common recursive pattern is to prepend a new head for each element during an iteration and to then reverse once (rewrite once); one rewrite for the whole list rather than one list rewrite per element added.

4 Likes

Got it, thanks!

I didn’t use programmed input… It took one day for me to finish part 2 from the start because I made a real brick game in terminal then I chose to play by myself, manually. :joy:

I really enjoyed it because I learned how to make a terminal game in Elixir, like this awesome 2048 game I found. There were a lot of techs I didn’t know before.

And here is my solution, for your information.

3 Likes