-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday04.py
78 lines (65 loc) · 1.75 KB
/
day04.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
78
#!/usr/bin/env python3
"""Day 4: Camp Cleanup."""
import pathlib
def part1(pairs: list) -> int:
"""
Part One.
>>> pairs = [
... [{2, 3, 4}, {6, 7, 8}],
... [{2, 3}, {4, 5}],
... [{5, 6, 7}, {7, 8, 9}],
... [{2, 3, 4, 5, 6, 7, 8}, {3, 4, 5, 6, 7}],
... [{6}, {4, 5, 6}],
... [{2, 3, 4, 5, 6}, {4, 5, 6, 7, 8}],
... ]
>>> part1(pairs)
2
"""
return sum(1 for a, b in pairs if a <= b or b <= a)
def part2(pairs: list) -> int:
"""
Part Two.
>>> pairs = [
... [{2, 3, 4}, {6, 7, 8}],
... [{2, 3}, {4, 5}],
... [{5, 6, 7}, {7, 8, 9}],
... [{2, 3, 4, 5, 6, 7, 8}, {3, 4, 5, 6, 7}],
... [{6}, {4, 5, 6}],
... [{2, 3, 4, 5, 6}, {4, 5, 6, 7, 8}],
... ]
>>> part2(pairs)
4
"""
return sum(1 for a, b in pairs if a & b)
def load_input(input_text: str) -> list:
"""
Load input text.
>>> input_text = '''2-4,6-8
... 2-3,4-5
... 5-7,7-9
... 2-8,3-7
... 6-6,4-6
... 2-6,4-8'''
>>> pairs = [
... [{2, 3, 4}, {6, 7, 8}],
... [{2, 3}, {4, 5}],
... [{5, 6, 7}, {7, 8, 9}],
... [{2, 3, 4, 5, 6, 7, 8}, {3, 4, 5, 6, 7}],
... [{6}, {4, 5, 6}],
... [{2, 3, 4, 5, 6}, {4, 5, 6, 7, 8}],
... ]
>>> load_input(input_text) == pairs
True
"""
return [
[
set(range(*[int(i) + n for n, i in enumerate(a.split("-"))]))
for a in r.split(",")
]
for r in input_text.splitlines()
]
if __name__ == "__main__":
input_text = (pathlib.Path(__file__).parent / "input04").read_text()
pairs = load_input(input_text.strip())
print(f"Part One: {part1(pairs)}")
print(f"Part Two: {part2(pairs)}")