-
Notifications
You must be signed in to change notification settings - Fork 0
/
day7.py
52 lines (35 loc) · 1.13 KB
/
day7.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
from helper.utils import *
DAY = 7
@time_function
def prepare_data():
raw_data = parse_file_rows_to_list(DAY, split_row_on=",")
return list_str_to_list_number(raw_data[0])
@time_function
def part_a(data):
return _calc_fuel_consumption(data)
@time_function
def part_b(data):
return _calc_fuel_consumption(data, gaussian_sum=True)
def _calc_fuel_consumption(positions, gaussian_sum=False):
gather_at = _calc_median(positions) if not gaussian_sum else int(sum(positions) / len(positions))
total_consumption = 0
for depth in positions:
steps = abs(depth - gather_at)
if gaussian_sum:
total_consumption += steps * (steps + 1) / 2
else:
total_consumption += steps
return int(total_consumption)
def _calc_median(data: List[int]) -> int:
data = sorted(data)
data_len = len(data)
data_middle_index = data_len // 2
if data_len % 2 != 0:
return data[data_middle_index]
return (data[data_middle_index] + data[data_middle_index - 1]) // 2
def main():
data = prepare_data()
part_a(data)
part_b(data)
if __name__ == '__main__':
main()