示例#1
0
        static Inhabitants CalculateTotals(char[,] m)
        {
            Inhabitants totals = new Inhabitants();
            int         width  = m.GetLength(0);
            int         height = m.GetLength(1);

            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    char c = m[x, y];
                    if (c == open)
                    {
                        totals.open++;
                    }
                    else if (c == tree)
                    {
                        totals.tree++;
                    }
                    else if (c == lumber)
                    {
                        totals.lumber++;
                    }
                }
            }
            return(totals);
        }
示例#2
0
        static void PartB()
        {
            map = ReadInput();
            //PrintMap(map);
            //Console.WriteLine();
            int  iMax       = 1000000000;
            bool done       = false;
            int  iter       = 0;
            int  indexFound = -1;

            for (int i = 0; (i < iMax) && !done; i++)
            {
                //Console.Clear();
                //PrintMap(map);
                //Thread.Sleep(100);
                if ((indexFound = PreviousEqualMap()) > 0)
                {
                    done = true;
                    iter = i;
                }
                else
                {
                    IterateMap();
                }
            }
            int         offs      = indexFound;
            int         cycle     = iter - indexFound;
            int         cOffs     = (iMax - offs) % cycle;
            int         iMaxIndex = offs + cOffs;
            Inhabitants totals    = CalculateTotals(maps[iMaxIndex]);
            int         value     = totals.tree * totals.lumber;

            Console.WriteLine("Part B: Result is " + value + " (took " + iter + " iterations).");
        }
示例#3
0
        static void IterateMap()
        {
            char[,] nextMap = (char[, ])map.Clone();
            int      width  = map.GetLength(0);
            int      height = map.GetLength(1);
            Position p      = new Position();

            for (p.y = 0; p.y < height; p.y++)
            {
                for (p.x = 0; p.x < width; p.x++)
                {
                    Inhabitants n = CalculateNeighbors(p);
                    if (map[p.x, p.y] == open)
                    {
                        nextMap[p.x, p.y] = (n.tree >= 3) ? tree : open;
                    }
                    else if (map[p.x, p.y] == tree)
                    {
                        nextMap[p.x, p.y] = (n.lumber >= 3) ? lumber : tree;
                    }
                    else if (map[p.x, p.y] == lumber)
                    {
                        nextMap[p.x, p.y] = ((n.lumber >= 1) && (n.tree >= 1)) ? lumber : open;
                    }
                }
            }
            map = nextMap;
        }
示例#4
0
        static Inhabitants CalculateNeighbors(Position p)
        {
            Inhabitants neighbors = new Inhabitants();
            int         xMax      = map.GetLength(0) - 1;
            int         yMax      = map.GetLength(1) - 1;

            for (int x = p.x - 1; x <= p.x + 1; x++)
            {
                if ((x >= 0) && (x <= xMax))
                {
                    if (p.y - 1 >= 0)
                    {
                        UpdateNeighbors(map[x, p.y - 1], ref neighbors);
                    }
                    if (p.y + 1 <= yMax)
                    {
                        UpdateNeighbors(map[x, p.y + 1], ref neighbors);
                    }
                }
            }
            if (p.x - 1 >= 0)
            {
                UpdateNeighbors(map[p.x - 1, p.y], ref neighbors);
            }
            if (p.x + 1 <= xMax)
            {
                UpdateNeighbors(map[p.x + 1, p.y], ref neighbors);
            }
            return(neighbors);
        }
示例#5
0
 static void UpdateNeighbors(char c, ref Inhabitants neighbors)
 {
     if (c == open)
     {
         neighbors.open++;
     }
     else if (c == tree)
     {
         neighbors.tree++;
     }
     else if (c == lumber)
     {
         neighbors.lumber++;
     }
 }
示例#6
0
        static void PartA()
        {
            map = ReadInput();
            //PrintMap(map);
            //Console.WriteLine();
            int iMax = 10;

            for (int i = 0; i < iMax; i++)
            {
                IterateMap();
            }
            //PrintMap(map);
            Inhabitants totals = CalculateTotals(map);
            int         value  = totals.tree * totals.lumber;

            Console.WriteLine("Part A: Result is " + value + ".");
        }
示例#7
0
        static int PreviousEqualMap()
        {
            Inhabitants a = CalculateTotals(map);

            if (mapTotals.Count > 0)
            {
                for (int i = 0; i < mapTotals.Count; i++)
                {
                    if (a.Equals(mapTotals[i]))
                    {
                        if (MapsEqual(map, maps[i]))
                        {
                            return(i);
                        }
                    }
                }
            }
            mapTotals.Add(a);
            maps.Add(map);
            return(-1);
        }
示例#8
0
 public bool Equals(Inhabitants i)
 {
     return((open == i.open) && (tree == i.tree) && (lumber == i.lumber));
 }