diff options
| author | blenovo <bk@gmail.com> | 2025-08-16 00:38:26 +0200 |
|---|---|---|
| committer | blenovo <bk@gmail.com> | 2025-08-16 00:38:26 +0200 |
| commit | f6ff66ea2da1f30303f17a2dac341411fb325881 (patch) | |
| tree | 934b79fc762547e474a5277723c0c5087b56698f /2018/aoc2018-d02.py | |
| parent | edf82358166db84f74419f9a83d328390cc6b356 (diff) | |
final puzzle and little cleanup for 2018blen
Diffstat (limited to '2018/aoc2018-d02.py')
| -rw-r--r-- | 2018/aoc2018-d02.py | 43 |
1 files changed, 43 insertions, 0 deletions
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); |
