solution.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #!/usr/bin/python3
  2. import sys
  3. def part1():
  4. print('part 1')
  5. max_cal = 0
  6. max_elf = 0
  7. current_cal = 0
  8. current_elf = 1
  9. for line in sys.stdin:
  10. if line.strip() == '':
  11. if current_cal > max_cal:
  12. max_cal = current_cal
  13. max_elf = current_elf
  14. current_cal = 0
  15. current_elf += 1
  16. continue
  17. current_cal += int(line.rstrip('\n'))
  18. print(f'elf {max_elf} has {max_cal} calories')
  19. print(f'{max_cal}')
  20. def part2(n):
  21. print('part 2')
  22. top = [0 for i in range(n)]
  23. cal = 0
  24. for line in sys.stdin:
  25. if line.strip() == '':
  26. if cal > min(top):
  27. new_top = []
  28. j = 0
  29. for i in range(n):
  30. if top[j] > cal:
  31. new_top.append(top[j])
  32. j += 1
  33. else:
  34. new_top.append(cal)
  35. cal = 0
  36. top = new_top
  37. cal = 0
  38. continue
  39. cal += int(line.strip())
  40. print(f'top {top} -> {sum(top)}')
  41. if sys.argv[1] in '1':
  42. part1()
  43. else:
  44. part2(3)