finish level5/disorderly-escape

This commit is contained in:
Seongbeom Park 2022-03-21 16:31:19 +09:00
parent 7d237786c0
commit 7f1b481181
2 changed files with 35 additions and 71 deletions

View File

@ -37,6 +37,11 @@
* Feedback * Feedback
> [level5/dodge-the-lasers] (1) Some of the text readme.txt are duplicated. (2) Though the input type is string, Java input example uses quotes for string instead of double quotation marks. (3) While expected output type is string, in the output example does not use quotes. So the readers can confuse the return type. > [level5/dodge-the-lasers] (1) Some of the text readme.txt are duplicated. (2) Though the input type is string, Java input example uses quotes for string instead of double quotation marks. (3) While expected output type is string, in the output example does not use quotes. So the readers can confuse the return type.
### disorderly-escape
* Completed in: 9 days, 17 hrs, 30 mins, 3 secs.
* Reference
* [Burnside's lemma: counting up to symmetries, Youtube](https://www.youtube.com/watch?v=D0d9bYZ_qDY)
## Encrypted message ## Encrypted message
CFcSBwgCCBoHRhkKU1cGAA4AGU5YQR5THBwNFwoGGAxTQQMQVBUSBg4EAAwQRhUQVBUHFAQTGRpT QQMQVBkPERkECQAWDVwXX1BGEwgJBAwCBFRVHQRGUlFBShwaDVZTGBUFVUdBShsVA1tZBwNGUlFB ShoVB1wXX1BGFAQOSklOQR5HGh5AVRY= CFcSBwgCCBoHRhkKU1cGAA4AGU5YQR5THBwNFwoGGAxTQQMQVBUSBg4EAAwQRhUQVBUHFAQTGRpT QQMQVBkPERkECQAWDVwXX1BGEwgJBAwCBFRVHQRGUlFBShwaDVZTGBUFVUdBShsVA1tZBwNGUlFB ShoVB1wXX1BGFAQOSklOQR5HGh5AVRY=

View File

@ -5,14 +5,11 @@ def solution(w, h, s):
memo_f = {w:math.factorial(w), h:math.factorial(h)} memo_f = {w:math.factorial(w), h:math.factorial(h)}
memo_sp = {} memo_sp = {}
cases = 0 cases = 0
for c in generate_cycle_count(w, h, memo_f): for weight, n_cycles in generate_cycle_count(w, h, memo_f):
if c not in memo_sp: if n_cycles not in memo_sp:
memo_sp[c] = s ** c memo_sp[n_cycles] = s ** n_cycles
cases += memo_sp[c] cases += weight * memo_sp[n_cycles]
result = cases return str(cases / (memo_f[w] * memo_f[h]))
result /= memo_f[w]
result /= memo_f[h]
return str(result)
def generate_cycle_count(w, h, memo_f = {}): def generate_cycle_count(w, h, memo_f = {}):
memo_c = {} memo_c = {}
@ -24,10 +21,8 @@ def generate_cycle_count(w, h, memo_f = {}):
for i in pw.split(): for i in pw.split():
for j in ph.split(): for j in ph.split():
cycle_count += gcd(i, j, memo_gcd) cycle_count += gcd(i, j, memo_gcd)
cases = count_permutation(pw, memo_f, memo_p) * count_permutation(ph, memo_f, memo_p) variations = count_permutation(pw, memo_f, memo_p) * count_permutation(ph, memo_f, memo_p)
#print pw, ph, cases, cycle_count yield variations, cycle_count
for i in range(cases):
yield cycle_count
# generate cycle list in a string format # generate cycle list in a string format
# e.g., a string with length 5 can be composed with '2 2 1' which means there are 3 cycles, and foreach size is 2, 2 and 1. # e.g., a string with length 5 can be composed with '2 2 1' which means there are 3 cycles, and foreach size is 2, 2 and 1.
@ -84,67 +79,31 @@ def count_permutation(s, memo_f, memo_p):
memo_p[s] = result memo_p[s] = result
return memo_p[s] return memo_p[s]
def _solution(w, h, s):
return str(sum([s**cycle for cycle in _generate_cycles(w, h)])/(math.factorial(w)*math.factorial(h)))
def _generate_cycles(w, h):
identity = np.arange(w*h).reshape((w, h))
for i in _generate_permutation(list(range(w))):
for j in _generate_permutation(list(range(h))):
#print i, j, _count_cycle(identity[i][:, j])
yield _count_cycle(identity[i][:, j])
def _generate_permutation(numbers):
if len(numbers) == 1:
yield numbers
else:
for i, n in enumerate(numbers):
for j in _generate_permutation(numbers[:i] + numbers[i+1:]):
yield [n] + j
def _count_cycle(arr):
cycle = np.zeros(arr.shape, dtype=int)
w, h = arr.shape
cycle_id = 0
for i in range(w):
for j in range(h):
x, y = i, j
if cycle[x, y] == 0:
cycle_id += 1
while cycle[x, y] == 0:
cycle[x, y] = cycle_id
x, y = arr[x, y]/h, arr[x, y]%h
return cycle_id
tests = [ tests = [
#([2, 3, 4], '430'), ([2, 3, 4], '430'),
#([2, 2, 2], '7'), ([2, 2, 2], '7'),
#([1, 1, 2], '2'), ([1, 1, 2], '2'),
#([1, 1, 3], '3'), ([1, 1, 3], '3'),
#([2, 1, 2], '3'), ([2, 1, 2], '3'),
#([1, 2, 2], '3'), ([1, 2, 2], '3'),
#([3, 1, 2], '4'), ([3, 1, 2], '4'),
#([1, 3, 2], '4'), ([1, 3, 2], '4'),
#([4, 1, 2], '5'), ([4, 1, 2], '5'),
#([1, 4, 2], '5'), ([1, 4, 2], '5'),
#([5, 1, 2], '6'), ([5, 1, 2], '6'),
#([1, 5, 2], '6'), ([1, 5, 2], '6'),
#([6, 1, 2], '7'), ([6, 1, 2], '7'),
#([1, 6, 2], '7'), ([1, 6, 2], '7'),
([12, 1, 2], '13'), ([12, 1, 2], '13'),
#([1, 12, 2], '13'), ([1, 12, 2], '13'),
#([2, 3, 2], '13'), ([2, 3, 2], '13'),
#([3, 3, 2], '36'), ([3, 3, 2], '36'),
#([4, 4, 2], '317'), ([4, 4, 2], '317'),
#([5, 4, 2], '1053'), ([5, 4, 2], '1053'),
#([5, 5, 2], '5624'), ([5, 5, 2], '5624'),
#([6, 5, 2], '28576'), ([6, 5, 2], '28576'),
#([6, 6, 2], '251610'), ([6, 6, 2], '251610'),
#([12, 12, 20], '251610'), ([12, 12, 20], '97195340925396730736950973830781340249131679073592360856141700148734207997877978005419735822878768821088343977969209139721682171487959967012286474628978470487193051591840'),
] ]
for i, o in tests: for i, o in tests: