-
Notifications
You must be signed in to change notification settings - Fork 2
/
aoc202204.py
77 lines (54 loc) · 1.67 KB
/
aoc202204.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 4, 2022: Camp Cleanup."""
# Standard library imports
import pathlib
import sys
def parse_data(puzzle_input):
"""Parse input."""
return [parse_pair(line) for line in puzzle_input.split("\n")]
def parse_pair(line):
"""Parse one pair of assignments.
## Example:
>>> parse_pair("5-7,6-9")
((5, 7), (6, 9))
"""
return tuple(tuple(int(ids) for ids in elf.split("-")) for elf in line.split(","))
def part1(pairs):
"""Solve part 1."""
return sum(full_overlap(first, second) for first, second in pairs)
def part2(pairs):
"""Solve part 2."""
return sum(any_overlap(first, second) for first, second in pairs)
def full_overlap(first, second):
"""Does one assignment fully overlap the other?
## Examples:
>>> full_overlap((5, 7), (6, 9))
False
>>> full_overlap((4, 11), (6, 9))
True
"""
low_1, high_1 = first
low_2, high_2 = second
return (low_1 <= low_2 and high_1 >= high_2) or (
low_2 <= low_1 and high_2 >= high_1
)
def any_overlap(first, second):
"""Do the assignments overlap at all?
## Examples:
>>> any_overlap((5, 7), (6, 9))
True
>>> any_overlap((1, 3), (4, 7))
False
"""
low_1, high_1 = first
low_2, high_2 = second
return low_1 <= high_2 and low_2 <= high_1
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().strip())
print("\n".join(str(solution) for solution in solutions))