From 968cea3fb64c1bfc45c4860561434be765ec2baf Mon Sep 17 00:00:00 2001 From: Seongbeom Park Date: Sun, 23 Jan 2022 03:35:33 +0900 Subject: [PATCH 1/3] 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]) -- 2.47.2 From 84894a7e43a51ab83b66a32cd33999c1723c1c7e Mon Sep 17 00:00:00 2001 From: Seongbeom Park Date: Sun, 23 Jan 2022 04:17:53 +0900 Subject: [PATCH 2/3] removing itertools of level4/running-with-bunnies Verifying solution... Test 1 passed! Test 2 passed! Test 3 failed [Hidden] Test 4 passed! [Hidden] Test 5 failed [Hidden] Test 6 failed [Hidden] Test 7 failed [Hidden] Test 8 failed [Hidden] Test 9 failed [Hidden] Test 10 failed [Hidden] --- level4/running-with-bunnies/solution.py | 69 +++++++++++++------------ 1 file changed, 35 insertions(+), 34 deletions(-) diff --git a/level4/running-with-bunnies/solution.py b/level4/running-with-bunnies/solution.py index ce5da1a..f69abd8 100644 --- a/level4/running-with-bunnies/solution.py +++ b/level4/running-with-bunnies/solution.py @@ -1,52 +1,53 @@ import numpy as np -import itertools def solution(times, times_limit): - # find shortest path - nodes = len(times) + best_times = get_best_times(times) + best_plan = get_best_plan(best_times, times_limit) + return best_plan + +def get_best_times(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 + spots = len(times) + bunny_count = spots-2 + best_times = np.reshape(t, (spots, 1, spots)) + one_step = np.reshape(t.T, (1, spots, spots)) 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 + temp = np.reshape(np.amin(best_times + one_step, axis=2), (spots, 1, spots)) + new_best_times = np.minimum(best_times, temp) + if np.array_equal(best_times, new_best_times): + best_times = new_best_times break; - shortest = new_shortest - for i in range(nodes): - if shortest[i][0][i] < 0: + best_times = new_best_times + for i in range(spots): + if best_times[i][0][i] < 0: infinite_time = True break; if infinite_time: - return list(range(nodes-2)) - shortest = np.reshape(shortest, (nodes, nodes)) - #print shortest + return list(range(bunny_count)) + best_times = np.reshape(best_times, t.shape) + return best_times - # calc time for all paths +def get_best_plan(best_times, times_limit): best_plan = [] - for order in itertools.permutations(range(1, nodes)): + last_position = 0 + bulkhead = len(best_times)-1 + bunny_count = len(best_times)-2 + for plan in get_plans(bunny_count): 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 - + for bunny_id in plan: + next_position = bunny_id+1 + estimated_time += best_times[last_position][next_position] + last_position = next_position + estimated_time += best_times[last_position][bulkhead] + if estimated_time <= times_limit: + best_plan = get_better_plan(best_plan, plan) return best_plan +# TODO: implement get_plans +def get_plans(bunny_count): + return [[1], [0], [1, 2], [0, 1, 2], [0, 1]] + def get_better_plan(a, b): if len(a) > len(b): return a -- 2.47.2 From fe97fb60134fb687ffa812634cd9020726b4cb53 Mon Sep 17 00:00:00 2001 From: Seongbeom Park Date: Sun, 23 Jan 2022 05:28:36 +0900 Subject: [PATCH 3/3] finish level4/running-with-bunnies --- README.md | 5 +++ level4/running-with-bunnies/solution.py | 43 ++++++++++++++++--------- 2 files changed, 33 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 34c8b2a..e063cf9 100644 --- a/README.md +++ b/README.md @@ -13,3 +13,8 @@ ### bomb-baby * Completed in: 1 hr, 46 mins, 55 secs. * GCD + +## Level4 +### running-with-bunnies +* Completed in: 1 day, 4 hrs, 40 mins, 40 secs. +* Shortest Paths diff --git a/level4/running-with-bunnies/solution.py b/level4/running-with-bunnies/solution.py index f69abd8..e81afbe 100644 --- a/level4/running-with-bunnies/solution.py +++ b/level4/running-with-bunnies/solution.py @@ -1,14 +1,20 @@ import numpy as np +import copy def solution(times, times_limit): - best_times = get_best_times(times) - best_plan = get_best_plan(best_times, times_limit) + spots = len(times) + bulkhead_position = spots-1 + bunnies = spots-2 + + best_times = get_best_times(times, spots, bunnies) + if best_times is None: + return list(range(bunnies)) + + best_plan = get_best_plan(best_times, times_limit, bunnies, bulkhead_position) return best_plan -def get_best_times(times): +def get_best_times(times, spots, bunnies): t = np.array(times) - spots = len(times) - bunny_count = spots-2 best_times = np.reshape(t, (spots, 1, spots)) one_step = np.reshape(t.T, (1, spots, spots)) infinite_time = False @@ -24,29 +30,35 @@ def get_best_times(times): infinite_time = True break; if infinite_time: - return list(range(bunny_count)) + return None best_times = np.reshape(best_times, t.shape) return best_times -def get_best_plan(best_times, times_limit): +def get_best_plan(best_times, times_limit, bunnies, bulkhead_position, start_position = 0): best_plan = [] - last_position = 0 - bulkhead = len(best_times)-1 - bunny_count = len(best_times)-2 - for plan in get_plans(bunny_count): + for plan in get_plans([], list(range(bunnies)) + [None]): + if get_better_plan(best_plan, plan) == best_plan: + continue estimated_time = 0 + last_position = start_position for bunny_id in plan: next_position = bunny_id+1 estimated_time += best_times[last_position][next_position] last_position = next_position - estimated_time += best_times[last_position][bulkhead] + estimated_time += best_times[last_position][bulkhead_position] if estimated_time <= times_limit: best_plan = get_better_plan(best_plan, plan) + best_plan.sort() return best_plan -# TODO: implement get_plans -def get_plans(bunny_count): - return [[1], [0], [1, 2], [0, 1, 2], [0, 1]] +def get_plans(saved, remain): + for id in remain: + remain_bunnies = copy.deepcopy(remain) + remain_bunnies.remove(id) + if id == None: + yield saved + for id in get_plans(saved + [id], remain_bunnies): + yield id def get_better_plan(a, b): if len(a) > len(b): @@ -64,6 +76,7 @@ def get_better_plan(a, b): 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]], -- 2.47.2