import operator import sys reports = [] input = sys.stdin.read() for line in input.splitlines(): reports.append(list(map(int, line.strip().split()))) def is_safe(report, *, skipping=False): if skipping: # Brute force all the possible combinations return any( is_safe(report[:i] + report[i + 1 :], skipping=False) for i in range(len(report)) ) # If the first two numbers are the same, we know the report isn't safe. if report[0] == report[1]: return False # Otherwise, figure out if we're increasing or decreasing. elif report[0] < report[1]: op = operator.lt else: op = operator.gt for i in range(1, len(report)): # If we're not moving in the right direction, it's not safe. if not op(report[i - 1], report[i]): return False # If the difference is too big, it's not safe either. diff = abs(report[i - 1] - report[i]) if diff == 0 or diff > 3: return False return True partone = sum(is_safe(r) for r in reports) print(partone) parttwo = sum(is_safe(r, skipping=True) for r in reports) print(parttwo)