From 50241ab5e23941bae26174fc96e3df75d84817d4 Mon Sep 17 00:00:00 2001 From: bradleysigma <42644678+bradleysigma@users.noreply.github.com> Date: Sat, 26 Dec 2020 22:41:11 +1000 Subject: [PATCH] Updates --- .gitignore | 1 + aoc.py | 140 ++++++++++++++++++++++++++++++----------------------- day10.py | 26 +++++----- day13.py | 2 +- day23.py | 4 +- day8.py | 3 +- 6 files changed, 97 insertions(+), 79 deletions(-) diff --git a/.gitignore b/.gitignore index fd20fdd..b02eef0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ *.pyc +.mypy_cache \ No newline at end of file diff --git a/aoc.py b/aoc.py index 5e49a3a..aaa08b4 100644 --- a/aoc.py +++ b/aoc.py @@ -8,10 +8,8 @@ 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"): @@ -19,6 +17,8 @@ def strlist(n, t=list, d="\n"): 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))) @@ -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 @@ -54,34 +53,66 @@ 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) @@ -89,26 +120,24 @@ def __floor__(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)) @@ -116,45 +145,39 @@ def __hash__(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) @@ -164,26 +187,24 @@ 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)) @@ -191,14 +212,13 @@ def __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) @@ -206,26 +226,24 @@ def __rsub__(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) diff --git a/day10.py b/day10.py index 06fb568..3187e5c 100644 --- a/day10.py +++ b/day10.py @@ -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") diff --git a/day13.py b/day13.py index edeced2..fc00c9f 100644 --- a/day13.py +++ b/day13.py @@ -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(",")] diff --git a/day23.py b/day23.py index dee6d88..6c5a32f 100644 --- a/day23.py +++ b/day23.py @@ -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] @@ -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] diff --git a/day8.py b/day8.py index d6ba8be..66c297a 100644 --- a/day8.py +++ b/day8.py @@ -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