-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.py
37 lines (32 loc) · 1.19 KB
/
parser.py
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
from itertools import zip_longest
from book import Book
from library import Library
from problem import Problem
def grouper(n, iterable, fillvalue=None):
"grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
args = [iter(iterable)] * n
return zip_longest(fillvalue=fillvalue, *args)
def read_input(fname):
with open(f"data/{fname}") as f:
lines = f.readlines()
metadata = lines[0].split(" ")
B = int(metadata[0])
L = int(metadata[1])
D = int(metadata[2])
books_score = [int(i) for i in lines[1].strip().split(" ")]
all_books = {i: books_score[i] for i in range(len(books_score))}
all_books_list = []
problem = Problem(n_books=B, n_libraries=L, n_days=D, scores=books_score)
for i, group in enumerate(grouper(2, lines[2:])):
l1, l2 = group[0], group[1]
if l2 is None:
break
library = Library(i, *[int(i) for i in l1.strip().split(" ")])
# import ipdb
#
# ipdb.set_trace()
books = {(int(i)): all_books[int(i)] for i in l2.strip().split(" ")}
all_books_list.append(books)
library.add_books(books)
problem.add_library(library)
return problem, all_books, all_books_list