Spaces:
Running
Running
File size: 1,128 Bytes
a4da721 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
def is_safe(report):
increasing = all(report[i] <= report[i+1] for i in range(len(report)-1))
decreasing = all(report[i] >= report[i+1] for i in range(len(report)-1))
if not (increasing or decreasing):
return False
for i in range(len(report) - 1):
diff = abs(report[i] - report[i+1])
if not (1 <= diff <= 3):
return False
return True
def solve_part1(reports):
safe_count = 0
for report in reports:
if is_safe(report):
safe_count += 1
return safe_count
def solve_part2(reports):
safe_count = 0
for report in reports:
if is_safe(report):
safe_count += 1
continue
for i in range(len(report)):
temp_report = report[:i] + report[i+1:]
if is_safe(temp_report):
safe_count += 1
break
return safe_count
with open("./input.txt", "r") as f:
reports = []
for line in f:
reports.append(list(map(int, line.strip().split())))
result1 = solve_part1(reports)
print(result1)
result2 = solve_part2(reports)
print(result2) |