day6

Day 6: Tuning Trouble

Detect a start-of-packet marker: a sequence of four characters that are all different.

Identify the first position where the four most recently received characters were all different.

How many characters need to be processed before the first start-of-packet marker is detected?

Alert

Any thoughts?


Example:

Here is the starting position:

examples = {
    "mjqjpqmgbljsphdztnvjfqwrcgsmlb": 7,
    "bvwbjplbgvbhsrlpgdmjqwftvncz": 5,
    "nppdvjthqldpwncqszvftbrmjlhg": 6,
    "nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg": 10,
    "zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw": 11
}

Took about 7-10 minutes to get to here.

Wrote this as first4 but part 2 is just firstn so I modified in place.

Code
def get_pos_firstn_uniq(s: str,   # The string with the packet or message
                        n: int=4  # How many chars make up the packet or message
                       ) -> int:  # Index of char after the first n uniq chars
    """Find the first n unique chars in the string. Return index of next char."""
    for i in range(len(s) - n):
        _ = s[i:i+n]
        if len(set(_)) == n:
            return  i+n

source

get_pos_firstn_uniq

 get_pos_firstn_uniq (s:str, n:int=4)

Find the first n unique chars in the string. Return index of next char.

Type Default Details
s str The string with the packet or message
n int 4 How many chars make up the packet or message
Returns int Index of char after the first n uniq chars
[get_pos_firstn_uniq(k) == v for k,v in examples.items()]
[True, True, True, True, True]

Part 1

Get the data

input_name = f"data/{NAME}_input.txt"
with open(f"data/{NAME}_input.txt") as f:
    data = f.read()
data[:10]
'pwjwljjjvq'

Run

get_pos_firstn_uniq(data)
1235

Part 2

Note

Now use 14 instead of 4

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 19
bvwbjplbgvbhsrlpgdmjqwftvncz: first marker after character 23
nppdvjthqldpwncqszvftbrmjlhg: first marker after character 23
nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg: first marker after character 29
zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw: first marker after character 26

How many characters need to be processed before the first start-of-message marker is detected?

assert [get_pos_firstn_uniq(x, n=14) for x in examples.keys()] == [19, 23, 23, 29, 26]
get_pos_firstn_uniq(data, n=14)
3051

Only took another 5-10 minutes to get here.


Footer: nbdev magic