using System;
using System.Linq;
using System.Text;
using System.Collections.Generic;

public class Program
{
    // funkcija, kas pārveido virkni, katru otro burtu padarot par lielo
    public static string TransformString(string str)
    {
        StringBuilder transformed = new StringBuilder();
        for (int i = 0; i < str.Length; i++)
        {
            if (i % 2 == 1) 
            {
                transformed.Append(char.ToUpper(str[i]));
            }
            else
            {
                transformed.Append(str[i]);
            }
        }
        return transformed.ToString();
    }

    // funkcija, kas ģenerē gadījuma skaitli dotajā diapazonā
    public static int GetRandomInt(Random rand, int min, int max)
    {
        return rand.Next(min, max + 1); 
    }

    // funkcija, kas ģenerē un formatē tabulu konsoles izvadei
    public static string GenerateTable(List<string> headers, List<List<object>> data)
    {
        StringBuilder tableOutput = new StringBuilder();
        int[] colWidths = new int[headers.Count];

        // aprēķina kolonnu platumus
        for (int i = 0; i < headers.Count; i++)
        {
            colWidths[i] = headers[i].Length;
        }

        foreach (var row in data)
        {
            for (int i = 0; i < row.Count; i++)
            {
                colWidths[i] = Math.Max(colWidths[i], row[i].ToString().Length);
            }
        }

        // funkcija, kas ģenerē rindu
        Func<char, string> createSeparator = (char sepChar) =>
        {
            StringBuilder separator = new StringBuilder("+");
            foreach (int width in colWidths)
            {
                separator.Append(new string(sepChar, width + 2));
                separator.Append("+");
            }
            return separator.ToString();
        };

        // funkcija, kas formatē rindu
        Func<List<object>, char, string> formatRow = (row, sepChar) =>
        {
            StringBuilder formatted = new StringBuilder("| ");
            for (int i = 0; i < row.Count; i++)
            {
                formatted.Append(row[i].ToString().PadRight(colWidths[i]));
                formatted.Append($" {sepChar} ");
            }
            formatted.Remove(formatted.Length - 3, 3); // noņem pēdējo atdalītāju
            formatted.Append(" |");
            return formatted.ToString();
        };

        // pirmā atdalītāja rinda (datu formātā)
        tableOutput.AppendLine(createSeparator('-'));

        // galvenes rinda
        tableOutput.AppendLine(formatRow(headers.Cast<object>().ToList(), '|'));

        
        tableOutput.AppendLine(createSeparator('-'));

        
        foreach (var row in data)
        {
            tableOutput.AppendLine(formatRow(row, '|'));
            tableOutput.AppendLine(createSeparator('-'));
        }

        return tableOutput.ToString();
    }

    public static void Main(string[] args)
    {
        Console.OutputEncoding = Encoding.UTF8; // nodrošina pareizu burtu izvadi
        Random rand = new Random();

        Console.Write("Ievadiet savu vārdu: ");
        string vards = Console.ReadLine() ?? "";

        Console.Write("Ievadiet savu uzvārdu: ");
        string uzvards = Console.ReadLine() ?? "";
        
        string transformedVards = TransformString(vards);
        string transformedUzvards = TransformString(uzvards);
        
        // --- Pirmā tabula ---
        int numRows = transformedVards.Length;
        int numCols = transformedUzvards.Length;

        List<string> headers = new List<string> { "X" };
        headers.AddRange(transformedUzvards.Select(c => c.ToString()));
        headers.AddRange(new string[] { "sum", "min", "max" });

        List<List<object>> data = new List<List<object>>();

        // aizpilda galveno 2D masīvu ar gadījuma skaitļiem
        List<List<int>> mainArray = new List<List<int>>();
        for (int i = 0; i < numRows; i++)
        {
            List<int> row = new List<int>();
            for (int j = 0; j < numCols; j++)
            {
                row.Add(GetRandomInt(rand, vards.Length, vards.Length + uzvards.Length));
            }
            mainArray.Add(row);
        }

        // aizpilda datu rindas pirmajai tabulai
        for (int i = 0; i < numRows; i++)
        {
            List<object> rowData = new List<object> { transformedVards[i].ToString() };
            rowData.AddRange(mainArray[i].Cast<object>());

            // aprēķina sum, min, max katrai rindai
            int sum = mainArray[i].Sum();
            int min = mainArray[i].Min();
            int max = mainArray[i].Max();
            rowData.Add(sum);
            rowData.Add(min);
            rowData.Add(max);
            data.Add(rowData);
        }
        
        Console.WriteLine("Pirma tabula");
        Console.WriteLine(GenerateTable(headers, data)); // pārveidots, lai neiekļautu aprēķinu datus

        // otrā tabula
        List<List<int>> reversedMainArray = new List<List<int>>();
        for (int j = 0; j < numCols; j++)
        {
            List<int> row = new List<int>();
            for (int i = 0; i < numRows; i++)
            {
                row.Add(mainArray[i][j]);
            }
            reversedMainArray.Add(row);
        }

        List<string> reversedHeaders = new List<string> { "X" };
        reversedHeaders.AddRange(transformedVards.Select(c => c.ToString()));
        reversedHeaders.AddRange(new string[] { "sum", "min", "max" });

        List<List<object>> reversedData = new List<List<object>>();

        // aizpilda datu rindas tabulai
        for (int i = 0; i < numCols; i++)
        {
            List<object> rowData = new List<object> { transformedUzvards[transformedUzvards.Length - 1 - i].ToString() }; // Uzvārda burti no beigām
            rowData.AddRange(reversedMainArray[i].Cast<object>());

            // aprēķina sum, min, max katrai rindai
            int sum = reversedMainArray[i].Sum();
            int min = reversedMainArray[i].Min();
            int max = reversedMainArray[i].Max();
            rowData.Add(sum);
            rowData.Add(min);
            rowData.Add(max);
            reversedData.Add(rowData);
        }
        
        Console.WriteLine("\nOtra tabula:");
        Console.WriteLine(GenerateTable(reversedHeaders, reversedData)); // Pārveidots, lai neiekļautu aprēķinu datus
    }
}
