예제 #1
0
        static void ImportData()
        {
            using (Stream stream = File.OpenRead(@"..\..\..\Day17_InputSample.txt"))
                using (StreamReader reader = new StreamReader(stream))
                {
                    string        s;
                    List <string> lines = new List <string>();

                    while ((s = reader.ReadLine()) != null)
                    {
                        lines.Add(s);
                    }

                    for (int x = 0; x < lines[0].Length; x++)
                    {
                        for (int y = 0; y < lines.Count(); y++)
                        {
                            Point <int> p = new Point <int>(x, y, 0);
                            bool        a = false;
                            if (lines[y][x] == '#')
                            {
                                a = true;
                            }

                            points.Add(new Day17Point(p, a));
                        }
                    }

                    foreach (Day17Point p in points)
                    {
                        for (int y = p.Point.y - 1; y <= p.Point.y + 1; y++)
                        {
                            for (int x = p.Point.x - 1; x <= p.Point.x + 1; x++)
                            {
                                try
                                {
                                    Day17Point dp = points.Single(s => s.Point.x == x && s.Point.y == y);
                                    p.Neighbors.Add(dp);
                                }

                                catch { }
                            }
                        }
                    }
                }
        }
예제 #2
0
        static void ProcessTurn2()
        {
            List <Day17Point> newPoints = new List <Day17Point>();
            Stopwatch         sw        = new Stopwatch();

            sw.Start();

            Parallel.ForEach(points, p =>
            {
                for (int z = p.Point.z - 1; z <= p.Point.z + 1; z++)
                {
                    for (int y = p.Point.y - 1; y <= p.Point.y + 1; y++)
                    {
                        for (int x = p.Point.x - 1; x <= p.Point.x + 1; x++)
                        {
                            for (int w = p.Point.w - 1; w <= p.Point.w + 1; w++)
                            {
                                if (w == p.Point.w && x == p.Point.x && y == p.Point.y && z == p.Point.z)
                                {
                                }

                                else
                                {
                                    Day17Point np = new Day17Point(false, x, y, z, w);

                                    if (!points.Contains(np) && !newPoints.Contains(np))
                                    {
                                        newPoints.Add(np);
                                    }
                                }
                            }
                        }
                    }
                }
            });

            points.AddRange(newPoints.Distinct().ToList());
            Console.Write($"{sw.ElapsedMilliseconds}ms   ");
            sw.Reset();
            sw.Start();

            Parallel.ForEach(points, p =>
            {
                int activeNeighbors = 0;
                for (int z = p.Point.z - 1; z <= p.Point.z + 1; z++)
                {
                    for (int y = p.Point.y - 1; y <= p.Point.y + 1; y++)
                    {
                        for (int x = p.Point.x - 1; x <= p.Point.x + 1; x++)
                        {
                            for (int w = p.Point.w - 1; w <= p.Point.w + 1; w++)
                            {
                                if (w == p.Point.w && x == p.Point.x && y == p.Point.y && z == p.Point.z)
                                {
                                }

                                else
                                {
                                    try
                                    {
                                        if (points.Single(s => s.Point.x == x && s.Point.y == y && s.Point.z == z && s.Point.w == w).Active)
                                        {
                                            activeNeighbors++;
                                        }
                                    }

                                    catch { }
                                }
                            }
                        }
                    }
                }

                if (p.Active && (activeNeighbors < 2 || activeNeighbors > 3))
                {
                    p.Swap = true;
                }

                if (!p.Active && activeNeighbors == 3)
                {
                    p.Swap = true;
                }
            });
            Console.Write($"{sw.ElapsedMilliseconds}ms   ");
            sw.Reset();
            sw.Start();

            Parallel.ForEach(points, p => p.Update());
            Console.Write($"{sw.ElapsedMilliseconds}ms\n");
            sw.Stop();
        }