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.

qhwa

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. :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.

ferd

ferd

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

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!

Where Next?

Popular in Challenges Top

Aetherus
This topic is about Day 3 of the Advent of Code 2020 . Thanks to @egze, we have a private leaderboard: https://adventofcode.com/2020/le...
New
Aetherus
I tried to use combinatorial to solve today’s puzzles but failed (my brain burned out :exploding_head:). In the end I just used brute for...
New
bjorng
This topic is about Day 5 of the Advent of Code 2021. We have a private leaderboard (shared with users of Erlang Forums ): https://adve...
New
bjorng
Note: This topic is to talk about Day 12 of the Advent of Code 2019. There is a private leaderboard for elixirforum members. You can joi...
New
maennchen
Ok, that was a rough one today. I haven’t found a way to improve the algorithm further. Part 1 runs in .5 seconds, Part 2 in ~ 5 minutes...
New
bjorng
This topic is about Day 10 of the Advent of Code 2021. We have a private leaderboard (shared with users of Erlang Forums ): https://adv...
New
adamu
Probably not the most efficient implementation, because part 1 took >1 ms and part 2 >4ms, but the code was simple enough. def p...
New
New
Qqwy
Note by the Moderators: This topic is to talk about Day 6 of the Advent of Code. For general discussion about the Advent of Code 2018 an...
New
christhekeele
Thought I’d kick today’s thread off! Parsing Enum rocks, so most of my code was actually in parsing input. ▶ Preprocessing input Part 1...
New

Other popular topics Top

JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
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 27727 240
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New

We're in Beta

About us Mission Statement