diff --git a/2024/2/1.py b/2024/2/1.py new file mode 100644 index 0000000..82fe294 --- /dev/null +++ b/2024/2/1.py @@ -0,0 +1,21 @@ +verbose = True + +safe = 0 +unsafe = 0 +with open("input", "r") as fp: + while line := fp.readline(): + nums = list(map(int, line.split(" "))) + 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): + unsafe += 1 + if verbose: + print(f"unsafe: {line}", end="") + break + else: + safe += 1 + if verbose: + print(f" safe: {line}", end="") + +print(f"Found {safe} safe and {unsafe} unsafe reports.") diff --git a/2024/2/README.md b/2024/2/README.md index eb51f31..2ff0c59 100644 --- a/2024/2/README.md +++ b/2024/2/README.md @@ -26,15 +26,42 @@ The engineers are trying to figure out which reports are *safe*. The Red-Nosed r In the example above, the reports can be found safe or unsafe by checking those rules: - `7 6 4 2 1`: *Safe* because the levels are all decreasing by `1` or `2`. - `1 2 7 8 9`: *Unsafe* because `2 7` is an increase of `5`. - `9 7 6 2 1`: *Unsafe* because `6 2` is a decrease of `4`. - `1 3 2 4 5`: *Unsafe* because `1 3` is increasing but `3 2` is decreasing. - `8 6 4 4 1`: *Unsafe* because `4 4` is neither an increase or a decrease. - `1 3 6 7 9`: *Safe* because the levels are all increasing by `1`, `2`, or `3`. +- `7 6 4 2 1`: *Safe* because the levels are all decreasing by `1` or `2`. +- `1 2 7 8 9`: *Unsafe* because `2 7` is an increase of `5`. +- `9 7 6 2 1`: *Unsafe* because `6 2` is a decrease of `4`. +- `1 3 2 4 5`: *Unsafe* because `1 3` is increasing but `3 2` is decreasing. +- `8 6 4 4 1`: *Unsafe* because `4 4` is neither an increase or a decrease. +- `1 3 6 7 9`: *Safe* because the levels are all increasing by `1`, `2`, or `3`. So, in this example, `2` reports are *safe*. 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: * + +--- Part Two --- + +The engineers are surprised by the low number of safe reports until they realize they forgot to tell you about the Problem Dampener. + +The Problem Dampener is a reactor-mounted module that lets the reactor safety systems *tolerate a single bad level* in what would otherwise be a safe report. It's like the bad level never happened! + +Now, the same rules apply as before, except if removing a single level from an unsafe report would make it safe, the report instead counts as safe. + +More of the above example's reports are now safe: + +- `7 6 4 2 1`: `Safe` without removing any level. +- `1 2 7 8 9`: `Unsafe` regardless of which level is removed. +- `9 7 6 2 1`: `Unsafe` regardless of which level is removed. +- `1 3 2 4 5`: `Safe` by removing the second level, `3`. +- `8 6 4 4 1`: `Safe` by removing the third level, `4`. +- `1 3 6 7 9`: `Safe` without removing any level. + +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: