Advertisement
SimonJkAdamek

AOC day 1

Dec 1st, 2022 (edited)
1,149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.57 KB | Software | 0 0
  1. #!/usr/bin/env python3
  2. #PART1
  3. ########################################
  4. calories=[]
  5. temp=0
  6.  
  7. with open("file.txt") as file:
  8.     for line in file:                
  9.         if line.strip():              
  10.             temp+=int(line)          
  11.         else:                        
  12.             calories.append(temp)    
  13.             temp=0                    
  14.     calories.append(temp)             print(max(calories))                  
  15. ########################################
  16.  
  17. #PART1
  18. ########################################
  19. maximum=None
  20. temp=0
  21.  
  22. with open("file.txt") as file:
  23.     for line in file:
  24.         if line.strip():
  25.             temp+=int(line)
  26.         else:
  27.             if not maximum or maximum<temp:
  28.                 maximum=temp
  29.             temp=0
  30.     if maximum<temp:
  31.         maximum=temp
  32. print(maximum)
  33. ########################################
  34.  
  35. #PART2
  36. ########################################
  37. maximum=[0,0,0]
  38. temp=0
  39.  
  40. with open("input.txt") as file:
  41.     for line in file:
  42.         if line.strip():
  43.             temp+=int(line)
  44.         else:
  45.             if maximum[0]<temp:
  46.                 maximum[2]=maximum[1]          
  47.                 maximum[1]=maximum[0]
  48.                 maximum[0]=temp
  49.             elif maximum[1]<temp:
  50.                 maximum[2]=maximum[1]
  51.                 maximum[1]=temp
  52.             elif maximum[2]<temp:
  53.                 maximum[2]=temp
  54.             temp=0
  55.     if maximum[0]<temp:
  56.         maximum[2]=maximum[1]
  57.         maximum[1]=maximum[0]
  58.         maximum[0]=temp
  59. print("Maximum is", (maximum[0]+maximum[1]+maximum[2]))
  60.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement