Day 1 - Calorie Counting
went a bit overboard because it was fun to give all elves names and a "proper" inventory.main
							parent
							
								
									f1e287a3cf
								
							
						
					
					
						commit
						0a735eb84b
					
				
											
												Binary file not shown.
											
										
									
								
											
												Binary file not shown.
											
										
									
								
											
												Binary file not shown.
											
										
									
								| @ -0,0 +1,48 @@ | ||||
| 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 | ||||
| 
 | ||||
											
												Binary file not shown.
											
										
									
								
											
												Binary file not shown.
											
										
									
								| @ -0,0 +1,47 @@ | ||||
| from entities.items.item import Item | ||||
| from entities.items.food import Food | ||||
| 
 | ||||
| 
 | ||||
| class Elf: | ||||
|     """ | ||||
|     The absolute overkill, because I can. | ||||
|     """ | ||||
|     ELF_COUNT = 0 | ||||
| 
 | ||||
|     def __init__(self): | ||||
|         self.__number = Elf.ELF_COUNT | ||||
|         Elf.ELF_COUNT += 1 | ||||
|         self.__name = Elf.fetch_elf_name(self.__number) | ||||
|         self.__inventory = [] | ||||
| 
 | ||||
|     @staticmethod | ||||
|     def fetch_elf_name(i: int) -> str: | ||||
|         name_list = ["Dash", "Evergreen", "Sugarplum", "Pixie", "Pudding", "Perky", "Candycane", "Glitter-toes", | ||||
|                      "Happy", "Angel-Eyes", "Sugar-Socks", "McJingles", "Frost", "Tinsel", "Twinkle", "Jingle", "Ginger", | ||||
|                      "Joy", "Merry", "Pepper", "Sparkle", "Tinsel", "Winter", "Trinket", "Buddy", "Noel", "Snowball", | ||||
|                      "Tiny", "Elfin", "Candy", "Carol", "Angel", "Nick", "Plum", "Holly", "Snow", "Pine", "Garland", | ||||
|                      "Joseph", "Gabriel", "Hope", "Cedar"] | ||||
|         return name_list[i % len(name_list)] | ||||
| 
 | ||||
|     def __str__(self): | ||||
|         return f"#{self.__number} {self.__name}" | ||||
| 
 | ||||
|     def get_inventory(self) -> list: | ||||
|         return self.__inventory | ||||
| 
 | ||||
|     def add_to_inventory(self, item: Item): | ||||
|         self.__inventory.append(item) | ||||
| 
 | ||||
|     def remove_from_inventory(self, slot: int): | ||||
|         """ | ||||
|         Removes item of the given slot number from the inventory using pop(). | ||||
|         That means there are no empty spaces. | ||||
| 
 | ||||
|         Starts with 0. | ||||
|         :param slot: | ||||
|         :return: | ||||
|         """ | ||||
|         self.__inventory.pop(slot) | ||||
| 
 | ||||
|     def get_inventory_calories(self) -> int: | ||||
|         return sum([x.calorie_value for x in list(filter(lambda x: isinstance(x, Food), self.get_inventory()))]) | ||||
											
												Binary file not shown.
											
										
									
								
											
												Binary file not shown.
											
										
									
								
											
												Binary file not shown.
											
										
									
								| @ -0,0 +1,8 @@ | ||||
| from entities.items.item import Item | ||||
| 
 | ||||
| 
 | ||||
| class Food(Item): | ||||
| 
 | ||||
|     def __init__(self, calorie: int): | ||||
|         super().__init__(name="") | ||||
|         self.calorie_value = calorie | ||||
| @ -0,0 +1,5 @@ | ||||
| 
 | ||||
| class Item: | ||||
| 
 | ||||
|     def __init__(self, name: str): | ||||
|         self.name = name | ||||
| @ -0,0 +1,14 @@ | ||||
| 1000 | ||||
| 2000 | ||||
| 3000 | ||||
| 
 | ||||
| 4000 | ||||
| 
 | ||||
| 5000 | ||||
| 6000 | ||||
| 
 | ||||
| 7000 | ||||
| 8000 | ||||
| 9000 | ||||
| 
 | ||||
| 10000 | ||||
											
												
													File diff suppressed because it is too large
													Load Diff
												
											
										
									
								| @ -0,0 +1,15 @@ | ||||
| from calories.file.calorie_list import CalorieList | ||||
| 
 | ||||
| 
 | ||||
| if __name__ == "__main__": | ||||
|     elves = CalorieList("./input/day1/input.lst").parse_file() | ||||
|     for elf in elves: | ||||
|         print(f"Elf {elf} has calories: {elf.get_inventory_calories()}") | ||||
|     elves.sort(key=lambda x: x.get_inventory_calories(), reverse=True) | ||||
|     print(f"Most calories has: {elves[0]} with {elves[0].get_inventory_calories()} calories in their inventory!") | ||||
| 
 | ||||
|     print(f"The top 3 calorie elves are: {elves[0]} ({elves[0].get_inventory_calories()}), " | ||||
|           f"{elves[1]} ({elves[1].get_inventory_calories()}) and " | ||||
|           f"{elves[2]} ({elves[2].get_inventory_calories()})") | ||||
|     print(f"Their total is: {sum([x.get_inventory_calories() for x in elves[:3]])}") | ||||
| 
 | ||||
					Loading…
					
					
				
		Reference in New Issue