Skip to content

Instantly share code, notes, and snippets.

@gnyman
Created December 6, 2022 13:19
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/233af222cb20f34770305aebb73ce786 to your computer and use it in GitHub Desktop.
Save gnyman/233af222cb20f34770305aebb73ce786 to your computer and use it in GitHub Desktop.
import sys
# Get the filename from the command line argument
filename = sys.argv[1]
# Open the file and read its contents
with open(filename) as f:
data = f.read()
# Define a function to find the packet start
def find_packet_start(data):
# Initialize the window of four characters
window = ["", "", "", ""]
# Process each character in the data
for i, c in enumerate(data):
# Shift the window to the left and add the new character to the right
window = [window[1], window[2], window[3], c]
# Check if the four characters in the window are all different
if len(set(window)) == 4:
# Return the number of characters processed so far
return i + 1
# Find the packet start and print the result
result = find_packet_start(data)
print(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment