def safe(levels: list[int]) -> bool: i = 0 # figure out the diffs diffs = [] while i < len(levels) - 1: curr, nxt = levels[i], levels[i+1] diffs.append(curr - nxt) i += 1 if 0 in diffs: return False neg = len(list(filter(lambda d: d < 0, diffs))) pos = len(list(filter(lambda d: d > 0, diffs))) big = len(list(filter(lambda d: abs(d) > 3, diffs))) if big > 0: return False return (neg == 0 and pos == len(diffs)) or (neg == len(diffs) and pos == 0) with open('1.input') as file: reports = [[int(i) for i in line.split()] for line in file] print(sum([1 if r else 0 for r in [safe(report) for report in reports]]))