Compare commits

..

No commits in common. "38923eb7a7c098890d454f7095d0f3569dd1e168" and "8c27fca7697d08a05da04e36bc80fbc7022461da" have entirely different histories.

5 changed files with 70 additions and 85 deletions

View File

@ -1,7 +1,5 @@
#!/usr/bin/env python3
from functools import cache
up = lambda v: (v[0], v[1] + 1)
down = lambda v: (v[0], v[1] - 1)
left = lambda v: (v[0] - 1, v[1])
@ -13,12 +11,20 @@ moves = {'U' : up,
'R' : right
}
def memoize(f):
cache = dict()
def memf(path):
if path not in cache:
cache[path] = f(path)
return cache[path]
return memf
def iterate(f, x, N):
for i in range(N):
yield x
x = f(x)
@cache
@memoize
def lines(path):
pos = (0, 0)
r = []

View File

@ -2,15 +2,14 @@
from intcode import preproc
from itertools import permutations
from lib import last
from functools import cache
from lib import memoize, last
stack_size = 5
fst_amp_input = 0
def partI(amp):
run_amp = cache(lambda *args : last(amp(*args)))
run_amp = memoize(lambda *args : last(amp(*args)))
phase_range = range(stack_size)
best = 0

View File

@ -1,13 +1,14 @@
#!/usr/bin/env python3
from lib import Color
ZERO, ONE, TWO = "012"
BLACK = ZERO
WHITE = ONE
BLACK = 0
WHITE = 1
color = {WHITE : u"\u2B1C",
BLACK : u"\u2B1B"
}
encode = {WHITE : Color.WHITE, BLACK : Color.BLACK}
class Image:
def __init__(self, data, w, h):
@ -30,7 +31,7 @@ class Image:
return '\n'.join(image[i:i+w] for i in range(0, len(image), w))
def __str__(self):
return repr(self).replace(BLACK, encode[BLACK]).replace(WHITE, encode[WHITE])
return repr(self).replace(ZERO, color[BLACK]).replace(ONE, color[WHITE])

View File

@ -1,33 +1,64 @@
#!/usr/bin/env python3
from math import gcd
from fractions import Fraction
from itertools import product
from functools import total_ordering
from collections import defaultdict
from lib import vector
ASTEROID = '#'
def zrange(x, y, step):
if x == y == step == 0:
def yzero():
while True:
yield 0
return yzero()
return range(x, y, step)
def angle(v):
x, y = v
if x == 0:
return (2 * int(y > 0), 0)
halve = 1 if x > 0 else 3
return (halve, Fraction(y, x))
if v.x == 0:
return (2 * int(v.y > 0), 0)
halve = 1 if v.x > 0 else 3
return (halve, Fraction(v.y, v.x))
def rebase(base, vectors):
return (v - base for v in vectors if v != base)
class Map(list):
@total_ordering
class Vector:
def __init__(self, x, y):
self.x = x
self.y = y
def __add__(self, other):
return Vector(self.x + other.x, self.y + other.y)
def __sub__(self, other):
return self + Vector(-other.x, -other.y)
def __eq__(self, other):
return self.x == other.x and self.y == other.y
def __lt__(self, other):
return (self.x**2 + self.y**2) < (other.x**2 + other.y**2)
def __str__(self):
return str((self.x, self.y))
def __repr__(self):
return str(self)
class Map:
def __init__(self, map):
self.map = map
def __getitem__(self, v):
return self.map[v.y][v.x]
def __str__(self):
return '\n'.join(self.map)
def is_asteroid(self, v):
x, y = v
return self[y][x] == ASTEROID
return self[v] == ASTEROID
def vectors(self):
X, Y = len(self[0]), len(self)
return (vector(v) for v in product(range(X), range(Y)))
X, Y = len(self.map[0]), len(self.map)
return (Vector(x, y) for x, y in product(range(X), range(Y)))
def asteroid_vectors(self):
return filter(lambda v: self.is_asteroid(v), self.vectors())
@ -63,8 +94,8 @@ def partI(packed):
def partII(packed):
asteroid_map, asteroid, _ = packed
x, y = asteroid_map.vaporize_seq(asteroid)[199]
return 100*x + y
v = asteroid_map.vaporize_seq(asteroid)[199]
return 100*v.x + v.y
from main import Tests

68
lib.py
View File

@ -2,23 +2,11 @@
import itertools
from collections import deque
from enum import Enum
# Random definitons ---------------------------------------
class Color:
WHITE = u"\u2B1C"
BLACK = u"\u2B1B"
# Iterator recipes ----------------------------------------
def last(iterator):
"""Exhausts an iterator and returns its last element."""
return deque(iterator, maxlen=1)[0]
def exhaust(iterator):
"""Exhausts an iterator."""
deque(iterator, maxlen=0)
# Custom collections --------------------------------------
class defaultlist(list):
"""Default list class. Allows writing and reading out of bounds."""
def __init__(self, lst, val_factory):
@ -36,56 +24,16 @@ class defaultlist(list):
return super().__getitem__(i)
# Basic linear algebra ------------------------------------
class vector(tuple):
"""Vector class. Convert iterator to vector."""
def __neg__(self):
return vector(-x for x in self)
def memoize(f):
cache = dict()
def memf(*args):
key = tuple(args)
if key not in cache:
cache[key] = f(*args)
return cache[key]
return memf
def __add__(self, other):
o = tuple(other)
if len(self) != len(o):
raise ValueError("vectors have different lengths")
return vector(x + y for x, y in zip(self, o))
def __sub__(self, other):
return self + (- vector(other))
def __mul__(self, other):
if type(other) == int:
return vector(other * x for x in self)
o = tuple(other)
if len(o) != len(self):
raise ValueError("vectors have different lengths")
return sum(x * y for x, y in zip(self, o))
def __truediv__(self, d):
return vector(x/d for x in self)
__radd__ = __add__
__rmul__ = __mul__
def __rsub__(self, other):
return other + (-self)
def rotate90pos(v):
"""Rotates vector v by 90 degrees in positive direction."""
x, y = v
return vector((-y, x))
def rotate90neg(v):
"""Rotates vector v by 90 degrees in negative direction."""
x, y = v
return vector((x, -y))
class Cardinals(Enum):
UP = vector((0, 1))
DOWN = vector((0, -1))
LEFT = vector((-1, 0))
RIGHT = vector((1, 0))
ORIGIN = vector((0, 0))
# Graphs ---------------------------------------------------
class Graph(dict):
nodes = dict.keys