From 089d498d69c8c565320cf3a2eb56ac2bd3f54dee Mon Sep 17 00:00:00 2001 From: Kees van Kempen Date: Wed, 18 Dec 2024 18:29:52 +0100 Subject: [PATCH] 2024(2): continue instead of break :') --- 2024/2/2.py | 40 ++++++++++++++++++++++++++++++++++++++++ 2024/2/README.md | 6 +++++- 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 2024/2/2.py diff --git a/2024/2/2.py b/2024/2/2.py new file mode 100644 index 0000000..f83cfdb --- /dev/null +++ b/2024/2/2.py @@ -0,0 +1,40 @@ +verbose = True + +def is_safe(nums): + print("is_safe") + increasing = (nums[1] - nums[0]) > 0 + for i in range(1, len(nums)): + diff = nums[i] - nums[i - 1] + if abs(diff) > 3 or abs(diff) == 0 or increasing != (diff > 0): + if verbose: + print(f"unsafe: {line}", end="") + return False + if verbose: + print(f" safe: {line}", end="") + return True + + +safe = 0 +unsafe = 0 +with open("input", "r") as fp: + while line := fp.readline(): + nums = list(map(int, line.split(" "))) + if is_safe(nums): + safe += 1 + continue + + # Try omitting one of the numbers. + if verbose: + print("START try to omit one:") + for i in range(len(nums)): + new_nums = nums.copy() + new_nums.pop(i) + if is_safe(new_nums): + safe += 1 + break + else: + unsafe += 1 + if verbose: + print("STOP trying to omit one.") + +print(f"Found {safe} safe and {unsafe} unsafe reports.") diff --git a/2024/2/README.md b/2024/2/README.md index 2ff0c59..6700a64 100644 --- a/2024/2/README.md +++ b/2024/2/README.md @@ -39,6 +39,7 @@ Analyze the unusual data from the engineers. *How many reports are safe?* To begin, [get your puzzle input](input). + Your puzzle answer was `472`. The first half of this puzzle is complete! It provides one gold star: * @@ -64,4 +65,7 @@ Thanks to the Problem Dampener, `4` reports are actually *safe*! Update your analysis by handling situations where the Problem Dampener can remove a single level from unsafe reports. *How many reports are now safe?* -Answer: + +Your puzzle answer was `520`. + +Both parts of this puzzle are complete! They provide two gold stars: **