Skip to content

Instantly share code, notes, and snippets.

@brunogamacatao
Created December 15, 2023 22:04
Show Gist options
  • Save brunogamacatao/1422b15a6620a3f3f0165b827462fe9a to your computer and use it in GitHub Desktop.
Save brunogamacatao/1422b15a6620a3f3f0165b827462fe9a to your computer and use it in GitHub Desktop.
Advent of code, day 6, O(1) solution
import sys
import math
def get_integer_roots(a, b, c):
delta = b * b - 4 * a * c
x1 = (-b + math.sqrt(delta)) / 2 * a
x2 = (-b - math.sqrt(delta)) / 2 * a
return (x1, x2)
def get_number_of_better_solutions(total_time, max_distance_so_far):
roots = get_integer_roots(-1, total_time, -max_distance_so_far)
start = math.ceil(roots[0])
finish = math.floor(roots[1])
if float(start) == roots[0]: start += 1
if float(finish) != roots[1]: finish += 1
return finish - start
if __name__ == '__main__':
times = list(map(int, sys.stdin.readline().strip().split(':')[1].split()))
distances = list(map(int, sys.stdin.readline().strip().split(':')[1].split()))
prod = 1
for t, d in zip(times, distances):
prod *= get_number_of_better_solutions(t, d)
print(prod)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment