advent_of_code_2024

Module day_21

source
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Β§

  • The input buttons on pad that controls robot arm movements
  • KeyPadButton πŸ”’
    A meta-type for including the enter button on each keypad type
  • NumericButton πŸ”’
    The input buttons on pad for entering numeric door unlock codes

TraitsΒ§

  • 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Β§