You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.5 KiB
Python

import os
from entities.items.food import Food
from entities.elf import Elf
class CalorieList:
"""
Day 1 - Calorie List
The Elves take turns writing down the number of Calories contained by the various meals, snacks, rations, etc. that they've brought with them, one item per line.
Each Elf separates their own inventory from the previous Elf's inventory (if any) by a blank line.
"""
def __init__(self, path: str):
if not os.path.isfile(path):
raise FileNotFoundError(f"File was not found or isn't a file: {path}")
self.file_path = path
def parse_file(self):
"""
read the file and parse its contents
:return:
"""
elf_invs = []
with open(self.file_path, "r") as file:
curr_elf_inventory = []
for line in file:
line = line.replace("\n", "")
if line.isnumeric():
curr_elf_inventory.append(int(line))
elif len(line) == 0: # line is empty, next inventory
elf_invs.append(curr_elf_inventory)
curr_elf_inventory = list()
else:
raise ValueError(f"Unexpected line encountered in calorie list file: {line}")
elf_invs.append(curr_elf_inventory)
elves = []
for inv in elf_invs:
elf = Elf()
for calories in inv:
elf.add_to_inventory(Food(calories))
elves.append(elf)
return elves