#advent of code 2018 #day 02 #part 1 and 2 PuzzleInput = open("02.in","r"); boxes = []; for line in PuzzleInput: line = line.replace("\n",""); boxes.append(line); duplicates = dict(); duplicates[2] = set(); duplicates[3] = set(); for box in boxes: boxdict = dict.fromkeys(set(list(box)),0); for bchar in boxdict: boxdict[bchar] = box.count(bchar); for num in [2,3]: if num in boxdict.values(): duplicates[num].add(box); p1 = len(duplicates[3]) * len(duplicates[2]); print("part 1 =", p1); visited = set(); p2 = None; for box1 in boxes: for box2 in boxes: if box1 == box2: continue; if box2 in visited: continue; matched = 0; difference = set(); for i in range(len(box1)): if box1[i] != box2[i] : difference.add(box1[i]); if len(difference) > 1: break; if len(difference) == 1: DifferentChar = difference.pop(); p2 = box1.replace(DifferentChar,""); break; if p2: break; visited.add(box1); print("part 2 =", p2);