Exemplo n.º 1
0
    private static string Solution2(string[] input, bool debug = false)
    {
        List <Instruction> instructions = ALU.Parse(input).ToList();

        long modelNumber = 11111111111111;

        ALU submarineAlu = new();

        if (debug)
        {
            submarineAlu.Debug = debug;
            Console.WriteLine();
            Console.WriteLine($"{modelNumber}");
        }

        int[] digits = modelNumber.ToString().Select(d => int.Parse($"{d}")).ToArray();
        submarineAlu.ExecuteInstructions(instructions, digits);

        if (debug)
        {
            Console.WriteLine();
            if (submarineAlu.HasException)
            {
                Console.WriteLine(submarineAlu.ExceptionMessage);
            }
            else
            {
                Console.WriteLine($"{modelNumber} w: {submarineAlu.W} x: {submarineAlu.X} y: {submarineAlu.Y} z: {submarineAlu.Z} ");
            }
        }

        if (submarineAlu.Z == 0)
        {
            return(string.Join("", digits.Select(d => $"{d}")));
        }
        else
        {
            foreach ((int w1, int w2, int offset) in submarineAlu.Conditions)
            {
                if (offset >= 0)
                {
                    digits[w1] = 1 + offset;
                    digits[w2] = 1;
                }
                else
                {
                    digits[w2] = 1 - offset;
                    digits[w1] = 1;
                }
            }
            return(string.Join("", digits.Select(d => $"{d}")));
        }
    }