Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from itertools import permutations
- def gcd(x,y):
- if y == 0: return x
- return gcd(y, x%y)
- counts = {}
- def conv(a):
- return a / 3, a % 3
- def iconv(a, b):
- return a*3 + b
- def canonical(ori,tgt, dx,dy):
- if ori < tgt:
- return (ori,dx,dy)
- return (tgt,-dx,-dy)
- for perm in permutations(range(9)):
- occ = []
- ok = True
- all_lines = []
- for ori, tgt in zip(perm, perm[1:]):
- row_o, col_o = conv(ori)
- row_t, col_t = conv(tgt)
- dx = row_t - row_o
- dy = col_t - col_o
- lines = [canonical(ori,tgt,dx,dy)]
- g = abs(gcd(dx,dy))
- if g != 1:
- dx /= g
- dy /= g
- mid = iconv(row_o + dx, col_o + dy)
- if mid not in occ:
- ok = False
- break
- lines = [canonical(ori,mid,dx,dy), canonical(mid,tgt,dx,dy)]
- all_lines.extend(lines)
- occ.append(ori)
- if ok:
- lines_set = frozenset(all_lines)
- counts[lines_set] = counts.get(lines_set, 0) + 1
- res = counts.items()
- res.sort(key=lambda x: x[1], reverse=True)
- print res[0]
- print res[1]
- print res[2]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement