示例#1
0
        private void GenerateSettings(bool loopMode)
        {
            ulong checkSum = 10;
            ulong start    = 1234;
            ulong end      = 43210;

            if (loopMode)
            {
                start    = 56789;
                end      = 98765;
                checkSum = 35;
            }
            computerSettings = new List <ulong[]>();
            for (ulong i = start; i <= end; i++)
            {
                ulong[] currNumber = NumberLists.SetLength(NumberLists.MakeArray(i), 5, false);
                if (NumberLists.CrossSum(currNumber) == checkSum && NumberLists.UniqueDigits(currNumber))
                {
                    computerSettings.Add(currNumber.ToArray());
                }
            }
            if (computerSettings.Count != 120)
            {
                throw new Exception("Wrong amount of settings generated");
            }
        }
示例#2
0
 private ulong[] GetLastValue(ulong upper)
 {
     ulong[] number = GetFirstValue(NumberLists.MakeArray(upper));
     if (NumberLists.MakeNumber(number) > upper)
     {
         ulong maxVal = number[0] - 1;
         number    = NumberLists.SetAll(number, 9);
         number[0] = maxVal;
     }
     return(number);
 }
示例#3
0
        public override string Solve(string input, bool part2)
        {
            isPart2 = part2;
            string[] range = input.Split('-');
            if (range.Length != 2)
            {
                throw new ArgumentOutOfRangeException("input", "Only two Values allowed!");
            }
            if (!ulong.TryParse(range[0], out start) || !ulong.TryParse(range[1], out ulong uLimit))
            {
                throw new ArgumentException("Input is not a Number!", "input");
            }
            if (range[0].Length != 6 || range[1].Length != 6)
            {
                throw new ArgumentOutOfRangeException("input", "The numbers must be exactly 6 digits long!");
            }
            if (uLimit <= start)
            {
                throw new ArgumentOutOfRangeException("input", "The Selected range is 0 or negative!");
            }

            end = GetLastValue(uLimit);

            ulong[]      currNumber = GetFirstValue(NumberLists.MakeArray(start));
            int          doublePos  = GetDoublePos(currNumber, out ulong doubleValue);
            bool         endReached = true;
            List <ulong> values     = new List <ulong>();
            List <ulong> discarded  = new List <ulong>();

            do
            {
                if (GetDoublePos(currNumber) != -1)
                {
                    values.Add(NumberLists.MakeNumber(currNumber));
                }
                else
                {
                    discarded.Add(NumberLists.MakeNumber(currNumber));
                }
                currNumber = IncreaseValueAt(currNumber, currNumber.Length - 1);
                endReached = true;
                for (int i = 0; i < currNumber.Length; i++)
                {
                    if (currNumber[i] < end[i])
                    {
                        endReached = false;
                        continue;
                    }
                }
            } while (!endReached);

            ulong endValue = NumberLists.MakeNumber(currNumber);

            if (endValue <= uLimit && GetDoublePos(currNumber) != -1)
            {
                values.Add(endValue);
            }
            else
            {
                discarded.Add(endValue);
            }

            StringBuilder valueList = new StringBuilder();

            foreach (ulong value in values)
            {
                valueList.AppendLine(value.ToString());
            }
            valueList.AppendLine("DISCARDED:");
            foreach (ulong value in discarded)
            {
                valueList.AppendLine(value.ToString());
            }

            return(string.Format("{0}\r\nFound {1} Values:\r\n", valueList.ToString(), values.Count));
        }