From f6ff66ea2da1f30303f17a2dac341411fb325881 Mon Sep 17 00:00:00 2001 From: blenovo Date: Sat, 16 Aug 2025 00:38:26 +0200 Subject: final puzzle and little cleanup for 2018 --- 2018/aoc2018-d02.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 2018/aoc2018-d02.py (limited to '2018/aoc2018-d02.py') diff --git a/2018/aoc2018-d02.py b/2018/aoc2018-d02.py new file mode 100644 index 0000000..7142537 --- /dev/null +++ b/2018/aoc2018-d02.py @@ -0,0 +1,43 @@ +#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); -- cgit v1.2.3