#!/usr/bin/env python3 with open('input.txt', 'r') as f: package_ids = [x.strip() for x in f.readlines()] # print(package_ids) def find_close(package_ids): for i, el_1 in enumerate(package_ids[:len(package_ids)]): for el_2 in package_ids[i + 1:]: diffs = 0 for j, char_1 in enumerate(el_1): char_2 = el_2[j] if char_1 != char_2: diffs += 1 if diffs == 1: return el_1, el_2 def remove_differing(id_1, id_2): for i, char_1 in enumerate(id_1): char_2 = id_2[i] if char_1 != char_2: return id_1[:i] + id_1[i + 1:] close_1, close_2 = find_close(package_ids) common = remove_differing(close_1, close_2) print('Close IDs:', close_1, close_2) print('Common letters:', common)