Module aoc::year2022::day06

source ·
Expand description

§Tuning Trouble

One solution to this problem is to use the windows method to slide over groups of the desired size, then construct a HashSet from the characters. If the HashSet is the same size as the window then we know that all characters are unique, as sets contain no duplicate elements.

We’ll use a faster approach that minimizes the work needed. Instead of creating a set for each window, we’ll maintain the last position seen of each character. As we advance character by character we lookup the previous position. If this is within the packet size, then we advance the start of the packet to exclude that character. Once the packet has reached the desired size then we return the current index.

Functions§

  • find 🔒
    The cardinality of the input is only 26 so a fixed size array can store the last position of each character.
  • Return the input directly.
  • Find the first unique set of size 4
  • Find the first unique set of size 14