1
0
2024_Advent_of_Code/day2/main.py
Nicolas Green 0cf8b0b8bf
day2
2024-12-03 11:13:11 -05:00

40 lines
1.3 KiB
Python

safe_reports = 0
with open("input.txt", "r") as file_input:
for row in file_input:
report = list(map(int, row.split(" ")))
increasing = None
report_size = len(report)
safe_count = 0
for i in range(report_size):
if i == 0:
continue
current_number = report[i]
last_number = report[i-1]
difference = last_number - current_number
# Figure out if we're increasing or decreasing values in this row
if increasing is None:
if difference < 0 and (abs(difference) < 4):
increasing = True
elif difference > 0 and (abs(difference) < 4):
increasing = False
else:
break
# Once we figure it out just make sure we stay in our 3 pt range and don't
# decrease if we're supposed to be increasing or vice versa
elif increasing and (-4 < difference < 0):
safe_count += 1
elif not (increasing) and (0 < difference < 4):
safe_count += 1
else:
break
if safe_count == (report_size - 2):
safe_reports += 1
print(safe_reports)