Ejemplo n.º 1
0
        static int B(string[] input)
        {
            const int             MAX_GENERATION = 1_000_000_000;
            Dictionary <int, int> seenValues     = new Dictionary <int, int>();


            var area = CollectionArea.FromInput(input);

            while (area.Generation < MAX_GENERATION)
            {
                area.Step();
                if (area.Generation > 1000)
                {
                    if (seenValues.ContainsKey(area.Value()))
                    {
                        var gen       = area.Generation;
                        var prevGen   = seenValues[area.Value()];
                        var gendiff   = gen - prevGen;
                        var skipSteps = (MAX_GENERATION - area.Generation) / gendiff;
                        area.SkipForward(skipSteps * gendiff);
                    }
                    else
                    {
                        seenValues[area.Value()] = area.Generation;
                    }
                }
            }
            return(area.Value());
        }
Ejemplo n.º 2
0
        public static CollectionArea FromInput(string[] input)
        {
            var area = new CollectionArea(input.Length)
            {
                Generation = 0,
            };

            for (int row = 1; row < input.Length + 1; row++)
            {
                for (int column = 1; column < input.Length + 1; column++)
                {
                    area.acres[row, column] = parseAcre(input[row - 1][column - 1]);
                }
            }


            return(area);

            acre parseAcre(char c)
            {
                switch (c)
                {
                case '.': return(acre.open);

                case '|': return(acre.wood);

                case '#': return(acre.lumberyard);

                default:
                    throw new Exception("parse error");
                }
            }
        }
Ejemplo n.º 3
0
        static int A(string[] input, bool debug = false)
        {
            var area = CollectionArea.FromInput(input);

            if (debug)
            {
                Console.WriteLine(area.ToString());
            }
            for (int i = 0; i < 10; i++)
            {
                area.Step();
                if (debug)
                {
                    Console.WriteLine(area.ToString());
                }
            }
            return(area.Value());
        }