Guest User

Untitled

a guest
Dec 1st, 2022
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.81 KB | None | 0 0
  1. # Open the input file
  2. with open("input.txt", "r") as input_file:
  3.   # Read the input
  4.   calories = []
  5.   current_elf_calories = []
  6.   for line in input_file:
  7.     line = line.strip()
  8.     if line == "":
  9.       # Empty line, start a new list of calories for the next Elf
  10.       calories.append(current_elf_calories)
  11.       current_elf_calories = []
  12.     else:
  13.       # Non-empty line, add the number of calories to the current Elf's list
  14.       current_elf_calories.append(int(line))
  15.  
  16.   # Add the last Elf's calories to the list (if any)
  17.   if current_elf_calories:
  18.     calories.append(current_elf_calories)
  19.  
  20.   # Find the total number of calories for each Elf
  21.   totals = [sum(c) for c in calories]
  22.  
  23.   # Find the maximum number of calories and print the result
  24.   max_calories = max(totals)
  25.   print(max_calories)
  26.  
  27.  
  28.  
  29.  
Advertisement
Add Comment
Please, Sign In to add comment