Skip to content
advent of code 2018

day 02

inventory management system

approach

this one is unfinished as committed. part 1 is done: twos_threes counts each id's letters in a dict and reports whether some letter appears exactly twice and some exactly three times; the checksum multiplies the two column sums.

part 2 gets halfway. differs_by_one zips two ids and counts the positions where they disagree, and part2 collects every pair of ids that differ in at most one place. but the function stops there: it never pulls out the letters the pair has in common, and it returns nothing.

the trick

zip two ids together and count the mismatched positions to find the pair that differs by one letter.

python
def differs_by_one(s1,s2):
	return len(list(filter(lambda x:x[0]!=x[1],list(zip(*[s1,s2])))))<=1

def part2():
	candidates=[]
	for i in range(len(strs)):
		for j in range(i+1,len(strs)):
			if differs_by_one(strs[i],strs[j]):
				candidates.append((i,j))