Day 6 - Advent of Code 2022 Hints
- Use a data structure to keep track of the last four characters received in the datastream.
- As you receive each character, add it to the data structure and check if the last four characters are all different.
- If they are not, remove the first character in the data structure and add the new character to the end.
- Once the start-of-packet marker is found, return the number of characters processed before it was detected.
- Create a data structure to store the last four characters received in the datastream.
- Initialize a variable num_chars_processed to 0.
- Begin processing the datastream one character at a time.
- For each character received:
- Increment num_chars_processed by 1.
- Add the character to the end of the data structure.
- If the data structure contains four characters, check if they are all different.
- If they are all different, return num_chars_processed.
- If the data structure contains more than four characters, remove the first character from the data structure.
- If the end of the datastream is reached without finding the start-of-packet marker, return -1.
The steps for detecting the start-of-packet marker can be used with a few modifications:
- Instead of using a data structure to store the last four characters received, use a data structure to store the last 14 characters.
- Instead of checking if the last four characters are all different, check if the last 14 characters are all different.
- If the last 14 characters are not all different, remove the first character in the data structure and add the new character to the end, as before.
- If the end of the datastream is reached without finding the start-of-message marker, return -1, as before.
- Additionally, the variable num_chars_processed will need to be incremented by 14 each time a start-of-message marker is found, rather than by 4 as in the previous problem. This is because a start-of-message marker consists of 14 distinct characters rather than 4.