2025: improve folder names
This commit is contained in:
40
2024/02/2.py
Normal file
40
2024/02/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.")
|
||||
Reference in New Issue
Block a user