Created
December 3, 2024 17:34
-
-
Save matthewbauer/9a1c0933e72076ce2832f6bf0c46b035 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def is_increasing(a, b): | |
return b - a > 0 and b - a <= 3 | |
def is_decreasing(a, b): | |
return b - a < 0 and b - a >= -3 | |
def is_unsafe(list, bad_level): | |
n1 = int(list[0]) | |
n2 = int(list[1]) | |
if is_increasing(n1, n2): | |
last_n = n2 | |
for n in list[2:]: | |
if not (is_increasing(last_n, int(n))): | |
if bad_level: | |
return True | |
else: | |
bad_level = True | |
else: | |
last_n = int(n) | |
elif is_decreasing(n1, n2): | |
last_n = n2 | |
for n in list[2:]: | |
if not (is_decreasing(last_n, int(n))): | |
if bad_level: | |
return True | |
else: | |
bad_level = True | |
else: | |
last_n = int(n) | |
else: | |
return True | |
return False | |
file = open("day2.csv", "r") | |
unsafe = 0 | |
lines = file.read().splitlines() | |
for line in lines: | |
list = line.split(" ") | |
bad_level = False | |
if is_unsafe(list, False) and is_unsafe(list[1:], True) and is_unsafe([list[0]] + list[2:], True): | |
unsafe += 1 | |
print(len(lines) - unsafe) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment