def solve(f): t = 0 extra = 0 for line in f: report = [int(x) for x in line.split()] safe = issafe(report) t += int(safe) if not safe: for i in range(len(report)): if issafe(report[:i] + report[i+1:]): extra += 1 break print(t) print(t + extra) def issafe(report): d = [y-x for x,y in zip(report,report[1:])] if all(1<=x<=3 for x in d): return True if all(-3<=x<=-1 for x in d): return True return False solve(open("sample1.in")) solve(open("input"))