Ejemplo n.º 1
0
        public static Day03Result Part2()
        {
            const string   path  = Helpers.inputPath + @"\day03\input.txt";
            IList <string> input = Helpers.GetFileAsLines(path);

            IList <(int, int)> slopes = new List <(int, int)>
            {
                (1, 1),
                (3, 1),
                (5, 1),
                (7, 1),
                (1, 2)
            };

            IList <SlopeResult> slopeResults = slopes.Select(s => new SlopeResult
            {
                Dx       = s.Item1,
                Dy       = s.Item2,
                TreesHit = GetTreesHit(input, s.Item1, s.Item2)
            }).ToList();

            return(new Day03Result
            {
                Answer = slopeResults.Aggregate((long)1, (a, v) => a * v.TreesHit),
                Details = new Day03Details
                {
                    Height = input.Count,
                    Width = input[0].Length,
                    SlopeResults = slopeResults
                }
            });
        }
Ejemplo n.º 2
0
        //######################################################################
        // Part 1 + 2 Solutions
        //######################################################################

        public static int Part1()
        {
            const string   path   = Helpers.inputPath + @"\day13\input.txt";
            IList <string> inputs = Helpers.GetFileAsLines(path);

            int         arriveTime = int.Parse(inputs[0]);
            Regex       rx         = new Regex("[0-9]+");
            IList <int> ids        = rx.Matches(inputs[1]).Select(m => int.Parse(m.Value)).ToList();

            int minId   = -1;
            int minWait = -1;

            foreach (int id in ids)
            {
                int intervalWait = id - (arriveTime % id);
                if (intervalWait < minWait || minWait < 0)
                {
                    minWait = intervalWait;
                    minId   = id;
                }
            }

            return(minId * minWait);
        }
Ejemplo n.º 3
0
        public static long Part2()
        {
            const string   path   = Helpers.inputPath + @"\day13\input.txt";
            IList <string> inputs = Helpers.GetFileAsLines(path);

            Regex       rx  = new Regex("[0-9]+|x");
            IList <int> ids = rx.Matches(inputs[1]).Select(m => {
                if (m.Value.Equals("x"))
                {
                    return(1);
                }
                else
                {
                    return(int.Parse(m.Value));
                }
            }).ToList();

            int  largestIndex  = ids.IndexOf(ids.Max());
            long t             = -largestIndex;
            long multipleToAdd = ids[largestIndex];

            while (t <= 0)
            {
                t += multipleToAdd;
            }

            long tDebug = 0;

            IList <int> idIndices = ids
                                    .Select((id, index) => { if (id > 1)
                                                             {
                                                                 return(index);
                                                             }
                                                             else
                                                             {
                                                                 return(-1);
                                                             } })
                                    .Where(i => i >= 0)
                                    .ToList();

            // whether this index is considered in the multipleToAdd variable
            IDictionary <int, bool> lockedIn = new Dictionary <int, bool>();

            foreach (int i in idIndices)
            {
                lockedIn.Add(i, false);
            }
            lockedIn[largestIndex] = true;
            while (true)
            {
                foreach (int i in idIndices)
                {
                    if ((t + i) % ids[i] == 0 && !lockedIn[i])
                    {
                        // MULTIPLY THE NUMBER TO ADD BY ids[i] ONCE ids[i] WORKS WITH THE CURRENT t VALUE
                        multipleToAdd *= ids[i];
                        lockedIn[i]    = true;
                        if (lockedIn.Values.Aggregate(true, (acc, val) => acc & val))
                        {
                            return(t);
                        }
                    }
                }
                t += multipleToAdd;
            }
        }