https://www.codingame.com/training/expert/heart-of-the-city

Send your feedback or ask for help here!

Created by @SamSi,validated by @MadKnight,@anst and @SatineChatounette.

If you have any issues, feel free to ping them.

https://www.codingame.com/training/expert/heart-of-the-city

Send your feedback or ask for help here!

Created by @SamSi,validated by @MadKnight,@anst and @SatineChatounette.

If you have any issues, feel free to ping them.

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.

With some head scratching, google skills and Wikipedia-grade math.

So, doable in scripting languages, just have to use more elegant approach than bit-bashing.

Then again, there are other, very concise solutions in Python, that donâ€™t involve hard math whatsoever. or so it seems. maybe itâ€™s hidden behind the lines.

an update to my wailing from years ago. i kept returning to this puzzle once or twice a year and tried a bit of this and that. i found some mathematical shortcuts. which might seem â€śsimpleâ€ť once understood. but some of it really is not easy to understand. at least for me. i tried some programmatic optimization. some worked some not. most just marginal.

eventually i got a python solution which was able to pass the last test *some of the time*. i could have tried submitting that a couple of times until it passed but i did not want to do that. so i coded the same algorithm in c and it passed consistently.

this was some years ago. i forgot whether i still needed to optimize some on the c code. perhaps yes.

well. just wanted to share. maybe encourage some frustrated codingamer trying this puzzle.

I tried calculating slopes, and whenever we find a slope that is already visited: donâ€™t increase the count, else: increase the count by one.

I tried splitting the map to eighths, solving only one eighth, and outputting count*8+8, however, in both javascript and python I get only 57%, my results are corrects, but Iâ€™m a little slow and memory consuming I think? Any ideas?

Does your code do any memoisation?

Only the `visitedSlopes`

variable, other than that, IDK if it is even possible to memoize anything else