-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_03.py
116 lines (100 loc) · 3.1 KB
/
day_03.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
"""AOC Day 3"""
from os import read
import pathlib
import time
TEST_INPUT = """00100
11110
10110
10111
10101
01111
00111
11100
10000
11001
00010
01010"""
def read_input(input_path: str) -> str:
"""take input file path and return a str with the file's content"""
with open(input_path, 'r') as input_file:
input_data = input_file.read().strip()
return input_data
def extract(input_data: str) -> list:
"""take input data and return the appropriate data structure"""
lines = input_data.split('\n')
for i, line in enumerate(lines):
lines[i] = list(map(int, line))
return lines
def common_bit(lines: list, column_number: int) -> int:
"""common_bit take a list of strings representing binary numbers and a column number and return an int that can be 0, > 0 or < 0 if the number of 1s and 0s in that column are equal, more 1s or more 0s"""
cb = 0
for bits in lines:
if bits[column_number] == 0:
cb -= 1
elif bits[column_number] == 1:
cb += 1
return cb
def part1(entries: list) -> int:
"""part1 solver take the entries and return the part1 solution"""
gamma_rate = ''
epsilon_rate = ''
for i in range(len(entries[0])):
cb = common_bit(entries, i)
if cb > 0:
gamma_rate += '1'
epsilon_rate += '0'
elif cb < 0:
gamma_rate += '0'
epsilon_rate += '1'
return int(gamma_rate, 2)*int(epsilon_rate, 2)
def part2(entries: list) -> int:
"""part2 solver take the entries and return the part2 solution"""
og_rating = ''
co2s_rating = ''
nums = entries.copy()
i = 0
while len(nums) > 1:
cb = common_bit(nums, i)
new_nums = list()
for num in nums:
if cb >= 0 and num[i] == 1:
new_nums.append(num)
elif cb < 0 and num[i] == 0:
new_nums.append(num)
nums = new_nums.copy()
i += 1
og_rating = ''.join(map(str,nums[0]))
nums = entries.copy()
i = 0
while len(nums) > 1:
cb = common_bit(nums, i)
new_nums = list()
for num in nums:
if cb >= 0 and num[i] == 0:
new_nums.append(num)
elif cb < 0 and num[i] == 1:
new_nums.append(num)
nums = new_nums.copy()
i += 1
co2s_rating = ''.join(map(str,nums[0]))
return int(og_rating, 2)*int(co2s_rating, 2)
def test_input_day_03():
"""pytest testing function"""
entries = extract(TEST_INPUT)
assert part1(entries) == 198
assert part2(entries) == 230
def test_bench_day_03(benchmark):
"""pytest-benchmark function"""
benchmark(main)
def main():
"""main function"""
input_path = str(pathlib.Path(__file__).resolve().parent.parent) + "/inputs/" + str(pathlib.Path(__file__).stem)
start_time = time.time()
input_data = read_input(input_path)
entries = extract(input_data)
print("Part 1: %d" % part1(entries))
print("Part 2: %d" % part2(entries))
end_time = time.time()
print("Execution time: %f" % (end_time-start_time))
if __name__ == "__main__":
main()