74 lines
1.5 KiB
Python
74 lines
1.5 KiB
Python
from sys import stdin, stdout
|
|
import time
|
|
|
|
class Bucket:
|
|
def __init__(self, N, R, T, CB):
|
|
self.q = 0.0
|
|
self.N = N
|
|
self.R = R
|
|
self.T = T
|
|
self.CB = CB
|
|
self.__last_pop = 0
|
|
self.__last_state = False
|
|
|
|
def push(self):
|
|
result = self.__push_c(1)
|
|
self.__cb()
|
|
return result
|
|
|
|
def pop(self):
|
|
result = self.__pop()
|
|
self.__cb()
|
|
return result
|
|
|
|
def __cb(self):
|
|
new_state = self.q > self.T
|
|
if new_state == self.__last_state:
|
|
return
|
|
self.__last_state = new_state
|
|
self.CB(new_state)
|
|
|
|
def state(self):
|
|
return self.__last_state
|
|
|
|
def __push_c(self, c):
|
|
self.__pop()
|
|
if self.q+c > self.N:
|
|
return False
|
|
self.q += c
|
|
print(f"{int(self.q)} = + {int(c+.5)}")
|
|
return True
|
|
|
|
def __pop(self):
|
|
now = self.__now()
|
|
remove_up_to = (now - self.__last_pop) / self.R
|
|
if remove_up_to > self.q:
|
|
remove_up_to = self.q
|
|
self.q -= remove_up_to
|
|
print(f"{int(self.q)} = - {int(remove_up_to+.5)}")
|
|
self.__last_pop = now
|
|
|
|
def __now(self):
|
|
return time.time()
|
|
|
|
def main(stream):
|
|
bucket = None
|
|
N = 3
|
|
R = 2
|
|
T = 1
|
|
bucket = Bucket(N, R, T, lambda state: print(f"state is now {state} ({bucket.state()}), q={bucket.q}"))
|
|
|
|
while readline():
|
|
bucket.push()
|
|
stdout.flush()
|
|
|
|
def readline():
|
|
try:
|
|
line = input()
|
|
return True
|
|
except EOFError:
|
|
return False
|
|
|
|
if __name__ == "__main__":
|
|
main(stdin)
|