bjorng

bjorng

Erlang Core Team

Advent of Code - Day 19

Note: This topic is to talk about Day 19 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 for Day 19.

I did not even attempt to make a solution that would solve part 2 for any input (in a reasonable time).

Instead, I looked at the trace of the execution of my input and found the inner loop. I then wrote an optimized version of the inner loop.

To run the optimized version, I implemented a general mechanism for installing a breakpoint handler and installed my optimized inner loop as a breakpoint handler. The breakpoint handler will only be installed for code similar to my input program (it must be the same instructions, but it registers could be different).

My solution runs in less than 20 seconds.

stefanchrobot

stefanchrobot

I actually wrote a decompiler/debugger in Elixir to poke with the program. It was sooo much fun!


With the tool at hand, after a few initial steps in which the program initializes the register 5 with a big number, I could run “seti 10 0 5” while the program was running. After that it was quite easy to trace what the program is computing.

sasajuric

sasajuric

Author of Elixir In Action

This idea with installing the optimized loop is very interesting.

I just reverse engineered the entire program. I started by inspecting the first 100 states to quickly figure out where’s the inner loop, what is its input, and what is the effect of a single pass through it on the registers. Then I annotated my program and spent some time understanding what it does. After awhile, it became clear to me that the program first computes the input number, stores it to r4, and then iteratively finds all the divisors of that number, adding them into r0 (which is set to 0 before entering the loop). So finally, I wrote the equivalent program in Elixir :slight_smile:

The complete solution is here, and it takes about 6 sec combined for both parts.

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