-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaoc202206_sets.py
53 lines (37 loc) · 1.16 KB
/
aoc202206_sets.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
"""AoC 6, 2022: Tuning Trouble."""
# Standard library imports
import pathlib
import sys
def parse_data(puzzle_input):
"""Parse input."""
return puzzle_input
def part1(sequence):
"""Solve part 1."""
return find_marker(sequence, 4)
def part2(sequence):
"""Solve part 2."""
return find_marker(sequence, 14)
def find_marker(sequence, length):
"""Find the first marker of the given length.
A marker of length N is a sequence of N characters that are all different.
## Examples:
>>> find_marker("geirarne", 3)
3
>>> find_marker("abcdefghijklmnopqrstuvwxyz", 20)
20
>>> find_marker("aaaaaaaaaabccccccc", 3)
12
"""
for n in range(length, len(sequence)):
if len(set(sequence[n - length : n])) == length:
return n
def solve(puzzle_input):
"""Solve the puzzle for the given input."""
data = parse_data(puzzle_input)
yield part1(data)
yield part2(data)
if __name__ == "__main__":
for path in sys.argv[1:]:
print(f"\n{path}:")
solutions = solve(puzzle_input=pathlib.Path(path).read_text().rstrip())
print("\n".join(str(solution) for solution in solutions))