diff --git a/challenges/2021/01-sonarSweep/py/__init__.py b/challenges/2021/01-sonarSweep/py/__init__.py index 36ca721..378bd06 100644 --- a/challenges/2021/01-sonarSweep/py/__init__.py +++ b/challenges/2021/01-sonarSweep/py/__init__.py @@ -6,23 +6,22 @@ def parse(instr: str) -> List[int]: return [int(x) for x in instr.splitlines() if x != ""] -class Challenge(BaseChallenge): +def count_increases(data: List[int]) -> int: + c = 0 + for i in range(1, len(data)): + if data[i] > data[i - 1]: + c += 1 + return c + +class Challenge(BaseChallenge): @staticmethod def one(instr: str) -> int: data = parse(instr) - c = 0 - for i in range(1, len(data)): - if data[i] > data[i-1]: - c += 1 - return c + return count_increases(data) @staticmethod def two(instr: str) -> int: data = parse(instr) - c = 0 - sums = [sum(data[i:i+3]) for i in range(len(data)-2)] - for i in range(1, len(sums)): - if sums[i] > sums[i-1]: - c += 1 - return c + sums = [sum(data[i : i + 3]) for i in range(len(data) - 2)] + return count_increases(sums)