---
# System prepended metadata

title: 2020 google hash code competition ver 0

---

# 2020 google hash code competition ver 0
https://hackmd.io/OkCZsFgUQ2SxJgtudCpLYQ
```python=
testcasepool = [
'f_libraries_of_the_world.txt',
'e_so_many_books.txt',
'd_tough_choices.txt',
'c_incunabula.txt',
'b_read_on.txt',
'a_example.txt']
for testcase in testcasepool:

    score_dict = dict()  # book score
    lib = dict()
    res = []

    with open(testcase, 'r') as f:
        first_line = f.readline().strip().split(' ')
        # print(first_line)
        n_book, n_library, n_day = map(int, first_line)
        # print(n_book, n_library, n_day)
        second_line = f.readline().strip().split(' ')

        for i in range(n_book):
            score_dict[i] = int(second_line[i])
        # print(score_dict)

        for i in range(n_library):
            tmp = f.readline().strip().split(' ')
            book, day, scan = map(int, tmp)
            index = f.readline().strip().split(' ')
            index = map(int, index)
            index_value = dict()
            value_sum = 0
            for j in index:
                index_value[j] = score_dict[j]
                value_sum += score_dict[j]
            index_value = sorted(index_value, key=lambda x: index_value[x], reverse=True)

            lib_volume = min((n_day - day) * scan, book)
            lib[i] = [book, day, scan, index_value, lib_volume]

    print(lib)


    lib_by_vol = sorted(lib, key=lambda x: lib[x][4], reverse=True)
    print(lib_by_vol)

    # find how many lib we can use
    tmp = 0
    max_lib = 0

    for i in range(n_library):
        tmp += lib[lib_by_vol[i]][2]
        # print(tmp)
        if tmp >= n_day:
            break
    max_lib = i + 1
    res.append(str(max_lib))

    
    # to decide the order of books we want to scan
    for ele in lib_by_vol[0:max_lib]:
        # res.append([ele, num_of_book_we_want_to_send])

        numofbooks_scanned = min(lib[ele][4], lib[ele][0])  # make sure we will not request more than lib can provide

        send_book_index = []
        for this_book in range(numofbooks_scanned):
            if lib[ele][3][this_book] in score_dict:
                send_book_index.append(str(lib[ele][3][this_book]))  # lib[ele][3] is booklisk of this lib
                del score_dict[lib[ele][3][this_book]]

        res.append(str(ele) + ' ' + str(len(send_book_index)))
        res.append(" ".join(send_book_index))


    with open(testcase + '_result.txt', 'w') as f:
        for i in res:
            f.writelines(i + '\n')
