From c6c640870c9a3629721e2c2cf1148fdfb9334c94 Mon Sep 17 00:00:00 2001 From: cupcakearmy Date: Sun, 4 Dec 2022 11:43:18 +0100 Subject: [PATCH] 04 --- 2022/04/README.md | 11 ++++++ 2022/04/python/main.py | 90 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 2022/04/README.md create mode 100644 2022/04/python/main.py diff --git a/2022/04/README.md b/2022/04/README.md new file mode 100644 index 0000000..937e16b --- /dev/null +++ b/2022/04/README.md @@ -0,0 +1,11 @@ +# 04 + +A good usage for the `__contains__` python method, love the dunder :) + +
+ Solutions +
    +
  1. 431
  2. +
  3. 823
  4. +
+
diff --git a/2022/04/python/main.py b/2022/04/python/main.py new file mode 100644 index 0000000..59d7b63 --- /dev/null +++ b/2022/04/python/main.py @@ -0,0 +1,90 @@ +#!/usr/bin/env python + +from dataclasses import dataclass +from os.path import dirname, join + +# Day 04 + +# Common + + +def read_input(filename): + data = join(dirname(__file__), '..', filename) + with open(data) as f: + return f.read().strip() + + +test = read_input('test.txt') +data = read_input('input.txt') + + +@dataclass +class CleaningJob: + start: int + end: int + + def __len__(self): + return self.end - self.start + + def __contains__(self, other) -> bool: + if isinstance(other, CleaningJob): + return self.start <= other.start and self.end >= other.end + elif type(other) is int: + return self.start <= other <= self.end + else: + return False + + def overlaps(self, other: 'CleaningJob') -> bool: + return other.start in self or other.end in self or other in self + + +@dataclass +class CleaningPair: + one: CleaningJob + two: CleaningJob + + @staticmethod + def parse(data: str): + jobs = [ + CleaningJob(*map(int, job.split('-'))) + for job in data.split(',') + ] + return CleaningPair(*jobs) + + def is_duplicated(self): + return self.one in self.two or self.two in self.one + + def overlaps(self): + return self.one.overlaps(self.two) or self.two.overlaps(self.one) + + +@dataclass +class CleaningSquad: + pairs: list[CleaningPair] + + @staticmethod + def parse(data: str): + return CleaningSquad([ + CleaningPair.parse(line) + for line in data.split('\n') + ]) + + def check_duplicate(self): + return sum([pair.is_duplicated() for pair in self.pairs]) + + def check_overlap(self): + return sum([pair.overlaps() for pair in self.pairs]) + + # 1 +print('1.') +squad = CleaningSquad.parse(test) +print(squad.check_duplicate()) +squad = CleaningSquad.parse(data) +print(squad.check_duplicate()) + +# 2 +print('\n2.') +squad = CleaningSquad.parse(test) +print(squad.check_overlap()) +squad = CleaningSquad.parse(data) +print(squad.check_overlap())