2022 Day 8: Treetop Tree House

This one was fun. I’m kinda tired of fighting with containers, so I did a quick first pass on the input file to determine the dimensions, then created an instance of a generic package that defines Row and Column types with the appropriate bounds. I got caught up confusing > and >= for the height comparison for a bit, but that’s just because I didn’t read the directions carefully enough the first time.

I also thought this one was more straightforward than the previous. This was one very welcome case where once I could solve the example input, I could solve the puzzle input.

using plain old arrays here.

I misread the part 2 instructions, thinking I could only see trees that are higher than the one before… oh, and at first I forgot I had to use reverse loops for this.

Also used two-dimensional arrays. I read in the first line to determine the size, then create the array (and fill the first row), then read in the rest of the file. The rest was easy, except I made two mistakes which took some minutes to resolve:

  • My parser dropped some characters (copy-paste error), leaving part of the grid uninitialized;
  • When computing the scenic score, I had off-by-one errors, counting too much (when hitting the border) or not enough (when hitting a larger tree).

Do you have a similar pattern on your forest?


Love this graph! Could you please post the (gnuplot?) code to reproduce it?

I have just dumped the map as CSV and opened it in Excel…

this inspired me to add a PPM graphics export to my solution: aoc2022-Ada/day08.adb at main - aoc2022-Ada - Codeberg.org

Resulting in these images (palette values copied from your image):

Test data (scaled resulting image by x10, converted to png using Gimp):

Real data (scaled resulting image by x2, converted to png using Gimp):

1 Like

My forest pattern is similar.

Hey cool!
Obviously the contour plot in Excel is distorting the real picture a lot (it is made for less noisy data).
Your approach is much better.
Is it okay if I add your PPM output - with credit and a link - into my code?
I have changed the green value of the 4th palette color to 16#ff# (the color is then cyan).


1 Like

I used the code from Rosettacode with slight modifications. Feel free to link and credit them, too. (I should add a link to my source, too…)

While I was able to get gnatprove to accept my program, I am unsatisfied with one check I did where I had to make sure Num_Visible was less than Natural’Last. This is a sketch of the loop where it is happening:

Num_Visible := 0;
for y in 1 .. Height loop
   for x in 1 .. Width loop
      --   some other stuff
      if Is_Visible then
         Num_Visible := Num_Visible + 1;
      end if;
   end loop;
end loop;

Since Height and Width are both of a type that is in the range 1 … 100, I’m not sure why gnatprove can’t figure out that the Num_Visible increment can’t happen more than Height * Width times. I have tried making loop invariants saying that Num_Visible is alway <= Height * Width, but gnatprove can’t see that. Anybody have any suggestions?

Code is here: https://github.com/wutka/advent-of-code-2022-spark/blob/4d3629a147aa6e78bb7589c2e41036acf481aad7/day8/src/day8.adb

1 Like
  • Using In Strong Typing We Trust
    :sunglasses: :sunglasses: :sunglasses:
    See puzzle 08

Hummm … 376 SLoC
Did I make it too wordy ?

Playing with gnuplot.

  1. From the “input.txt” file, create a “forest.txt” file with tree sizes separated by a space:

  2. In the 2nd part, create the file “scenic_score.txt” with such value of each tree:

For both images:

set xrange [0:98]
set yrange [0:98]
set pm3d
set view map
set key outside
set dgrid3d 99,99
unset surface
splot "forest.txt" matrix with pm3d notitle
1 Like

You need to write two loop invariants (one for each loop), and bound by x and y (not Height and Width). Also, you need to specify exactly where you are in the counting process at each step. This proves it for me:

      for y in 1 .. Height loop
         pragma Loop_Invariant (Num_Visible <= (y - 1) * Width);
         for x in 1 .. Width loop
            pragma Loop_Invariant (Num_Visible <= (y - 1) * Width + (x - 1));

Wow, yes, that worked! Thank you!