-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpart_two.py
40 lines (28 loc) · 874 Bytes
/
part_two.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
from typing import override
from infrastructure.solutions.base import Solution
class Year2019Day1Part2Solution(Solution):
@classmethod
@override
def parse_input(cls, text_input: str) -> dict[str, list[int]]:
masses = []
for line in text_input.split('\n'):
if line.isdigit():
masses.append(int(line))
return {'masses': masses}
@classmethod
@override
def solve(cls, masses: list[int]) -> int:
"""
Time: O(n*log3(m))
Space: O(1)
Where n - length of list with masses,
m - maximum mass value
"""
fuel = 0
for mass in masses:
while mass > 0:
mass = max(0, (mass // 3) - 2)
fuel += mass
return fuel
if __name__ == '__main__':
print(Year2019Day1Part2Solution.main())