Solve 2024 day 2

This commit is contained in:
felixm 2024-12-02 00:09:58 -05:00
parent 12662b912c
commit 893a8e3dbb

42
2024/d2.py Normal file
View File

@ -0,0 +1,42 @@
from lib import get_data
from lib import ints
data = get_data(__file__)
s1 = 0
s2 = 0
for line in data.splitlines():
xs = ints(line)
deltas = []
for i in range(len(xs) - 1):
deltas.append(xs[i + 1] - xs[i])
if abs(xs[i + 1] - xs[i]) not in [1, 2, 3]:
break
else:
if deltas[0] < 0 and all(deltas[i] < 0 for i in range(len(deltas))):
s1 += 1
elif deltas[0] > 0 and all(deltas[i] > 0 for i in range(len(deltas))):
s1 += 1
sublists = [xs[:i] + xs[i + 1 :] for i in range(len(xs))]
found = False
for xs in sublists:
deltas = []
for i in range(len(xs) - 1):
deltas.append(xs[i + 1] - xs[i])
if abs(xs[i + 1] - xs[i]) not in [1, 2, 3]:
break
else:
if deltas[0] < 0 and all(deltas[i] < 0 for i in range(len(deltas))):
found = True
if deltas[0] > 0 and all(deltas[i] > 0 for i in range(len(deltas))):
found = True
if found:
s2 += 1
print(s1)
print(s2)