2024(2): continue instead of break :')
This commit is contained in:
40
2024/2/2.py
Normal file
40
2024/2/2.py
Normal file
@ -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.")
|
||||||
@ -39,6 +39,7 @@ Analyze the unusual data from the engineers. *How many reports are safe?*
|
|||||||
|
|
||||||
To begin, [get your puzzle input](input).
|
To begin, [get your puzzle input](input).
|
||||||
|
|
||||||
|
|
||||||
Your puzzle answer was `472`.
|
Your puzzle answer was `472`.
|
||||||
|
|
||||||
The first half of this puzzle is complete! It provides one gold star: *
|
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?*
|
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: **
|
||||||
|
|||||||
Reference in New Issue
Block a user