Exemple #1
0
        public void TestParser2(string input, int x, int y, Type type)
        {
            var seat = SeatParser.Parse2(input.Replace("|", Environment.NewLine))
                       .GetCoords(x, y);

            Assert.IsType(type, seat);
        }
Exemple #2
0
        public void SolvePuzzle2()
        {
            var input = new FileReader()
                        .GetResource("AdventOfCode2020.Tests.Day11.PuzzleInput.txt");

            var seats      = SeatParser.Parse2(input);
            var seatFinder = new SeatFinder();

            Map currentMap = null;
            var cont       = false;
            int count      = 0;

            do
            {
                var map = currentMap?.DeepCopy();
                currentMap = seatFinder.Execute2(currentMap ?? seats);

                if (map == null)
                {
                    cont = true;
                }
                else if (map.Compare(currentMap))
                {
                    cont = false;
                }
                count++;
            } while (cont);

            var spaces = currentMap !.GetAllSpaces().Count(x => x is OccupiedSeat);

            _testOutputHelper.WriteLine(spaces.ToString());
            _testOutputHelper.WriteLine(count.ToString());
        }
Exemple #3
0
        public void RulesTests(string input, int expectedOccupiedSeats)
        {
            var seats = SeatParser
                        .Parse2(input.Replace("|", Environment.NewLine));

            var seatFinder = new SeatFinder();
            var newMap     = seatFinder.Execute(seats);
            var space      = newMap.GetAllSpaces();

            Assert.Equal(expectedOccupiedSeats, space.Count(floor => floor is OccupiedSeat));
        }
        public async Task Update(AreaDto entity)
        {
            if (entity == null)
            {
                throw new ArgumentNullException();
            }

            if (entity.LayoutId <= 0)
            {
                throw new AreaException("LayoutId is invalid");
            }

            if (!IsDescriptionUnique(entity, false))
            {
                throw new AreaException("Area description isn't unique");
            }

            if (entity.SeatList == null || !entity.SeatList.Any())
            {
                throw new AreaException("Incorrect state of area. An area must have atleast one seat");
            }

            using (var transaction = CustomTransactionScope.GetTransactionScope())
            {
                var delete = await _context.AreaRepository.GetAsync(entity.Id);

                _context.AreaRepository.Delete(delete);
                var createNew = AreaParser.MapToArea(entity);
                _context.AreaRepository.Create(createNew);
                await _context.SaveAsync();

                foreach (var seat in entity.SeatList)
                {
                    seat.AreaId = createNew.Id;

                    if (!IsSeatUnique(seat, false))
                    {
                        throw new SeatException("Seat already exists");
                    }

                    var seatAdd = SeatParser.MapToSeat(seat);
                    _context.SeatRepository.Create(seatAdd);
                }
                await _context.SaveAsync();

                transaction.Complete();
            }
        }
Exemple #5
0
        public async Task Seat_is_unique(int row, int number, int areaId)
        {
            //Arrange
            var areaService = _container.Resolve <IStoreService <AreaDto, int> >();
            var context     = _container.Resolve <IWorkUnit>();
            var area        = await areaService.Get(areaId);

            var seats = await context.SeatRepository.FindByAsync(x => x.AreaId == areaId);

            var seat = new SeatDto {
                Row = row, Number = number, AreaId = areaId
            };

            area.SeatList.AddRange(seats.Select(x => SeatParser.MapToSeatDto(x)).ToList());
            area.SeatList.Add(seat);

            //Assert
            Assert.DoesNotThrowAsync(async() => await areaService.Update(area));
        }
Exemple #6
0
        public void TestParser()
        {
            var input = @"L.LL.LL.LL
LLLLLLL.LL
L.L.L..L..
LLLL.LL.LL
L.LL.LL.LL
L.LLLLL.LL
..L.L.....
LLLLLLLLLL
L.LLLLLL.L
L.LLLLL.LL";
            var seats = SeatParser.Parse2(input)
                        .GetAllSpaces();

            Assert.Equal(0, seats.Count(floor => floor is OccupiedSeat));
            Assert.Equal(29, seats.Count(floor => floor is Floor));
            Assert.Equal(71, seats.Count(floor => floor is EmptySeat));
        }
Exemple #7
0
        public void Example1()
        {
            var input = @"L.LL.LL.LL
LLLLLLL.LL
L.L.L..L..
LLLL.LL.LL
L.LL.LL.LL
L.LLLLL.LL
..L.L.....
LLLLLLLLLL
L.LLLLLL.L
L.LLLLL.LL";

            var seats      = SeatParser.Parse2(input);
            var seatFinder = new SeatFinder();

            Map currentMap = null;
            var cont       = false;

            do
            {
                var map = currentMap?.DeepCopy();
                currentMap = seatFinder.Execute(currentMap ?? seats);

                _testOutputHelper.WriteLine(currentMap?.Print() ?? "Map was null");

                if (map == null)
                {
                    cont = true;
                }
                else if (map.Compare(currentMap))
                {
                    cont = false;
                }
            } while (cont);

            var spaces = currentMap !.GetAllSpaces().Count(x => x is OccupiedSeat);

            Assert.Equal(37, spaces);
        }
Exemple #8
0
        public Task <VenueDto> GetFullModel(int id)
        {
            var data = (from venues in _context.VenueRepository.GetList()
                        join layotus in _context.LayoutRepository.GetList() on venues.Id equals layotus.VenueId
                        join areas in _context.AreaRepository.GetList() on layotus.Id equals areas.LayoutId
                        join seats in _context.SeatRepository.GetList() on areas.Id equals seats.AreaId
                        where venues.Id == id
                        select new { Venue = venues, Layout = layotus, Area = areas, Seat = seats }).ToList();

            if (!data.Any())
            {
                return(null);
            }

            var venue  = data.FirstOrDefault().Venue;
            var result = VenueParser.MapToVenueDto(venue);

            foreach (var row in data)
            {
                var layout = result.LayoutList.SingleOrDefault(x => x.Id == row.Layout.Id);
                //add layout if it isn't exist in a result
                if (layout is null)
                {
                    layout = LayoutParser.MapToLayoutDto(row.Layout);
                    result.LayoutList.Add(layout);
                }

                var area = layout.AreaList.SingleOrDefault(x => x.Id == row.Area.Id);
                //add area if it isn't exist in a result
                if (area is null)
                {
                    area = AreaParser.MapToAreaDto(row.Area);
                    layout.AreaList.Add(area);
                }

                area.SeatList.Add(SeatParser.MapToSeatDto(row.Seat));
            }

            return(Task.FromResult(result));
        }
Exemple #9
0
        public async Task Seat_is_unique_excpected_exception(int row, int number, int areaId)
        {
            //Arrange
            var areaService = _container.Resolve <IStoreService <AreaDto, int> >();
            var context     = _container.Resolve <IWorkUnit>();
            var area        = await areaService.Get(areaId);

            var seats = await context.SeatRepository.FindByAsync(x => x.AreaId == areaId);

            var seat = new SeatDto {
                Row = row, Number = number, AreaId = areaId
            };

            area.SeatList.AddRange(seats.Select(x => SeatParser.MapToSeatDto(x)).ToList());
            area.SeatList.Add(seat);

            //Act
            var exception = Assert.CatchAsync <SeatException>(async() => await areaService.Update(area));

            //Assert
            StringAssert.AreEqualIgnoringCase(exception.Message, "Seat already exists");
        }