-
Notifications
You must be signed in to change notification settings - Fork 0
/
06_tuning-trouble.py
executable file
·37 lines (27 loc) · 1.07 KB
/
06_tuning-trouble.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/env python3
#
# Copyright (c) 2022, xphade <github.com/xphade>
# SPDX-License-Identifier: MIT
import time
from aoc_utils import get_input_path, print_elapsed_time
def find_unique_sequence(datastream: str, distinct_characters: int) -> int:
i, j = 0, distinct_characters
while j < len(datastream):
if len(set(datastream[i:j])) == distinct_characters:
return j
i += 1
j += 1
assert False # If we get here, we did not find a unique sequence (invalid input).
def main():
data_path = get_input_path("Day 06: Tuning Trouble")
with open(data_path, "r") as file:
datastream = file.read().strip("\n")
start = time.monotonic()
processed_chars_start = find_unique_sequence(datastream, 4)
processed_chars_message = find_unique_sequence(datastream, 14)
stop = time.monotonic()
print(f"Characters processed to find start marker: {processed_chars_start}")
print(f"Characters processed to find message: {processed_chars_message}")
print_elapsed_time(start, stop)
if __name__ == "__main__":
main()