202 lines
5.5 KiB
Plaintext
202 lines
5.5 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 133,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from os import listdir\n",
|
|
"from os.path import isfile, join"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 134,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"['a_example.txt',\n",
|
|
" 'b_read_on.txt',\n",
|
|
" 'd_tough_choices.txt',\n",
|
|
" 'e_so_many_books.txt',\n",
|
|
" 'f_libraries_of_the_world.txt']"
|
|
]
|
|
},
|
|
"execution_count": 134,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"my_path = 'data/'\n",
|
|
"files = [f for f in listdir(my_path) if isfile(join(my_path, f)) and f[-1]=='t']\n",
|
|
"files"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 135,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"def max_score_per_lib(lib_index):\n",
|
|
" return sum([sorted_scores[x] for x in libraries[lib_index].books][:libraries[lib_index].days_to_send_books*libraries[lib_index].M])"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 136,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"class Library:\n",
|
|
" def __init__(self, ID, N, T, M, days_to_send_books, books):\n",
|
|
" self.ID = ID\n",
|
|
" self.N = N\n",
|
|
" self.T = T\n",
|
|
" self.M = M\n",
|
|
" self.days_to_send_books = days_to_send_books\n",
|
|
" \n",
|
|
" books_score = {b: scores.get(b) for b in books}\n",
|
|
" self.books = sorted(books_score, key=books_score.get, reverse=True)\n",
|
|
" \n",
|
|
" self.books_to_send = self.books[:min(len(books), self.days_to_send_books*self.M)]\n",
|
|
" self.max_score_in_remaining_time = sum(self.books_to_send)\n",
|
|
"\n",
|
|
" def update(self, remaining_days):\n",
|
|
" self.days_to_send_books = remaining_days - T\n",
|
|
" self.books_to_send = self.books[:min(len(books), self.days_to_send_books*self.M)]\n",
|
|
" self.max_score_in_remaining_time = sum(self.books_to_send)\n",
|
|
" return self\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 137,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"def read_file(filename):\n",
|
|
" file_path = my_path + filename\n",
|
|
" with open(file_path) as f:\n",
|
|
" content = f.readlines()\n",
|
|
" content = [x.strip() for x in content] \n",
|
|
" return content"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": []
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 138,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"filename = 'd_tough_choices.txt'\n",
|
|
"\n",
|
|
"content = read_file(filename)\n",
|
|
"\n",
|
|
"# books, libraries, days\n",
|
|
"B, L, D = [int(v) for v in content[0].split(' ')]\n",
|
|
"\n",
|
|
"scores = [int(v) for v in content[1].split(' ')]\n",
|
|
"scores = {i: scores[i] for i in range(len(scores))}\n",
|
|
"\n",
|
|
"num_lib = int((len(content) - 2)/2)\n",
|
|
"libraries, available = list(), list()\n",
|
|
"for l in range(num_lib):\n",
|
|
" i = 2 + 2*l\n",
|
|
" # books, signup, books per day\n",
|
|
" N, T, M = [int(v) for v in content[i].split(' ')]\n",
|
|
" books = {int(v) for v in content[i+1].split(' ')}\n",
|
|
" libraries.append(Library(l, N, T, M, D-T, books))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 139,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"available = libraries.copy()\n",
|
|
"available = [lib for lib in available if lib.days_to_send_books > 0]\n",
|
|
"passed_days = 0\n",
|
|
"libs, books_by_library = [], {}\n",
|
|
"\n",
|
|
"while(len(available) > 0):\n",
|
|
" available.sort(key=lambda x: x.max_score_in_remaining_time, reverse=True)\n",
|
|
" to_signup, available = available[0], available[1:]\n",
|
|
" libs.append(to_signup.ID)\n",
|
|
" books_by_library[to_signup.ID] = to_signup.books_to_send\n",
|
|
" passed_days += to_signup.T\n",
|
|
" available = [lib.update(D - passed_days) for lib in available]\n",
|
|
" available = [lib for lib in available if lib.days_to_send_books > 0]"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 140,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"sub_path = 'submissions/'\n",
|
|
"\n",
|
|
"# libs = [lib.id] in order of signup\n",
|
|
"# books_by_library = {id: [books]}\n",
|
|
"def create_submission(A, libs, books_by_library, name, sub_path=sub_path):\n",
|
|
" with open(sub_path + name, 'w+') as sub:\n",
|
|
" sub.write(f'{A}\\n')\n",
|
|
" for lib in libs:\n",
|
|
" books_order = books_by_library.get(lib)\n",
|
|
" sub.write(f'{lib} {len(books_order)}\\n')\n",
|
|
" sub.write(str(books_order).strip('[]').replace(',', '') + '\\n')"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 141,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"create_submission(len(libs), libs, books_by_library, filename)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": []
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "Python 3",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": "3.7.3"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 4
|
|
}
|