Beispiel #1
0
        public static int Calculate(string fileName)
        {
            var input          = SantasBoilerPlate.ReadFileAsString(fileName);
            var inputArray     = input.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
            var lengthOfInputs = inputArray[0].Length;
            var no_of_zeros    = new int[lengthOfInputs];
            var no_of_ones     = new int[lengthOfInputs];

            foreach (var line in inputArray)
            {
                var intArray = line.Select(c => int.Parse(c.ToString())).ToArray();
                for (var bitPosition = 0; bitPosition < intArray.Count(); bitPosition++)
                {
                    var bitValue = intArray[bitPosition];

                    if (bitValue == 1)
                    {
                        no_of_ones[bitPosition] += 1;
                    }
                    else
                    {
                        no_of_zeros[bitPosition] += 1;
                    }
                }
            }

            var gamma   = new int[lengthOfInputs];
            var epsilon = new int[lengthOfInputs];

            for (var position = 0; position < lengthOfInputs; position++)
            {
                if (no_of_ones[position] > no_of_zeros[position])
                {
                    gamma[position]   = 1;
                    epsilon[position] = 0;
                }
                else
                {
                    gamma[position]   = 0;
                    epsilon[position] = 1;
                }
            }

            var gammaInt   = GetIntFromBitArray(gamma);
            var epsilonInt = GetIntFromBitArray(epsilon);



            return(gammaInt * epsilonInt);
        }
Beispiel #2
0
        private Dictionary <string, List <Wire> > WireCircuitBoard(string textFile)
        {
            var input = SantasBoilerPlate.ReadFileAsString(textFile ?? _textFile);
            var wires = input.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
            var board = new Dictionary <string, List <Wire> >();

            for (var wireId = 0; wireId < wires.Length; wireId++)
            {
                var wireMap = wires[wireId].Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

                var currentLocation = new Point(0, 0);
                var wire            = new Wire {
                    Id = wireId + 1, ManhattanLength = Int32.MaxValue
                };
                foreach (var direction in wireMap)
                {
                    currentLocation = Populate(board, direction, new Point(currentLocation.X, currentLocation.Y), wire);
                }
            }

            return(board);
        }