bjorng
Advent of Code 2019 - Day 13
Note: This topic is to talk about Day 13 of the Advent of Code 2019.
There is a private leaderboard for elixirforum members. You can join it by following this link and entering the following code:
39276-eeb74f9a
Most Liked
ferd
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.
ihabunek
I made a video of the game being played:
https://asciinema.org/a/iCFURolasxTLhQsXCYll9rmKk
ferd
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.
qhwa
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. 
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.
ferd
Today was the best! I ended up implementing the whole game in interactive mode with ncurses support (which I had never used before) and it freaking rules. Then I plugged an “AI” back in it to beat the game while I watch and get the final score.
I actually had a lot of fun with this one. Great job to the intcode computer too!







