-
Notifications
You must be signed in to change notification settings - Fork 0
/
day_1.py
75 lines (60 loc) · 1.96 KB
/
day_1.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
from __future__ import division, print_function
import os
from my_utils.tests import test_function
def solve_capcha(capcha_str):
"""Function which calculates the solution to part 1
Arguments
---------
capcha_str : str, a string of numbers
Returns
-------
total : int, the sum of adjacent matches
"""
capcha = [int(cc) for cc in list(capcha_str)]
total = 0
for ii in range(len(capcha)):
if capcha[ii] == capcha[ii - 1]:
total += capcha[ii]
return total
def solve_capcha2(capcha_str):
"""Function which calculates the solution to part 2
Arguments
---------
capcha_str : str, a string of numbers
Returns
-------
total : int, the sum of 'half way' matches
"""
capcha = [int(cc) for cc in list(capcha_str)]
total = 0
capcha_len = len(capcha)
for ii in range(capcha_len):
if capcha[ii] == capcha[ii - capcha_len//2]:
total += capcha[ii]
return total
part_1 = solve_capcha
part_2 = solve_capcha2
def main(test_datas, functions, puzzle_input=None):
for ii, (test_data, fun) in enumerate(zip(test_datas, functions)):
nr_errors = test_function(fun, test_data)
if nr_errors == 0:
print('Pt. {} Tests Passed'.format(ii+1))
if puzzle_input is not None:
fn = os.path.basename(__file__)
for ii, fun in enumerate(functions):
ans = fun(puzzle_input)
print('{} Pt. {} Solution: {}'.format(fn, ii+1, ans))
if __name__ == "__main__":
test_data1 = {
'inputs': ['1122', '1111', '1234', '91212129'],
'outputs': [3, 4, 0, 9]
}
test_data2 = {
'inputs': ['1212', '1221', '123425', '123123', '12131415'],
'outputs': [6, 0, 4, 12, 4]
}
with open('./inputs/day_1.txt') as f:
puzzle_input = f.read().strip()
main(test_datas=[test_data1, test_data2],
functions=[part_1, part_2],
puzzle_input=puzzle_input)