I ended up using zip to help with the comparison if the strings in part 2:
#!/usr/bin/env python
fromcollectionsimportCounterdeffind_similar_box_ids(all_box_ids):forbox_id_1inall_box_ids:forbox_id_2inall_box_ids:# This is kind of a naive Levenshtein distance!
letter_pairs=zip(box_id_1,box_id_2)duplicates=list(filter(lambdapair:pair[0]==pair[1],letter_pairs))iflen(duplicates)==len(box_id_1)-1:return''.join(pair[0]forpairinduplicates)if__name__=='__main__':withopen('2-input.txt')asbox_file:all_box_ids=box_file.read().splitlines()print(find_similar_box_ids(all_box_ids))
Nice! Definitely think that's the easiest way to do number 1. Zip also makes sense for the second, though not using it allowed me to do the early return!
My part one ended up looking super similar!
I ended up using
zip
to help with the comparison if the strings in part 2:Nice! Definitely think that's the easiest way to do number 1. Zip also makes sense for the second, though not using it allowed me to do the early return!
True!