@@ 0,0 1,136 @@
+program day08;
+var
+ inFile: Text;
+ map: array[1..120, 1..120] of Char;
+ rows, cols: Integer;
+ r, c, n: Integer;
+
+function TreeVisibleInRow(rrr: Integer; ccc: Integer; incr: Integer): Boolean;
+var
+ i: Integer;
+begin
+ i := ccc;
+ while (i > 1) and (i < cols) do
+ begin
+ if map[rrr, i + incr] >= map[rrr, ccc] then
+ begin
+ TreeVisibleInRow := false;
+ Exit;
+ end;
+ i := i + incr;
+ end;
+ TreeVisibleInRow := true;
+end;
+
+function TreeVisibleInCol(rrr: Integer; ccc: Integer; incr: Integer): Boolean;
+var
+ i: Integer;
+begin
+ i := rrr;
+ while (i > 1) and (i < rows) do
+ begin
+ if map[i + incr, ccc] >= map[rrr, ccc] then
+ begin
+ TreeVisibleInCol := false;
+ Exit;
+ end;
+ i := i + incr;
+ end;
+ TreeVisibleInCol := true;
+end;
+
+function TreeVisible(rr: Integer; cc: Integer): Boolean;
+begin
+ if TreeVisibleInRow(rr, cc, 1) then
+ begin
+ TreeVisible := true;
+ Exit;
+ end;
+
+ if TreeVisibleInRow(rr, cc, -1) then
+ begin
+ TreeVisible := true;
+ Exit;
+ end;
+
+ if TreeVisibleInCol(rr, cc, 1) then
+ begin
+ TreeVisible := true;
+ Exit;
+ end;
+
+ if TreeVisibleInCol(rr, cc, -1) then
+ begin
+ TreeVisible := true;
+ Exit;
+ end;
+
+ TreeVisible := false;
+end;
+
+begin
+ if ParamCount <> 1 then
+ begin
+ Writeln('Usage: day08 file');
+ Halt;
+ end;
+
+ Assign(inFile, ParamStr(1));
+ Reset(inFile);
+ Write('Reading ');
+
+ rows := 0;
+ cols := 0;
+
+ r := 0;
+ while not Eof(inFile) do
+ begin
+ r := r + 1;
+
+ if r mod 10 = 0 then
+ Write(r, ' ');
+
+ c := 0;
+ while not Eoln(inFile) do
+ begin
+ c := c + 1;
+ Read(inFile, map[r, c]);
+ end;
+
+ if cols = 0 then
+ cols := c
+ else
+ if cols <> c then
+ begin
+ Writeln('Error: First row had ', cols,
+ ' columns but row ', r, ' has ', c);
+ Halt;
+ end;
+
+ Readln(inFile);
+ end;
+ rows := r;
+
+ Writeln(rows, ' rows, ', cols, ' columns each');
+ Close(inFile);
+
+ { Initialize result to the always-visible trees on the edges. }
+ n := 2 * (rows + cols - 2);
+
+ { Check the inner square of trees, to determine which ones are visble. }
+ Write('Munging ');
+ for r := 2 to (rows - 1) do
+ begin
+ if r mod 10 = 0 then
+ Write(r, ' ');
+
+ for c := 2 to (cols - 1) do
+ begin
+ if TreeVisible(r, c) then
+ n := n + 1;
+ end;
+ end;
+ Writeln(r, ' rows');
+
+ Writeln('Part 1: ', n);
+end.