Posts

Showing posts from March, 2017

AOC 2016 - Day 2 Afterthoughts

AOC 2016 - Day 2 AfterthoughtsAfter I first finished coding the Advent of Code Day 2 challenge and then again after I wrote the corresponding blog entry, I felt dissatisfied with my solution.The reasons for this are numerous:The keypad was never formally modeled as a type.The move function basically encoded the constraints of the keypad, with rather brittle assumptions about what that keypad looks like.The move function operated on strings, as opposed to a more 'proper' data type.For a toy problem such as the AOC challenge, the poor choice I made there is barely acceptable. However, in the 'real world', this type of decision can mean the difference between gibberish and maintainable code.Let's take this problem one step further. If you look closely, you will see that the keypad is really just a specialized version of a general 2-dimensional (2D) board.How Can A 2D Board Be Represented As A Functional Data Structure?In general, 2D boards are often represented as som…

AOC 2016 - Day 2

AOC 2016 - Day 2tl;dr Code for Day 2, Parts 1 and 2 can be found on GitHub.BackgroundOn Day 1, you located the Easter Bunny headquarters. On Day 2, you have entered the building, but need to use the bathroom. You have to break the code on the door lock to enter the bathroom.Problem - Part 1The problem for Day 2 is in a similar vein to the Day 1 problem. Your instructions are that you are using a 3x3 numeric grid, similar to the one below, and you have to decipher a set of instructions where you move up, down, left, and right on the keypad to arrive at each digit of the code.123456789 And here are some sample instructions for a 4-digit keypad code.ULL RRDDD LURDL UUUUD For Part 1, each digit of the code is calculated by starting at 5 and following the instructions.If you hit an edge and the next instruction would take you past the edge, you do not move.Solution - Part 1Similar to last time, I started by defining a simple type to hold my instructions and a corresponding function to transf…