bjorng

bjorng

Erlang Core Team

Advent of Code 2021 - Day 18

This topic is about Day 18 of the Advent of Code 2021.

We have a private leaderboard (shared with users of Erlang Forums):

https://adventofcode.com/2021/leaderboard/private/view/370884

The entry code is:
370884-a6a71927

Most Liked

ramuuns

ramuuns

soo much tedious debugging of recursion, but I love the fact that the parsing is pretty much just a Code.eval_string

also I’m pretty sure I shot myself in the foot for having each node be a {depth, [left, right]} as I think this was making some of the recursion annoying and it was easy to accidentally create the node wrong and whatnot.

Anyhow, both parts run in about 100ms - aoc/day-18.ex at 1542d38365d74533b324c4f790699767ce81b8e9 · ramuuns/aoc · GitHub

Update:
since I hate myself and apparently “enjoy spending a few more hours debugging recursion”, I implemented another version, that runs slightly faster where each node is a {depth, needs_split?, [left, right]}. Besides the ~10% perf improvement, it does slightly simplify the splitting operation at the cost of having to keep track if a node (or any of its children) need to be split. Code for the “improved” version: aoc/day-18.ex at 3419958dee43037aded833dc46c4fde183443add · ramuuns/aoc · GitHub

zevv

zevv

No sir, I did not at all like today’s puzzle sir. Every year there is at least one like those: too much text describing a tedious arbitrary process that’s a PITA to debug because you missed one of the many rules.

Anyway, I fought with a solution for too long where the numbers were stored as nested lists, but the recursion caused a pain behind my eyes. I fell back to just storing the numbers in a list, and exploding them by keeping track of a left and right carry that gets added to the next number to arrive. It suits nice with pattern matching, the end result is not too bad IMHO (even though some of the code looks more like Perl then Elixir), and fits in under a 100 lines,

bjorng

bjorng

Erlang Core Team

Here is my solution:

UPDATE: I’ve also implemented a solution in Erlang with a different internal representation that simplifies the explode operation.

stevensonmt

stevensonmt

Quite late, but I really struggled with getting the explode logic to work. I also overcomplicated things implementing a tree structure rather than just using nested lists. I am excited that I was finally able to make it work though because I’ve always struggled with tree logic and I think this exercise helped solidify it a bit. I still don’t quite understand why I had to rebuild the tree after each update of the left side in the explode function. Really adds to the time complexity (would be O(n) at least anyway, but with that it’s at least O(3n) with n = length(steps). I also only really figured it out after I broke down and wrote tests for each step along the way. First time I’ve really written somewhat non-trivial tests from scratch in elixir.

Where Next?

Popular in Challenges Top

Aetherus
This topic is about the Advent of Code 2021 - Day 4. Thanks to @bjorng , we now have a new Private Leaderboard. The entry code is: 370...
New
bjorng
This topic is about Day 16 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 3 of the Advent of Code 2019 . There is a private leaderboard for elixirforum members. You can jo...
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
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
Aetherus
This topic is about Day 7 of the Advent of Code 2020 . Thanks to @egze, we have a private leaderboard: https://adventofcode.com/2020/le...
New
stevensonmt
Anyone else think the prompt for this challenge is contradictory? The rules for comparing packets include If both values are lists, c...
New
bjorng
This topic is about Day 1 of the Advent of Code 2021. We have a private leaderboard (shared with users of Erlang Forums): https://adven...
New
Aetherus
Today’s problem is really tense. I don’t think I can do it without libgraph.
New
connorlay
Note by the Moderators: This topic is for general discussion about the Advent of Code 2018. To prevent people from being spoiled about s...
New

Other popular topics Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
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
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
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
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

We're in Beta

About us Mission Statement