-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday04.py
More file actions
75 lines (45 loc) · 1.42 KB
/
Copy pathday04.py
File metadata and controls
75 lines (45 loc) · 1.42 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
#!/usr/bin/env python3
"""
Check number range overlapped condition.
x1-y1 and x2-y2
Timing:
python3 = 30 ms
"""
from aoc import map_list, read_input
# ------------------------------------------------------------------------------
def parse_range(range_str):
"""
"2-88,13-89" => [2,88,13,89]
1. replace "-" to ","
2. split by ","
3. map str to int
"""
s = range_str.replace("-", ",").split(",")
return map_list(int, s)
def not_overlapped(x1, y1, x2, y2):
"""
2-4,6-8 --> 4 < 6 or 8 < 2
"""
return y1 < x2 or y2 < x1
def overlapped(x1, y1, x2, y2):
return not not_overlapped(x1, y1, x2, y2)
def fully_overlapped(x1, y1, x2, y2):
"""
2-8,4-6 --> (2 <= 4 and 8 >= 6) or ( 4 <= 2 and 6 >= 8)
"""
return (x1 <= x2 and y1 >= y2) or (x2 <= x1 and y2 >= y1)
def solve(day=4, test=False):
range_list = read_input(day, test).splitlines()
range_list = map_list(parse_range, range_list)
# count fully overlapped range
part1 = sum(fully_overlapped(*r) for r in range_list)
# count (partially) overlapped range
part2 = sum(overlapped(*r) for r in range_list)
return part1, part2
# ------------------------------------------------------------------------------
# res = solve(test=True)
# assert res == (2, 4)
res = solve()
assert res == (515, 883)
print(*res)
# ------------------------------------------------------------------------------