Advent-Of-Code-2024/q2.py
2024-12-06 05:12:35 +00:00

50 lines
1.3 KiB
Python

def q1(text: str):
levels = text.splitlines()
count = 0
for level in levels:
curr = [int(l) for l in level.split()]
increasing = all(curr[i] < curr[i + 1] for i in range(len(curr) - 1))
decreasing = all(curr[i] > curr[i + 1] for i in range(len(curr) - 1))
diff_criterion = all(
1 <= abs(curr[i] - curr[i + 1]) <= 3 for i in range(len(curr) - 1)
)
count += 1 if diff_criterion and (increasing or decreasing) else 0
return count
def q2(text: str):
levels = text.splitlines()
count = 0
for level in levels:
curr = [int(l) for l in level.split()]
for i in range(len(curr) + 1):
curr_i = curr.copy()
if i < len(curr):
del curr_i[i]
increasing = all(curr_i[i] < curr_i[i + 1] for i in range(len(curr_i) - 1))
decreasing = all(curr_i[i] > curr_i[i + 1] for i in range(len(curr_i) - 1))
diff_criterion = all(
1 <= abs(curr_i[i] - curr_i[i + 1]) <= 3 for i in range(len(curr_i) - 1)
)
if diff_criterion and (increasing or decreasing):
count += 1
break
return count
if __name__ == "__main__":
with open("i2.txt") as f:
text = f.read()
print(q1(text))
print(q2(text))