From d447a9705cb77d51f2b0ee1a3a34eae278c95826 Mon Sep 17 00:00:00 2001 From: Xiretza Date: Wed, 2 Dec 2020 08:02:49 +0100 Subject: [PATCH] 2020 day2/python: add solution --- 2020/day2/day2.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100755 2020/day2/day2.py diff --git a/2020/day2/day2.py b/2020/day2/day2.py new file mode 100755 index 0000000..feef90c --- /dev/null +++ b/2020/day2/day2.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python +import re +from collections import Counter + +r = re.compile(r'(\d+)-(\d+) (.): (.*)') + +def requirements_met_1(min_required, max_required, letter, password): + return min_required <= Counter(password)[letter] <= max_required + +def requirements_met_2(first_pos, second_pos, letter, password): + return (password[first_pos-1] == letter) != (password[second_pos-1] == letter) + +def do_line(req_func, line): + min_required, max_required, letter, password = r.match(line).groups() + return req_func(int(min_required), int(max_required), letter, password) + +if __name__ == '__main__': + with open('input.txt', 'r') as f: + lines = list(f.readlines()) + + for func in (requirements_met_1, requirements_met_2): + print(len(list(filter(lambda line: do_line(func, line), lines))))