From e3d4f30aeb6e423aed85b2e8315616e0436063d2 Mon Sep 17 00:00:00 2001 From: Armin Friedl Date: Mon, 2 Dec 2024 17:50:16 +0100 Subject: [PATCH] Day 02 --- day02.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 day02.py diff --git a/day02.py b/day02.py new file mode 100644 index 0000000..a102d56 --- /dev/null +++ b/day02.py @@ -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)