diff --git a/04/cleanup.py b/04/cleanup.py new file mode 100644 index 0000000..85e32e7 --- /dev/null +++ b/04/cleanup.py @@ -0,0 +1,39 @@ + +import fileinput +from typing import Iterable + +class Range: + + rmin: int + rmax: int + length: int + + def __init__(self, rang: str): + self.rmin,self.rmax = map(lambda x: int(x), rang.strip().split('-')) + self.length = self.rmax - self.rmin + 1 + + +def solve(lines: Iterable[str]) -> int: + score = 0 + for line in lines: + elf1, elf2 = map(lambda x: Range(x), line.strip().split(',')) + if elf2.length > elf1.length: + elf2,elf1 = (elf1, elf2) + if elf1.rmin <= elf2.rmin and elf1.rmax >= elf2.rmax: + score += 1 + return score + +def solveb(lines: Iterable[str]) -> int: + score = 0 + for line in lines: + elf1, elf2 = map(lambda x: Range(x), line.strip().split(',')) + if elf2.length > elf1.length: + elf2,elf1 = (elf1, elf2) + if elf1.rmin <= elf2.rmax and elf1.rmax >= elf2.rmin: + score += 1 + return score + + +if __name__ == "__main__": + print(solve(fileinput.input())) + print(solveb(fileinput.input())) \ No newline at end of file diff --git a/04/example b/04/example new file mode 100644 index 0000000..99a66c5 --- /dev/null +++ b/04/example @@ -0,0 +1,6 @@ +2-4,6-8 +2-3,4-5 +5-7,7-9 +2-8,3-7 +6-6,4-6 +2-6,4-8 \ No newline at end of file