Advertisement
VSZM

AoC2022Day1ChatGPT

Dec 8th, 2022
525
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.44 KB | None | 0 0
  1. # Open the input file and read the lines into a list
  2. with open("input.txt") as f:
  3.     lines = f.readlines()
  4.  
  5. # Create a dictionary to store the number of Calories each Elf is carrying
  6. elf_calories = {}
  7.  
  8. # Keep track of the current Elf
  9. current_elf = ""
  10.  
  11. # Loop through each line in the input
  12. for line in lines:
  13.     # If the line is empty, this indicates the end of an Elf's inventory
  14.     if line == "\n":
  15.         # If we've reached the end of an Elf's inventory, set the current Elf to an empty string
  16.         # This will cause the next non-empty line to be treated as the start of a new Elf's inventory
  17.         current_elf = ""
  18.         continue
  19.  
  20.     # If the current Elf is not set, this indicates the start of a new Elf's inventory
  21.     if current_elf == "":
  22.         # Set the current Elf to the name on this line
  23.         current_elf = line.strip()
  24.         # Add the Elf to the dictionary with an initial value of 0
  25.         elf_calories[current_elf] = 0
  26.         continue
  27.  
  28.     # If we've reached this point, the line must be a food item with its Calories listed
  29.     # Parse the Calories from the line and add it to the current Elf's total
  30.     calories = int(line.strip())
  31.     elf_calories[current_elf] += calories
  32.  
  33. # Find the Elf with the most Calories by finding the maximum value in the dictionary
  34. max_calories = max(elf_calories.values())
  35.  
  36. # Print the result
  37. print(f"The Elf with the most Calories is carrying {max_calories} Calories.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement