The preparations are finally complete; you and the Elves leave camp on foot and begin to make your way toward the star fruit grove.
As you move through the dense undergrowth, one of the Elves gives you a handheld device. He says that it has many fancy features, but the most important one to set up right now is the communication system.
However, because he's heard you have significant experience dealing with signal-based systems, he convinced the other Elves that it would be okay to give you their one malfunctioning device - surely you'll have no problem fixing it.
As if inspired by comedic timing, the device emits a few colorful sparks.
To be able to communicate with the Elves, the device needs to lock on to their signal. The signal is a series of seemingly-random characters that the device receives one at a time.
To fix the communication system, you need to add a subroutine to the device that detects a start-of-packet marker in the datastream. In the protocol being used by the Elves, the start of a packet is indicated by a sequence of four characters that are all different.
The device will send your subroutine a datastream buffer (your puzzle input); your subroutine needs to identify the first position where the four most recently received characters were all different. Specifically, it needs to report the number of characters from the beginning of the buffer to the end of the first such four-character marker.
For example, suppose you receive the following datastream buffer:
mjqjpqmgbljsphdztnvjfqwrcgsmlb
After the first three characters (mjq
) have been received, there haven't been enough characters received yet to find the marker. The first time a marker could occur is after the fourth character is received, making the most recent four characters mjqj
. Because j
is repeated, this isn't a marker.
The first time a marker appears is after the seventh character arrives. Once it does, the last four characters received are jpqm, which are all different. In this case, your subroutine should report the value 7, because the first start-of-packet marker is complete after 7 characters have been processed.
Here are a few more examples:
bvwbjplbgvbhsrlpgdmjqwftvncz
: first marker after character 5nppdvjthqldpwncqszvftbrmjlhg
: first marker after character 6nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg
: first marker after character 10zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw
: first marker after character 11How many characters need to be processed before the first start-of-packet marker is detected?
# Python imports
from pathlib import Path
test = Path("data/day06_test.txt") # test data
data = Path("data/day06_data.txt") # puzzle data
def parse_input(fpath: Path) -> list[str]:
"""Parse puzzle input into a list of datastream strings
:param fpath: path to puzzle input
"""
datastreams = []
# Parse files
with fpath.open() as ifh:
for line in [_.strip() for _ in ifh.readlines()]:
datastreams.append(line)
return datastreams
def find_unique_marker(datastream: str, markerlen: int) -> tuple[str, int]:
"""Return first internal string of given length with all unique characters, and its end point
:param datastream: data string
:param markerlen: length of internal string with all unique characters to be found
"""
# Iterate over positions in string until first internal string of
# all unique characters is found; return first such string, and
# the position of the last character (1-indexed)
for idx in range(len(datastream)):
if len(set(datastream[idx:idx+markerlen])) == markerlen:
return datastream[idx:idx+markerlen], idx + markerlen
def part1(fpath: Path) -> str:
"""Solve the puzzle
:param fpath: path to puzzle input
"""
datastreams = parse_input(fpath)
for stream in datastreams:
print(find_unique_marker(stream, 4))
part1(test)
('jpqm', 7) ('vwbj', 5) ('pdvj', 6) ('rfnt', 10) ('zqfr', 11)
part1(data)
('hfvd', 1142)
Your device's communication system is correctly detecting packets, but still isn't working. It looks like it also needs to look for messages.
A start-of-message marker is just like a start-of-packet marker, except it consists of 14 distinct characters rather than 4.
Here are the first positions of start-of-message markers for all of the above examples:
mjqjpqmgbljsphdztnvjfqwrcgsmlb
: first marker after character 19bvwbjplbgvbhsrlpgdmjqwftvncz
: first marker after character 23nppdvjthqldpwncqszvftbrmjlhg
: first marker after character 23nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg
: first marker after character 29zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw
: first marker after character 26How many characters need to be processed before the first start-of-message marker is detected?
def part2(fpath: Path) -> str:
"""Solve the puzzle
:param fpath: path to puzzle input
"""
datastreams = parse_input(fpath)
for stream in datastreams:
print(find_unique_marker(stream, 14))
part2(test)
('qmgbljsphdztnv', 19) ('vbhsrlpgdmjqwf', 23) ('ldpwncqszvftbr', 23) ('wmzdfjlvtqnbhc', 29) ('jwzlrfnpqdbhtm', 26)
part2(data)
('tnfsdzpvcgbjqw', 2803)