passwords = [str(p) for p in range(172851,675869+1)] def monotonic(password): digits = list(password) if digits == sorted(digits): return True return False def at_least_two_adjacent(password): digits = list(password) for i, digit in enumerate(digits): if i+1 == len(digits): return False if digits[i+1] == digit: return True def exactly_two_adjacent(password): if not at_least_two_adjacent(password): return False digits = list(password) for digit in digits: if password.count(digit) == 2: return True part1 = [p for p in passwords if monotonic(p) and at_least_two_adjacent(p)] part2 = [p for p in passwords if monotonic(p) and exactly_two_adjacent(p)] print(len(part1), len(part2))