Skip to content

Instantly share code, notes, and snippets.

@segasai
Created August 23, 2023 11:18
Show Gist options
  • Save segasai/7405c5ce25e7e27ad8755f73d7f070f9 to your computer and use it in GitHub Desktop.
Save segasai/7405c5ce25e7e27ad8755f73d7f070f9 to your computer and use it in GitHub Desktop.
zadachka.py
def reader(fname):
arrivals = {}
with open(fname, 'r') as fd:
N = int(fd.readline())
for i in range(N):
arr = fd.readline().split(' ')
t1 = int(arr[0])
dt = int(arr[1])
typ = arr[2].rstrip()
arrivals[t1] = (dt, typ)
return arrivals
def process(arrivals):
n_car_slots = 70
n_bus_slots = 30
keys = list(sorted(arrivals.keys()))
departures = {}
ngoodbus = 0
nleave = 0
for curt in keys:
# print(n_car_slots, n_bus_slots)
# process departures till this
for curt_d in list(sorted(departures.keys())):
# print('y', curt_d)
if curt_d <= curt:
for slottyp in departures[curt_d]:
if slottyp == 'A':
n_car_slots += 1
else:
n_bus_slots += 1
# delete departures
del departures[curt_d]
else:
break
print('x', n_car_slots, n_bus_slots)
curdt, curtyp = arrivals[curt]
if curtyp == 'A':
if n_car_slots > 0:
n_car_slots -= 1
slottyp = 'A'
elif n_bus_slots > 0:
n_bus_slots -= 1
slottyp = 'B'
else:
print('oops car')
nleave += 1
continue
if curtyp == 'B':
if n_bus_slots > 0:
n_bus_slots -= 1
ngoodbus += 1
slottyp = 'B'
else:
print('oops bus')
nleave += 1
continue
dept = curt + curdt
if dept not in departures:
departures[dept] = []
departures[dept].append(slottyp)
# print('aa', curt, departures)
return (ngoodbus, nleave)
if __name__ == '__main__':
arrivals = reader('/tmp/26.txt')
# print(arrivals)
print(process(arrivals))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment