#!/usr/bin/python def solve_part1(lines): result = 0 for line in lines: # Rule 1: at least three vowels nvowels = 0 for v in "aeiou": nvowels += line.count(v) rule1 = nvowels >= 3 # Rule 2: at least one double letter rule2 = False for i in range(len(line) - 1): if line[i] == line[i + 1]: rule2 = True break # Rule 3: no "ab", "cd", "pq" or "xy" rule3 = True for s in ["ab", "cd", "pq", "xy"]: if line.count(s) > 0: rule3 = False break if rule1 and rule2 and rule3: result += 1 return result def solve_part2(lines): result = 0 for line in lines: # Rule 1: twice-occurring two-letter sequence rule1 = False for i in range(len(line) - 1): s = line[i : i + 2] if line.count(s) >= 2: rule1 = True break # Rule 2: ABA-like sequence rule2 = False for i, c in enumerate(line): if i < len(line) - 2 and line[i + 2] == c: rule2 = True break if rule1 and rule2: result += 1 return result def main(): # Read list of strings from input text file with open("input.txt", "r") as f: lines = f.read().splitlines() print("Part 1 solution:", solve_part1(lines)) # 258 for me print("Part 2 solution:", solve_part2(lines)) # 53 for me if __name__ == "__main__": main()