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