Calculations are not showing

Eduardo Gomez 3,416 Reputation points
2023-03-25T20:15:43.23+00:00

Input.txt

I have this txt and I must perform some calculations

A “Calculate” button o Calculation 1:
If Program, Noun, and Verb are specified. Calculate and display the result of position zero in parameter Result of Calculation 2: If Program and Result are specified. Calculate the noun and verb that produce said result and displays them in their respective fields. noun and verb should always be between 0 and 999. If no result is found, a message indicating this should be displayed. o NOTE: In both cases, an error window should be displayed if there is any unwanted behavior. • Things that will be considered: o Asynchronous calculation to not block the user interface o Parallel calculation to get the results faster"

A made the UI and VM

    private async Task CalculateAction(string num) {
            int[]? intcodeProgram;
            switch (num) {
                case "1":
                    if (!string.IsNullOrEmpty(programPath)
                        && !string.IsNullOrEmpty(_SustantivoText)
                        && !string.IsNullOrEmpty(VerboText)) {
                        try {
                            intcodeProgram = ReadIntcodeProgramFromFile(programPath!);
                            ModifyIntcodeProgram(intcodeProgram, int.Parse(SustantivoText), int.Parse(VerboText));
                            int result = ExecuteIntcodeProgram(intcodeProgram);

                            res = "The value at position 0 is: " + result;

                            // create a text file with the answer
                            using StreamWriter writer = new("answer1.txt");
                            await writer.WriteAsync(res);
                        } catch (Exception ex) {
                            MessageBox.Show($"Error executing program: {ex.Message}");
                        }
                    } else {
                        MessageBox.Show("Please specify the IntCode program path, sustantivo and verbo values.");
                    }
                    break;
                case "2":
                    if (!string.IsNullOrEmpty(programPath) && !string.IsNullOrEmpty(res)) {
                        try {
                            const int MAX_VALUE = 999999;
                            const int DESIRED_OUTPUT = 19690720;

                            intcodeProgram = ReadIntcodeProgramFromFile(programPath);

                            bool resultFound = false;

                            await Task.Run(() => {
                                Parallel.For(0, MAX_VALUE + 1, (noun) => {
                                    for (int verb = 0; verb <= MAX_VALUE; verb++) {
                                        int[] modifiedProgram = (int[])intcodeProgram.Clone();
                                        ModifyIntcodeProgram(modifiedProgram, noun, verb);
                                        int result = ExecuteIntcodeProgram(modifiedProgram);

                                        if (result == DESIRED_OUTPUT) {
                                            SustantivoText = $"{noun}";
                                            VerboText = $"{verb}";
                                            resultFound = true;
                                        }
                                    }
                                });
                            });

                            if (resultFound) {
                                MessageBox.Show($"Sustantivo: {SustantivoText}\nVerbo: {VerboText}");
                            } else {
                                MessageBox.Show("No result found.");
                            }
                        } catch (Exception ex) {
                            MessageBox.Show(ex.Message);
                        }
                    }
                    break;
                default:
                    MessageBox.Show("Invalid argument for CalculateAction");
                    break;
            }
        }

 <TextBox
            Grid.Row="1"
            Grid.Column="1"
            VerticalAlignment="Center"
            Text="{Binding SustantivoText, UpdateSourceTrigger=PropertyChanged}" />

        <TextBox
            Grid.Row="2"
            Grid.Column="1"
            VerticalAlignment="Center"
            Text="{Binding VerboText, UpdateSourceTrigger=PropertyChanged}" />

and for the second case, it doesn't want to show anything

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,681 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Ayomide Oluwaga 946 Reputation points
    2023-03-25T23:39:26.9633333+00:00

    Hello Eduardo,

    Try the following code if it works, let me know:

    private async Task CalculateAction(string num)
    {
        int[] intcodeProgram = new int[0];
        switch (num)
        {
            case "1":
                // Check if all required input values are provided
                if (!string.IsNullOrEmpty(programPath)
                    && !string.IsNullOrEmpty(SustantivoText)
                    && !string.IsNullOrEmpty(VerboText))
                {
                    try
                    {
                        // Read the Intcode program from the file
                        intcodeProgram = ReadIntcodeProgramFromFile(programPath);
    
                        // Modify the Intcode program with the provided input values
                        ModifyIntcodeProgram(intcodeProgram, int.Parse(SustantivoText), int.Parse(VerboText));
    
                        // Execute the Intcode program and get the result
                        int result = ExecuteIntcodeProgram(intcodeProgram);
    
                        // Display the result
                        string resultMessage = "The value at position 0 is: " + result;
                        MessageBox.Show(resultMessage);
    
                        // Create a text file with the answer
                        using StreamWriter writer = new StreamWriter("answer1.txt");
                        await writer.WriteAsync(resultMessage);
                    }
                    catch (IOException ex)
                    {
                        MessageBox.Show($"Error reading file: {ex.Message}");
                    }
                    catch (InvalidOperationException ex)
                    {
                        MessageBox.Show($"Error executing program: {ex.Message}");
                    }
                }
                else
                {
                    MessageBox.Show("Please specify the IntCode program path, sustantivo and verbo values.");
                }
                break;
            case "2":
                if (!string.IsNullOrEmpty(programPath))
                {
                    try
                    {
                        const int MAX_VALUE = 999999;
                        const int DESIRED_OUTPUT = 19690720;
    
                        // Read the Intcode program from the file
                        intcodeProgram = ReadIntcodeProgramFromFile(programPath);
    
                        bool resultFound = false;
    
                        // Find the input values that produce the desired output
                        await Task.Run(() =>
                        {
                            Parallel.For(0, MAX_VALUE + 1, (noun) =>
                            {
                                for (int verb = 0; verb <= MAX_VALUE; verb++)
                                {
                                    int[] modifiedProgram = (int[])intcodeProgram.Clone();
                                    ModifyIntcodeProgram(modifiedProgram, noun, verb);
                                    int result = ExecuteIntcodeProgram(modifiedProgram);
    
                                    if (result == DESIRED_OUTPUT)
                                    {
                                        SustantivoText = $"{noun}";
                                        VerboText = $"{verb}";
                                        resultFound = true;
                                    }
                                }
                            });
                        });
    
                        // Display the input values that produce the desired output
                        if (resultFound)
                        {
                            MessageBox.Show($"Sustantivo: {SustantivoText}\nVerbo: {VerboText}");
                        }
                        else
                        {
                            MessageBox.Show("No result found.");
                        }
                    }
                    catch
    
    
    

  2. Ayomide Oluwaga 946 Reputation points
    2023-03-26T17:32:55.0233333+00:00

    Sorry, the code I provided is incomplete and the exception handling block is cut off. Here is a complete code with exception handling for the two cases, (Let me know if it works as expected):

    using System;
    using System.IO;
    using System.Linq;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace IntcodeProgram
    {
        public partial class MainForm : Form
        {
            private string programPath;
            private string SustantivoText;
            private string VerboText;
    
            public MainForm()
            {
                InitializeComponent();
            }
    
            private async void RunButton_Click(object sender, EventArgs e)
            {
                string num = NumTextBox.Text.Trim();
    
                try
                {
                    await CalculateAction(num);
                }
                catch (Exception ex)
                {
                    MessageBox.Show($"Error: {ex.Message}");
                }
            }
    
            private async Task CalculateAction(string num)
            {
                int[] intcodeProgram = new int[0];
    
                switch (num)
                {
                    case "1":
                        // Check if all required input values are provided
                        if (!string.IsNullOrEmpty(programPath)
                            && !string.IsNullOrEmpty(SustantivoText)
                            && !string.IsNullOrEmpty(VerboText))
                        {
                            try
                            {
                                // Read the Intcode program from the file
                                intcodeProgram = ReadIntcodeProgramFromFile(programPath);
    
                                // Modify the Intcode program with the provided input values
                                ModifyIntcodeProgram(intcodeProgram, int.Parse(SustantivoText), int.Parse(VerboText));
    
                                // Execute the Intcode program and get the result
                                int result = ExecuteIntcodeProgram(intcodeProgram);
    
                                // Display the result
                                string resultMessage = "The value at position 0 is: " + result;
                                MessageBox.Show(resultMessage);
    
                                // Create a text file with the answer
                                using StreamWriter writer = new StreamWriter("answer1.txt");
                                await writer.WriteAsync(resultMessage);
                            }
                            catch (IOException ex)
                            {
                                throw new Exception($"Error reading file: {ex.Message}");
                            }
                            catch (InvalidOperationException ex)
                            {
                                throw new Exception($"Error executing program: {ex.Message}");
                            }
                        }
                        else
                        {
                            MessageBox.Show("Please specify the IntCode program path, sustantivo and verbo values.");
                        }
                        break;
    
                 
      case "2":
                if (!string.IsNullOrEmpty(programPath))
                {
                    try
                    {
                        const int MAX_VALUE = 999999;
                        const int DESIRED_OUTPUT = 19690720;
    
                        // Read the Intcode program from the file
                        intcodeProgram = ReadIntcodeProgramFromFile(programPath);
    
                        bool resultFound = false;
    
                        // Find the input values that produce the desired output
                        await Task.Run(() =>
                        {
                            Parallel.For(0, MAX_VALUE + 1, (noun) =>
                            {
                                for (int verb = 0; verb <= MAX_VALUE; verb++)
                                {
                                    int[] modifiedProgram = (int[])intcodeProgram.Clone();
                                    ModifyIntcodeProgram(modifiedProgram, noun, verb);
                                    int result = ExecuteIntcodeProgram(modifiedProgram);
    
                                    if (result == DESIRED_OUTPUT)
                                    {
                                        SustantivoText = $"{noun}";
                                        VerboText = $"{verb}";
                                        resultFound = true;
                                    }
                                }
                            });
                        });
    
                        // Display the input values that produce the desired output
                        if (resultFound)
                        {
                            MessageBox.Show($"Sustantivo: {SustantivoText}\nVerbo: {VerboText}");
                        }
                        else
                        {
                            MessageBox.Show("No result found.");
                        }
                    }
                    catch (IOException ex)
                    {
                        MessageBox.Show($"Error reading file: {ex.Message}");
                    }
                    catch (InvalidOperationException ex)
                    {
                        MessageBox.Show($"Error executing program: {ex.Message}");
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show($"An unexpected error occurred: {ex.Message}");
                    }
                }
                else
                {
                    MessageBox.Show("Please specify the IntCode program path.");
                }
                break;
            default:
                break;
        }
    }