-
Notifications
You must be signed in to change notification settings - Fork 0
/
day3.py
67 lines (49 loc) · 1.52 KB
/
day3.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
from helper.utils import time_function, parse_file_rows_to_list
from collections import defaultdict
DAY = 3
def prepare_data():
raw_data = parse_file_rows_to_list(DAY)
return raw_data
@time_function
def part_a(data):
freqs = determine_one_freqs(data)
gamma = ""
epsi = ""
for pos in freqs:
if freqs[pos] >= len(data)/2:
gamma += "1"
epsi += "0"
else:
gamma += "0"
epsi += "1"
return int(gamma, 2) * int(epsi, 2)
@time_function
def part_b(data):
oxy = filter_by_bit(0, data)
co2 = filter_by_bit(0, data, keep_most_common=False)
return int(oxy, 2) * int(co2, 2)
def filter_by_bit(bit_pos, nums, keep_most_common=True):
if len(nums) == 1:
return nums[0]
frequencies = determine_one_freqs(nums)
most_common = "1" if frequencies[bit_pos] >= len(nums)/2 else "0"
numbers_kept = []
for num in nums:
if keep_most_common and num[bit_pos] == most_common:
numbers_kept.append(num)
elif not keep_most_common and num[bit_pos] != most_common:
numbers_kept.append(num)
return filter_by_bit(bit_pos + 1, numbers_kept, keep_most_common=keep_most_common)
def determine_one_freqs(nums):
frequencies = defaultdict(int)
for i in range(len(nums[0])):
for num in nums:
if num[i] == "1":
frequencies[i] += 1
return frequencies
def main():
data = prepare_data()
part_a(data)
part_b(data)
if __name__ == '__main__':
main()