advent-of-code-2024/day2.2.py

49 lines
1.0 KiB
Python

""" day2 part 1 """
import fileinput
count = 0
def is_safe(report):
prev = report[0]
direction = None
safe = True
for item in report[1:]:
delta = item - prev
# print(direction,delta)
if delta == 0 or delta > 3 or delta < -3:
# print(f'unsafe (delta={delta})')
safe = False
break
if (direction is not None) and ((delta > 0) != (direction > 0)):
# print(f'unsafe (delta={delta} dir={direction})')
safe = False
break
direction = delta
prev = item
return safe
def remove_one(report):
for i in range(len(report)):
yield report[:i] + report[i+1:]
for line in fileinput.input():
report = [int(x) for x in line.split()]
if is_safe(report):
count += 1
print("safe already")
else:
for n,r2 in enumerate(remove_one(report)):
if is_safe(r2):
print(f"safe when removed {n} ({r2})")
count += 1
break
print(count)