Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions puzzles/solutions/2022/d06/p1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import sys


START_OF_PACKET_MINIMUM_UNIQUE_SEQUENCE_LENGTH = 4

def get_unique_sequence_start_index(datastream: str, minimum_unique_sequence_length:int) -> int:
"""
:param datastream: datastream to process
:param minimum_unique_sequence_length: minimum length of a unique sequence
:return: start index of the first unique sequence
"""
for index in range(minimum_unique_sequence_length - 1, len(datastream)):
sequence = datastream[index - minimum_unique_sequence_length:index]
if len(set(sequence)) == minimum_unique_sequence_length:
return index


def get_answer(input_text: str):
"""Return how many characters need to be processed before the first start-of-packet marker is detected."""
return get_unique_sequence_start_index(input_text, START_OF_PACKET_MINIMUM_UNIQUE_SEQUENCE_LENGTH)


if __name__ == "__main__":
try:
print(get_answer(sys.argv[1]))
except IndexError:
pass # Don't crash if no input was passed through command line arguments.
17 changes: 17 additions & 0 deletions puzzles/solutions/2022/d06/p2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import sys

import p1


START_OF_MESSAGE_MINIMUM_UNIQUE_SEQUENCE_LENGTH = 14


def get_answer(input_text: str):
"""Return how many characters need to be processed before the first start-of-message marker is detected."""
return p1.get_unique_sequence_start_index(input_text, START_OF_MESSAGE_MINIMUM_UNIQUE_SEQUENCE_LENGTH)

if __name__ == "__main__":
try:
print(get_answer(sys.argv[1]))
except IndexError:
pass # Don't crash if no input was passed through command line arguments.
12 changes: 12 additions & 0 deletions puzzles/solutions/2022/d07/p1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import sys


def get_answer(input_text: str):
raise NotImplementedError


if __name__ == "__main__":
try:
print(get_answer(sys.argv[1]))
except IndexError:
pass # Don't crash if no input was passed through command line arguments.