Skip to content

Instantly share code, notes, and snippets.

@matthewbauer
Created December 3, 2024 17:34
Show Gist options
  • Save matthewbauer/9a1c0933e72076ce2832f6bf0c46b035 to your computer and use it in GitHub Desktop.
Save matthewbauer/9a1c0933e72076ce2832f6bf0c46b035 to your computer and use it in GitHub Desktop.
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