This commit is contained in:
Armin Friedl 2024-12-02 17:50:16 +01:00
parent ba4105f018
commit e3d4f30aeb

45
day02.py Normal file
View file

@ -0,0 +1,45 @@
#!/usr/bin/env python3
import operator
# 1
m = list()
with open("day02") as f:
for l in f:
m.append(list(map(int, l.split())))
def cmp2(l, op):
e1 = l[0]
for e2 in l[1: ]:
if op(e1, e2): e1 = e2
else: return False
return True
def safe(x):
if not cmp2(x, operator.lt) and not cmp2(x, operator.gt):
return False
if not cmp2(x, lambda e1, e2: abs(e1-e2) <= 3):
return False
return True
res = 0
for e in m:
if safe(e): res+=1
print(res)
# 2
def ssafe(x):
if safe(x): return True
for i in range(len(x)):
nx = x[:i] + x[i+1:]
if safe(nx): return True
return False
res = 0
for l in m:
if ssafe(l): res += 1
print(res)