-
Notifications
You must be signed in to change notification settings - Fork 0
/
2.py
99 lines (82 loc) · 3.1 KB
/
2.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
from pathlib import Path
import sys
def is_char_repeat(label, count):
char_tested = []
unique_char_list = list(set(label)) # created list of unique characters using list and set
for unique_char in unique_char_list:
unique_char_count = 0
for char in label:
if unique_char == char:
unique_char_count += 1
# now counted how many times unique_char repeats in label
if unique_char_count == count:
return 1
return 0
def get_checksum(input_file):
two_count = 0
three_count = 0
with open(input_file,"r") as f:
content = f.read()
labels = content.split('\n')
for label in labels:
two_count += is_char_repeat(label, 2)
three_count += is_char_repeat(label, 3)
return two_count * three_count
def part1(input_file):
return get_checksum(input_file)
# for part 2
# given list of codes
# find codes that are identical except for one character being different
# how to achieve this - algorithm choices
# option 1:
# pick one string at a time, and compare with every other string
# while comparing that string with another, if even one character different then move on to next string
# move to next string and loop from that string onwards
# option 2: (to make it efficient)
# sort the strings - but is this useful? what if the character that is different is the first character
# probably not useful
def differ_by_one_char(code1, code2):
diff_char_count = 0
for char_loc in range(0,len(code1)):
if code1[char_loc] != code2[char_loc]:
diff_char_count+=1
if diff_char_count > 1:
return False, None
# if we are here then the previous return has not been reached and loop is over
# create answer code
if diff_char_count == 1:
answer_code = ""
for char_loc in range(0,len(code1)):
if code1[char_loc] == code2[char_loc]:
answer_code += code1[char_loc]
return True, answer_code
def part2(input_file):
# Load the file into a list
with open(input_file, 'r') as f:
codes = [line.rstrip() for line in f]
# loop on the list
found_answer = False
for key, code in enumerate(codes):
# loop again on all the strings after the current string
# because already compared this string with the strings
# prior to it in the list
for code2 in codes[key+1:]:
found_answer, answer_code = differ_by_one_char(code, code2)
if found_answer:
code_1 = code
code_2 = code2
break
if found_answer:
break
# at this point the code should have found code and code 2
# print(f'{code_1} and {code_2}')
# print(f'Answer is {answer_code}')
return answer_code
if __name__ == "__main__":
# this if statement is to be able to run it from within VS Code
if len(sys.argv) > 1:
input_file = sys.argv[1]
else:
input_file = Path(__file__).stem + '.txt'
print(part1(input_file))
print(part2(input_file))