Community Puzzle : Heart of the City

Hi everybody,

I am trying to code a solution for Heart of the City, but it seems I am wrong with test case 3:
I count 88 and the answer is 80

Can you help me to see my fault ?
… 1…2…3…4…5… 6…7…8…9…10.11
1 …
2 …
3 …
4 …
5 …
6… .C.
7 …
8 …
9…
10
11

I count 11x11 - 1 - 8x4 = 88

Here are the hidden buildings that are missing (), with the buildings hiding them ():

… 1…2…3…4…5… 6…7…8…9…10.11
1…
2…
3…
4…
5…
6… .C.
7…
8…
9…
10
11

6 Likes

And that’s when you realize the problem is a little bit harder that what you first imagined

1 Like

I found it was a very good challenge but if you do not like mathematics, it is a bit hard to solve (especially for the last case).

I’m stuck on this one.
I can’t find a simple formula and my python attempt with loops and common divisors is not efficient enough for testcases 5-7.
I’m also thinking about integer factorization and primes.
Can somebody give my a hint?

1 Like

You are in the good way if you think about arithmetic. Try to have a look at the dividers of a number…

1 Like

I had an interesting journey with this puzzle.

First it was “piece of cake”, and then shortly after the “aha, not as easy as it seems …”

Then some thinking which I liked a lot, and the hidden structure was revealed. This allowed me to write a naive solution (I guess similar to @RoDr1g0 ) which solved nicely cases 1…4.

Then started the deep dive, and in this particular puzzle, the deep dive involves college-level arithmetic (as hinted above), number theory, some code optimizations etc.

I found it somewhat interesting to go to this level of in-depth math, and did some google searches, which allowed me to find an optimized solution for cases 5,6.

Mind you, this requires very good understanding of math. Not sure I saw such things in my BA (and I did learn math etc…)

The “ultimate” required too much deep-dive, so I left it alone.

I’m new to this site, and I like it much.
I would prefer in such challenges that the “required knowledge” is listed up-front. Had I known that this requires such a level of math I may have chosen to steer elsewhere (“coders strike back”, gold league right now. Yami … )

Edit - I jumped the gun too soon. Check @irmo322 comment below. Indeed there is a (very) elegant solution to this puzzle.

2 Likes

I managed to work out the mathematics behind the problem description all by myself. Now, so I thought, I just need to implement the mathematics in code.

My naive implementation was not fast enough for the bigger numbers. That was expected. But my code optimizations were still not fast enough.

I searched around and found some stackexchange codegolf challenge which described this exact problem. Sadly even the presumably highly optimized code from the stackexchange codegolf was not fast enough for the bigger numbers. So I gave up on this puzzle.

I had fun working out the mathematics. I had fun implementing the mathematics. I had fun trying to optimize my naive implementations. I gave up after realizing that I probably need advanced mathematics to solve the big numbers.

In other words: the big numbers are a tad too big. Make them big enough that naive implementations fail. But not so big that it requires advanced mathematics. Or mark the puzzles as requiring advanced mathematics.

[yep, a moderator removed the link]

It probably spoils too much of the puzzle so mods will probably remove the link.

I tried the python version.

1 Like

Hi there!

Indeed it’s a pretty interesting puzzle.

For the last test, I have two hints for you:
1- You don’t necessary need to dive into hard mathematics. There is a solution which can be explained to a (smart) 12 years old teenager.
1 - Use a fast language as C. For example, my solution wasn’t fast enough if written in lua (a script langage) but passes the last test when written in C.

Have fun

1 Like

My hat’s off to you , @irmo322. Beautiful solution.

Not sure about the 12 years old, but indeed a simple and elegant approach.
Thank you for providing the inspiration.

Your point about the difference between scripting and C is something I’ve also thought about. I wasn’t sure that the site can factor the difference between the execution speed of different languages… Following your experience it seems that there are indeed gaps, and compiled languages are probably faster. Interesting.

Thank you for the nice comment!

n=41
I don’t think that it helps, when i do this.

1 Like

Oh my god, this image is so helpful! I tried creating this in ASCII, but it gave me a headache to try to read it. You can actually analyze the factors in this! Notice how in row 6, each square is red if the corresponding square in row 3 or in row 2 is red? And it just continues! Thank you!

I have it solved in native Python, all testcases.