Example #1
0
        public void RunRound()
        {
            char[,] newMap = new char[SeatMap.GetLength(0), SeatMap.GetLength(1)];

            for (int i = 0; i < CurrentMap.GetLength(0); i++)
            {
                for (int j = 0; j < CurrentMap.GetLength(1); j++)
                {
                    char newState = CurrentMap[i, j] switch
                    {
                        EmptySeat
                        =>
                        Rules.GetNumOccupiedSeats(CurrentMap, i, j) == 0
                                ? OccupiedSeat : EmptySeat,
                        OccupiedSeat
                        =>
                        Rules.GetNumOccupiedSeats(CurrentMap, i, j) < Rules.GetOccupiedThreshold()
                                ? OccupiedSeat : EmptySeat,
                        _ => CurrentMap[i, j]
                    };

                    newMap[i, j] = newState;
                }
            }

            Array.Copy(newMap, 0, CurrentMap, 0, newMap.Length);
        }
    }
        private static SeatMap ProcessRound1(SeatMap seatMap, out bool hasChanged)
        {
            var result = new SeatMap(seatMap.NumCols, seatMap.NumRows);

            hasChanged = false;

            for (var row = 0; row < seatMap.NumRows; row++)
            {
                for (var col = 0; col < seatMap.NumCols; col++)
                {
                    var slot          = seatMap.Slots[row][col];
                    var adjacentSlots = seatMap.GetAdjacentSlots(row, col);
                    if (slot == MapSlot.Empty && adjacentSlots.All(s => s != MapSlot.Occupied))
                    {
                        slot       = MapSlot.Occupied;
                        hasChanged = true;
                    }
                    else if (slot == MapSlot.Occupied && adjacentSlots.Count(s => s == MapSlot.Occupied) >= 4)
                    {
                        slot       = MapSlot.Empty;
                        hasChanged = true;
                    }

                    result.Slots[row][col] = slot;
                }
            }

            return(result);
        }
Example #3
0
        public static void Part2()
        {
            var inputStrings = File.ReadAllLines(inputFilePath);
            //var inputStrings = testInput;


            var seatMap = new SeatMap(inputStrings);

            Console.WriteLine($"Original:\n{seatMap}");
            var stepsTillStabilize = 0;

            while (true)
            {
                var changedSeats = seatMap.Step2();
                stepsTillStabilize++;
                //Console.WriteLine($"\nChanges: {changedSeats}\n{seatMap}");
                if (changedSeats == 0)
                {
                    break;
                }
            }
            Console.WriteLine($"\nSteps to Stabilize = {stepsTillStabilize}");
            var occupied = seatMap.Count('#');

            Console.WriteLine($"\nSeats Occupied = {occupied}");
        }
        private static SeatMap ReadSeatMap(List <string> lines)
        {
            var seatMap = new SeatMap(lines.First().Length, lines.Count);

            for (var row = 0; row < lines.Count; row++)
            {
                var line = lines[row];
                for (var col = 0; col < line.Length; col++)
                {
                    MapSlot slot;
                    var     character = line[col];
                    if (character == '#')
                    {
                        slot = MapSlot.Occupied;
                    }
                    else if (character == 'L')
                    {
                        slot = MapSlot.Empty;
                    }
                    else
                    {
                        slot = MapSlot.Floor;
                    }
                    seatMap.Slots[row][col] = slot;
                }
            }

            return(seatMap);
        }
Example #5
0
        public void Sample1Test()
        {
            var map = "L.LL.LL.LL".ParseSeatMap();

            var c = SeatMap.EmptyRule(map.Map, 0, 0);

            c.Should().Be(SeatMap.Occupied);
            c = SeatMap.EmptyRule(map.Map, 1, 0);
            c.Should().Be(SeatMap.Floor);
            c = SeatMap.EmptyRule(map.Map, 2, 0);
            c.Should().Be(SeatMap.Occupied);
            c = SeatMap.EmptyRule(map.Map, 3, 0);
            c.Should().Be(SeatMap.Occupied);
            c = SeatMap.EmptyRule(map.Map, 4, 0);
            c.Should().Be(SeatMap.Floor);
            c = SeatMap.EmptyRule(map.Map, 5, 0);
            c.Should().Be(SeatMap.Occupied);
            c = SeatMap.EmptyRule(map.Map, 6, 0);
            c.Should().Be(SeatMap.Occupied);
            c = SeatMap.EmptyRule(map.Map, 7, 0);
            c.Should().Be(SeatMap.Floor);
            c = SeatMap.EmptyRule(map.Map, 8, 0);
            c.Should().Be(SeatMap.Occupied);
            c = SeatMap.EmptyRule(map.Map, 9, 0);
            c.Should().Be(SeatMap.Occupied);

            map.ApplyRules();
            map.Map.Should().BeEquivalentTo(new char[][] { new char[] { '#', '.', '#', '#', '.', '#', '#', '.', '#', '#' } });
        }
Example #6
0
        public static void Solve()
        {
            var rowRoot = new TreeNode {
                LowerLimit = 0, UpperLimit = 127
            };

            rowRoot.BuildChildren();

            var colRoot = new TreeNode {
                LowerLimit = 0, UpperLimit = 7
            };

            colRoot.BuildChildren();

            var    file = new StreamReader(@"/Users/rbakken/RiderProjects/AdventOfCode/AdventOfCode/Day5/day_5.txt");
            string line;
            var    seatMap = new SeatMap();

            while ((line = file.ReadLine()) != null)
            {
                char[]   rowStr = line.Trim().Substring(0, 7).ToCharArray();
                char[]   colStr = line.Trim().Substring(7).ToCharArray();
                TreeNode row    = rowRoot;
                TreeNode col    = colRoot;
                int      rowNum = GetRow(row, rowStr);
                int      colNum = GetColumn(col, colStr);

                seatMap.MarkOccupied(rowNum, colNum);
            }

            seatMap.PrintSeatMap();
            Console.WriteLine($"Seat ID: {seatMap.FindSeatId()}");
        }
Example #7
0
        public void Example2()
        {
            var map0    = new SeatMap(Step0Txt);
            var map1    = new SeatMap(Step1_2Txt);
            var map2    = new SeatMap(Step2_2Txt);
            var map3    = new SeatMap(Step3_2Txt);
            var map4    = new SeatMap(Step4_2Txt);
            var map5    = new SeatMap(Step5_2Txt);
            var map6    = new SeatMap(Step6_2Txt);
            var incMap1 = new SeatMap(map0, true);
            var incMap2 = new SeatMap(incMap1, true);
            var incMap3 = new SeatMap(incMap2, true);
            var incMap4 = new SeatMap(incMap3, true);
            var incMap5 = new SeatMap(incMap4, true);
            var incMap6 = new SeatMap(incMap5, true);
            var incMap7 = new SeatMap(incMap6, true);

            Assert.AreEqual(map1, incMap1);
            Assert.AreEqual(map2, incMap2);
            Assert.AreEqual(map3, incMap3);
            Assert.AreEqual(map4, incMap4);
            Assert.AreEqual(map5, incMap5);
            Assert.AreEqual(map6, incMap6);
            Assert.AreEqual(map6, incMap7);
        }
Example #8
0
        public SeatMap GetModel()
        {
            var model = new SeatMap();

            CommonMethods.CopyObjectProperties(this, model);

            return(model);
        }
Example #9
0
        public void TestEquals()
        {
            var map1 = "L.LL.LL.LL\nLLLLLLL.LL".ParseSeatMap();
            var map2 = "L.LL.LL.LL\nLLLLLLL.LL".ParseSeatMap();
            var map3 = "L.LL.LL.LL\nLLLLLLL.L#".ParseSeatMap();

            SeatMap.AreEqual(map1.Map, map2.Map).Should().Be(true);
            SeatMap.AreEqual(map1.Map, map3.Map).Should().Be(false);
        }
Example #10
0
        public static void Run()
        {
            var inputMap   = new SeatMap(File.ReadAllText("day11/input.txt"));
            var stabilized = UntilStabilized(inputMap);

            Console.WriteLine(stabilized.Seats.Count(s => s == SeatState.Occupied));
            var stabilizedNew = UntilStabilized(inputMap, true);

            Console.WriteLine(stabilizedNew.Seats.Count(s => s == SeatState.Occupied));
        }
Example #11
0
        int PartOne(string input)
        {
            int neighborThreshold = 4;

            var lines          = input.Split("\r\n");
            var seatIndices    = GetSeatIndices(input);
            var emptySeatMap   = new SeatMap(lines[0].Length, lines.Count(), seatIndices);
            var fullSeatMap    = new SeatMap(emptySeatMap._xs, emptySeatMap._ys, seatIndices, true);
            var steadyStateMap = FindSteadyState(emptySeatMap, fullSeatMap, GetNeighboursAdjacent, neighborThreshold);

            return(steadyStateMap._occupied.Count(c => c));
        }
Example #12
0
        public SeatMapModel CreateViewModel(SeatMap model)
        {
            SeatMapModel viewModel = null;

            if (model != null)
            {
                viewModel = new SeatMapModel();
                CommonMethods.CopyObjectProperties(model, viewModel);
            }

            return(viewModel);
        }
Example #13
0
        public static SeatMap UntilStabilized(SeatMap map, bool newRules = false)
        {
            var history = new HashSet <SeatMap>();

            while (true)
            {
                var newMap = new SeatMap(map, newRules);
                if (!history.Add(newMap))
                {
                    return(map);
                }
                map = newMap;
            }
        }
Example #14
0
        public SeatMap GetSeatMap(List <FlightSeatMap> flightSeatMapValues, List <SellableSeat> sellableSeatValues, List <SeatMapItemType> seatMapItemTypes)
        {
            this.SellableSeats    = sellableSeatValues;
            this.SeatMapItemTypes = seatMapItemTypes;
            var seats      = this.MapSeats(flightSeatMapValues);
            var cabinTypes = seats.Select(x => x.CabinType).Where(x => !string.IsNullOrEmpty(x)).Distinct().ToList();
            var cabinList  = cabinTypes.Select(cabinType => this.MapCabin(cabinType, seats)).ToList();
            var seatMap    = new SeatMap
            {
                Description = flightSeatMapValues.FirstOrDefault()?.SeatMap
            };

            seatMap.Cabins.AddRange(cabinList);
            seatMap.NumberOfCabins = cabinList.Count;
            return(seatMap);
        }
Example #15
0
        public int CountAllOccupiedSeats()
        {
            int occupiedChairs = 0;

            for (int i = 0; i < SeatMap.GetLength(0); i++)
            {
                for (int j = 0; j < SeatMap.GetLength(1); j++)
                {
                    if (CurrentMap[i, j] == OccupiedSeat)
                    {
                        occupiedChairs++;
                    }
                }
            }
            return(occupiedChairs);
        }
Example #16
0
        public void Example1()
        {
            var map0    = new SeatMap(Step0Txt);
            var map1    = new SeatMap(Step1Txt);
            var map2    = new SeatMap(Step2Txt);
            var map3    = new SeatMap(Step3Txt);
            var map4    = new SeatMap(Step4Txt);
            var map5    = new SeatMap(Step5Txt);
            var incMap1 = new SeatMap(map0);
            var incMap2 = new SeatMap(incMap1);
            var incMap3 = new SeatMap(incMap2);
            var incMap4 = new SeatMap(incMap3);
            var incMap5 = new SeatMap(incMap4);
            var incMap6 = new SeatMap(incMap5);

            Assert.AreEqual(map1, incMap1);
            Assert.AreEqual(map2, incMap2);
            Assert.AreEqual(map3, incMap3);
            Assert.AreEqual(map4, incMap4);
            Assert.AreEqual(map5, incMap5);
            Assert.AreEqual(map5, incMap6);
        }