Skip to content

Instantly share code, notes, and snippets.

@HudsonGraeme
Created November 4, 2023 18:01
Show Gist options
  • Save HudsonGraeme/0c8972bfb1d826ca77d463882aa97519 to your computer and use it in GitHub Desktop.
Save HudsonGraeme/0c8972bfb1d826ca77d463882aa97519 to your computer and use it in GitHub Desktop.
Level 5
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
def counter(lst):
count_dict = {}
for item in lst:
if item in count_dict:
count_dict[item] += 1
else:
count_dict[item] = 1
return count_dict
def gcd(a, b):
while b:
a, b = b, a % b
return a
def cycle_count(c, n):
cc = factorial(n)
for a, b in counter(c).items():
cc //= (a**b) * factorial(b)
return cc
def cycle_partitions(n, i=1):
yield [n]
for i in range(i, n//2 + 1):
for p in cycle_partitions(n-i, i):
yield [i] + p
def solution(w, h, s):
grid = 0
for cpw in cycle_partitions(w):
for cph in cycle_partitions(h):
m = cycle_count(cpw, w) * cycle_count(cph, h)
grid += m * (s**sum([sum([gcd(i, j) for i in cpw]) for j in cph]))
return str(grid // (factorial(w) * factorial(h)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment