finish level4/escape pods #5

Merged
seongbeom_park merged 2 commits from level4/escape-pods into main 2022-02-05 16:36:26 +00:00
2 changed files with 36 additions and 0 deletions
Showing only changes of commit e8ef4a7c51 - Show all commits

View File

@ -18,3 +18,8 @@
### running-with-bunnies
* Completed in: 1 day, 4 hrs, 40 mins, 40 secs.
* Shortest Paths
## Level4
### escape-pods
* Completed in: 1 day, 2 hrs, 16 mins, 11 secs.
* Minimum cut

View File

@ -0,0 +1,31 @@
import numpy as np
def solution(entrances, exits, path):
p = np.array(path)
minimum_width = -1
all_rooms = set(range(len(p)))
for rooms, others in generate_rooms(all_rooms, set(entrances), set(exits), p):
width = calculate_cut_width(rooms, others, p)
if width < minimum_width or minimum_width == -1:
minimum_width = width
return minimum_width
def generate_rooms(all_rooms, prev_rooms, exits, path):
yield prev_rooms, all_rooms - set(prev_rooms)
for room, width in enumerate(sum(path[list(prev_rooms)])):
if room not in prev_rooms and room not in exits and width != 0:
next_rooms = prev_rooms | set([room])
for result in generate_rooms(all_rooms, next_rooms, exits, path):
yield result
def calculate_cut_width(rooms, others, p):
return np.sum(p[list(rooms)][:, list(others)])
tests = [
[[[0], [3], [[0, 7, 0, 0], [0, 0, 6, 0], [0, 0, 0, 8], [9, 0, 0, 0]]], 6],
[[[0, 1], [4, 5], [[0, 0, 4, 6, 0, 0], [0, 0, 5, 2, 0, 0], [0, 0, 0, 0, 4, 4], [0, 0, 0, 0, 6, 6], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]]], 16],
]
for test in tests:
result = solution(*test[0])
print(test[0], result == test[1], result, test[1])