import re def iter_lines(): with open('input.txt') as fh: for line in fh: m = re.match('([0-9]+)-([0-9]+) ([a-z]): (.*)\n', line) if not m: raise ValueError(line) i, j, c, s = m.groups() yield int(i), int(j), c, s def part1(): count = 0 for i, j, c, s in iter_lines(): a = s.count(c) if i <= a and a <= j: count += 1 print(count) def part2(): count = 0 for i, j, c, s in iter_lines(): if bool(s[i - 1] == c) != bool(s[j - 1] == c): count += 1 print(count) part1() part2()