advent/2018/02.py

30 lines
640 B
Python
Raw Permalink Normal View History

2018-12-06 06:08:41 +00:00
from collections import Counter
from itertools import combinations
with open('data/02.txt') as f:
data = [r.strip() for r in f]
totals = {2: 0, 3: 0}
for item in data:
counter = Counter(item)
for n in 2, 3:
if n in counter.values():
totals[n] += 1
print(f'Part 1:\n{totals[2] * totals[3]}\n')
def compare(a, b):
different = 0
for charA, charB in zip(a, b):
if charA != charB:
different += 1
if different > 1:
return False
return True
for a, b in combinations(data, 2):
if compare(a, b):
print(''.join(c for c, d in zip(a, b) if c == d))