Skip to content

Instantly share code, notes, and snippets.

@carrickkv2
Created December 3, 2022 19:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carrickkv2/b1edaede213da3795a47ba57153cc035 to your computer and use it in GitHub Desktop.
Save carrickkv2/b1edaede213da3795a47ba57153cc035 to your computer and use it in GitHub Desktop.
## 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