Note: If you’re attempting to read this as a Fediverse post, you might find it rather confusing. I recommend using a web browser to read it properly, following this link.
The challenge
We have a quadratic field filled with the symbols . and @. Our task is to find the number of @ symbols with less than 4 other @ symbols in adjacent positions, up, down, left, right and diagonally.
The idea
I personally find it easier to deal with numbers than with string comparisons, so I replace . and @ with 0 and 1. I also despise checking if I am at a border or not, so I put a frame of zeroes around the field and only look at the inside.
So, we only need a function to count the numbers 1 around a field with the one and to check if this number is less than 4, which leads us to this code:

The updated challenge
We now want to remove all the fields with few neighbours, those we found before. Once we are ready, we repeat the process until only fields with at least 4 neighbours are left. How many 1 could we remove in total?
The updated idea
We do the same as before, but instead of just counting ones with few neighbours, we’re storing their positions in a list. After each round we set all these positions to 0.


Leave a Reply