Advertisement
Davitkova

Eli_elves

Oct 18th, 2024 (edited)
12
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.59 KB | None | 0 0
  1. from collections import deque
  2.  
  3.  
  4. def make_toy(elf, material):
  5.  
  6.     if elf >= material:
  7.         return True
  8.  
  9.     return False
  10.  
  11.  
  12. elfs_energy = deque(int(num) for num in input().split())
  13. materials = [int(num) for num in input().split()]
  14.  
  15. cookie = 1
  16. total_energy = 0
  17. total_made_toys = 0
  18. count = 0
  19.  
  20. while elfs_energy and materials:
  21.  
  22.     first_elf = elfs_energy.popleft()
  23.  
  24.     if first_elf < 5:
  25.         continue
  26.  
  27.     last_material = materials.pop()
  28.     count += 1
  29.  
  30.     if count % 3 == 0:
  31.         last_material *= 2
  32.  
  33.     can_make_toy = make_toy(first_elf, last_material)
  34.  
  35.     if can_make_toy:
  36.  
  37.         first_elf -= last_material
  38.         total_energy += last_material
  39.  
  40.         if count % 5 == 0:
  41.             # No toy/toys and no cookie
  42.             elfs_energy.append(first_elf)
  43.             continue
  44.  
  45.         # Toy is not broken and dwarf deserves a cookie
  46.         if count % 3 == 0:
  47.             # Third turn. The dwarf succeeded to make 2 toys
  48.             total_made_toys += 2
  49.         else:
  50.             total_made_toys += 1
  51.  
  52.         elfs_energy.append(first_elf + cookie)
  53.  
  54.     else:
  55.         # not enough energy to make a toy
  56.         # Return the material in the original value:
  57.         materials.append(last_material) if count % 3 != 0 else materials.append(last_material // 2)
  58.         first_elf *= 2
  59.         elfs_energy.append(first_elf)
  60.  
  61.  
  62. print(f"Toys: {total_made_toys}")
  63. print(f"Energy: {total_energy}")
  64. if elfs_energy:
  65.     print(f"Elves left: {', '.join(str(el) for el in elfs_energy)}")
  66. if materials:
  67.     print(f"Boxes left: {', '.join(str(el) for el in materials)}")
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement