コード例 #1
0
        public static int GetScore(BookScanning bookScanning, BookScanningOutput bookScanningOutput)
        {
            scannedBooks.Clear();
            startingDay = 0;
            int score = 0;

            for (int i = 0; i < bookScanningOutput.NumberOfScannedLibraries; i++)
            {
                libraryId    = bookScanningOutput.ScannedLibraries[i].Item1;
                startingDay += bookScanning.Libraries[libraryId].SigningTime;
                booksPerDay  = bookScanning.Libraries[libraryId].BooksShippedPerDay;
                var enumerator = bookScanningOutput.ScannedLibraries[i].Item2.GetEnumerator();
                enumerator.MoveNext();

                bool valid = true;

                for (int j = 0; j < bookScanning.NumberOfDays - startingDay; j++)
                {
                    if (!valid)
                    {
                        break;
                    }

                    for (int k = 0; k < booksPerDay; k++)
                    {
                        if (!valid)
                        {
                            break;
                        }

                        int book = enumerator.Current;
                        if (!scannedBooks.Contains(book))
                        {
                            scannedBooks.Add(book);
                        }
                        if (!enumerator.MoveNext())
                        {
                            valid = false;
                        }
                    }
                }
            }

            foreach (var book in scannedBooks)
            {
                score += bookScanning.BookScores[book];
            }
            return(score);
        }
コード例 #2
0
        public BookScanning ReadFromFile(string levelName)
        {
            var model     = new BookScanning();
            var inputPath = Directory.GetParent(Environment.CurrentDirectory).Parent.Parent.FullName + "\\Data\\Input\\";

            var          fullPath = inputPath + levelName;
            StreamReader sr       = new StreamReader(fullPath);
            var          allText  = sr.ReadToEnd();
            var          lines    = allText.Split('\n');

            var firstLine = lines[0].Split(" ");

            //read the first line
            model.NumberOfBooks     = int.Parse(firstLine[0]);
            model.NumberOfLibraries = int.Parse(firstLine[1]);
            model.NumberOfDays      = int.Parse(firstLine[2]);

            //read the second line
            var secondLine = lines[1].Split(" ");

            foreach (var word in secondLine)
            {
                model.BookScores.Add(int.Parse(word));
            }

            for (int i = 2; i < (model.NumberOfLibraries + 2); i++)
            {
                var library      = new Library();
                var libraryLine1 = lines[(i - 1) * 2].Split(" ");
                var libraryLine2 = lines[(i - 1) * 2 + 1].Split(" ");

                library.NumberOfBooks      = int.Parse(libraryLine1[0]);
                library.SigningTime        = int.Parse(libraryLine1[1]);
                library.BooksShippedPerDay = int.Parse(libraryLine1[2]);
                library.Index = model.Libraries.Count;

                libraryLine2.ToList().ForEach(word => library.Books.Add(int.Parse(word)));

                model.Libraries.Add(library);
            }

            return(model);
        }
コード例 #3
0
        public BookScanningOutput Solve(BookScanning model)
        {
            var librariesOrderedByOutput = model.Libraries
                                           .OrderByDescending(l =>
            {
                var bookScore    = l.Books.Sum(b => model.BookScores[b]);
                var avgBookScore = bookScore / l.Books.Count;

                var outputPotential = avgBookScore * l.BooksShippedPerDay;
                var availableTime   = model.NumberOfDays - l.SigningTime;

                return(outputPotential * availableTime);
            }).ToList();

            var usedBooks       = new List <int>();
            var signedLibraries = new List <LibraryData>
            {
                new LibraryData
                {
                    Library     = librariesOrderedByOutput.First(),
                    SignedBooks = new List <int>(),
                    SignedDay   = 0,
                    IsSigning   = true
                }
            };

            for (var day = 0; day < model.NumberOfDays; day++)
            {
                Console.WriteLine($"day: {day}");
                Console.WriteLine($"lib: {signedLibraries.Count}");

                for (var i = 0; i < signedLibraries.Count; i++)
                {
                    if (signedLibraries[i].SignedDay + signedLibraries[i].Library.SigningTime > day)
                    {
                        Console.WriteLine($"finished signing day: {day}, {signedLibraries[i].SignedDay + signedLibraries[i].Library.SigningTime}");

                        var newBooks = signedLibraries[i].Library.Books.Where(b => !usedBooks.Contains(b))
                                       .OrderByDescending(b => model.BookScores[b])
                                       .Take(signedLibraries[i].Library.BooksShippedPerDay);

                        signedLibraries[i].IsSigning = false;
                        signedLibraries[i].SignedBooks.AddRange(newBooks);
                        usedBooks.AddRange(newBooks);
                    }

                    if (!signedLibraries[signedLibraries.Count - 1].IsSigning)
                    {
                        Console.WriteLine($"new library day: {day}");

                        signedLibraries.Add(new LibraryData
                        {
                            Library     = librariesOrderedByOutput[signedLibraries.Count],
                            SignedBooks = new List <int>(),
                            SignedDay   = day,
                            IsSigning   = true
                        });
                    }
                }
            }

            return(new BookScanningOutput()
            {
                NumberOfScannedLibraries = signedLibraries.Count,
                ScannedLibraries = signedLibraries.Select(s =>
                                                          (s.Library.Index, s.SignedBooks)).ToList()
            });
コード例 #4
0
 public ClasicSolutionD(BookScanning pizzaModel)
 {
     this.pizzaModel = pizzaModel;
 }