{"componentChunkName":"component---src-templates-article-js","path":"/advent-of-code-2020","result":{"data":{"markdownRemark":{"html":"<p><a href=\"https://adventofcode.com/\">Advent of Code</a> is a Christmas-theme programming competition that takes place every year during the first 25 days of December. Usually, it's your job to save Christmas!</p>\n<p>I've been participating every year for the past five years, and I always end up getting addicted and spending a lot of time on the challenges, so this year I decided that I wouldn't be participating. Naturally, this resistance lasted a single day, and, except for day 1, I managed to solve all exercises in their publish-day.</p>\n<p>I wanted to share some insight into the challenges that I found interesting and enjoyed the most.</p>\n<h2>Day 10: Adapter Array</h2>\n<p>This challenge consisted of helping our hero to connect a set of <em>joltage</em> adapters. Part 1 was pretty straight-forward. However, the second part was a bit more challenging. This was the first challenge that wasn't solvable using a brute-force solution (as visible in the leaderboard). I ended up using a <code>DFS</code> + cache solution, although the challenge was screaming for dynamic programming:</p>\n<pre><code class=\"language-scala\">def dfs(joltages: ArrayBuffer[Long], i: Int): Long = {\n    if (cache.contains(i)) return cache(i)\n\n    cache(i) = (1 to 3).map(j => {\n        if ((i + j &#x3C; joltages.length) &#x26;&#x26; (joltages(i + j) &#x3C;= joltages(i) + 3)) dfs(joltages, i + j)\n        else 0\n    }).sum\n\n    cache(i)\n}\n</code></pre>\n<h2>Day 13: Shuttle Search</h2>\n<p>This one consisted of fixing a set of bus schedules. Similar to the previous one, part 1 was quite trivial. When reading part 2, I had some flashbacks of the Discrete Mathematics course I took about four years ago, specifically about the <a href=\"https://en.wikipedia.org/wiki/Chinese_remainder_theorem\">Chinese Remainder Theorem</a>. In short, it consists of finding a number <code>N</code>, such that:</p>\n<pre><code>N = a (mod x)\nN = b (mod y)\nN = c (mod z)\n...\n</code></pre>\n<p>With a few tweaks and tricks, this can be applied to our problem. Assuming that the input is <code>17,x,13,19</code> and that we want to find the earliest timestamp <code>T</code>:</p>\n<pre><code>T = 0 (mod 17)\nT + 2 = 0 (mod 13)\nT + 3 = 0 (mod 19)\n</code></pre>\n<p>...and by applying a bit of mod-space algebra ...</p>\n<pre><code>T = 0 (mod 17)\nT = 13 - 2 (mod 13) = 11 (mod 13)\nT = 19 - 3 (mod 19) = 16 (mod 19)\n</code></pre>\n<p>Although after browing through the <a href=\"https://www.reddit.com/r/adventofcode/\">AoC Reddit</a> I found out that there were other ways of solving this, I'm absolutely sure this is the coolest one 😎</p>\n<h2>Day 17: Conway Cubes</h2>\n<p>Although not difficult, this challenge took <a href=\"https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life\">Conway's Game of Life</a> to the next level by creating a 3-dimensional and 4-dimensional version in parts 1 and 2, respectively. Even though I'm pretty happy about my solution, it doesn't need to be very efficient since the problem statement only asks for 6 iterations.</p>\n<h2>Day 18: Operation Order</h2>\n<p>This one consisted of evaluating a numeric expression. The twist is that operators have different levels of precedence, which was a bit counter-intuitive! I used a stack-based approach to solve it linearly, similarly to my solution for a <a href=\"https://leetcode.com/problems/basic-calculator-ii/\">LeetCode Calculator Problem</a> I had solved about a year.</p>\n<h2>Day 23: Crab Cups</h2>\n<p>In this challenge, the goal was to play a version of the cups game with a crab (yeap, it's a Christmas-themed competition). Part 1 had a very small-sized input, so it was solvable without the need for a very elaborate solution. However, the input size was much larger in part 2. Since I was pretty much expecting the differences in input size to be substantial between both parts, I tried to come up with an efficient solution from the start. </p>\n<p>This problem was, by nature, very similar to the <a href=\"https://leetcode.com/problems/lru-cache/\">LeetCode LRU Cache</a> problem I had solved a few months before.</p>\n<h2>Day 24: Lobby Layout</h2>\n<p>I find hexagonal grids to be very interesting for some reason (as do <a href=\"https://uxdesign.cc/why-do-bees-love-hexagons-119cfd0d95a9\">bees</a>, go figure!), so I really enjoyed this one. After reading the statement, I instantly remembered about a similar <a href=\"https://adventofcode.com/2017/day/11\">Advent of Code 2017 Challenge</a> I had solved a few years back, so I ended up using the same 2D-matrix coordinate representation I used back then:</p>\n<pre><code class=\"language-scala\">final val STEPS_DELTAS = Map(\n    \"w\" -> new Cell(-2, 0),\n    \"e\" -> new Cell(2, 0),\n    \"sw\" -> new Cell(0, 1),\n    \"se\" -> new Cell(1, 1),\n    \"nw\" -> new Cell(-1, -1),\n    \"ne\" -> new Cell(0, -1)\n)\n</code></pre>\n<p>However, a <a href=\"https://hugopeixoto.net/articles/advent-of-code-2020-week-3-4.html\">friend of mine</a> pointed me to an article that actually uses a lot let space with a slightly different representation:</p>\n<pre><code class=\"language-scala\">final val STEPS_DELTAS = Map(\n    \"w\" -> new Cell(-1, 0),\n    \"e\" -> new Cell(1, 0),\n    \"sw\" -> new Cell(0, 1),\n    \"se\" -> new Cell(1, 1),\n    \"nw\" -> new Cell(-1, -1),\n    \"ne\" -> new Cell(0, -1)\n)\n</code></pre>\n<hr>\n<p>I solved all the challenges using <code>Scala</code>, and I ended up improving my coding skills with this language quite a bit since I pretty much learned something new every other day. For me, this year's edition was the most enjoyable one by far, which motivated me to solve every challenge in their publish-day. I will be back next year with more Xmas-coding!</p>\n<p>Cheers, I (sincerely) hope that 2021 is better than 2020 for everyone!</p>","frontmatter":{"date":"07 January, 2021","slug":"advent-of-code-2020","title":"Advent of Code 2020"}}},"pageContext":{"slug":"advent-of-code-2020"}},"staticQueryHashes":["3649515864","63159454"]}