aoc-2024/2/2.py

31 lines
831 B
Python
Raw Normal View History

2024-12-03 08:39:30 +00:00
def error_count(levels: list[int]) -> bool:
i = 0
# figure out the diffs
diffs = []
while i < len(levels) - 1:
curr, nxt = levels[i], levels[i+1]
diffs.append(curr - nxt)
i += 1
neg = len(list(filter(lambda d: d < 0, diffs)))
pos = len(list(filter(lambda d: d > 0, diffs)))
big = len(list(filter(lambda d: abs(d) > 3, diffs)))
zer = len(list(filter(lambda d: d == 0, diffs)))
errors = big + zer
errors += int((neg == 0 and pos == len(diffs))) + int((neg == len(diffs) and pos == 0))
return errors
with open('data.input') as file:
reports = [[int(i) for i in line.split()] for line in file]
# 0 or 1 is now tolerable
safe = 0
for report in reports:
# print(error_count(report))
safe += int(error_count(report) <= 1)
else:
print('safe', safe)