Euler problem 11.5

A project created by using Lazarus on one platform can be compiled on any other one which Free Pascal compiler supports. For desktop applications a single source can target macOS, Linux, and Windows, with little or no modification. An example is the Lazarus IDE itself, created from a single code base and available on all major platforms including the Raspberry Pi.

— Wikipedia on Lazarus (software)

.

.

What is the greatest product of four adjacent numbers in the same direction (up, down, left, right, or diagonally) in the 20×20 grid?

program e11;          

uses
   SysUtils, math;

type
   T2d_int_array = array of array of integer;

const
   filename = 't.txt';

var
   current_dir: string;
   ptext: TextFile;
   
   line_string: string;
   s: string = '';
   lines: array of string;
   array_string_2d: array of array of string;
   array_int_2d: T2d_int_array;
   
   isbegin: boolean = true;
   i: integer;
   j: integer;
   lenx: integer = 0;
   leny: integer = 0;
   c: integer = 0;


function max4_iter(a2d: T2d_int_array;
                  i, j: integer;
                   acc: longint): integer;
var
   dia_l: longint = 0;
   dia_r: longint = 0;
   hori: longint = 0;
   vert: longint = 0;
   k: integer = 0;
   accm: longint = 0;
   x: integer = 0;
   y: integer = 0;

begin
   if i <= (lenx - 4) then begin
      hori := 1;
      for k := 0 to 3 do begin
         hori := hori*a2d[i+k, j];
      end;
   end;

   if j <= (leny - 4) then begin
      vert := 1;
      for k := 0 to 3 do begin
         vert := vert*a2d[i, j+k];
      end;
   end;

   if (i <= (lenx - 4)) and (j <= (leny - 4)) then
   begin
      dia_r := 1;
      for k := 0 to 3 do begin
         dia_r := dia_r*a2d[i+k, j+k];
      end;
   end;

   if (i >= 3) and (j <= (leny - 4)) then begin
      dia_l := 1;
      for k := 0 to 3 do begin
         dia_l := dia_l*a2d[i-k, j+k];
      end;
   end;

   if (i = (lenx - 1)) and (j = (leny - 1)) then
   begin
      max4_iter := acc;
   end else begin
      accm := max(acc,
                  max(hori,
                      max(vert,
                          max(dia_l, dia_r))));
      
      if i = lenx - 1 then begin
         x := 0;
         y := j + 1;
      end else begin
         x := i + 1;
         y := j;
      end;
      max4_iter := max4_iter(a2d, x, y, accm);
   end;
end;

begin

   current_dir := GetCurrentDir;

   writeln(filename, ':');
   writeln('==================================');

   if not FileExists(filename) then begin
      writeln(filename, ' does not exist.');
      exit;
   end;

   assign(ptext, filename);
   reset(ptext);

   setLength(lines, 0);
   s := '';
   isbegin := true;
   while not eof(ptext) do begin
      readln(ptext, line_string);
      if isbegin then begin
         s := s + line_string;
         isbegin := false;
      end else begin
         s := s + LineEnding + line_string;
      end;
   end;
   close(ptext);

   lines := s.split(LineEnding);
   lenx := length(lines);
   setLength(array_string_2d, lenx);

   for i := 0 to (lenx - 1) do begin
      array_string_2d[i] := lines[i].split(' ');
   end;
   leny := length(array_string_2d[0]);

   setLength(array_int_2d, lenx);
   for i := 0 to (lenx - 1) do begin
      setLength(array_int_2d[i], leny);
   end;

   for i := 0 to (lenx - 1) do begin
      for j := 0 to (leny - 1) do begin
         val(array_string_2d[i][j],
             array_int_2d[i][j],
             c);
      end;
   end;

   writeln(max4_iter(array_int_2d, 0, 0, 0));
   writeln('==================================');
end.

— Me@2022-12-14 03:56:23 PM

.

.

2022.12.14 Wednesday (c) All rights reserved by ACHK