DEV Community

Discussion on: AoC Day 2: Inventory Management System

Collapse
 
r0f1 profile image
Florian Rohrer

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.

Collapse
 
rpalo profile image
Ryan Palo

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

Collapse
 
r0f1 profile image
Florian Rohrer

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.

Thread Thread
 
rpalo profile image
Ryan Palo

Ok that’s cool, just checking. Neat!