-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaoc202402.py
77 lines (55 loc) · 1.87 KB
/
aoc202402.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
"""AoC 2, 2024: Red-Nosed Reports."""
# Standard library imports
import pathlib
import sys
def parse_data(puzzle_input):
"""Parse input."""
return [
[int(number) for number in line.split()] for line in puzzle_input.split("\n")
]
def part1(data):
"""Solve part 1."""
return sum(is_safe(report) for report in data)
def part2(data):
"""Solve part 2."""
return sum(is_safe_if_dampened(report) for report in data)
def is_safe(report):
"""Check if a report is safe.
A report consistst of consecutive levels and is safe if:
- The levels are either all increasing or all decreasing.
- Any two adjacent levels differ by at least one and at most three.
## Examples:
>>> is_safe([1, 2, 4, 7, 8, 10, 13])
True
>>> is_safe([7, 6, 4, 2, 1])
True
>>> is_safe([8, 6, 4, 4, 1])
False
>>> is_safe([1, 5])
False
"""
incs = [second - first for first, second in zip(report, report[1:])]
return all(1 <= inc <= 3 for inc in incs) or all(-3 <= inc <= -1 for inc in incs)
def is_safe_if_dampened(report):
"""Check if a report is safe if it's dampened.
Tolerate a single bad level, and count the report as a safe if removing a
single level makes it safe.
## Examples:
>>> is_safe_if_dampened([8, 6, 4, 4, 1])
True
>>> is_safe_if_dampened([1, 5])
True
>>> is_safe_if_dampened([9, 7, 6, 2, 1])
False
"""
return any(is_safe(report[:idx] + report[idx + 1 :]) for idx in range(len(report)))
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))