-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday2.py
More file actions
63 lines (43 loc) · 1.23 KB
/
Copy pathday2.py
File metadata and controls
63 lines (43 loc) · 1.23 KB
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
text_file = '''C:/Users/Davinder/PycharmProjects/adventofcode2018/day2input.txt'''
with open(text_file, 'r') as f:
data = [str(line.strip()) for line in f]
# func to decompose ids into dic of key letter and count letter
def to_dict(x):
d = {}
for i in list(x):
if i in d:
d[i] += 1
else:
d[i] = 1
return d
# looping through dictionary to flag 2's and 3's
def two_three_detector(x):
two = 0
three = 0
for k in x:
if x[k] == 2:
two = 1
elif x[k] == 3:
three = 1
return two, three
def return_checksum():
twos = 0
threes = 0
for i in data:
twos += two_three_detector(to_dict(i))[0]
threes += two_three_detector(to_dict(i))[1]
print twos * threes
# to find count of differences between two string inputs
def differences(x, y):
diffs = 0
for i in range(26):
if not x[i] == y[i]:
diffs += 1
return diffs
# comparison loop comparing one to another only once using indexing
def find_match():
for i in range(len(data)):
for j in range(i + 1, len(data)):
if differences(data[i], data[j]) == 1:
return data[i], data[j]
print find_match()