From 968cea3fb64c1bfc45c4860561434be765ec2baf Mon Sep 17 00:00:00 2001 From: Seongbeom Park Date: Sun, 23 Jan 2022 03:35:33 +0900 Subject: [PATCH] start level4/running-with-bunnies Verifying solution... Test 1 failed Test 2 failed Test 3 failed [Hidden] Test 4 passed! [Hidden] Test 5 failed [Hidden] Test 6 failed [Hidden] Test 7 passed! [Hidden] Test 8 failed [Hidden] Test 9 failed [Hidden] Test 10 failed [Hidden] --- level4/running-with-bunnies/constraints.txt | 21 ++++++ level4/running-with-bunnies/readme.txt | 65 +++++++++++++++++ level4/running-with-bunnies/solution.py | 77 +++++++++++++++++++++ 3 files changed, 163 insertions(+) create mode 100644 level4/running-with-bunnies/constraints.txt create mode 100644 level4/running-with-bunnies/readme.txt create mode 100644 level4/running-with-bunnies/solution.py diff --git a/level4/running-with-bunnies/constraints.txt b/level4/running-with-bunnies/constraints.txt new file mode 100644 index 0000000..f201987 --- /dev/null +++ b/level4/running-with-bunnies/constraints.txt @@ -0,0 +1,21 @@ +Java +==== +Your code will be compiled using standard Java 8. All tests will be run by calling the solution() method inside the Solution class + +Execution time is limited. + +Wildcard imports and some specific classes are restricted (e.g. java.lang.ClassLoader). You will receive an error when you verify your solution if you have used a blacklisted class. + +Third-party libraries, input/output operations, spawning threads or processes and changes to the execution environment are not allowed. + +Your solution must be under 32000 characters in length including new lines and and other non-printing characters. + +Python +====== +Your code will run inside a Python 2.7.13 sandbox. All tests will be run by calling the solution() function. + +Standard libraries are supported except for bz2, crypt, fcntl, mmap, pwd, pyexpat, select, signal, termios, thread, time, unicodedata, zipimport, zlib. + +Input/output operations are not allowed. + +Your solution must be under 32000 characters in length including new lines and and other non-printing characters. diff --git a/level4/running-with-bunnies/readme.txt b/level4/running-with-bunnies/readme.txt new file mode 100644 index 0000000..72f855e --- /dev/null +++ b/level4/running-with-bunnies/readme.txt @@ -0,0 +1,65 @@ +Running with Bunnies +==================== + +You and the bunny workers need to get out of this collapsing death trap of a space station -- and fast! Unfortunately, some of the bunnies have been weakened by their long work shifts and can't run very fast. Their friends are trying to help them, but this escape would go a lot faster if you also pitched in. The defensive bulkhead doors have begun to close, and if you don't make it through in time, you'll be trapped! You need to grab as many bunnies as you can and get through the bulkheads before they close. + +The time it takes to move from your starting point to all of the bunnies and to the bulkhead will be given to you in a square matrix of integers. Each row will tell you the time it takes to get to the start, first bunny, second bunny, ..., last bunny, and the bulkhead in that order. The order of the rows follows the same pattern (start, each bunny, bulkhead). The bunnies can jump into your arms, so picking them up is instantaneous, and arriving at the bulkhead at the same time as it seals still allows for a successful, if dramatic, escape. (Don't worry, any bunnies you don't pick up will be able to escape with you since they no longer have to carry the ones you did pick up.) You can revisit different spots if you wish, and moving to the bulkhead doesn't mean you have to immediately leave -- you can move to and from the bulkhead to pick up additional bunnies if time permits. + +In addition to spending time traveling between bunnies, some paths interact with the space station's security checkpoints and add time back to the clock. Adding time to the clock will delay the closing of the bulkhead doors, and if the time goes back up to 0 or a positive number after the doors have already closed, it triggers the bulkhead to reopen. Therefore, it might be possible to walk in a circle and keep gaining time: that is, each time a path is traversed, the same amount of time is used or added. + +Write a function of the form solution(times, time_limit) to calculate the most bunnies you can pick up and which bunnies they are, while still escaping through the bulkhead before the doors close for good. If there are multiple sets of bunnies of the same size, return the set of bunnies with the lowest worker IDs (as indexes) in sorted order. The bunnies are represented as a sorted list by worker ID, with the first bunny being 0. There are at most 5 bunnies, and time_limit is a non-negative integer that is at most 999. + +For instance, in the case of +[ + [0, 2, 2, 2, -1], # 0 = Start + [9, 0, 2, 2, -1], # 1 = Bunny 0 + [9, 3, 0, 2, -1], # 2 = Bunny 1 + [9, 3, 2, 0, -1], # 3 = Bunny 2 + [9, 3, 2, 2, 0], # 4 = Bulkhead +] +and a time limit of 1, the five inner array rows designate the starting point, bunny 0, bunny 1, bunny 2, and the bulkhead door exit respectively. You could take the path: + +Start End Delta Time Status + - 0 - 1 Bulkhead initially open + 0 4 -1 2 + 4 2 2 0 + 2 4 -1 1 + 4 3 2 -1 Bulkhead closes + 3 4 -1 0 Bulkhead reopens; you and the bunnies exit + +With this solution, you would pick up bunnies 1 and 2. This is the best combination for this space station hallway, so the solution is [1, 2]. + +Languages +========= + +To provide a Java solution, edit Solution.java +To provide a Python solution, edit solution.py + +Test cases +========== +Your code should pass the following test cases. +Note that it may also be run against hidden test cases not shown here. + +-- Java cases -- +Input: +Solution.solution({{0, 1, 1, 1, 1}, {1, 0, 1, 1, 1}, {1, 1, 0, 1, 1}, {1, 1, 1, 0, 1}, {1, 1, 1, 1, 0}}, 3) +Output: + [0, 1] + +Input: +Solution.solution({{0, 2, 2, 2, -1}, {9, 0, 2, 2, -1}, {9, 3, 0, 2, -1}, {9, 3, 2, 0, -1}, {9, 3, 2, 2, 0}}, 1) +Output: + [1, 2] + +-- Python cases -- +Input: +solution.solution([[0, 2, 2, 2, -1], [9, 0, 2, 2, -1], [9, 3, 0, 2, -1], [9, 3, 2, 0, -1], [9, 3, 2, 2, 0]], 1) +Output: + [1, 2] + +Input: +solution.solution([[0, 1, 1, 1, 1], [1, 0, 1, 1, 1], [1, 1, 0, 1, 1], [1, 1, 1, 0, 1], [1, 1, 1, 1, 0]], 3) +Output: + [0, 1] + +Use verify [file] to test your solution and see how it does. When you are finished editing your code, use submit [file] to submit your answer. If your solution passes the test cases, it will be removed from your home folder. diff --git a/level4/running-with-bunnies/solution.py b/level4/running-with-bunnies/solution.py new file mode 100644 index 0000000..ce5da1a --- /dev/null +++ b/level4/running-with-bunnies/solution.py @@ -0,0 +1,77 @@ +import numpy as np +import itertools + +def solution(times, times_limit): + # find shortest path + nodes = len(times) + t = np.array(times) + shortest = np.reshape(t, (nodes, 1, nodes)) + one_step = np.reshape(t.T, (1, nodes, nodes)) + #print shortest + #print one_step + infinite_time = False + while not infinite_time: + temp = np.reshape(np.amin(shortest + one_step, axis=2), (nodes, 1, nodes)) + #print temp + new_shortest = np.minimum(shortest, temp) + #print new_shortest + if np.array_equal(shortest, new_shortest): + shortest = new_shortest + break; + shortest = new_shortest + for i in range(nodes): + if shortest[i][0][i] < 0: + infinite_time = True + break; + if infinite_time: + return list(range(nodes-2)) + shortest = np.reshape(shortest, (nodes, nodes)) + #print shortest + + # calc time for all paths + best_plan = [] + for order in itertools.permutations(range(1, nodes)): + estimated_time = 0 + order = (0,) + order + bunnies = [] + for i in range(nodes-1): + start_node, end_node = order[i], order[i+1] + estimated_time += shortest[start_node][end_node] + if end_node == nodes-1: + break; + bunnies += [end_node-1] + if estimated_time <= times_limit and len(bunnies) > 0: + bunnies.sort() + best_plan = get_better_plan(best_plan, bunnies) + #print best_plan + + return best_plan + +def get_better_plan(a, b): + if len(a) > len(b): + return a + if len(a) < len(b): + return b + if a == b: + return a + for i in range(len(a)): + if a[i] == b[i]: + continue + if a[i] < b[i]: + return a + if a[i] > b[i]: + return b + return a + +tests = [ + [[[0, 2, 2, 2, -1], [9, 0, 2, 2, -1], [9, 3, 0, 2, -1], [9, 3, 2, 0, -1], [9, 3, 2, 2, 0]], 1, [1, 2]], + [[[0, 1, 1, 1, 1], [1, 0, 1, 1, 1], [1, 1, 0, 1, 1], [1, 1, 1, 0, 1], [1, 1, 1, 1, 0]], 3, [0, 1]], + [[[0, 2, 2, 2, -1], [9, 0, 2, 2, -1], [9, 3, 0, 2, -1], [9, 3, 2, 0, -1], [9, 3, 2, 2, 0]], -1, []], + [[[0, 2, 2, 2, -1], [9, 0, 2, 2, -1], [9, 3, 0, 2, -1], [9, 3, 2, 0, -1], [9, 3, 2, 2, 0]], 0, [1]], + [[[0, 2, 2, 2, -1], [9, 0, 2, 2, -1], [9, 3, 0, 2, -1], [9, 3, 2, 0, -1], [9, 3, 2, 2, 0]], 2, [0, 1]], + [[[0, 2, 2, 2, -1], [9, 0, 2, 2, -1], [9, 3, 0, 2, -1], [9, 3, 2, 0, -1], [9, 3, 2, 2, 0]], 3, [0, 1, 2]], + ] + +for test in tests: + result = solution(test[0], test[1]) + print (test[0], test[1], result == test[2], result, test[2])