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 givenUpdate
, check for each page that a page already in the update list does not need to come after the current page.