forked from narimiran/AdventOfCode2017
-
Notifications
You must be signed in to change notification settings - Fork 1
/
day02.py
58 lines (48 loc) · 1.1 KB
/
day02.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from collections import Counter
with open('./inputs/02.txt') as f:
ids = f.readlines()
def count23(id):
count = Counter(id)
exactly_two = bool({k for k, v in count.items() if v == 2})
exactly_three = bool({k for k, v in count.items() if v == 3})
return exactly_two, exactly_three
def solve(ids):
twos = 0
threes = 0
for id in ids:
two, three = count23(id)
twos += two
threes += three
return twos, threes
test = """
abcdef
bababc
abbcde
abcccd
aabcdd
abcdee
ababab
"""
test_ids = test.splitlines()
print(count23('bababc'))
a, b = solve(test_ids)
print(a, b, a*b)
a, b = solve(ids)
print(len(ids))
print(a, b, a*b)
# second part
# distance bewteen ids
def distance(id1, id2):
return sum([0 if c1 == c2 else 1 for c1, c2 in zip(id1, id2)])
print(distance("abcde", "axcye"))
# cycle over all pairs of ids
for i, id1 in enumerate(ids):
for id2 in ids[(i+1):]:
dist = distance(id1, id2)
if dist == 1:
break
if dist == 1:
break
print(id1)
print(id2)
print(''.join([c for i, c in enumerate(id1) if id2[i] == c]))