[py] Port 2018_02

This commit is contained in:
Joscha 2022-12-06 15:18:41 +01:00
parent 31d6098c58
commit 167e0125ac
3 changed files with 59 additions and 64 deletions

57
py/aoc/y2018/d02.py Normal file
View file

@ -0,0 +1,57 @@
# PART 1
def has_n_chars(word, n):
count = {}
for char in word:
count[char] = count.get(char, 0) + 1
return n in count.values()
def count_words(words):
twice = 0
thrice = 0
for word in words:
if has_n_chars(word, 2):
twice += 1
if has_n_chars(word, 3):
thrice += 1
return twice, thrice
def checksum(words):
twice, thrice = count_words(words)
return twice * thrice
# PART 2
def differ_by(a, b):
count = 0
for x, y in zip(a, b):
if x != y:
count += 1
return count
def find_ids(words):
for i, a in enumerate(words):
for b in words[i:]:
if differ_by(a, b) == 1:
return a, b
def common_chars(a, b):
result = []
for x, y in zip(a, b):
if x == y:
result.append(x)
return "".join(result)
def solve(inputstr):
words = inputstr.splitlines()
print(f"Part 1: {checksum(words)}")
a, b = find_ids(words)
print(f"Part 2: {common_chars(a, b)}")