Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Open the input file and read the lines into a list
- with open("input.txt") as f:
- lines = f.readlines()
- # Create a dictionary to store the number of Calories each Elf is carrying
- elf_calories = {}
- # Keep track of the current Elf
- current_elf = ""
- # Loop through each line in the input
- for line in lines:
- # If the line is empty, this indicates the end of an Elf's inventory
- if line == "\n":
- # If we've reached the end of an Elf's inventory, set the current Elf to an empty string
- # This will cause the next non-empty line to be treated as the start of a new Elf's inventory
- current_elf = ""
- continue
- # If the current Elf is not set, this indicates the start of a new Elf's inventory
- if current_elf == "":
- # Set the current Elf to the name on this line
- current_elf = line.strip()
- # Add the Elf to the dictionary with an initial value of 0
- elf_calories[current_elf] = 0
- continue
- # If we've reached this point, the line must be a food item with its Calories listed
- # Parse the Calories from the line and add it to the current Elf's total
- calories = int(line.strip())
- elf_calories[current_elf] += calories
- # Find the Elf with the most Calories by finding the maximum value in the dictionary
- max_calories = max(elf_calories.values())
- # Print the result
- print(f"The Elf with the most Calories is carrying {max_calories} Calories.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement