Advent of Code Day 2

Day2

Solution to day 1 of Advent of Code https://adventofcode.com/2018/day/2

Part 1

Making use of Counter from collections to count the number of occurences of each letter.

In [1]:
from collections import Counter

a, b = 0,0
with open('datafiles/aoc2input.txt','r') as f:
    for line in f.readlines():
        c = Counter(line)
        if 2 in c.values():
            a+=1
        if 3 in c.values():
            b+=1

print(a*b)
5750

Part 2

Testing all combinations with combinations from itertools, each combination is tested by zipping two strings together and summing over differences. We make use of True evaluating to 1 and False evaluating to 0 when summing.

In [2]:
from itertools import combinations
with open('datafiles/aoc2input.txt','r') as f:
    lines = f.readlines()
    for a,b in combinations(lines, 2):
        if sum(i != j for i, j in zip(a, b)) == 1:
            print("".join(i for i,j in zip(a,b) if i==j ))
            break
tzyvunogzariwkpcbdewmjhxi

links

social