simeonshopov

Cups and bottles

Apr 7th, 2020
544
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.36 KB | None | 0 0
  1. #!/usr/local/bin/python3.7
  2. # -*- coding: utf-8 -*import
  3. from _collections import deque
  4.  
  5. cups = deque([int(x) for x in input().split()])
  6. bottles = deque([int(x) for x in input().split()])
  7. no_more_cups = False
  8. no_more_bottles = False
  9. wasted = 0
  10.  
  11.  
  12. def overfill(c: int, b: int, cs: deque, bs: deque, w: int):
  13.     w += b - c
  14.     c -= b
  15.     bs.pop()
  16.     cs.popleft()
  17.     return w, cs, bs, c, b
  18.  
  19.  
  20. while True:
  21.     if not cups:
  22.         no_more_cups = True
  23.         break
  24.     if not bottles:
  25.         no_more_bottles = True
  26.         break
  27.     bottle = bottles[-1]
  28.     cup = cups[0]
  29.     if bottle >= cup:
  30.         wasted, cups, bottles, cup, bottle = overfill(cup, bottle, cups, bottles, wasted)
  31.     if bottle < cup:
  32.         while True:
  33.             if cup <= 0:
  34.                 break
  35.             if bottle >= cup:
  36.                 wasted, cups, bottles, cup, bottle = overfill(cup, bottle, cups, bottles, wasted)
  37.             else:
  38.                 cup -= bottle
  39.                 bottles.pop()
  40.                 bottle = bottles[-1]
  41.  
  42. if no_more_cups:
  43.     bottles = [str(x) for x in bottles]
  44.     print(f'Bottles: {" ".join([bottles[i] for i in range(len(bottles) - 1, -1, -1)])}')
  45.     print(f'Wasted litters of water: {wasted}')
  46. elif no_more_bottles:
  47.     cups = [str(x) for x in cups]
  48.     print(f'Cups: {" ".join(cups)}')
  49.     print(f'Wasted litters of water: {wasted}')
Advertisement
Add Comment
Please, Sign In to add comment