-
Notifications
You must be signed in to change notification settings - Fork 0
/
day_1.py
47 lines (29 loc) · 1.32 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
import logging
import math
from typing import List
logging.basicConfig(level=logging.INFO)
# PART 1 : What is the sum of the fuel requirements?
def parse_txt_file(filepath: str) -> List[int]:
with open(filepath) as f:
return [int(mass) for mass in f.readlines()]
mass_of_nodes = parse_txt_file('day-1.txt')
def fuel_required_given_mass(mass: int) -> int:
return math.floor(mass / 3) - 2
def total_fuel_required(mass_of_nodes: List[int]) -> int:
fuels_required = [fuel_required_given_mass(mass) for mass in mass_of_nodes]
return sum(fuels_required)
logging.info(f'Total fuel required: {total_fuel_required(mass_of_nodes)}')
# PART 2: What is the sum of the fuel requirements for all the modules?
def get_fuel_required_for_all_modules(mass_of_nodes: List[int]) -> int:
fuel_required_for_modules = []
for mass in mass_of_nodes:
fuel_required = fuel_required_given_mass(mass)
fuel_required_for_modules.append(fuel_required_given_mass(mass))
while fuel_required > 0:
fuel_required = fuel_required_given_mass(fuel_required)
if fuel_required > 0:
fuel_required_for_modules.append(fuel_required)
return sum(fuel_required_for_modules)
logging.info(
f'Total fuel required for all modules: {get_fuel_required_for_all_modules(mass_of_nodes)}'
)