advent_of_code_2024

Module day_22

source
Expand description

This is my solution for Advent of Code - Day 22: Monkey Market

parse_input parses the inout file as a list of ints.

iterate_and_sum solves part 1, using pseudorandom_sequence to provide an iterator of the sequence from the seed by repeatedly calling NumberExtensions::next_secret.

bananas_from_best_diff_sequence solves part 2 by keeping track of the bananas earned by each leading sequence of four diffs, and then picking the maximum. For performance this packs the sequence into a 20-bit int using shift_diff_into_sequence_id to manage that.

TraitsΒ§

FunctionsΒ§

  • Solves part 2, Build a map from price difference sequences to bananas bought, and pick the best.
  • iterate_and_sum πŸ”’
    Solves part 1 - take the 2000th value from each seed’s pseudorandom sequence and sum
  • parse_input πŸ”’
    The input file is one integer seed per line
  • This finds the price of banana each sequence of four diffs that are present in the sequence will fetch. These are added to a mutable Vec indexed by the sequence_id, which is the previous four diffs packed into an int by shift_diff_into_sequence_id. Once a diff has been seen for each seed, future instances of that sequence are ignored. Again this is managed using a Vec that keeps track of the last id that wrote to a specific sequence_id, which is allocated once and passed in for performance.
  • Generate a pseudorandom iterator from a seed by repeatedly calling NumberExtensions::next_secret
  • The entry point for running the solutions with the β€˜real’ puzzle input.
  • For performance reasons the sequence of four diffs before the current price is packed into a 20-bit integer