aoc2024/day02.py

46 lines
725 B
Python
Raw Permalink Normal View History

2024-12-02 16:50:16 +00:00
#!/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)