Green Valleys

note: the button ‘forum’ of the medium puzzle ‘green valleys’ does not bring to its specific forum page. i created this one.

i got 100% on tests, but last validator (alps and valleys) failed. i was ready to ask for some help, but i remembered to do some customized tests, and could find a bug in my code just with that quite tiny test (honestly, I found it with a bit of random):

1 3 1
3 1 2
2 3 1

so, in short, when at least one of your validators fails and no none of your tests, create your own customized tests. even small tests should help to debug.

I am quite sure already said in other posts, but this is the second time I face this pattern.


Hi @otpv,
Good advice :slight_smile:
So from your example I’d say the answer is supposed to be 1?
Could you explain what was your bug exactly?
Thanks! :slight_smile:

hi @Eldoir, thanks for feedback.

in short, the bug is a “side” case.
my buggy code successfully ran the test above, but debug logs showed 2 valleys (1 ; 1 2 1 2 1) instead of 3 ( 1 ; 1 2 1 1 ; 2).
it considered the bottom-left 2 has part of the largets valley in the right side.

in details, i decided to represent the grid with a “1D” array, instead a 2D array.
i wanted to avoid nested loop and pairs x,y, i.e. and use a queue with just simple elements.
so the grid becomes a list: (1 3 1 3 1 2 2 3 1).
the downside to use a list in this case is to take care of the elements being on the sides of the grid when you search for the neighbors of those elements.
i just forgot to use the modulo when searching a right-hand-side neighbor, which obvisouly does not exist for an element in the most-right side of the grid.

so, the origin of the issue is more my laziness and/or my arrogance to not read carefully a piece of code that I have already written several times for other trainings. snif!

i hope tho have been enough clear, without revealing too much the solution.

and yes, the answer is 1 (for the right-hand-side valley 1 2 1 1) . :slight_smile:


Great puzzle! Well done all around.

Great test case! My hint for Python users: Negative indexes aren’t out of bounds, they wrap :wink:

Good luck everyone, fun puzzle.

Fun little puzzle that deals with notions of detecting a group in a matrix, a common exercise in image processing.

I am getting 412 instead of 420 in the alps and valleys test. I am not sure why 412is not the right answer. It looks like the biggest valley to me from the data set
Any advice?

When I print out the map for alps test case, the valley with value 412 only has three other nodes connected to it (499, 706, 707). There are bigger valleys to be found. What other values are in the same valley as that 412?

Hi! Isn’t there two valleys in test case One valley?

1 Like

Beware, if two valleys have got the same size, use the lowest from both of them.

1 Like

How are there no valleys on 04 ? isn’t it almost one big valley, since 7 tiles are under the required height, or have I misunderstood something?

First line contains required height:
143 241 121
122 134 145
187 231 210
Do you see the same input? Which one is under 120?

1 Like

The required height for no valleys is 210 for me? The input is the same for the rest

I do not really understan. Is it the “bulit in” test case or did you copy (and modify) as a custom test case? Test cases must be the same for every users.

I came here with the same question.
The description states “Up to height H, the place is green, every tile under (or equal) this height is part of a valley, and every tile above it is white in snow, and part of the mountains.”

However, in order for their to only be one valley in the “One Valley” test case, it appears that we would have to consider a height exactly equal to H as being snow rather than a valley, which conflicts with the statement I’ve quoted above.

I am also getting 412.

My valley grouping for Alps and valleys is:

(lowest point: 412, valley size: 48) [
  519, 654, 474, 627, 415, 789, 855, 666,
  420, 819, 656, 657, 851, 632, 664, 490,
  445, 567, 569, 853, 753, 806, 718, 694,
  727, 568, 620, 535, 412, 809, 724, 499,
  569, 552, 706, 841, 716, 707, 737, 542,
  433, 600, 473, 732, 529, 533, 653, 476
(lowest point: 415, valley size: 47) [
  651, 469, 709, 654, 474, 627, 415, 457,
  789, 855, 666, 420, 819, 656, 657, 851,
  632, 664, 569, 853, 753, 806, 718, 694,
  727, 568, 620, 535, 724, 841, 716, 707,
  737, 542, 433, 528, 538, 482, 643, 600,
  473, 732, 529, 533, 653, 476, 766

Your “valleys” do not connect either horizontally or vertically to each other.

I have all the tests green, except for the three valleys.

With a snow point at 100, so we see 3 valleys:
( 91; 51, 73; 81, 87, 79)

And the test expects 51, but this is the deepest point overall, not the one from the largest valley.
Am I missing something ?

100 is also part of a valley, because the puzzle statement states:

every tile under (or equal) this height is part of a valley

Thanks! indeed I read it but my comparator was wrong :man_facepalming: it works fine now

1 Like