Coverage for src\aoc2022\days\day6.py: 100%

8 statements  

« prev     ^ index     » next       coverage.py v7.0.0, created at 2022-12-23 05:19 +0000

1from src.aoc2022 import utils 

2 

3 

4def detect_start_of_packet(sequence: str, num_distinct: int) -> int: 

5 """ 

6 Identifies the position of the starting packet defined by num_distinct 

7 consecutive unique characters 

8 

9 :param sequence: Sequence of characters to detect the start packet 

10 :type sequence: str 

11 

12 :param num_distinct: Number of consecutive distinct characters needed 

13 :type num_distinct: int 

14 

15 :return: Position of the end of the starting packet 

16 :rtype: int 

17 """ 

18 for i in range(num_distinct - 1, len(sequence)): 

19 offset = i - num_distinct 

20 vals = sequence[offset:i] 

21 if len(set(vals)) == num_distinct: 

22 return i 

23 return -1 

24 

25 

26if __name__ == "__main__": # pragma: no cover 

27 data = utils.get_raw_data("./src/aoc2022/data/day6.txt") 

28 

29 res = detect_start_of_packet(data, 4) 

30 

31 print(f"Start of packet found after {res} chars.") 

32 

33 res = detect_start_of_packet(data, 14) 

34 

35 print(f"Start of packet found after {res} chars.")