|
| 1 | +--- |
| 2 | +layout: posts |
| 3 | +title: Using a Python Dictionary to Represent a 2D Grid |
| 4 | +tags: python TIL |
| 5 | +--- |
| 6 | + |
| 7 | +It's <a href="https://adventofcode.com/">Advent of Code</a> time! Problems featuring a two dimensional grid are not uncommon, so developing a strategy to deal with this is useful. |
| 8 | + |
| 9 | +In the past I would use a pair of nested arrays to represent this grid. This was a bit messy, you would have to reference grid coordinates "backwards" as `Y, X`, and it would require all sorts of additional out-of-bounds checking. |
| 10 | + |
| 11 | +An improvement is to use a dictionary where the key is a tuple of "X,Y" coordinates. |
| 12 | + |
| 13 | +### Sample Input |
| 14 | +Just a simple 3x3 grid of sequential whole numbers. I'm not going to actually bother to convert these to integers for our example, because the value actually doesn't matter for this strategy. |
| 15 | +{% highlight python %} |
| 16 | +>>> print(input) |
| 17 | +012 |
| 18 | +345 |
| 19 | +678 |
| 20 | +{% endhighlight %} |
| 21 | + |
| 22 | +### Populating the Dictionary |
| 23 | +{% highlight python %} |
| 24 | +grid = {} |
| 25 | +for y, line in enumerate(input.split('\n')): |
| 26 | + for x, val in enumerate(line): |
| 27 | + grid[(x, y)] = val |
| 28 | +{% endhighlight %} |
| 29 | + |
| 30 | +### Accessing the Data |
| 31 | +Use a tuple for the dictionary key. `x=1, y=2` |
| 32 | +{% highlight python %} |
| 33 | +>>> grid[(1, 2)] |
| 34 | +'7' |
| 35 | +{% endhighlight %} |
| 36 | + |
| 37 | +### Searching the Grid |
| 38 | +Dictionary comprehension can be used to search the grid for keys that hold the desired value. |
| 39 | +{% highlight python %} |
| 40 | +>>> [key for key, val in grid.items() if val == '7'] |
| 41 | +[(1, 2)] |
| 42 | +{% endhighlight %} |
| 43 | + |
| 44 | +### Finding Neighbors |
| 45 | +In the Advent of Code problems, finding the contents of neighboring locations is also very common. This function will return a list of all neighboring locations, for a given position. |
| 46 | +{% highlight python %} |
| 47 | +def get_neighbors(grid: dict, pos: tuple) -> list[tuple]: |
| 48 | + x0, y0 = pos |
| 49 | + candidates = [ |
| 50 | + (x0 - 1, y0 + 1),(x0, y0 - 1),(x0 + 1, y0 - 1), |
| 51 | + (x0 - 1, y0), (x0 + 1, y0), |
| 52 | + (x0 - 1, y0 - 1),(x0, y0 + 1),(x0 + 1, y0 + 1), |
| 53 | + ] |
| 54 | + return [p for p in candidates if p in grid] |
| 55 | +{% endhighlight %} |
| 56 | + |
| 57 | +We can see that the `1,2` coordinate location (value: `7`) has five valid neighboring locations: |
| 58 | +{% highlight python %} |
| 59 | +>>> get_neighbors(grid, (1,2)) |
| 60 | +[(1, 1), (2, 1), (0, 2), (2, 2), (0, 1)] |
| 61 | +{% endhighlight %} |
| 62 | + |
| 63 | + |
| 64 | + |
| 65 | + |
| 66 | +#### Future Learning - Complex Numbers |
| 67 | +I've seen people discuss using <a href="https://en.wikipedia.org/wiki/Complex_number">complex numbers</a> to represent the grids. I honestly haven't wrapped my head around the math for that, but I would like to learn. |
0 commit comments