Пример #1
0
        private void ParseInput(String inputLine)
        {
            BagColor = StringOps.SubStringPre(inputLine, "contain").Trim();
            BagColor = BagColor.Replace("bags", "").Trim();
            BagColor = BagColor.Replace("bag", "").Trim();

            string working = StringOps.SubStringPost(inputLine, "contain").Trim();

            working = working.Replace(".", "");
            working = working.Replace("bags", "");
            working = working.Replace("bag", "");

            String[] bags = working.Split(',');

            foreach (string bag in bags)
            {
                if (!bag.Trim().Equals("no other"))
                {
                    string qty  = StringOps.SubStringPre(bag.Trim(), " ");
                    int    iqty = Convert.ToInt32(qty);

                    string bagColour = StringOps.SubStringPost(bag.Trim(), " ").Trim();

                    containBags.Add(bagColour, iqty);
                }
            }
        }
Пример #2
0
        protected override String DoSolve(String[] input)
        {
            string mask = "";
            Dictionary <UInt64, UInt64> memory = new Dictionary <ulong, ulong>();

            foreach (String line in input)
            {
                if (line.Contains("mask"))
                {
                    mask = StringOps.SubStringPost(line, "=").Trim();
                }

                else if (line.Contains("mem"))
                {
                    UInt64 address = Convert.ToUInt64(StringOps.SubStringPre(line, "=").Replace("mem[", "").Replace("]", ""));
                    UInt64 value   = Convert.ToUInt64(StringOps.SubStringPost(line, "="));

                    DecodeValue(mask, ref memory, address, value);
                }
            }

            UInt64 sum = 0;

            foreach (KeyValuePair <UInt64, UInt64> pair in memory)
            {
                sum = sum + pair.Value;
            }

            return($"Result { sum }.");
        }
Пример #3
0
        protected override String DoSolve(String[] input)
        {
            int validCount = 0;

            foreach (String line in input)
            {
                String policy     = StringOps.SubStringPre(line, ":").Trim();
                char   repeatChar = policy[policy.Length - 1];

                String working = policy.Replace(repeatChar.ToString(), "").Trim();

                String min = StringOps.SubStringPre(line, "-");

                working = working.Remove(0, working.IndexOf("-")).Trim();
                working = working.Replace("-", "").Trim();


                String max = working; //StringOps.SubStringPostAndPre(line, repeatChar.ToString(), "-");

                String pwd = StringOps.SubStringPost(line, ":").Trim();

                int charCount = 0;

                if ((pwd[Convert.ToInt32(min) - 1] == repeatChar) ^ (pwd[Convert.ToInt32(max) - 1] == repeatChar))
                {
                    validCount++;
                }
            }



            return($"Santa is on floor { validCount }.");
        }
Пример #4
0
        protected override String DoSolve(String[] input)
        {
            ITableSeating seatingPlan = Factory.CreateTableSeating();

            //read the input
            foreach (String line in input)
            {
                String sourceName     = StringOps.SubStringPre(line, "would ").Trim();
                int    happinessLevel = 0;

                String acquaintanceName = StringOps.SubStringPost(line, "to ").Trim().Replace(".", "");

                String happiness = StringOps.SubStringPre(line, "happiness");
                happiness = StringOps.SubStringPost(happiness, "would ");

                if (happiness.Contains("lose"))
                {
                    happinessLevel = Convert.ToInt32(StringOps.SubStringPost(happiness, "lose").Trim()) * (-1);
                }
                else
                {
                    happinessLevel = Convert.ToInt32(StringOps.SubStringPost(happiness, "gain").Trim());
                }

                seatingPlan.AddRelationship(Factory.CreateRelationship(sourceName, acquaintanceName, happinessLevel));
            }

            //calculate

            return($"The Best Happiness Level is { seatingPlan.BestHappinessSeatingPlan() }.");
        }
Пример #5
0
        protected override String DoSolve(String[] input)
        {
            List <IIngredient> ingredients = new List <IIngredient>();

            //get the input
            foreach (String line in input)
            {
                String ingredientName = StringOps.SubStringPre(line, ":");
                int    capacity       = Convert.ToInt32(StringOps.SubStringPostAndPre(line, "capacity", ", durability").Trim());
                int    durability     = Convert.ToInt32(StringOps.SubStringPostAndPre(line, "durability", ", flavor").Trim());
                int    flavor         = Convert.ToInt32(StringOps.SubStringPostAndPre(line, "flavor", ", texture").Trim());
                int    texture        = Convert.ToInt32(StringOps.SubStringPostAndPre(line, "texture", ", calories").Trim());
                int    calories       = Convert.ToInt32(StringOps.SubStringPost(line, "calories").Trim());

                ingredients.Add(Factory.CreateIngredient(ingredientName, capacity, durability, flavor, texture, calories));
            }

            List <int[]> ingredientPermutations = Combinations.CombinationsThatSumTo(new int[ingredients.Count()], 100);

            int maxScore = 0;

            foreach (int[] ingredientPermutation in ingredientPermutations)
            {
                int cookieScore = Factory.CreateCookie(ingredients, ingredientPermutation).CalculateScore();

                if (cookieScore >= maxScore)
                {
                    maxScore = cookieScore;
                }
            }

            return($"The Best Cookie has a score of {maxScore}!");
        }
Пример #6
0
        private void ParseInput(String input)
        {
            Name = StringOps.SubStringPre(input, ":").Trim();

            Range1Min = Convert.ToInt32(StringOps.SubStringPostAndPre(input, ":", "-").Trim());
            Range1Max = Convert.ToInt32(StringOps.SubStringPost(StringOps.SubStringPre(input, " or ").Trim(), "-").Trim());

            Range2Min = Convert.ToInt32(StringOps.SubStringPre(StringOps.SubStringPost(input, " or ").Trim(), "-").Trim());
            Range2Max = Convert.ToInt32(StringOps.SubStringPost(StringOps.SubStringPost(input, " or ").Trim(), "-").Trim());
        }
Пример #7
0
        private void Initialize(string input)
        {
            string allergens = StringOps.SubStringPost(input, "(").Replace(")", "").Trim().Replace("contains", "").Trim();

            Allergens = allergens.Split(", ").ToList();

            string ingredients = StringOps.SubStringPre(input, "(").Trim();

            Ingredients = ingredients.Split(" ").ToList();
        }
Пример #8
0
        private void ParseInputLine(String input, ref List <IWire> wires, ref List <LogicGate> gates)
        {
            IWire inputWire1;
            IWire inputWire2;
            IWire outputWire;

            String outputWireName = StringOps.SubStringPost(input, "->").Trim(); // input.Substring(input.IndexOf("->") + 2, input.Length - (input.IndexOf("->") + 2)).Trim() ;

            outputWire = InitializeWire(outputWireName, ref wires);

            String expression = input.Substring(0, input.IndexOf("->") - 1);

            if (expression.ToUpper().Contains("AND"))
            {
                inputWire1 = InitializeWire(StringOps.SubStringPre(expression, "AND").Trim(), ref wires);
                inputWire2 = InitializeWire(StringOps.SubStringPost(expression, "AND").Trim(), ref wires);

                gates.Add(Factory.CreateLogicGateAND(ref inputWire1, ref inputWire2, ref outputWire));
            }
            else if (expression.ToUpper().Contains("OR"))
            {
                inputWire1 = InitializeWire(StringOps.SubStringPre(expression, "OR").Trim(), ref wires);
                inputWire2 = InitializeWire(StringOps.SubStringPost(expression, "OR").Trim(), ref wires);

                gates.Add(Factory.CreateLogicGateOR(ref inputWire1, ref inputWire2, ref outputWire));
            }
            else if (expression.ToUpper().Contains("NOT"))
            {
                inputWire2 = InitializeWire(StringOps.SubStringPost(expression, "NOT").Trim(), ref wires);

                gates.Add(Factory.CreateLogicGateNOT(ref inputWire2, ref outputWire));
            }
            else if (expression.ToUpper().Contains("RSHIFT"))
            {
                inputWire1 = InitializeWire(StringOps.SubStringPre(expression, "RSHIFT").Trim(), ref wires);
                inputWire2 = InitializeWire(StringOps.SubStringPost(expression, "RSHIFT").Trim(), ref wires);

                gates.Add(Factory.CreateLogicRShift(ref inputWire1, ref inputWire2, ref outputWire));
            }
            else if (expression.ToUpper().Contains("LSHIFT"))
            {
                inputWire1 = InitializeWire(StringOps.SubStringPre(expression, "LSHIFT").Trim(), ref wires);
                inputWire2 = InitializeWire(StringOps.SubStringPost(expression, "LSHIFT").Trim(), ref wires);

                gates.Add(Factory.CreateLogicLShift(ref inputWire1, ref inputWire2, ref outputWire));
            }
            else
            {   //Assign
                inputWire1 = InitializeWire(expression, ref wires);

                gates.Add(Factory.CreateLogicGateAssign(ref inputWire1, ref outputWire));
            }
        }
Пример #9
0
        private void ParseInput(String[] inputLines)
        {
            foreach (string line in inputLines)
            {
                string[] elements = line.Split(' ');
                foreach (string element in elements)
                {
                    string parameter = StringOps.SubStringPre(element, ":");
                    string value     = StringOps.SubStringPost(element, ":");

                    switch (parameter)
                    {
                    case "byr":
                        BYR = value;
                        break;

                    case "iyr":
                        IYR = value;
                        break;

                    case "eyr":
                        EYR = value;
                        break;

                    case "hgt":
                        HGT = value;
                        break;

                    case "hcl":
                        HCL = value;
                        break;

                    case "ecl":
                        ECL = value;
                        break;

                    case "pid":
                        PID = value;
                        break;

                    case "cid":
                        CID = value;
                        break;

                    default:
                        throw new Exception($"Parameter is {parameter}, Value is {value}.");
                    }
                }
            }

            IsValid       = CheckIsValid();
            IsStrictValid = CheckIsStrictValid();
        }
Пример #10
0
        private void BuildTile(List <string> input)
        {
            ImageData = new List <string>();

            foreach (string line in input)
            {
                if (line.Contains("Tile"))
                {
                    TileID = Convert.ToInt32(StringOps.SubStringPost(line.Replace(":", ""), "Tile").Trim());
                }
                else
                {
                    ImageData.Add(line);
                }
            }
        }
Пример #11
0
        private void BuildRule(string input)
        {
            RuleID = Convert.ToInt32(StringOps.SubStringPre(input, ":"));

            if (input.Contains('"'))
            {
                MatchChar = input.Substring(input.IndexOf('"') + 1, 1)[0];
            }
            else
            {
                SubRules = new List <int[]>();

                if (input.Contains('|'))
                {
                    string working = StringOps.SubStringPost(input, ":").Trim();

                    string[] rulesOr = working.Split('|');


                    foreach (string ruleOr in rulesOr)
                    {
                        string[] rules    = ruleOr.Trim().Split(' ');
                        int[]    rulesInt = new int[rules.Length];

                        for (int i = 0; i < rules.Length; i++)
                        {
                            rulesInt[i] = Convert.ToInt32(rules[i]);
                        }

                        SubRules.Add(rulesInt);
                    }
                }
                else
                {
                    string[] rules    = StringOps.SubStringPost(input, ":").Trim().Split(' ');
                    int[]    rulesInt = new int[rules.Length];

                    for (int i = 0; i < rules.Length; i++)
                    {
                        rulesInt[i] = Convert.ToInt32(rules[i]);
                    }

                    SubRules.Add(rulesInt);
                }
            }
        }
Пример #12
0
        protected override String DoSolve(String[] input)
        {
            List <IRacingReindeer> reindeerRace = new List <IRacingReindeer>();

            //read the input
            foreach (String line in input)
            {
                String reindeerName = StringOps.SubStringPre(line, "can");
                int    flySpeed     = Convert.ToInt32(StringOps.SubStringPost(StringOps.SubStringPre(line, "km/s"), "fly").Trim());
                int    flyDuration  = Convert.ToInt32(StringOps.SubStringPost(StringOps.SubStringPre(line, "seconds,"), "km/s for").Trim());
                int    restDuration = Convert.ToInt32(StringOps.SubStringPost(StringOps.SubStringPre(line, "seconds."), "rest for").Trim());

                reindeerRace.Add(Factory.CreateRacingReindeer(reindeerName, flySpeed, flyDuration, restDuration));
            }

            //Race!
            int currentSecond;
            int raceDurationSeconds = 2503;

            for (currentSecond = 0; currentSecond < raceDurationSeconds; currentSecond++)
            {
                foreach (IRacingReindeer racingReindeer in reindeerRace)
                {
                    racingReindeer.Race1Second();
                }
            }

            //who wins & how far have they travelled?
            int    maxDistance         = 0;
            String winningReindeerName = "";

            foreach (IRacingReindeer racingReindeer in reindeerRace)
            {
                if (racingReindeer.DistanceTravelled > maxDistance)
                {
                    maxDistance         = racingReindeer.DistanceTravelled;
                    winningReindeerName = racingReindeer.Reindeer.Name;
                }
            }

            return($"{ winningReindeerName} is winning at {maxDistance} km!");
        }
Пример #13
0
        protected override String DoSolve(String[] input)
        {
            List <IPath <String> > paths = new List <IPath <String> >();

            //read the input
            foreach (String line in input)
            {
                Int32 distance = Convert.ToInt32(StringOps.SubStringPost(line, "=").Trim());

                String ends = StringOps.SubStringPre(line, "=").Trim();

                IPath <String> path = Factory.CreatePath(StringOps.SubStringPre(ends, "to").Trim(), StringOps.SubStringPost(ends, "to").Trim(), distance);
                paths.Add(path);
            }

            IPathCollection pathCollection = Factory.CreatePathCollection(paths);

            Int32 longestPath = pathCollection.LongestDistance();

            return($"The Shortest Route is { longestPath }.");
        }
Пример #14
0
        protected override String DoSolve(String[] input)
        {
            List <IRacingReindeerPoints> reindeerRace = new List <IRacingReindeerPoints>();

            //read the input
            foreach (String line in input)
            {
                String reindeerName = StringOps.SubStringPre(line, "can");
                int    flySpeed     = Convert.ToInt32(StringOps.SubStringPost(StringOps.SubStringPre(line, "km/s"), "fly").Trim());
                int    flyDuration  = Convert.ToInt32(StringOps.SubStringPost(StringOps.SubStringPre(line, "seconds,"), "km/s for").Trim());
                int    restDuration = Convert.ToInt32(StringOps.SubStringPost(StringOps.SubStringPre(line, "seconds."), "rest for").Trim());

                reindeerRace.Add(Factory.CreateRacingReindeerPoints(reindeerName, flySpeed, flyDuration, restDuration));
            }

            //Race!
            int currentSecond;
            int raceDurationSeconds = 2503;

            for (currentSecond = 0; currentSecond < raceDurationSeconds; currentSecond++)
            {
                foreach (IRacingReindeerPoints racingReindeer in reindeerRace)
                {
                    racingReindeer.Race1Second();
                }

                //Assign Points to the leader(s)
                //Find the most distance travelled
                int currentMaxDistance = 0;
                foreach (IRacingReindeerPoints racingReindeer in reindeerRace)
                {
                    if (racingReindeer.DistanceTravelled > currentMaxDistance)
                    {
                        currentMaxDistance = racingReindeer.DistanceTravelled;
                    }
                }

                //assign points to the reindeer with that maxdistance
                foreach (IRacingReindeerPoints racingReindeer in reindeerRace)
                {
                    if (racingReindeer.DistanceTravelled == currentMaxDistance)
                    {
                        racingReindeer.Points++;
                    }
                }
            }

            //who wins & how far have they travelled?
            int    maxPoints           = 0;
            String winningReindeerName = "";

            foreach (IRacingReindeerPoints racingReindeer in reindeerRace)
            {
                if (racingReindeer.Points > maxPoints)
                {
                    maxPoints           = racingReindeer.Points;
                    winningReindeerName = racingReindeer.Reindeer.Name;
                }
            }



            return($"{ winningReindeerName} is winning at {maxPoints} points!");
        }
Пример #15
0
 public static String SubStringPostAndPre(String input, String postDelimiter, String preDelimeter)
 {
     return(StringOps.SubStringPost(StringOps.SubStringPre(input, preDelimeter), postDelimiter));
 }