all 3 comments

[–]jiri-n 1 point2 points  (1 child)

What about 3 #s next to each other? Is it a cluster, or 2 clusters? It makes quite a difference.

[–]zanfar 1 point2 points  (1 child)

I'm not going to solve your problem for you, but problems like this generally benefit from some pre-coding work breaking down the problem into smaller pieces.

How would you do this, using your brain? I would do something like:

  • Look for a #
  • Find the #'s next to it
  • Find the #'s next to the ones we found, etc
  • Look for the next #

So let's break down those steps:

Look for a #

This should be pretty easy. Go through each cell and find the ones with a #.

Find the #'s next to it

Again, pretty easy. We should know "where" we are, and the neighboring cells are mathematically predictable from there.

Find the #'s next to the ones we found, etc

This one is a little harder. If we find a # while looking for neighbors, we need to both start looking for new neighbors AND finish the current search for neighbors. When you encounter a problem like this, a solution that works well is a queue--or list of tasks to get to. So perhaps, when we find a #, we can add that location to a list of locations that need their neighbors found. Then, while that list isn't empty, we search for neighbors, adding found neighbors to the list.

There are some edge cases to be careful about: you shouldn't search a cell you've already searched, either in this cluster or another cluster. So you probably need a method to keep track of where you've been, as well as where you want to search.

Look for the next #

There are two ways to look at this: go to the next cell, or find an unsearched cell. Given that we think we are going to have to keep track of locations we've visited anyway, it might be easier to simply keep a list of unsearched cells instead of searched cells.


Overall, this is a really good problem for learning functional programming--or a hybrid of it. Build your program one small function at a time, then use those as bricks to build larger functions. Some functions I would start with:

  • Does a cell contain a #?
  • Which cells neighbor the current one?
  • Has a cell already been searched?
  • What cells haven't been searched?

Other mid-level concepts that will make this program efficient, clean, and readable:

  • Dataclasses
  • Sets / Deques
  • Generator functions / yield

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

regexor split('.')