#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Mon Dec 2 18:25:45 2024 @author: bernie """ def solve_part_1(inpt): diff_lines = [[int(nr1) - int(nr2) for nr1, nr2 in zip(line, line[1:])] for line in inpt] no_safe_reports = sum( all(nr >= 1 and nr <= 3 for nr in line) or all(nr <= -1 and nr >= -3 for nr in line) for line in diff_lines) print(f"safe reports (part 1): {no_safe_reports}") def _is_report_safe(line): all_in_range = all(nr1 != nr2 and abs(nr1 - nr2) <= 3 for nr1, nr2 in zip(line, line[1:])) all_increasing = all(nr1 > nr2 for nr1, nr2 in zip(line, line[1:])) all_decreasing = all(nr1 < nr2 for nr1, nr2 in zip(line, line[1:])) return all_in_range and (all_increasing or all_decreasing) def solve_part_2(inpt): inpt = [[int(nr) for nr in line] for line in inpt] no_safe_reports = 0 for line in inpt: if _is_report_safe(line): no_safe_reports += 1 continue for idx_to_rm in range(len(line)): modified_line = line[:idx_to_rm] + line[idx_to_rm+1:] if _is_report_safe(modified_line): no_safe_reports += 1 break print(f"safe reports (part 2): {no_safe_reports}") if __name__ == "__main__": with open("02_input.txt", "r") as inpt_file: inpt = [line.split(" ") for line in inpt_file.readlines()] solve_part_1(inpt) solve_part_2(inpt)