Skip to content

Instantly share code, notes, and snippets.

@gnyman
Created December 6, 2022 13:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gnyman/b58aef5a80e97ec2ae2fc1869452eb47 to your computer and use it in GitHub Desktop.
Save gnyman/b58aef5a80e97ec2ae2fc1869452eb47 to your computer and use it in GitHub Desktop.
# Parse the input
data = open('input.txt').read().strip()
# Keep track of the last four characters received
last_four = []
# Process each character in the data
for i, ch in enumerate(data):
# Add the character to the list of last four characters
last_four.append(ch)
# If we have more than four characters, remove the first one
if len(last_four) > 4:
last_four.pop(0)
# If the last four characters are all different, we have found
# a start-of-packet marker, so print the number of characters
# processed and stop
if len(set(last_four)) == 4:
print(i+1)
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment