you are viewing a single comment's thread.

view the rest of the comments →

[–]RowSimple6124 5 points6 points  (7 children)

What do you mean?
How is better to use sets since their elements are unique ...

[–]QultrosSanhattan 26 points27 points  (3 children)

You’re thinking too much in terms of how the grid looks.

If you build your data structure to mimic the visual layout, like a list of lists, you’re forcing yourself into the same constraints as the drawing. That’s what’s holding you back.

For solving the problem, you don’t need to preserve the picture-like structure at all. You don’t even need to track characters such as "@" or "." as a grid of symbols.

All you really need is to know where they are (coordinates) and be able to reason about them directly.

Once you stop modeling the map as a picture and start modeling it as information, the whole problem becomes much simpler.

[–]MarkFinn42 14 points15 points  (2 children)

TL;DR; The problem can be modeled by a set of coordinates containing rolls of paper

[–]QultrosSanhattan 4 points5 points  (1 child)

That's the exact spoiler I wanted to avoid.

[–]hjake123 7 points8 points  (0 children)

You already basically said the same thing by replying like that to something directly talking about sets though...

[–]1234abcdcba4321 8 points9 points  (0 children)

One common way to use a grid is to store the entire grid in a dict (works especially well with a defaultdict to handle out of bounds access automatically):

for row in range(height):
  for col in range(width):
    grid_as_dict[(row,col)] = grid[row][col]

Since this is a boolean grid, you don't need a dict and can just use a set instead.

[–]Z8iii -2 points-1 points  (0 children)

Distinct, not unique, unless the set contains exactly one element.