Skip to content

Commit

Permalink
Fail 2013 qualification D.treasure (memory+time)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukmdo committed Apr 17, 2013
1 parent 51b3adb commit bcafe2a
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
77 changes: 77 additions & 0 deletions 2013/qualification/Dtreasure.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
def _open_combinations(chests_opened, chests, keys):
keys_set = set(keys)
chests_opened_set = set(chests_opened)
combinations = []

for chest in chests:
chest_num, open_key, *found_keys = chest
if chest_num in chests_opened_set:
continue
if open_key in keys_set:
new_chests_opened = list(chests_opened)
new_chests_opened.append(chest_num)

new_keys = list(keys)
new_keys.remove(open_key) # any key of type open_key
new_keys.extend(found_keys)

combination = (tuple(new_chests_opened), tuple(new_keys))
combinations.append(combination)

return combinations


def get_open_combination(init_keys, chests):
combinations = [_open_combinations([], chests, init_keys)]
# import pdb; pdb.set_trace()

for step in range(1, len(chests)):
if len(combinations) < step:
break # no combinations from prev step
last_combinations = combinations[-1]
print(len(combinations))
print(len(last_combinations))

for combination in last_combinations:
new_combinations = _open_combinations(combination[0], chests, combination[1])
if len(combinations) <= step:
combinations.append([])
combinations[step].extend(new_combinations)

last_combinations.clear() # free some memory

if len(combinations) < len(chests):
return None
options = [c[0] for c in combinations[-1]]
if not options:
return None
best_combination = sorted(options)[0]

return tuple(best_combination)


def solve(num_cases):
for case_num in range(1, num_cases + 1):
case_info = input().split(' ')
num_init_keys, num_chests = int(case_info[0]), int(case_info[1])

init_keys = [int(key_type) for key_type in input().split(' ')]
chests = []
for chest_num in range(1, num_chests + 1):
chest_info = [int(item) for item in input().split(' ')]
open_key = chest_info[0]
keys_in_chest = chest_info[2:]

chest = [chest_num, open_key] + keys_in_chest
chests.append(tuple(chest))

solved = get_open_combination(init_keys, chests) or 'IMPOSSIBLE'
print('Case #{case_num}: {solved}'.format(
case_num=case_num, solved=solved))

if __name__ == '__main__':
import sys
with open(sys.argv[-1], 'r') as f:
input = f.readline
num_cases = int(input())
solve(num_cases)
23 changes: 23 additions & 0 deletions 2013/qualification/Dtreasure_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import unittest
from Dtreasure import get_open_combination


class TestFoo(unittest.TestCase):
def test_case1(self):
open_combination = get_open_combination(
(1,), [(1, 1,), (2, 1, 1, 3), (3, 2,), (4, 3, 2)])
self.assertEqual(open_combination, (2, 1, 4, 3))

def test_case2(self):
open_combination = get_open_combination(
(1, 1, 1), [(1, 1,), (2, 1,), (3, 1,)])
self.assertEqual(open_combination, (1, 2, 3))

def test_case3(self):
open_combination = get_open_combination(
(2,), [(1, 1, 1, 1)])
self.assertEqual(open_combination, None)


if __name__ == '__main__':
unittest.main()

0 comments on commit bcafe2a

Please sign in to comment.