-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path04.py
44 lines (29 loc) · 894 Bytes
/
04.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
from dataclasses import dataclass
from typing import Tuple
with open("input/04.txt") as f:
lines = f.read().split()
@dataclass
class CustomRange:
low: int
top: int
def convert(r: str) -> CustomRange:
a, b = r.split("-")
return CustomRange(int(a), int(b))
def a_fully_in_b(a: CustomRange, b: CustomRange):
return a.low <= b.low and a.top >= b.top
count = 0
for line in lines:
first, second = [convert(item) for item in line.split(",")]
if a_fully_in_b(first, second) or a_fully_in_b(second, first):
count += 1
# Part A
print(count)
# Part B
def a_partially_in_b(a: CustomRange, b: CustomRange):
return a.top >= b.low and a.low <= b.top
count = 0
for line in lines:
first, second = [convert(item) for item in line.split(",")]
if a_partially_in_b(first, second) or a_partially_in_b(second, first):
count += 1
print(count)