-
Notifications
You must be signed in to change notification settings - Fork 0
/
day06.py
81 lines (68 loc) · 2.13 KB
/
day06.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env python3
"""Day 6: Tuning Trouble."""
import pathlib
def get_distinct_characters_index(datastream: str, distinctnumber: int) -> int:
"""
Get the first distinct characters index in datastream.
>>> datastream = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
>>> get_distinct_characters_index(datastream, 2)
-1
>>> datastream = 'mjqjpqmgbljsphdztnvjfqwrcgsmlb'
>>> get_distinct_characters_index(datastream, 4)
7
>>> datastream = 'mjqjpqmgbljsphdztnvjfqwrcgsmlb'
>>> get_distinct_characters_index(datastream, 14)
19
"""
distinct_characters_index = -1
for i in range(len(datastream)):
if len(set(datastream[i : i + distinctnumber])) == distinctnumber:
distinct_characters_index = i + distinctnumber
break
else:
continue
return distinct_characters_index
def part1(datastream: str) -> int:
"""
Part One.
>>> datastream = 'mjqjpqmgbljsphdztnvjfqwrcgsmlb'
>>> part1(datastream)
7
>>> datastream = 'bvwbjplbgvbhsrlpgdmjqwftvncz'
>>> part1(datastream)
5
>>> datastream = 'nppdvjthqldpwncqszvftbrmjlhg'
>>> part1(datastream)
6
>>> datastream = 'nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg'
>>> part1(datastream)
10
>>> datastream = 'zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw'
>>> part1(datastream)
11
"""
return get_distinct_characters_index(datastream, 4)
def part2(datastream) -> int:
"""
Part Two.
>>> datastream = 'mjqjpqmgbljsphdztnvjfqwrcgsmlb'
>>> part2(datastream)
19
>>> datastream = 'bvwbjplbgvbhsrlpgdmjqwftvncz'
>>> part2(datastream)
23
>>> datastream = 'nppdvjthqldpwncqszvftbrmjlhg'
>>> part2(datastream)
23
>>> datastream = 'nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg'
>>> part2(datastream)
29
>>> datastream = 'zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw'
>>> part2(datastream)
26
"""
return get_distinct_characters_index(datastream, 14)
if __name__ == "__main__":
datastream = (pathlib.Path(__file__).parent / "input06").read_text().strip()
print(f"Part One: {part1(datastream)}")
print(f"Part Two: {part2(datastream)}")