import re #2-5 m: mfvxmmm regex = re.compile(r'(\d+)-(\d+) (\w): (\w+)', re.I) items = (re.match(regex, x).groups() for x in open("input.txt")) items = map(lambda x: [int(x[0]), int(x[1]), *x[2:]], items) def in_range(lower, upper, char, string): return (string[lower-1] == char) != (string[upper-1] == char) count = sum(1 for x in items if in_range(*x)) print(count)