From beef70848db8cd89a14353278f75a1ab198c6a9f Mon Sep 17 00:00:00 2001 From: kleph Date: Mon, 2 Dec 2024 16:17:39 +0100 Subject: [PATCH] 2024 - Day 2 part 2 --- 2024/2/2_2.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 2024/2/2_2.py diff --git a/2024/2/2_2.py b/2024/2/2_2.py new file mode 100644 index 0000000..b091448 --- /dev/null +++ b/2024/2/2_2.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python +# 2024 - Advent Of Code 2 - part 2 + +# file = 'input_example.txt' +file = 'input.txt' + +# pylint: disable=consider-using-with +input_lines = [line.strip('\n') for line in open(file, encoding="utf-8")] + + +def is_safe(levels): + s_diff = 0 if levels[0] > levels[1] else 1 + + for l in range(1, len(levels)): + diff = levels[l] - levels[l - 1] + if abs(diff) > 3 or diff == 0: + return False + + s_diff2 = 0 if levels[l] < levels[l - 1] else 1 + if s_diff ^ s_diff2 != 0: + # print("sign change") + return False + + return True + +safe = 0 +for reports in input_lines: + list_levels = [ int(r) for r in reports.split(' ') ] + + if is_safe(list_levels): + safe += 1 + else: + for try_level in range(0, len(list_levels)): + new_levels = list_levels.copy() + del new_levels[try_level] + if is_safe(new_levels): + safe += 1 + break + +print(f'{safe=}')