Skip to content

Commit

Permalink
Updates
Browse files Browse the repository at this point in the history
  • Loading branch information
bradleysigma committed Dec 26, 2020
1 parent c8ca834 commit 50241ab
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 79 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@

*.pyc
.mypy_cache
140 changes: 79 additions & 61 deletions aoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ def tock(units="s"):


def read(n):
f = open(f"input{n}.txt")
s = f.read()
f.close()
return s
with open(f"input{n}.txt") as f:
return f.read()


def strlist(n, t=list, d="\n"):
return t(read(n).strip("\n").split(d))


def intlist(n, t=list, d="\n"):
if d == "":
return t(map(int, read(n).strip("\n")))
return t(map(int, read(n).strip("\n").split(d)))


Expand All @@ -33,14 +33,13 @@ def __abs__(self):
def __add__(self, other):
if isinstance(other, (int, float, complex)):
return vec(i + other for i in self)
elif isinstance(other, (vec, tuple, list)):
if isinstance(other, (vec, tuple, list)):
if len(self) != len(other):
raise IndexError(f"iterable lengths mismatched ({len(self) - len(other)})")
return vec(i + j for i, j in zip(self, other))
elif isinstance(other, (map)):
if isinstance(other, (map, reversed, range)):
return self + vec(other)
else:
raise TypeError(f"cannot add {type(other).__name__} to vec")
raise TypeError(f"cannot add {type(other).__name__} to vec")

def __radd__(self, other):
return self + other
Expand All @@ -54,107 +53,131 @@ def __ceil__(self):
def __divmod__(self, other):
if isinstance(other, (int, float, complex)):
return vec(i // other for i in self), vec(i % other for i in self)
elif isinstance(other, (vec, tuple, list)):
if isinstance(other, (vec, tuple, list)):
if len(self) != len(other):
raise IndexError(f"iterable lengths mismatched ({len(self) - len(other)})")
return vec(i // j for i, j in zip(self, other)), vec(i % j for i, j in zip(self, other))
elif isinstance(other, (map)):
if isinstance(other, (map, reversed, range)):
return divmod(self, vec(other))
else:
raise TypeError(f"cannot divmod {type(other).__name__} with vec")
raise TypeError(f"cannot divmod {type(other).__name__} with vec")

def __rdivmod__(self, other):
if isinstance(other, (int, float, complex)):
return vec(other // i for i in self), vec(other % i for i in self)
elif isinstance(other, (vec, tuple, list)):
if isinstance(other, (vec, tuple, list)):
if len(self) != len(other):
raise IndexError(f"iterable lengths mismatched ({len(self) - len(other)})")
return vec(j // i for i, j in zip(self, other)), vec(j % i for i, j in zip(self, other))
elif isinstance(other, (map)):
if isinstance(other, (map, reversed, range)):
return divmod(vec(other), self)
else:
raise TypeError(f"cannot divmod {type(other).__name__} with vec")
raise TypeError(f"cannot divmod {type(other).__name__} with vec")

def __eq__(self, other):
if isinstance(other, vec):
if len(self) != len(other):
return False
return all(i == j for i, j in zip(self, other))
else:
return False
return False

def __ne__(self, other):
return not self == other

def __gt__(self, other):
if isinstance(other, (int, float, complex)):
return all(i > other for i in self)
if isinstance(other, (vec, tuple, list)):
if len(self) != len(other):
raise IndexError(f"iterable lengths mismatched ({len(self) - len(other)})")
return all(i > j for i, j in zip(self, other))
if isinstance(other, (map, reversed, range)):
return self > vec(other)
raise TypeError(f"cannot < {type(other).__name__} with vec")

def __ge__(self, other):
if isinstance(other, vec):
return self > other or self == other
return False

def __lt__(self, other):
if isinstance(other, (int, float, complex)):
return all(i < other for i in self)
if isinstance(other, (vec, tuple, list)):
if len(self) != len(other):
raise IndexError(f"iterable lengths mismatched ({len(self) - len(other)})")
return all(i < j for i, j in zip(self, other))
if isinstance(other, (map, reversed, range)):
return self < vec(other)
raise TypeError(f"cannot < {type(other).__name__} with vec")

def __le__(self, other):
if isinstance(other, vec):
return self < other or self == other
return False

def __floor__(self):
return vec(i.__floor__() for i in self)

def __floordiv__(self, other):
if isinstance(other, (int, float, complex)):
return vec(i // other for i in self)
elif isinstance(other, (vec, tuple, list)):
if isinstance(other, (vec, tuple, list)):
if len(self) != len(other):
raise IndexError(f"iterable lengths mismatched ({len(self) - len(other)})")
return vec(i // j for i, j in zip(self, other))
elif isinstance(other, (map)):
if isinstance(other, (map, reversed, range)):
return self // vec(other)
else:
raise TypeError(f"cannot multiply {type(other).__name__} to vec")
raise TypeError(f"cannot multiply {type(other).__name__} to vec")

def __rfloordiv__(self, other):
if isinstance(other, (int, float, complex)):
return vec(other // i for i in self)
elif isinstance(other, (vec, tuple, list)):
if isinstance(other, (vec, tuple, list)):
if len(self) != len(other):
raise IndexError(f"iterable lengths mismatched ({len(self) - len(other)})")
return vec(j // i for i, j in zip(self, other))
elif isinstance(other, (map)):
if isinstance(other, (map, reversed, range)):
return vec(other) // self
else:
raise TypeError(f"cannot multiply {type(other).__name__} to vec")
raise TypeError(f"cannot multiply {type(other).__name__} to vec")

def __hash__(self):
return hash(tuple(self))

def __mod__(self, other):
if isinstance(other, (int, float, complex)):
return vec(i % other for i in self)
elif isinstance(other, (vec, tuple, list)):
if isinstance(other, (vec, tuple, list)):
if len(self) != len(other):
raise IndexError(f"iterable lengths mismatched ({len(self) - len(other)})")
return vec(i % j for i, j in zip(self, other))
elif isinstance(other, (map)):
if isinstance(other, (map, reversed, range)):
return self % vec(other)
else:
raise TypeError(f"cannot divmod {type(other).__name__} with vec")
raise TypeError(f"cannot divmod {type(other).__name__} with vec")

def __rmod__(self, other):
if isinstance(other, (int, float, complex)):
return vec(other % i for i in self)
elif isinstance(other, (vec, tuple, list)):
if isinstance(other, (vec, tuple, list)):
if len(self) != len(other):
raise IndexError(f"iterable lengths mismatched ({len(self) - len(other)})")
return vec(j % i for i, j in zip(self, other))
elif isinstance(other, (map)):
if isinstance(other, (map, reversed, range)):
return vec(other) % self
else:
raise TypeError(f"cannot divmod {type(other).__name__} with vec")
raise TypeError(f"cannot divmod {type(other).__name__} with vec")

def __mul__(self, other):
if isinstance(other, (int, float, complex)):
return vec(i * other for i in self)
elif isinstance(other, (vec, tuple, list)):
if isinstance(other, (vec, tuple, list)):
if len(self) != len(other):
raise IndexError(f"iterable lengths mismatched ({len(self) - len(other)})")
return vec(i * j for i, j in zip(self, other))
elif isinstance(other, (map)):
if isinstance(other, (map, reversed, range)):
return self * vec(other)
else:
raise TypeError(f"cannot multiply {type(other).__name__} to vec")
raise TypeError(f"cannot multiply {type(other).__name__} to vec")

def __rmul__(self, other):
return self * other

def __ne__(self, other):
return not self == other

def __neg__(self):
return vec(-i for i in self)

Expand All @@ -164,68 +187,63 @@ def __pos__(self):
def __pow__(self, other):
if isinstance(other, (int, float, complex)):
return vec(i ** other for i in self)
elif isinstance(other, (vec, tuple, list)):
if isinstance(other, (vec, tuple, list)):
if len(self) != len(other):
raise IndexError(f"iterable lengths mismatched ({len(self) - len(other)})")
return vec(i ** j for i, j in zip(self, other))
elif isinstance(other, (map)):
if isinstance(other, (map, reversed, range)):
return self ** vec(other)
else:
raise TypeError(f"cannot divmod {type(other).__name__} with vec")
raise TypeError(f"cannot divmod {type(other).__name__} with vec")

def __rpow__(self, other):
if isinstance(other, (int, float, complex)):
return vec(other ** i for i in self)
elif isinstance(other, (vec, tuple, list)):
if isinstance(other, (vec, tuple, list)):
if len(self) != len(other):
raise IndexError(f"iterable lengths mismatched ({len(self) - len(other)})")
return vec(j ** i for i, j in zip(self, other))
elif isinstance(other, (map)):
if isinstance(other, (map, reversed, range)):
return vec(other) ** self
else:
raise TypeError(f"cannot divmod {type(other).__name__} with vec")
raise TypeError(f"cannot divmod {type(other).__name__} with vec")

def __round__(self):
return vec(map(round, self))

def __sub__(self, other):
if isinstance(other, (int, float, complex)):
return vec(i - other for i in self)
elif isinstance(other, (vec, tuple, list)):
if isinstance(other, (vec, tuple, list)):
if len(self) != len(other):
raise IndexError(f"iterable lengths mismatched ({len(self) - len(other)})")
return vec(i - j for i, j in zip(self, other))
elif isinstance(other, (map)):
if isinstance(other, (map, reversed, range)):
return self - vec(other)
else:
raise TypeError(f"cannot add {type(other).__name__} to vec")
raise TypeError(f"cannot add {type(other).__name__} to vec")

def __rsub__(self, other):
return -(self - other)

def __truediv__(self, other):
if isinstance(other, (int, float, complex)):
return vec(i / other for i in self)
elif isinstance(other, (vec, tuple, list)):
if isinstance(other, (vec, tuple, list)):
if len(self) != len(other):
raise IndexError(f"iterable lengths mismatched ({len(self) - len(other)})")
return vec(i / j for i, j in zip(self, other))
elif isinstance(other, (map)):
if isinstance(other, (map, reversed, range)):
return self / vec(other)
else:
raise TypeError(f"cannot multiply {type(other).__name__} to vec")
raise TypeError(f"cannot multiply {type(other).__name__} to vec")

def __rtruediv__(self, other):
if isinstance(other, (int, float, complex)):
return vec(other / i for i in self)
elif isinstance(other, (vec, tuple, list)):
if isinstance(other, (vec, tuple, list)):
if len(self) != len(other):
raise IndexError(f"iterable lengths mismatched ({len(self) - len(other)})")
return vec(j / i for i, j in zip(self, other))
elif isinstance(other, (map)):
if isinstance(other, (map, reversed, range)):
return vec(other) / self
else:
raise TypeError(f"cannot multiply {type(other).__name__} to vec")
raise TypeError(f"cannot multiply {type(other).__name__} to vec")

def __trunc__(self):
return vec(i.__trunc__() for i in self)
Expand Down
26 changes: 13 additions & 13 deletions day10.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@

aoc.tock("ms")

##import aoc
##data = {0} | aoc.intlist(10, set)
##
##d = {i: 0 for i in range(6)}
##p = max(data)
##while p > 0:
## n = max({p - i for i in range(6)} - data)
## d[p-n] += 1
## p = n - 2
##print((lambda x: x * (len(data) - x))(sum(j + 1 in data for j in data)),
## 2**d[3] * 4**d[4] * 7**d[5])
##
##aoc.tock("ms")
# import aoc
# data = {0} | aoc.intlist(10, set)
#
# d = {i: 0 for i in range(6)}
# p = max(data)
# while p > 0:
# n = max({p - i for i in range(6)} - data)
# d[p-n] += 1
# p = n - 2
# print((lambda x: x * (len(data) - x))(sum(j + 1 in data for j in data)),
# 2**d[3] * 4**d[4] * 7**d[5])
#
# aoc.tock("ms")
2 changes: 1 addition & 1 deletion day13.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import aoc

# def gcd(a, b): return abs(a + b) if a * b == 0 else gcd(b, a % b)
# gcd = lambda a, b: abs(a + b) if a * b == 0 else gcd(b, a % b)
data = aoc.strlist(13)
t = int(data[0])
b = [int(i) for i in data[1].replace("x", "0").split(",")]
Expand Down
4 changes: 2 additions & 2 deletions day23.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
c[i][V] = j
r[j] = c[i]

p = c[V]
p = c[0]
for i in range(100):
m = p[N], p[N][N], p[N][N][N]
p[N] = p[N][N][N][N]
Expand Down Expand Up @@ -46,7 +46,7 @@
c[i][V] = j
r[j] = c[i]

p = c[V]
p = c[0]
for i in range(10000000):
m = p[N], p[N][N], p[N][N][N]
p[N] = p[N][N][N][N]
Expand Down
3 changes: 1 addition & 2 deletions day8.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@
i, j = alter[point]
point += j if i == "jmp" else 1
accy += j if i == "acc" else 0
if point >= len(alter):
break
if point >= len(alter): break
else:
continue
break
Expand Down

0 comments on commit 50241ab

Please sign in to comment.