Update 2015 solutions

This commit is contained in:
2024-10-20 15:19:25 -04:00
parent 87ab42743e
commit e73fa3bae7
16 changed files with 362 additions and 411 deletions

33
lib.py
View File

@@ -226,7 +226,7 @@ def count_trailing_repeats(lst):
class A_Star(object):
def __init__(self, starts, is_goal, h, d, neighbors):
"""
:param h: heuristic function
:param h: heuristic function (never overestimate)
:param d: cost from node to node function
:param neighbors: neighbors function
"""
@@ -297,6 +297,35 @@ def mod_inverse(a, m):
g, x, _ = egcd(a, m)
if g != 1:
raise Exception('Modular inverse does not exist')
raise Exception("Modular inverse does not exist")
else:
return x % m
class V:
def __init__(self, *args):
self.xs: tuple[int] = tuple(args)
def __eq__(self, other):
if isinstance(other, V):
return all(v1 == v2 for v1, v2 in zip(self.xs, other.xs))
elif hasattr(other, "__len__") and len(self.xs) == len(other):
return all(v1 == v2 for v1, v2 in zip(self.xs, other))
return False
def __getitem__(self, i: int):
return self.xs[i]
def __hash__(self):
return hash(self.xs)
def __add__(self, other):
if isinstance(other, V):
return V(*[v1 + v2 for v1, v2 in zip(self.xs, other.xs)])
assert hasattr(other, "__len__"), f"V.__add__({self}, {other}) missing `len`"
assert len(self.xs) == len(other), f"V.__add__({self}, {other}) `len` mismatch"
return V(*[v1 + v2 for v1, v2 in zip(self.xs, other)])
def __repr__(self):
s = ", ".join(map(str, self.xs))
return f"V({s})"