-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibrarySystem.cs
More file actions
171 lines (138 loc) · 5.17 KB
/
LibrarySystem.cs
File metadata and controls
171 lines (138 loc) · 5.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Threading;
using System.IO;
using System.Linq;
namespace Hashcode2020CSharp
{
class LibrarySystem
{
public string input { get; set; }
public string output { get; set; }
public int[] firstLine { get; set; } //0-Number of books, 1-Number of libraries, 2-Days for the process
public int[] fl { get; set; } //To create libraries //0-Numbre of books, 1-signup time, 2-Scan books per day
public List<Book> bs = new List<Book>(); //List with all books with their points
public List<Library> libraries = new List<Library>(); //List with all libraries
public LibrarySystem(string input, string output)
{
this.input = input;
this.output = output;
Thread TRun = new Thread(Run);
TRun.Start();
}
private void Run()
{
Reader();
StringBuilder sb = new StringBuilder();
int count = 0;
do
{
CalculateFactor();
libraries = libraries.OrderByDescending(x => x.factor).ToList();
sb.AppendLine(libraries[0].id + " " + libraries[0].books.Count);
foreach (Book bk in libraries[0].books)
{
sb.Append(bk.id + " ");
}
sb.Remove(sb.Length - 1, 1);
sb.Append("\n");
firstLine[2] -= libraries[0].timeSignUp;
List<Book> booksToRemove = libraries[0].books;
libraries.Remove(libraries[0]);
RemoveScanned(booksToRemove);
count++;
} while (firstLine[2] > 0 && libraries.Count != 0);
sb.Insert(0, count + "\n");
Writer(sb.ToString());
}
private void CalculateFactor()
{
foreach(Library lb in libraries)
{
lb.SetFactor(firstLine[2]);
}
}
private void RemoveScanned(List<Book> scannedBooks)
{
foreach (Library lb in libraries)
{
lb.RemoveItemsAlreadyScanned(scannedBooks);
}
}
private int[] ParseToIntArray (string[] sourceToParse)
{
int[] intArray = Array.ConvertAll<string, int>(sourceToParse, int.Parse);
return intArray;
}
private void parseInitialBooks (string[] sourceToParse)
{
//This method receive all book values and generate the list with all pairs of id and value
int[] intArray = Array.ConvertAll<string, int>(sourceToParse, int.Parse);
for (int i = 0; i < intArray.Length; i++)
{
bs.Add(new Book(i, intArray[i]));
}
}
private List<Book> ParseBooks(string[] sourceToParse)
{
//This method receive an string array and return a list with the books referred to the array id's
int[] intArray = Array.ConvertAll<string, int>(sourceToParse, int.Parse);
List<Book> returnedBooks = new List<Book>();
foreach (int i in intArray)
{
returnedBooks.Add(bs.Find(x => x.id == i));
}
//This will remove duplicatd books and return it
return returnedBooks.Distinct().ToList();
}
#region txt actions
private void Reader()
{
using (StreamReader fileInput = new StreamReader(input))
{
try
{
firstLine = ParseToIntArray(fileInput.ReadLine().Split());
parseInitialBooks(fileInput.ReadLine().Split());
} catch (Exception e) { ExceptionHandler.HandleException(e); }
int i = 0;
string ln;
Boolean pairOrNot = true;
while ((ln = fileInput.ReadLine()) != null)
{
if (pairOrNot)
{
try
{
fl = ParseToIntArray(ln.Split());
} catch (Exception e) { ExceptionHandler.HandleException(e); }
pairOrNot = false;
}
else
{
try
{
List<Book> sl = new List<Book>();
sl = ParseBooks(ln.Split());
libraries.Add(new Library(fl, sl, i));
i++;
} catch (Exception e) { ExceptionHandler.HandleException(e); }
pairOrNot = true;
}
}
}
Debug.WriteLine("Beast " + input + " feeded!");
}
private void Writer(string outputString)
{
using (StreamWriter fileOutput = new StreamWriter(output))
{
fileOutput.Write(outputString);
}
Debug.WriteLine("Finished :" + output);
}
#endregion
}
}