re: AoC Day 2: Inventory Management System VIEW POST

FULL DISCUSSION
 

Part 1

from collections import Counter

with open("input.txt") as f:
    ids = [Counter(l.strip()) for l in f]

count2 = 0
count3 = 0
for c in ids:
    if 2 in c.values(): count2 += 1
    if 3 in c.values(): count3 += 1

print(count2 * count3)

Part 2

from editdistance import eval as dist
from itertools import product

with open("input.txt") as f:
    ids = [l.strip() for l in f]

for a, b in product(ids, ids):
    d = dist(a, b)
    if d == 1:
        print(a, b)
        break

And from the output, I just copied and pasted the necessary characters that matched. That was faster than comming up with a custom method to do so.

 

Nice! Did you implement editdistance yourself, or is that an external library?

 

It is external. I found it via a quick google search. The edit distance measures how many operations - insertion, deletion or substitution - it takes to get from one string to the other. Since all the strings in the puzzle input have the same length, insertion and deletion do not come into play and it works out perfectly.

code of conduct - report abuse