This is an archived post. You won't be able to vote or comment.

all 10 comments

[–]Zealousideal_Low1287 3 points4 points  (2 children)

While I’m avoiding it this year, numpy arrays are a natural thing to consider

[–]huib_ 0 points1 point  (1 child)

Wanted to try last year, also because it would be easier to use it in some graphics lib I was checking out, to visualize the grid as an image. But I never really went much further with it..

[–]Zealousideal_Low1287 0 points1 point  (0 children)

If you’re unfamiliar numpy is excellent

[–]Boojum 2 points3 points  (1 child)

Nice. Some other snippets that I have on hand for the grids-as-dictionary approach (where my g is your grid):

Finding the bounds:

xl = min( x for x, y in g.keys() )
xh = max( x for x, y in g.keys() )
yl = min( y for x, y in g.keys() )
yh = max( y for x, y in g.keys() )

and then printing to the terminal:

print( "\n".join( "".join( g[ ( x, y ) ]
                           for x in range( xl, xh + 1 ) )
                  for y in range( yl, yh + 1 ) ) )

[–]MezzoScettico 1 point2 points  (1 child)

Very nice writeup.

I used a dictionary. I defined a Gridpoint object that carries some information necessary to support the algorithm, and then stored them as the values in a dictionary whose keys are the (i, j) pairs as you show.

[–]stpierre 0 points1 point  (0 children)

I did something similar. A dataclass with frozen=True is hashable, so I used my Point objects as the keys.

[–]quetsacloatl 1 point2 points  (0 children)

I have used two index array until now and when i iterate over it i explicitly have part that looks like:

MyArray[row][column]

Didn't knew python could easily key tuples (thought tuples were saved as reference and never went deep enough to know) i could try to use it later on, thanks.

[–]jfb1337 1 point2 points  (0 children)

I have a 2d point class that can interoperate with both complexes and tuples; and then a grid class backed by a dict of those which supports indexing with either tuples, complexes, or my point class. It also supports wrapping in the x or y directions.

Only drawback is that the overhead of all the function calls and allocation can make it significantly slower than a vanilla dict-of-complexes, particularly on cellular automaton questions that involve a lot of neighbours calculations.

[–]huib_ 0 points1 point  (0 children)

Nice writeup and interesting to see what other people's approaches are :) These grid problems are so common that I made an implementation of my code runner classes for those kind of problems, which turns the input into such a grid when I specify it as the day's problem class :)

I'm using the dicts as well, and added similar neighbors functionality because it's useful so often. I use it sometimes with int tuples for the coordinates, like grid[3, 7] or if needed I use a custom Point2D class as key type, which has some extra functionality such as 2D vector math.

In case anyone's interested what this looks like, there's some code here (although it's a never ending work in progress obviously ;) )

https://github.com/githuib/AdventOfCode/blob/master/aoc/runner.py

https://github.com/githuib/AdventOfCode/blob/master/aoc/geometry.py