advent_of_code_2024

Module day_5

source
Expand description

This is my solution for Advent of Code - Day 5: Print Queue

parse_input splits the input into the two sections, delegating these to parse_rules and parse_updates respectively.

Part 1 is solved by sum_valid_middle_pages, with the validation being done by validate_update, and get_middle split out for ease of reuse.

Part 2 is solved by sort_and_sum_invalid_middle_pages, with sort_pages doing the extra work, everything else is reused from part 1.

FunctionsΒ§

  • get_middle πŸ”’
    Utility to get the middle page of an update.
  • parse_input πŸ”’
    Split the list into two sections and parse each
  • parse_rules πŸ”’
    Build the Rules lookup by adding each rule in turn.
  • parse_updates πŸ”’
    Parse the lines of input into lists of page numbers
  • The entry point for running the solutions with the β€˜real’ puzzle input.
  • Solution to part 2 - similar to sum_valid_middle_pages, but finds invalid pages and sorts them before extracting thr middle and summing.
  • sort_pages πŸ”’
    Assuming all pairs of pages have rules that specify an ordering for that pair, use that to provide a sorting function for [Itertools::sorted_by].
  • Solution to part 1, find the valid update lists and sum their middle page.
  • validate_update πŸ”’
    For a given Update, check for each page that a page already in the update list does not need to come after the current page.

Type AliasesΒ§

  • Rules πŸ”’
    Rules represented as a lookup from page number to the set of pages that must come after
  • Update πŸ”’
    Updates as a list of page numbers