Created
December 3, 2022 19:11
-
-
Save carrickkv2/b1edaede213da3795a47ba57153cc035 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Part 1 | |
from __future__ import annotations | |
from pathlib import Path | |
def calculate_max_calories(input: str) -> int | str: | |
""" | |
Calculates the max number of calories in a given input file. Calories are int's. | |
""" | |
max_calories: float = float("-inf") | |
current_calories: int = 0 | |
path = Path(input) | |
if path.exists(): | |
with path.open(mode="r") as file: | |
for line in file: | |
if line != "\n": | |
current_calories += int(line) | |
else: | |
max_calories = max(current_calories, max_calories) | |
current_calories = 0 | |
return max_calories | |
return "File does not exist" | |
print(calculate_max_calories("input.txt")) | |
## Part 2 | |
def calculate_max_calories(input: str) -> int | str: | |
""" | |
Calculates the top 3 max number of calories in a given input file. Calories are int's. | |
""" | |
all_calories: list[int] = [] | |
current_calories: int = 0 | |
path = Path(input) | |
if path.exists(): | |
with path.open(mode="r") as file: | |
for line in file: | |
if line != "\n": | |
current_calories += int(line) | |
else: | |
all_calories.append(current_calories) | |
current_calories = 0 | |
all_calories.sort(reverse=True) | |
return sum(all_calories[:3]) | |
return "File does not exist" | |
print(calculate_max_calories("input.txt")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment