icfp25/harald/path.py

49 lines
1.1 KiB
Python

import functools
@functools.total_ordering
class Path:
def __init__(self, path=""):
if isinstance(path, list):
path = "".join(str(p) for p in path)
self.path = path
def __repr__(self):
return "." + self.path if self.path else "."
def __bool__(self):
return bool(self.path)
def __eq__(self, other):
return self.path == other.path
def __lt__(self, other):
return (len(self.path), self.path) < (len(other.path), other.path)
def __hash__(self):
return self.path.__hash__()
def __format__(self, spec):
return str(self).__format__(spec)
def __add__(self, other):
return Path(self.path + other.path)
def __len__(self):
return len(self.path)
def __getitem__(self, idx):
return Path(self.path.__getitem__(idx))
def last(self):
return Path(self.path[:-1]), int(self.path[-1])
def shorten(self, path, pmerge):
if self.path.startswith(pmerge.path):
return Path(path.path + self.path[len(pmerge.path) :])
else:
return self
DOORS = [Path(str(i)) for i in range(6)]