sasajuric

sasajuric

Author of Elixir In Action

Advent of Code - Day 14

Note: This topic is to talk about Day 14 of the Advent of Code.

For general discussion about the Advent of Code 2018 and links to topics of the other days, see this topic.

Most Liked

bjorng

bjorng

Erlang Core Team

Here is my solution.

It takes less than 3 seconds on my computer.

I store the recipes in a binary, appending to it using the binary syntax. An append operation to a binary is specially optimized by Erlang’s runtime system, in that it will allocate extra storage when appending so that the next append operation will be cheaper. See the section about constructing binaries in the Efficiency Guide.

sasajuric

sasajuric

Author of Elixir In Action

Here is my solution. It takes about 50s to finish. The long running time is because I’m dynamically building an array of about 20M elements. I resorted to using ets for this, as that works faster than using plain maps. I also briefly experimented with :array and procdict, but had no luck.

Assuming there’s no smarter algorithm, and that I didn’t make some terrible mistake, it seems that this is an example of where Erlang/Elixir are simply not performant enough. If we were able to preallocate a mutable array, this would finish much faster.

JEG2

JEG2

Author of Designing Elixir Systems with OTP

Here’s my Day 14 solution:

https://github.com/JEG2/advent_of_code_2018/blob/master/day_14/chocolate_charts.exs

I wrote pretty code then uglied it up for speed. I’m unsatisfied with where I ended up, needing about five minutes for part two.

Watch the process here (for 14 days):

Last Post!

sasajuric

sasajuric

Author of Elixir In Action

My guess is that you’re spending a lot of time in Tuple.append. Day 14 requires a lot of iterations, and modifying a tuple involves copying it, which is then going to be pretty slow.

It’s somewhat tricky to get a sensible running time here. I’ve tried with various approaches, and the best I was able to come up was around 50 seconds using ETS. However, there is a way to reduce the running time to a few seconds, as explained by @bjorng earlier in this thread.

Where Next?

Popular in Challenges Top

bjorng
Note: This topic is to talk about Day 6 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 18 of the Advent of Code 2019. There is a private leaderboard for elixirforum members. You can joi...
New
Aetherus
This topic is about Day 5 of the Advent of Code 2020 . Thanks to @egze, we have a private leaderboard: https://adventofcode.com/2020/le...
New
bjorng
This topic is about Day 14 of the Advent of Code 2020 . Thanks to @egze, we have a private leaderboard: https://adventofcode.com/2020/l...
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
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
Aetherus
Today’s problem is really tense. I don’t think I can do it without libgraph.
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New

We're in Beta

About us Mission Statement