mirror of
https://github.com/rjNemo/advent_of_code_2022
synced 2026-06-06 02:26:41 +00:00
day6 part 2
This commit is contained in:
parent
d46cca4ccf
commit
1aa2e47170
2 changed files with 44 additions and 17 deletions
|
|
@ -42,3 +42,21 @@ Here are a few more examples:
|
|||
- `zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw`: first marker after character **11**
|
||||
|
||||
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?**
|
||||
41
day6/main.py
41
day6/main.py
|
|
@ -1,24 +1,28 @@
|
|||
from common.file import read_data
|
||||
|
||||
test_cases = [
|
||||
{"input": "mjqjpqmgbljsphdztnvjfqwrcgsmlb", "expect": 7},
|
||||
{"input": "bvwbjplbgvbhsrlpgdmjqwftvncz", "expect": 5},
|
||||
{"input": "nppdvjthqldpwncqszvftbrmjlhg", "expect": 6},
|
||||
{"input": "nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg", "expect": 10},
|
||||
{"input": "zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw", "expect": 11},
|
||||
{"input": "mjqjpqmgbljsphdztnvjfqwrcgsmlb", "packet": 7, "message": 19},
|
||||
{"input": "bvwbjplbgvbhsrlpgdmjqwftvncz", "packet": 5, "message": 23},
|
||||
{"input": "nppdvjthqldpwncqszvftbrmjlhg", "packet": 6, "message": 23},
|
||||
{"input": "nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg", "packet": 10, "message": 29},
|
||||
{"input": "zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw", "packet": 11, "message": 26},
|
||||
]
|
||||
|
||||
|
||||
def end_of_start_marker_1(data: str) -> int:
|
||||
# use 2 counters
|
||||
def end_of_start_marker(data: str) -> int:
|
||||
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
|
||||
j = 4
|
||||
while j < len(data):
|
||||
# read data by window of 4 characters
|
||||
packet = data[i:j]
|
||||
# check if all are different
|
||||
j = size
|
||||
while j < len(signal):
|
||||
packet = signal[i:j]
|
||||
if len(set(packet)) == len(packet):
|
||||
# if yes return end counter
|
||||
return j
|
||||
|
||||
i += 1
|
||||
|
|
@ -29,8 +33,13 @@ def end_of_start_marker_1(data: str) -> int:
|
|||
|
||||
if __name__ == "__main__":
|
||||
for test in test_cases:
|
||||
res = end_of_start_marker_1(test["input"])
|
||||
assert res == test["expect"], f"{res} is not the right value, want {test['expect']}"
|
||||
res = end_of_start_marker(test["input"])
|
||||
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()
|
||||
print(end_of_start_marker_1(dataset[0]))
|
||||
print(end_of_start_marker(dataset[0]))
|
||||
print(end_of_message(dataset[0]))
|
||||
|
|
|
|||
Loading…
Reference in a new issue