finish level4/running-with-bunnies
This commit is contained in:
parent
84894a7e43
commit
fe97fb6013
@ -13,3 +13,8 @@
|
|||||||
### bomb-baby
|
### bomb-baby
|
||||||
* Completed in: 1 hr, 46 mins, 55 secs.
|
* Completed in: 1 hr, 46 mins, 55 secs.
|
||||||
* GCD
|
* GCD
|
||||||
|
|
||||||
|
## Level4
|
||||||
|
### running-with-bunnies
|
||||||
|
* Completed in: 1 day, 4 hrs, 40 mins, 40 secs.
|
||||||
|
* Shortest Paths
|
||||||
|
|||||||
@ -1,14 +1,20 @@
|
|||||||
import numpy as np
|
import numpy as np
|
||||||
|
import copy
|
||||||
|
|
||||||
def solution(times, times_limit):
|
def solution(times, times_limit):
|
||||||
best_times = get_best_times(times)
|
spots = len(times)
|
||||||
best_plan = get_best_plan(best_times, times_limit)
|
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
|
return best_plan
|
||||||
|
|
||||||
def get_best_times(times):
|
def get_best_times(times, spots, bunnies):
|
||||||
t = np.array(times)
|
t = np.array(times)
|
||||||
spots = len(times)
|
|
||||||
bunny_count = spots-2
|
|
||||||
best_times = np.reshape(t, (spots, 1, spots))
|
best_times = np.reshape(t, (spots, 1, spots))
|
||||||
one_step = np.reshape(t.T, (1, spots, spots))
|
one_step = np.reshape(t.T, (1, spots, spots))
|
||||||
infinite_time = False
|
infinite_time = False
|
||||||
@ -24,29 +30,35 @@ def get_best_times(times):
|
|||||||
infinite_time = True
|
infinite_time = True
|
||||||
break;
|
break;
|
||||||
if infinite_time:
|
if infinite_time:
|
||||||
return list(range(bunny_count))
|
return None
|
||||||
best_times = np.reshape(best_times, t.shape)
|
best_times = np.reshape(best_times, t.shape)
|
||||||
return best_times
|
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 = []
|
best_plan = []
|
||||||
last_position = 0
|
for plan in get_plans([], list(range(bunnies)) + [None]):
|
||||||
bulkhead = len(best_times)-1
|
if get_better_plan(best_plan, plan) == best_plan:
|
||||||
bunny_count = len(best_times)-2
|
continue
|
||||||
for plan in get_plans(bunny_count):
|
|
||||||
estimated_time = 0
|
estimated_time = 0
|
||||||
|
last_position = start_position
|
||||||
for bunny_id in plan:
|
for bunny_id in plan:
|
||||||
next_position = bunny_id+1
|
next_position = bunny_id+1
|
||||||
estimated_time += best_times[last_position][next_position]
|
estimated_time += best_times[last_position][next_position]
|
||||||
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:
|
if estimated_time <= times_limit:
|
||||||
best_plan = get_better_plan(best_plan, plan)
|
best_plan = get_better_plan(best_plan, plan)
|
||||||
|
best_plan.sort()
|
||||||
return best_plan
|
return best_plan
|
||||||
|
|
||||||
# TODO: implement get_plans
|
def get_plans(saved, remain):
|
||||||
def get_plans(bunny_count):
|
for id in remain:
|
||||||
return [[1], [0], [1, 2], [0, 1, 2], [0, 1]]
|
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):
|
def get_better_plan(a, b):
|
||||||
if len(a) > len(b):
|
if len(a) > len(b):
|
||||||
@ -64,6 +76,7 @@ def get_better_plan(a, b):
|
|||||||
return b
|
return b
|
||||||
return a
|
return a
|
||||||
|
|
||||||
|
|
||||||
tests = [
|
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, 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, 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]],
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user