Expand description
This is my solution for Advent of Code - Day 21: Keypad Conundrum
parse_input
Turns the codes into their value and sequence of NumericButton
presses. DirectionalButton
also exists to represent the keypads that move the robots rather than unlock the door. KeyPadButton
is a sort
of meta type that generically adds the A
for enter button to each keypad.
KeyPad
Holds most of the business logic, with keypad_chain
being used to create the chains specific to
each part.
Keys
is the trait that provides how each keypad is laid out, and is implemented for each of the input button
types.
KeyPad::key_presses
solves the puzzle, given a chain of the relevant length of that part. It delegates the
movement between key presses to KeyPad::presses_for_pair
, which in turn generates the possible paths between
the pair, and recurses to the next controller in the chain using KeyPad::controller_presses
. To make part 2
run quickly, KeyPad::presses_for_pair
caches the result for each pair at that level.
StructsΒ§
- Code πEncode a code as the list of numeric button presses needed. The terminating
A
is assumed. Also parse the code as a number for complexity calculation - KeyPad πEncodes a KeyPad. The layout comes from the button input type (T)(
EnumsΒ§
- Directional
Button πThe input buttons on pad that controls robot arm movements - KeyPad
Button πA meta-type for including the enter button on each keypad type - Numeric
Button πThe input buttons on pad for entering numeric door unlock codes
TraitsΒ§
- Coordinate
Extensions πHelpers for moving within a keypad - Keys πEncapsulates the layout of each set of keypad buttons
FunctionsΒ§
- keypad_
chain πBuild a chain of keypads controlled by robot arms of the provided size. This will be 2 for part 1, and 25 for part 2. - parse_
code πGiven a line of puzzle input parse it as a code. - parse_
input πTurn the puzzle input into one code per line - The entry point for running the solutions with the βrealβ puzzle input.
- sum_
complexities πMap the puzzles codes to their complexity and sum to get the puzzle solution
Type AliasesΒ§
- Coordinates π