2022: d04: ex2: add solution
This commit is contained in:
parent
affe5cd89b
commit
64a9d16216
24
2022/d04/ex2/ex2.py
Executable file
24
2022/d04/ex2/ex2.py
Executable file
|
@ -0,0 +1,24 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
import sys
|
||||
|
||||
|
||||
def solve(input: list[str]) -> int:
|
||||
def is_overlapping(line: str) -> bool:
|
||||
def assignment_to_set(elf: str) -> set[int]:
|
||||
start, end = elf.split("-")
|
||||
return set(range(int(start), int(end) + 1))
|
||||
|
||||
elf1, elf2 = map(assignment_to_set, line.split(","))
|
||||
return bool(elf1 & elf2)
|
||||
|
||||
return sum(map(is_overlapping, input))
|
||||
|
||||
|
||||
def main() -> None:
|
||||
input = sys.stdin.read().splitlines()
|
||||
print(solve(input))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
Loading…
Reference in a new issue