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

49 lines
1.0 KiB
Python
Raw Permalink Normal View History

2024-12-01 22:41:50 -08:00
""" 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
2024-12-03 13:57:28 -08:00
# print(direction,delta)
2024-12-01 22:41:50 -08:00
if delta == 0 or delta > 3 or delta < -3:
2024-12-03 13:57:28 -08:00
# print(f'unsafe (delta={delta})')
2024-12-01 22:41:50 -08:00
safe = False
break
if (direction is not None) and ((delta > 0) != (direction > 0)):
2024-12-03 13:57:28 -08:00
# print(f'unsafe (delta={delta} dir={direction})')
2024-12-01 22:41:50 -08:00
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
2024-12-03 13:57:28 -08:00
print("safe already")
2024-12-01 22:41:50 -08:00
else:
2024-12-03 13:57:28 -08:00
for n,r2 in enumerate(remove_one(report)):
2024-12-01 22:41:50 -08:00
if is_safe(r2):
2024-12-03 13:57:28 -08:00
print(f"safe when removed {n} ({r2})")
2024-12-01 22:41:50 -08:00
count += 1
break
print(count)