Skip to content

Instantly share code, notes, and snippets.

@vjeranc
Created December 6, 2023 11:18
Show Gist options
  • Save vjeranc/ca7cc9f348fc33f9695338bbf28f5a46 to your computer and use it in GitHub Desktop.
Save vjeranc/ca7cc9f348fc33f9695338bbf28f5a46 to your computer and use it in GitHub Desktop.
# ts, ds = [[int(i) for i in l.split()[1:]] for l in open(0).read().split('\n') if l] # part 1
ts, ds = [[int(i) for i in [''.join(l.split()[1:])]] for l in open(0).read().split('\n') if l] # part 2
def binary_search(f, l, r):
while l < r:
m = (l+r)//2
if f(m):
l = m+1
else:
r = m
return l
ans = 1
for T, D in zip(ts, ds):
f = lambda t: t*(T-t) - D
argmax = binary_search(lambda t: f(t) < f(t+1), 0, T) # no calculus assumed
sl = binary_search(lambda t: f(t) <= 0, 0, argmax)
sr = binary_search(lambda t: f(t) > 0, argmax, T)
ans *= sr-sl
print(ans)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment