Advent of Code day 21: it’s a marathon, not a sprint
….Aaaand here we are, in the last stretch of the #AdventOfCode 2019 puzzle-a-ganza! Like every year, the evil genius that is Eric Wastl has ramped up the puzzles gradually, and we’re now at those puzzles that you look at and go: “oof, another one? I just finished the previous day!”
And not just that, Advent of Code makes me act in a way befitting the Christmas theme. Trying to solve these puzzles has made me raise my eyes to heaven and pray for inspiration more than once – even though I’m an atheist :-).
It shows in my visualization code. Whereas my first visualizers were nifty things with graphics and curses, now all I just want to plunk some quick print statements in there to see what’s going on. So was born the
PrintVisualiser, the little brother of
It turned out to have a nice side benefit. I’m trying to stay away from “cheating” with numpy, and it turns out that for a sparse matrix, or even a not-so-sparse matrix, just using a dictionary indexed by x,y tuple works perfectly fine!
class PrintVisualizer(object): """Simplest visualizer, simply generates a bunch of print() statements.""" def __init__(self, area: (Point, Point) = None, default_char=EMPTY): self.points = dict() self.default_char = default_char self.area = area def plot(self, xy, c): self.points[xy] = c def print(self, padding=0): if self.area: min_xy, max_xy = self.area else: min_xy, max_xy = min_max(self.points, padding) for y in range(min_xy.y, max_xy.y + 1): line = list() for x in range(min_xy.x, max_xy.x + 1): if (x, y) in self.points: line.append(self.points[(x, y)]) else: line.append(self.default_char) print(''.join(line))
So, off to my last few days: let’s see if I can finish more than the 21 days I succeeded in last year!
Look at our consultancy services, training offers and careers below or contact us at email@example.com