Ejemplo n.º 1
0
        static void ApplyRuleSet2(SeatLayout previousLayout, SeatLayout currentLayout)
        {
            for (int row = 0; row < currentLayout.RowCount; row++)
            {
                for (int col = 0; col < currentLayout.ColumnCount; col++)
                {
                    if (previousLayout.IsFloor(row, col))
                    {
                        continue;
                    }
                    else
                    {
                        bool occupied   = previousLayout.IsOccupied(row, col);
                        int  withinView = previousLayout.CountWithinViewOccupied(row, col);

                        // If a seat is empty (L) and there are no visible
                        // occupied seats within view, the seat becomes occupied.
                        // The person can only see the first seat encountered in
                        // each direction.
                        if (!occupied && withinView == 0)
                        {
                            currentLayout.MarkOccupied(row, col);
                        }
                        // If a seat is occupied (#) and five or more seats
                        // within view are also occupied, the seat becomes empty.
                        // The person can only see the first seat encountered in
                        // each direction.
                        else if (occupied && withinView >= 5)
                        {
                            currentLayout.MarkEmpty(row, col);
                        }
                    }
                }
            }
        }
Ejemplo n.º 2
0
        static int GetStabilizedOccupiedCount(
            SeatLayout layout,
            Action <SeatLayout, SeatLayout> ApplyRules)
        {
            SeatLayout currentLayout = (SeatLayout)layout.Clone();

            // Console.WriteLine("Initial:");
            // Console.WriteLine("-----------------------------");
            // Console.WriteLine(currentLayout);
            // Console.WriteLine();

            // int iterations = 1;

            do
            {
                // Console.WriteLine($"Iteration: {++iterations}");

                SeatLayout lastLayout = currentLayout;
                currentLayout = (SeatLayout)currentLayout.Clone();
                ApplyRules(lastLayout, currentLayout);

                // Console.WriteLine("-----------------------------");
                // Console.WriteLine(currentLayout);
                // Console.WriteLine();
            } while (currentLayout.IsDirty);

            return(currentLayout.OccupiedSeatCount);
        }
Ejemplo n.º 3
0
        static void ApplyRuleSet1(SeatLayout previousLayout, SeatLayout currentLayout)
        {
            for (int row = 0; row < currentLayout.RowCount; row++)
            {
                for (int col = 0; col < currentLayout.ColumnCount; col++)
                {
                    if (previousLayout.IsFloor(row, col))
                    {
                        continue;
                    }
                    else
                    {
                        bool occupied = previousLayout.IsOccupied(row, col);
                        int  adjacent = previousLayout.CountAdjacentOccupied(row, col);

                        // If a seat is empty (L) and there are no occupied
                        // seats adjacent to it, the seat becomes occupied.
                        if (!occupied && adjacent == 0)
                        {
                            currentLayout.MarkOccupied(row, col);
                        }
                        // If a seat is occupied (#) and four or more seats
                        // adjacent to it are also occupied, the seat becomes empty.
                        else if (occupied && adjacent >= 4)
                        {
                            currentLayout.MarkEmpty(row, col);
                        }
                    }
                }
            }
        }
Ejemplo n.º 4
0
        static void Main(string[] args)
        {
            var lines  = File.ReadLines("input.txt");
            var layout = new SeatLayout(lines);

            Stopwatch watch = new();

            watch.Start();
            int stabilizedCount1 = GetStabilizedOccupiedCount(layout, ApplyRuleSet1);

            watch.Stop();
            Console.WriteLine($"Rule set 1 stabilized Count: {stabilizedCount1} - Elapsed: {watch.Elapsed}");

            watch.Reset();
            watch.Start();
            int stabilizedCount2 = GetStabilizedOccupiedCount(layout, ApplyRuleSet2);

            watch.Stop();
            Console.WriteLine($"Rule set 2 stabilized Count: {stabilizedCount2} - Elapsed: {watch.Elapsed}");
        }