advent-of-code/2024/2/two.py
2024-12-25 12:59:49 +01:00

38 lines
811 B
Python

#!/usr/bin/env python3
import sys
input_file = sys.argv[1]
with open(input_file) as fd:
lines = [line.rstrip() for line in fd.readlines()]
def is_safe(report) -> bool:
acc = sorted(report)
dec = acc[::-1]
if report != acc and report != dec:
return False
for i in range(len(report) - 1):
diff = abs(report[i] - report[i + 1])
if diff < 1 or diff > 3:
return False
else:
return True
safe_num = 0
for line in lines:
report = [int(level) for level in line.split(" ")]
possible_reports = [report]
for i in range(len(report)):
rep = report.copy()
rep.pop(i)
possible_reports.append(rep)
for rep in possible_reports:
if is_safe(rep):
safe_num += 1
break
print(safe_num)