solve extra/free-the-bunny-workers #10

Merged
seongbeom_park merged 2 commits from extra/free-the-bunny-workers into main 2022-04-19 09:57:21 +00:00
2 changed files with 37 additions and 0 deletions
Showing only changes of commit d06b937e1f - Show all commits

View File

@ -51,3 +51,6 @@ CFcSBwgCCBoHRhkKU1cGAA4AGU5YQR5THBwNFwoGGAxTQQMQVBUSBg4EAAwQRhUQVBUHFAQTGRpT QQM
### bringing-a-gun-to-a-trainer-fight ### bringing-a-gun-to-a-trainer-fight
* Completed in: 1 day, 23 hrs, 49 mins, 54 secs. * Completed in: 1 day, 23 hrs, 49 mins, 54 secs.
### free-the-bunny-workers
* Completed in: 3 hrs, 6 mins, 8 secs.

View File

@ -0,0 +1,34 @@
def solution(num_buns, num_required):
duplication_factor = num_buns - num_required + 1
result = [[] for bun in range(num_buns)]
key_id = 0
for dist in generate_key_distribution(num_buns, duplication_factor):
for bun in range(num_buns):
if dist[bun]:
result[bun] += [key_id]
key_id += 1
return result
def generate_key_distribution(num_buns, duplication):
if num_buns == duplication:
yield [True for i in range(num_buns)]
elif num_buns > 0:
if duplication > 0:
for dist in generate_key_distribution(num_buns - 1, duplication - 1):
yield [True] + dist
for dist in generate_key_distribution(num_buns - 1, duplication):
yield [False] + dist
tests = [
((3, 1), [[0], [0], [0]]),
((2, 2), [[0], [1]]),
((3, 2), [[0, 1], [0, 2], [1, 2]]),
((2, 1), [[0], [0]]),
((4, 4), [[0], [1], [2], [3]]),
((5, 3), [[0, 1, 2, 3, 4, 5], [0, 1, 2, 6, 7, 8], [0, 3, 4, 6, 7, 9], [1, 3, 5, 6, 8, 9], [2, 4, 5, 7, 8, 9]])
]
for i, o in tests:
result = solution(*i)
print(i, result == o, result, o)