Example #1
0
        /// <summary>
        /// Returns the value of the bot that compares microchips with the specified values.
        /// </summary>
        /// <param name="instructions">The instructions to process.</param>
        /// <param name="a">The first number to find the bot that compares it.</param>
        /// <param name="b">The second number to find the bot that compares it.</param>
        /// <param name="binsOfInterest">The numbers of the bins of interest for which to find the product of the microchip values.</param>
        /// <returns>
        /// A <see cref="Tuple{T1, T2}"/> that returns the number of the bot that compares microchips
        /// with the values specified by <paramref name="a"/> and <paramref name="b"/> and the product
        /// of the values of the microchips in the output bins with the numbers specified by <paramref name="binsOfInterest"/>.
        /// </returns>
        internal static Tuple<int, int> GetBotNumber(IEnumerable<string> instructions, int a, int b, IEnumerable<int> binsOfInterest)
        {
            int max = Math.Max(a, b);
            int min = Math.Min(a, b);

            int botOfInterest = -1;
            int productOfBinsOfInterest = 1;

            var processor = new InstructionProcessor();

            processor.Process(
                instructions,
                (bot, low, high) =>
                {
                    if (low == min && high == max)
                    {
                        botOfInterest = bot;
                    }
                });

            foreach (var bin in processor.Bins)
            {
                if (binsOfInterest.Contains(bin.Key))
                {
                    productOfBinsOfInterest *= bin.Value.Microchip.Value;
                }
            }

            return Tuple.Create(botOfInterest, productOfBinsOfInterest);
        }
Example #2
0
    /// <summary>
    /// Returns the value of the bot that compares microchips with the specified values.
    /// </summary>
    /// <param name="instructions">The instructions to process.</param>
    /// <param name="a">The first number to find the bot that compares it.</param>
    /// <param name="b">The second number to find the bot that compares it.</param>
    /// <param name="binsOfInterest">The numbers of the bins of interest for which to find the product of the microchip values.</param>
    /// <returns>
    /// A named tuple that returns the number of the bot that compares microchips
    /// with the values specified by <paramref name="a"/> and <paramref name="b"/> and the product
    /// of the values of the microchips in the output bins with the numbers specified by <paramref name="binsOfInterest"/>.
    /// </returns>
    internal static (int Bot, int Product) GetBotNumber(IEnumerable <string> instructions, int a, int b, IEnumerable <int> binsOfInterest)
    {
        int max = Math.Max(a, b);
        int min = Math.Min(a, b);

        int botOfInterest           = -1;
        int productOfBinsOfInterest = 1;

        var processor = new InstructionProcessor();

        processor.Process(
            instructions,
            (bot, low, high) =>
        {
            if (low == min && high == max)
            {
                botOfInterest = bot;
            }
        });

        foreach (var bin in processor.Bins)
        {
            if (binsOfInterest.Contains(bin.Key))
            {
                productOfBinsOfInterest *= bin.Value.Microchip !.Value;
            }
        }

        return(botOfInterest, productOfBinsOfInterest);
    }
        public void should_throw_missing_field_exception_when_apply_instruction_not_found()
        {
            // Arrange
            var filePath     = $"{Directory.GetCurrentDirectory()}\\InstructionSet\\MissingApplyInstruction.txt";
            var instructions = File.ReadAllText(filePath);

            var _sut = new InstructionProcessor();

            // Act & Assert
            Assert.Throws <MissingFieldException>(() => _sut.Process(instructions));
        }
        public void should_result_divide_by_zero_exception()
        {
            // Arrange
            var filePath     = $"{Directory.GetCurrentDirectory()}\\InstructionSet\\DivideByZeroException.txt";
            var instructions = File.ReadAllText(filePath);

            var _sut = new InstructionProcessor();

            // Act & Assert
            Assert.Throws <DivideByZeroException>(() => _sut.Process(instructions));
        }
        public void should_result_zero_when_instructions_are_empty()
        {
            // Arrange
            int expectedResult = 0;
            var instructions   = string.Empty;

            var _sut = new InstructionProcessor();

            // Act
            var actualResult = _sut.Process(instructions);

            // Assert
            Assert.Equal(expectedResult, actualResult);
        }
        public void should_successfully_process_instructions()
        {
            // Arrange
            int expectedResult = 16;
            var filePath       = $"{Directory.GetCurrentDirectory()}\\InstructionSet\\Sample.txt";
            var instructions   = File.ReadAllText(filePath);

            var _sut = new InstructionProcessor();

            // Act
            var actualResult = _sut.Process(instructions);

            // Assert
            Assert.Equal(expectedResult, actualResult);
        }