solve.py (920B)
1import sys 2sys.path.append("../common") 3import aoc 4 5data = aoc.data.split("\n") 6 7def solve1(args): 8 doubles = 0 9 triples = 0 10 for s in data: 11 counts = list((0,) * 26) 12 for c in s: 13 counts[ord(c[0])-97] += 1 14 if 2 in counts: 15 doubles += 1 16 if 3 in counts: 17 triples += 1 18 return doubles * triples 19 20def compare(s1, s2): 21 dif = 0 22 same = list() 23 for i in range(len(s1)): 24 if s1[i] != s2[i]: 25 dif += 1 26 else: 27 same.append(s1[i]) 28 return (dif, same) 29 30def solve2(args): 31 for i in range(len(data)): 32 for j in range(i, len(data)): 33 if i == j: 34 continue 35 res = compare(data[i], data[j]) 36 if res[0] == 1: 37 return "".join(res[1]) 38 39 40aoc.run(solve1, solve2, sols=[3952, "vtnikorkulbfejvyznqgdxpaw"])