day6 part 2

This commit is contained in:
Ruidy 2022-12-06 14:57:19 +01:00
parent d46cca4ccf
commit 1aa2e47170
2 changed files with 44 additions and 17 deletions

View file

@ -42,3 +42,21 @@ Here are a few more examples:
- `zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw`: first marker after character **11** - `zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw`: first marker after character **11**
How many characters need to be processed before the first start-of-packet marker is detected? How many characters need to be processed before the first start-of-packet marker is detected?
## Part Two
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 **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?**

View file

@ -1,24 +1,28 @@
from common.file import read_data from common.file import read_data
test_cases = [ test_cases = [
{"input": "mjqjpqmgbljsphdztnvjfqwrcgsmlb", "expect": 7}, {"input": "mjqjpqmgbljsphdztnvjfqwrcgsmlb", "packet": 7, "message": 19},
{"input": "bvwbjplbgvbhsrlpgdmjqwftvncz", "expect": 5}, {"input": "bvwbjplbgvbhsrlpgdmjqwftvncz", "packet": 5, "message": 23},
{"input": "nppdvjthqldpwncqszvftbrmjlhg", "expect": 6}, {"input": "nppdvjthqldpwncqszvftbrmjlhg", "packet": 6, "message": 23},
{"input": "nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg", "expect": 10}, {"input": "nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg", "packet": 10, "message": 29},
{"input": "zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw", "expect": 11}, {"input": "zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw", "packet": 11, "message": 26},
] ]
def end_of_start_marker_1(data: str) -> int: def end_of_start_marker(data: str) -> int:
# use 2 counters return _process_signal(data, 4)
def end_of_message(data: str) -> int:
return _process_signal(data, 14)
def _process_signal(signal: str, size: int) -> int:
i = 0 i = 0
j = 4 j = size
while j < len(data): while j < len(signal):
# read data by window of 4 characters packet = signal[i:j]
packet = data[i:j]
# check if all are different
if len(set(packet)) == len(packet): if len(set(packet)) == len(packet):
# if yes return end counter
return j return j
i += 1 i += 1
@ -29,8 +33,13 @@ def end_of_start_marker_1(data: str) -> int:
if __name__ == "__main__": if __name__ == "__main__":
for test in test_cases: for test in test_cases:
res = end_of_start_marker_1(test["input"]) res = end_of_start_marker(test["input"])
assert res == test["expect"], f"{res} is not the right value, want {test['expect']}" assert res == test["packet"], f"{res} is not the right value, want {test['packet']}"
for test in test_cases:
res = end_of_message(test["input"])
assert res == test["message"], f"{res} is not the right value, want {test['message']}"
dataset = read_data() dataset = read_data()
print(end_of_start_marker_1(dataset[0])) print(end_of_start_marker(dataset[0]))
print(end_of_message(dataset[0]))