-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday_1.py
More file actions
108 lines (88 loc) · 3.1 KB
/
day_1.py
File metadata and controls
108 lines (88 loc) · 3.1 KB
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
"""Solution for https://adventofcode.com/2019/day/1/"""
from argparse import ArgumentParser
import doctest
from math import floor
from pathlib import Path
from typing import List
DEFAULT_INPUT_FILE_PATH = "input_1.txt"
def main_1(module_masses: List[int]) -> int:
return sum([fuel_requirement(mod_mass) for mod_mass in module_masses])
def main_2(module_masses: List[int]) -> int:
return sum(
[
fuel_requirement(mod_mass, add_fuel_for_fuel=True)
for mod_mass in module_masses
]
)
def parse_input(input_path: Path) -> List[int]:
if not input_path.exists():
print(f"Bad input path. '{input_path}' does not exist.")
return []
input_text: str = input_path.read_text()
module_masses: List[int] = []
for str_mod_mass in input_text.split("\n"):
stripped_mod_mass: str = str_mod_mass.strip()
if stripped_mod_mass:
module_masses.append(int(stripped_mod_mass))
return module_masses
def fuel_requirement(module_mass: int, add_fuel_for_fuel: bool = False) -> int:
"""
>>> fuel_requirement(12)
2
>>> fuel_requirement(14)
2
>>> fuel_requirement(1969)
654
>>> fuel_requirement(100756)
33583
>>> fuel_requirement(14, add_fuel_for_fuel=True)
2
>>> fuel_requirement(1969, add_fuel_for_fuel=True)
966
>>> fuel_requirement(100756, add_fuel_for_fuel=True)
50346
:param module_mass: int, mass of the module
:param add_fuel_for_fuel: bool, default False, whether to recursively
calculate fuel required for weight of fuel added
:return: int, amount of fuel required
"""
fuel_required: int = floor(module_mass / 3) - 2
if not add_fuel_for_fuel:
return fuel_required
if fuel_required <= 0:
return 0
return fuel_required + fuel_requirement(fuel_required, add_fuel_for_fuel)
def build_arg_parser() -> ArgumentParser:
arg_parser = ArgumentParser()
arg_parser.add_argument(
"-i", "--input", help="Path for input file", default=DEFAULT_INPUT_FILE_PATH
)
arg_parser.add_argument("-r", "--run", help="Run the solution", action="store_true")
arg_parser.add_argument(
"-t", "--test", help="Run the tests for this solution", action="store_true"
)
return arg_parser
def run(arg_parser: ArgumentParser) -> None:
args = arg_parser.parse_args()
if args.test:
print("Running Tests...")
failures, num_tests = doctest.testmod()
if not failures:
print(f"Ran {num_tests} test, 0 failures")
else:
return
if not args.test or args.run:
print("Parsing input...")
parsed_input = parse_input(Path(args.input))
if not parsed_input:
print("Could not parse input.")
return
print("Computing answer for part 1...")
answer_1 = main_1(parsed_input)
print(f"Answer for part 1: {answer_1}")
print("Computing answer for part 2...")
answer_2 = main_2(parsed_input)
print(f"Answer for part 2: {answer_2}")
if __name__ == "__main__":
parser = build_arg_parser()
run(parser)