-
Notifications
You must be signed in to change notification settings - Fork 0
/
aoc2022_day06.py
56 lines (37 loc) · 1.35 KB
/
aoc2022_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
"""
Advent of Code 2022
Day 06: Tuning Trouble
"""
import pytest
def parse_input(file_name):
with open(file_name, "r", encoding="ascii") as data_file:
return data_file.read().strip()
def detect_marker(signal, window_size):
return next(
i
for i in range(window_size, len(signal))
if len(set(signal[i - window_size : i])) == window_size
)
def day06_part1(data):
return detect_marker(data, window_size=4)
def day06_part2(data):
return detect_marker(data, window_size=14)
@pytest.fixture(autouse=True, name="test_data")
def fixture_test_data():
return [
("mjqjpqmgbljsphdztnvjfqwrcgsmlb", 7, 19),
("bvwbjplbgvbhsrlpgdmjqwftvncz", 5, 23),
("nppdvjthqldpwncqszvftbrmjlhg", 6, 23),
("nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg", 10, 29),
("zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw", 11, 26),
]
def test_day06_part1(test_data):
assert all(day06_part1(signal) == marker1 for (signal, marker1, _) in test_data)
def test_day06_part2(test_data):
assert all(day06_part2(signal) == marker2 for (signal, _, marker2) in test_data)
if __name__ == "__main__":
input_data = parse_input("data/day06.txt")
print("Day 06 Part 1:")
print(day06_part1("abcdefd")) # Correct answer is 1848
print("Day 06 Part 2:")
print(day06_part2(input_data)) # Correct answer is 2308