ILP Part 86 — Skyscrapers

This is the eightieth sixth part of the ILP series. For your convenience you can find other parts in the table of contents in Part 1 – Boolean algebra

Today we are going to solve a modification of Skyscrapers riddle.

In regular Skyscrapers we have a square board with some skyscrapers on it where all skyscrapers in a given line have different heights. We do have numbers along the board which indicate how many buildings are visible from a given side. In modified version numbers indicate how many blocks are visible.

Also, one number in the initial input is not needed to find the solution as there is only one. Our goal is to find which number it is.

Code:

This should be pretty straightforward. First, we make sure that skyscrapers have different heights. Next, we create a helper function to sum visible blocks. We compare given skyscraper with all before it in the line, check if it’s higher and then include its height or not.

Now, the output:

We can now comment out numbers one by one and find that top 7 is redundant.