= {
examples "mjqjpqmgbljsphdztnvjfqwrcgsmlb": 7,
"bvwbjplbgvbhsrlpgdmjqwftvncz": 5,
"nppdvjthqldpwncqszvftbrmjlhg": 6,
"nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg": 10,
"zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw": 11
}
day6
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?
Example:
Here is the starting position:
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
int=4 # How many chars make up the packet or message
n: -> 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
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 |
== v for k,v in examples.items()] [get_pos_firstn_uniq(k)
[True, True, True, True, True]
Part 1
Get the data
= f"data/{NAME}_input.txt"
input_name with open(f"data/{NAME}_input.txt") as f:
= f.read()
data 10] data[:
'pwjwljjjvq'
Run
get_pos_firstn_uniq(data)
1235
Part 2
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]
=14) get_pos_firstn_uniq(data, n
3051
Only took another 5-10 minutes to get here.
Footer: nbdev magic
Took about 7-10 minutes to get to here.