bjorng

bjorng

Erlang Core Team

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

ferd

Author of Property-Based Testing with PropEr, LYSE, & Erlang in Anger

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

ihabunek

I made a video of the game being played:
https://asciinema.org/a/iCFURolasxTLhQsXCYll9rmKk

ferd

ferd

Author of Property-Based Testing with PropEr, LYSE, & Erlang in Anger

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.

Where Next?

Popular in Challenges Top

igorb
I found today a bit tedious: advent-of-code-2024/lib/advent_of_code2024/day15.ex at main · ibarakaiev/advent-of-code-2024 · GitHub.
New
bismark
Took me a minute to remember my binary math :smile: :grimacing:.. import Bitwise __DIR__ |> Path.join("puzzle.txt") |> File.strea...
New
igorb
So… that’s it? Everyone is stuck on part 2? :slight_smile: I looked at Reddit hints and thought I probably wouldn’t have come up with the...
New
kwando
Phew, this one took a while to get right. My naive attempts was way to slow so I reached for Dijkstras shortest path algorithm.. and that...
New
bjorng
This topic is about Day 6 of the Advent of Code 2021. We have a private leaderboard (shared with users of Erlang Forums ): https://adve...
New
shritesh
This was way too easy after the last few days. Simple map, filter and count. https://github.com/shritesh/advent/blob/main/2023/06.livemd
New
bjorng
Note: This topic is to talk about Day 3 of the Advent of Code 2019 . There is a private leaderboard for elixirforum members. You can jo...
New
bjorng
My solution finishes both parts in 5 seconds on my computer. That time should be possible to reduce by optimizing my rather naive tilt/2 ...
New
bjorng
Note: This topic is to talk about Day 9 of the Advent of Code 2019. There is a private leaderboard for elixirforum members. You can join...
New
bjorng
Note: This topic is to talk about Day 2 of the Advent of Code 2019 . There is a private leaderboard for elixirforum members. You can joi...
New

Other popular topics Top

danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29703 241
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31194 112
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New

We're in Beta

About us Mission Statement