Exemplo n.º 1
0
        public static void Day12a(long numGenerations = 1)
        {
            string        initialState = String.Empty;
            List <string> steps        = new List <string>();

            using (Stream stream = thisExe.GetManifestResourceStream("AdventOfCode2018._data.AdventOfCode_Day12.txt"))
                using (StreamReader reader = new StreamReader(stream))
                {
                    steps = StreamFunctions.EnumerateLines(reader).ToList();
                }

            if (steps[0].StartsWith("initial state: "))
            {
                initialState = steps[0].Remove(0, 15);
                steps.Remove(steps[0]);
            }

            if (steps[0] == "")
            {
                steps.Remove(steps[0]);
            }

            // TestStuff

            /*
             * initialState = "#..#.#..##......###...###";
             * steps.Clear();
             * steps = new List<string>( new string[] { "...## => #" ,
             *                                       "..#.. => #" ,
             *                                       ".#... => #" ,
             *                                       ".#.#. => #" ,
             *                                       ".#.## => #" ,
             *                                       ".##.. => #" ,
             *                                       ".#### => #" ,
             *                                       "#.#.# => #" ,
             *                                       "#.### => #" ,
             *                                       "##.#. => #" ,
             *                                       "##.## => #" ,
             *                                       "###.. => #" ,
             *                                       "###.# => #" ,
             *                                       "####. => #" } );
             */
            Day12Manager _mgr = new Day12Manager(initialState, steps, numGenerations);

            _mgr.RunWork();


            // 3547 is too low


            #region Support Methods

            #endregion
        }
Exemplo n.º 2
0
        static public void Day2a()
        {
            string[] lines;

            using (Stream stream = thisExe.GetManifestResourceStream("AdventOfCode2018._data.AdventOfCode_Day2.txt"))
                using (StreamReader reader = new StreamReader(stream))
                {
                    lines = StreamFunctions.EnumerateLines(reader).ToArray();
                }

            int count2 = 0;
            int count3 = 0;


            foreach (string s in lines)
            {
                bool got2s = false;
                bool got3s = false;

                char[] carr = s.ToCharArray();
                Dictionary <char, int> counts = new Dictionary <char, int>();

                foreach (char c in carr)
                {
                    if (counts.ContainsKey(c))
                    {
                        counts[c]++;
                    }
                    else
                    {
                        counts.Add(c, 1);
                    }
                }

                foreach (KeyValuePair <char, int> kvp in counts)
                {
                    if (kvp.Value == 3 && !got3s)
                    {
                        count3++;
                        got3s = true;
                    }
                    else if (kvp.Value == 2 && !got2s)
                    {
                        count2++;
                        got2s = true;
                    }
                }
            }

            MessageBox.Show("3-sets: " + count3.ToString() + "\n" + "2-sets: " + count2.ToString() + "\n\n" +
                            "Checksum: " + (count3 * count2).ToString());
        }
Exemplo n.º 3
0
        // Day1a is setup for Day1b, as I hadn't yet decided to keep separate functions for each.
        static public void Day1a()
        {
            string[] lines;

            using (Stream stream = thisExe.GetManifestResourceStream("AdventOfCode2018._data.AdventOfCode_Day1.txt"))
                using (StreamReader reader = new StreamReader(stream))
                {
                    lines = StreamFunctions.EnumerateLines(reader).ToArray();
                }

            int  value                      = 0;
            bool foundFirstDouble           = false;
            int  iteration                  = 0;
            Dictionary <int, int> frequency = new Dictionary <int, int>();

            //string[] lines = File.ReadAllLines("./_data/AdventOfCode_Day1.txt");

            if (frequency.ContainsKey(value))
            {
                frequency[value]++;
            }
            else
            {
                frequency.Add(value, 1);
            }

            while (foundFirstDouble == false)
            {
                foreach (string s in lines)
                {
                    iteration++;
                    string ss        = s.Trim();
                    string _operator = ss.Substring(0, 1);
                    int    _value    = Convert.ToInt32(ss.Substring(1, ss.Length - 1));

                    if (_operator == "-")
                    {
                        value = value - _value;
                    }
                    else if (_operator == "+")
                    {
                        value = value + _value;
                    }

                    if (frequency.ContainsKey(value))
                    {
                        frequency[value]++;
                    }
                    else
                    {
                        frequency.Add(value, 1);
                    }

                    if (frequency[value] > 1 && foundFirstDouble == false)
                    {
                        MessageBox.Show("First value to hit a frequency of two is: " + value.ToString() + " at iteration: " + iteration.ToString());
                        foundFirstDouble = true;
                        break;
                    }
                }
            }

            MessageBox.Show("Total value of file: " + value.ToString() + " after " + iteration.ToString() + " iterations.");
        }
Exemplo n.º 4
0
        static public void Day2b()
        {
            string[] lines;
            using (Stream stream = thisExe.GetManifestResourceStream("AdventOfCode2018._data.AdventOfCode_Day2.txt"))
                using (StreamReader reader = new StreamReader(stream))
                {
                    lines = StreamFunctions.EnumerateLines(reader).ToArray();
                }

            Stopwatch sw         = new Stopwatch();
            string    match1     = "";
            string    match2     = "";
            int       placevalue = 0;
            int       iteration  = 0;
            bool      found      = false;
            //lines = lines.OrderBy(a => a).ToArray();
            List <string> comparelines = new List <string>(lines);

            while (iteration < lines.Count() && !found)
            {
                sw.Start();
                foreach (string s in lines)
                {
                    comparelines.Remove(s);
                    string delta;

                    foreach (string comparer in comparelines)
                    {
                        iteration++;
                        delta = "";
                        if (match1 == "")
                        {
                            for (int i = 0; i < 26; i++)
                            {
                                if (comparer[i] != s[i])
                                {
                                    delta     += comparer[i];
                                    placevalue = i;
                                }
                                if (delta.Length > 1)
                                {
                                    break;
                                }
                            }

                            if (delta.Length == 1)
                            {
                                match1 = s;
                                match2 = comparer;
                                found  = true;
                                break;
                            }
                        }

                        if (found)
                        {
                            break;
                        }
                    }
                    if (found)
                    {
                        break;
                    }
                }
                sw.Stop();
            }

            string answer = match1.Remove(placevalue, 1);

            MessageBox.Show("Matched lines are: " + match1 + " & " + match2 + "\n\n" +
                            "Answer is: " + answer + "\n" + "Found in " + sw.ElapsedMilliseconds +
                            " ms. (" + sw.ElapsedTicks + " ticks)" + "\n" + "Found in " + iteration + " iterations.");
        }
Exemplo n.º 5
0
        // Day 2bThreaded is an attempt to thread the work and see if it would run faster.  It does, but only marginally since the data set is relatively small.
        static public void Day2bThreaded()
        {
            string[] lines;
            using (Stream stream = thisExe.GetManifestResourceStream("AdventOfCode2018._data.AdventOfCode_Day2.txt"))
                using (StreamReader reader = new StreamReader(stream))
                {
                    lines = StreamFunctions.EnumerateLines(reader).ToArray();
                }

            Stopwatch sw         = new Stopwatch();
            string    match1     = "";
            string    match2     = "";
            int       placevalue = 0;
            int       iteration  = 0;
            bool      found      = false;
            //lines = lines.OrderBy(a => a).ToArray();
            ConcurrentQueue <string> comparelines = new ConcurrentQueue <string>(lines);

            while (!found)
            {
                sw.Start();

                Parallel.ForEach(comparelines, (i, loopState) =>
                {
                    string s;
                    comparelines.TryDequeue(out s);
                    string delta;

                    foreach (string comparer in comparelines)
                    {
                        iteration++;
                        delta = "";
                        if (match1 == "")
                        {
                            for (int _i = 0; _i < 26; _i++)
                            {
                                if (comparer[_i] != s[_i])
                                {
                                    delta     += comparer[_i];
                                    placevalue = _i;
                                }
                                if (delta.Length > 1)
                                {
                                    break;
                                }
                            }

                            if (delta.Length == 1)
                            {
                                match1 = s;
                                match2 = comparer;
                                found  = true;
                                loopState.Stop();
                            }
                        }

                        if (found)
                        {
                            loopState.Stop();
                        }
                    }
                    if (found)
                    {
                        loopState.Stop();
                    }
                });

                sw.Stop();
            }

            string answer = match1.Remove(placevalue, 1);

            MessageBox.Show("Matched lines are: " + match1 + " & " + match2 + "\n\n" +
                            "Answer is: " + answer + "\n" + "Found in " + sw.ElapsedMilliseconds +
                            " ms. (" + sw.ElapsedTicks + " ticks)" + "\n" + "Found in " + iteration + " iterations.");
        }
Exemplo n.º 6
0
        // Day10b is a natural solution of Day10a in this setup.  Actually, I'm not sure how you could solve a without also already knowing b.  Hmmm...
        public static string Day10a(int updates = 1, int by = 1, bool invert = false)
        {
            string[] lines;
            List <Day10PointOfLight> points = new List <Day10PointOfLight>();

            using (Stream stream = thisExe.GetManifestResourceStream("AdventOfCode2018._data.AdventOfCode_Day10.txt"))
                using (StreamReader reader = new StreamReader(stream))
                {
                    lines = StreamFunctions.EnumerateLines(reader).ToArray();
                }

            foreach (string line in lines)
            {
                Day10PointOfLight p = new Day10PointOfLight(line);
                points.Add(p);
            }

            Day10PointManager _mgr = new Day10PointManager(points);

            Stopwatch sw = new Stopwatch();

            points = _mgr.RunWork(updates, by);

            char[,] starArray = _mgr.GetStarArray();

            StringBuilder sb = new StringBuilder();

            if (invert)
            {
                for (int x = 0; x < starArray.GetLength(0); x++)
                {
                    for (int y = 0; y < starArray.GetLength(1); y++)
                    {
                        if (starArray[x, y] == '*')
                        {
                            sb.Append("*");
                        }
                        else
                        {
                            sb.Append(" ");
                        }
                    }
                    sb.Append(Environment.NewLine);
                }
            }

            else
            {
                for (int y = 0; y < starArray.GetLength(1); y++)
                {
                    for (int x = 0; x < starArray.GetLength(0); x++)
                    {
                        if (starArray[x, y] == '*')
                        {
                            sb.Append("*");
                        }
                        else
                        {
                            sb.Append(" ");
                        }
                    }
                    sb.Append(Environment.NewLine);
                }
            }
            //_mgr.GetScatterData();

            return(sb.ToString());

            #region Support Methods

            #endregion
        }