-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path__init__.py
48 lines (33 loc) · 1.13 KB
/
__init__.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
from typing import *
from aocpy import BaseChallenge
import re
ElfTaskRange = Tuple[int, int]
ElfPair = Tuple[ElfTaskRange, ElfTaskRange]
parse_re = re.compile(r"(\d+)-(\d+),(\d+)-(\d+)")
def parse(instr: str) -> List[ElfPair]:
x = instr.strip().splitlines()
for i, ep in enumerate(x):
a, b, c, d = map(int, parse_re.match(ep).groups())
x[i] = ((a, b), (c, d))
return x
def make_set(tr: ElfTaskRange) -> Set[int]:
return set(range(tr[0], tr[1] + 1))
def count(inp: List[ElfPair], pred: Callable[[Set[int], Set[int], int], bool]) -> int:
# pred(first set, second set, length of intersection)
n = 0
for pair in inp:
a = make_set(pair[0])
b = make_set(pair[1])
intersection = a.intersection(b)
if pred(a, b, len(intersection)):
n += 1
return n
class Challenge(BaseChallenge):
@staticmethod
def one(instr: str) -> int:
inp = parse(instr)
return count(inp, lambda a, b, i: i == len(a) or i == len(b))
@staticmethod
def two(instr: str) -> int:
inp = parse(instr)
return count(inp, lambda _, __, i: i != 0)