lines = open("input", "r").read().split("\n") import re matches = [re.match(r"^([0-9]+)-([0-9]+) ([a-z]): ([a-z]+)$", line) for line in lines] matches = [m for m in matches if m is not None] valids = [True for m in matches if int(m.group(1)) <= len([c for c in m.group(4) if c == m.group(3)]) <= int(m.group(2))] print(len(valids))