From 7f1b481181c52cbcbce4957ad86ee234d0cc3e07 Mon Sep 17 00:00:00 2001 From: Seongbeom Park Date: Mon, 21 Mar 2022 16:31:19 +0900 Subject: [PATCH] finish level5/disorderly-escape --- README.md | 5 ++ level5/disorderly-escape/solution.py | 101 ++++++++------------------- 2 files changed, 35 insertions(+), 71 deletions(-) diff --git a/README.md b/README.md index 967e502..6cefd6f 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,11 @@ * 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. +### 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 CFcSBwgCCBoHRhkKU1cGAA4AGU5YQR5THBwNFwoGGAxTQQMQVBUSBg4EAAwQRhUQVBUHFAQTGRpT QQMQVBkPERkECQAWDVwXX1BGEwgJBAwCBFRVHQRGUlFBShwaDVZTGBUFVUdBShsVA1tZBwNGUlFB ShoVB1wXX1BGFAQOSklOQR5HGh5AVRY= diff --git a/level5/disorderly-escape/solution.py b/level5/disorderly-escape/solution.py index b90c70a..2e49686 100644 --- a/level5/disorderly-escape/solution.py +++ b/level5/disorderly-escape/solution.py @@ -5,14 +5,11 @@ def solution(w, h, s): memo_f = {w:math.factorial(w), h:math.factorial(h)} memo_sp = {} cases = 0 - for c in generate_cycle_count(w, h, memo_f): - if c not in memo_sp: - memo_sp[c] = s ** c - cases += memo_sp[c] - result = cases - result /= memo_f[w] - result /= memo_f[h] - return str(result) + for weight, n_cycles in generate_cycle_count(w, h, memo_f): + if n_cycles not in memo_sp: + memo_sp[n_cycles] = s ** n_cycles + cases += weight * memo_sp[n_cycles] + return str(cases / (memo_f[w] * memo_f[h])) def generate_cycle_count(w, h, memo_f = {}): memo_c = {} @@ -24,10 +21,8 @@ def generate_cycle_count(w, h, memo_f = {}): for i in pw.split(): for j in ph.split(): cycle_count += gcd(i, j, memo_gcd) - cases = count_permutation(pw, memo_f, memo_p) * count_permutation(ph, memo_f, memo_p) - #print pw, ph, cases, cycle_count - for i in range(cases): - yield cycle_count + variations = count_permutation(pw, memo_f, memo_p) * count_permutation(ph, memo_f, memo_p) + yield variations, cycle_count # 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. @@ -84,67 +79,31 @@ def count_permutation(s, memo_f, memo_p): memo_p[s] = result 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 = [ - #([2, 3, 4], '430'), - #([2, 2, 2], '7'), - #([1, 1, 2], '2'), - #([1, 1, 3], '3'), - #([2, 1, 2], '3'), - #([1, 2, 2], '3'), - #([3, 1, 2], '4'), - #([1, 3, 2], '4'), - #([4, 1, 2], '5'), - #([1, 4, 2], '5'), - #([5, 1, 2], '6'), - #([1, 5, 2], '6'), - #([6, 1, 2], '7'), - #([1, 6, 2], '7'), + ([2, 3, 4], '430'), + ([2, 2, 2], '7'), + ([1, 1, 2], '2'), + ([1, 1, 3], '3'), + ([2, 1, 2], '3'), + ([1, 2, 2], '3'), + ([3, 1, 2], '4'), + ([1, 3, 2], '4'), + ([4, 1, 2], '5'), + ([1, 4, 2], '5'), + ([5, 1, 2], '6'), + ([1, 5, 2], '6'), + ([6, 1, 2], '7'), + ([1, 6, 2], '7'), ([12, 1, 2], '13'), - #([1, 12, 2], '13'), - #([2, 3, 2], '13'), - #([3, 3, 2], '36'), - #([4, 4, 2], '317'), - #([5, 4, 2], '1053'), - #([5, 5, 2], '5624'), - #([6, 5, 2], '28576'), - #([6, 6, 2], '251610'), - #([12, 12, 20], '251610'), + ([1, 12, 2], '13'), + ([2, 3, 2], '13'), + ([3, 3, 2], '36'), + ([4, 4, 2], '317'), + ([5, 4, 2], '1053'), + ([5, 5, 2], '5624'), + ([6, 5, 2], '28576'), + ([6, 6, 2], '251610'), + ([12, 12, 20], '97195340925396730736950973830781340249131679073592360856141700148734207997877978005419735822878768821088343977969209139721682171487959967012286474628978470487193051591840'), ] for i, o in tests: