advent/2024/2/two.py

66 lines
1.5 KiB
Python
Raw Permalink Normal View History

2024-12-19 17:13:16 -06:00
from collections import Counter
reports = [i.rstrip() for i in open("input").readlines()]
reports = [[int(j) for j in i.split(' ')] for i in reports]
def reduce_report_to_state(report, outer_level=True):
last = None
diff = None
sign = 0
for index, item in enumerate(report):
bad = False
if not last:
last = item
continue
diff = item - last
if diff == 0:
print("diff bad")
bad = True
thissign = 1 if diff > 0 else -1
print(thissign, diff)
if sign == 0:
sign = thissign
if sign != thissign:
print("sign bad")
bad = True
adiff = abs(diff)
print(adiff)
if adiff > 3 or adiff < 1:
print("range bad")
bad = True
last = item
if bad:
return False
return True
def reduce_with_deference(report):
verdict = reduce_report_to_state(report)
if not verdict:
for i in range(len(report) - 1):
sliced = report[:i + 1] + report[i + 2:]
print(report)
print(sliced)
second_result = reduce_report_to_state(
sliced
)
if second_result:
return True
continue
return verdict
results = []
for i in reports:
results.append(reduce_with_deference(i))
print(
sum(
[1 if i else 0 for i in results]
)
)