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
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.
Popular in Challenges
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









