Last active
March 6, 2020 05:43
-
-
Save joonas-yoon/a2353c8e03ab0d3fe8edfd3ada0411da to your computer and use it in GitHub Desktop.
문제적 남자 : 브레인 유랑단 13회, 신비로운 문제
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://www.youtube.com/watch?v=Hx58twR_VY0 | |
NUMBERS = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", "twenty", "twentyone", "twentytwo", "twentythree", "twentyfour", "twentyfive", "twentysix", "twentyseven", "twentyeight", "twentynine", "thirty", "thirtyone", "thirtytwo", "thirtythree", "thirtyfour", "thirtyfive", "thirtysix", "thirtyseven", "thirtyeight", "thirtynine", "forty", "fortyone", "fortytwo", "fortythree", "fortyfour", "fortyfive", "fortysix", "fortyseven", "fortyeight", "fortynine", "fifty", "fiftyone", "fiftytwo", "fiftythree", "fiftyfour", "fiftyfive", "fiftysix", "fiftyseven", "fiftyeight", "fiftynine", "sixty", "sixtyone", "sixtytwo", "sixtythree", "sixtyfour", "sixtyfive", "sixtysix", "sixtyseven", "sixtyeight", "sixtynine", "seventy", "seventyone", "seventytwo", "seventythree", "seventyfour", "seventyfive", "seventysix", "seventyseven", "seventyeight", "seventynine", "eighty", "eightyone", "eightytwo", "eightythree", "eightyfour", "eightyfive", "eightysix", "eightyseven", "eightyeight", "eightynine", "ninety", "ninetyone", "ninetytwo", "ninetythree", "ninetyfour", "ninetyfive", "ninetysix", "ninetyseven", "ninetyeight", "ninetynine"] | |
# create square to be equal sums | |
def makeSquare(a, b, c, d): | |
return [a, b, c, d, a+d+-c, b+2*c-2*d, b+c-d, 2*c-d, a-2*c+2*d] | |
# all sum of rows are same? | |
def isMagicSquare(grid): | |
chk = {} | |
colsum = [0, 0, 0] | |
for i in range(len(grid)): | |
# each number in cell should be unique | |
if grid[i] in chk or grid[i] <= 0 or grid[i] >= 100: | |
return False | |
colsum[i % 3] += grid[i] | |
chk[grid[i]] = True | |
_sum = sum(grid[:3]) | |
for i in range(0, 3): | |
if _sum != sum(grid[3*i:3*(i+1)]): | |
return False | |
if _sum != colsum[i]: | |
return False | |
if _sum != grid[0]+grid[4]+grid[8]: return False | |
if _sum != grid[2]+grid[4]+grid[6]: return False | |
return True | |
def isValid(grid): | |
if not isMagicSquare(grid): | |
return False | |
square2 = [len(NUMBERS[x]) for x in grid] | |
return isMagicSquare(square2) | |
def print_grid(grid): | |
for i in range(3): | |
row = grid[3*i:3*(i+1)] | |
print(' '.join(list(map(str, row)))) | |
def hash(grid): | |
return '-'.join(sorted(list(map(str,grid)))) | |
used_number = {} | |
used_set = {} | |
MAX_N = 100 | |
for a in range(1, MAX_N): | |
used_number[a] = True | |
for b in range(1, MAX_N): | |
if b in used_number: continue | |
used_number[b] = True | |
for c in range(1, MAX_N): | |
if c in used_number: continue | |
used_number[c] = True | |
for d in range(1, MAX_N): | |
if d in used_number: continue | |
grid = makeSquare(a, b, c, d) | |
if isValid(grid): | |
cur_set = hash(grid) | |
if cur_set in used_set: continue | |
used_set[cur_set] = True | |
print_grid(grid) | |
print("") | |
del used_number[c] | |
del used_number[b] | |
del used_number[a] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment