-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday02.py
More file actions
executable file
·101 lines (85 loc) · 2.16 KB
/
day02.py
File metadata and controls
executable file
·101 lines (85 loc) · 2.16 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env python3
"""Day 2: Red-Nosed Reports."""
import pathlib
from itertools import combinations, pairwise
def is_safe(row: tuple) -> bool:
"""
Check if the row is safe.
>>> is_safe((7, 6, 4, 2, 1))
True
>>> is_safe((1, 2, 7, 8, 9))
False
>>> is_safe((9, 7, 6, 2, 1))
False
>>> is_safe((1, 3, 2, 4, 5))
False
>>> is_safe((8, 6, 4, 4, 1))
False
>>> is_safe((1, 3, 6, 7, 9))
True
"""
top = 3
pdiff = 0
for a, b in pairwise(row):
if ((diff := b - a) * pdiff < 0) or ((adiff := abs(diff)) > top) or (adiff < 1):
return False
pdiff = diff
return True
def part1(values: tuple) -> int:
"""
Part One.
>>> values = ((7, 6, 4, 2, 1),
... (1, 2, 7, 8, 9),
... (9, 7, 6, 2, 1),
... (1, 3, 2, 4, 5),
... (8, 6, 4, 4, 1),
... (1, 3, 6, 7, 9))
>>> part1(values)
2
"""
return sum(1 for value in values if is_safe(value))
def part2(values: tuple) -> int:
"""
Part Two.
>>> values = ((7, 6, 4, 2, 1),
... (1, 2, 7, 8, 9),
... (9, 7, 6, 2, 1),
... (1, 3, 2, 4, 5),
... (8, 6, 4, 4, 1),
... (1, 3, 6, 7, 9))
>>> part2(values)
4
"""
result = 0
for value in values:
if is_safe(value):
result += 1
else:
for combined_value in combinations(value, len(value) - 1):
if is_safe(combined_value):
result += 1
break
return result
def load_input(text: str) -> tuple:
"""
Load input text.
>>> text = '''7 6 4 2 1
... 1 2 7 8 9
... 9 7 6 2 1
... 1 3 2 4 5
... 8 6 4 4 1
... 1 3 6 7 9'''
>>> load_input(text) # doctest: +NORMALIZE_WHITESPACE
((7, 6, 4, 2, 1),
(1, 2, 7, 8, 9),
(9, 7, 6, 2, 1),
(1, 3, 2, 4, 5),
(8, 6, 4, 4, 1),
(1, 3, 6, 7, 9))
"""
return tuple(tuple(map(int, line.split())) for line in text.splitlines())
if __name__ == "__main__":
input_text = (pathlib.Path(__file__).parent / "input02").read_text()
values = load_input(input_text.strip())
print(f"Part One: {part1(values)}")
print(f"Part Two: {part2(values)}")