Skip to content

Instantly share code, notes, and snippets.

@haakonvt
Last active December 6, 2023 17:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save haakonvt/fd7c56fbaeeaa4fa5e508dbaac479091 to your computer and use it in GitHub Desktop.
Save haakonvt/fd7c56fbaeeaa4fa5e508dbaac479091 to your computer and use it in GitHub Desktop.
AOC 2023, day 6
import re
from math import floor, ceil, prod
from more_itertools import chunked
from sympy import Symbol, S, solve_univariate_inequality
t = Symbol("t", domain=S.Naturals)
def to_range(conditions):
ineq1, ineq2 = conditions.args
if ineq1.lhs == t: # rule: lhs < rhs
return range(floor(ineq2.lhs) + 1, ceil(ineq1.rhs))
return range(floor(ineq1.lhs) + 1, ceil(ineq2.rhs))
def compute(n):
return prod(
len(to_range(solve_univariate_inequality(-t**2 + t*time > dist, t)))
for time, dist in zip(*chunked(n, len(n)//2))
)
part_one = compute(list(map(int, re.findall("\d+", inp))))
part_two = compute(list(map(int, ("".join(re.findall("\d+", s)) for s in inp.splitlines()))))
print(part_one, part_two)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment