Skip to content

Commit

Permalink
Update docs on Sun Dec 22 08:15:51 UTC 2024
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] committed Dec 22, 2024
1 parent fb82262 commit 99e6244
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion 2024/21/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,16 @@ <h2 id="problem-name">Keypad Conundrum</h2>
<div id="notes"><p>As you teleport onto Santa&#39;s <em>Reindeer-class starship</em>, The Historians begin to panic: someone from their search party is <em>missing</em>. A quick life-form scan by the ship&#39;s computer reveals that when the missing Historian teleported, he arrived in another part of the ship.</p>
<p>The door to that area is locked, but the computer can&#39;t open it; it can only be opened by <em>physically typing</em> the door codes (your puzzle input) on the numeric keypad on the door.</p>
<p><em>Visit the website for the full story and <a href="https://adventofcode.com/2024/day/21">full puzzle</a> description.</em></p>
<p>Work In Progress...</p>
<p>This is the problem that sets casual players apart competitors. I had a hard time solving it. I think I was on track, but
I just couldn&#39;t visualize the whole idea of robots controlling robots controlling robots. I love recursion, but just
cannot mentally follow multiple layers of indirection. Anyway. I could solve it in the evening hours, so I&#39;m still on track with <em>Advent of Code 2024</em>.</p>
<p>It was clear from the exponential growth demonstrated by <em>part 1</em> that I wont be able to generate the final string to
be entered. Luckily the problem asked only for the length of it. (Which makes it really artifical: how would the elves enter
such a long key?)</p>
<p>I created a function called <code>EncodeKeys</code> which takes a sequence of keys to be pressed and an array of keypads, and returns the length of the shortest sequence needed to enter the given keys. An empty keypad array means that the sequence is simply entered by a human and no further encoding is needed. Otherwise, the sequence is entered by a robot that needs to be programmed using its keypad. In this case the keys are encoded using the first keypad in the array (the robot&#39;s keypad), character by character using <em>EncodeKey</em> which will recursively call back to <code>EncodeKeys</code> with the rest of the keypads.</p>
<p>The <code>_EncodeKey_</code> helper function is used to move the robot from position <em>A</em> to position <em>B</em> on the keypad and press the button. Usually, <em>A</em> and <em>B</em> are not in the same row and column, so the movement has both a horizontal and a vertical part. We could go both ways: horizontal first then vertical, or the other way around, and we need to check which movement results in fewer key presses. Lastly, we should not forget that the robot&#39;s hand should never move over the empty space <code>&#39; &#39;</code>.</p>
<p>Since every key sequence we need to enter ends with a button press <code>&#39;A&#39;</code>, we can always be sure that the robot will stay over the <code>&#39;A&#39;</code> button at the end. Since it initially starts over the <code>&#39;A&#39;</code> key as well, we have an invariant: at the beginning and the end of <code>EncodeKeys</code>, the robot is over the <code>&#39;A&#39;</code> key. It can move around while entering the keys, but the invariant holds when it finishes.</p>
<p>This is great because we don&#39;t have to keep track of it! The <code>Cache</code> used by <code>EncodeKey</code> doesn&#39;t need to care about the robot&#39;s position recursively. All it depends on is the current robot&#39;s position, the position of the next key to be entered, and the number of keypads left in the recursion. This reduces the problem space quite a bit and gives us a well-performing algorithm.</p>
</div>
<div id="code-container"><pre class="hljs language-csharp"><code>namespace AdventOfCode.Y2024.Day21;

Expand Down

0 comments on commit 99e6244

Please sign in to comment.