I love doing challenges like this and looking at other peoples solutions. I’ve started Advent of Code the past two years but I’ve never finished. I’m determined this year. I started reading Seven Languages in Seven Weeks and plan on attempting them in some of the more obscure languages (namely prolog and io).
Here are my solutions for Day1 and Day2 in elixir, I’m hoping to knock out Day3 tonight as soon as I figure out a pattern in the spiral.
I get similar times (~ 15 sec), and I also use map to store instructions. I briefly tried with ETS, which shaved a few secs. One thing which I thought of, but didn’t try was to optimize the case where the current instruction is 0. In that case, you can skip extra map store/lookup and immediately move on to the next iteration (though you need to somehow indicate that you made two moves). I didn’t really have the time to try it out, and it would uglify the code, so I just left it as it was.
Here’s my day 5 solution. I originally kept it simple using list lookups and replacements, and later made it a little faster by reducing the initial list into a map.
I spent some time (way too much time) trying to come up with a divide and conquer or backtracing algorithm to make things more efficient, but the changing offsets seemed to make this impossible. Sub-solutions can’t be guaranteed to be isolated. I’d love to see if someone found an efficient solution.