#!/usr/bin/python3 import re valid = 0 with open("input") as fh: for ln in fh: m = re.match(r"(\d+)-(\d+) (.): (.*)", ln) (first, second, char, pw) = m.groups() first = int(first) second = int(second) check = pw[first-1] + pw[second-1] num = check.count(char) valid += num == 1 print(first, second, char, pw, check, valid)