Created
April 5, 2018 01:04
-
-
Save mrphlip/0abd1bca2b31aa2d48d38c2583fa537c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import itertools | |
CX = 6 | |
CY = 3 | |
def neighbours(x, y, ignoredirection=None): | |
""" | |
Find all points that are connected to the supplied point. | |
Can pass the direction of the previous leg, to ensure that | |
the points are not colinear. | |
""" | |
if ignoredirection != 1: | |
for newx in range(CX): | |
if newx != x: | |
yield newx, y, 1 | |
if ignoredirection != 2: | |
for newy in range(CY): | |
if newy != y: | |
yield x, newy, 2 | |
if (x + y) % 2 == 0: | |
if ignoredirection != 3: | |
for newx in range(CX): | |
newy = y + (newx - x) | |
if newx != x and 0 <= newy < CY: | |
yield newx, newy, 3 | |
if ignoredirection != 4: | |
for newx in range(CX): | |
newy = y - (newx - x) | |
if newx != x and 0 <= newy < CY: | |
yield newx, newy, 4 | |
def is_neighbour(x1, y1, x2, y2): | |
""" | |
Check whether two points are connected in the grid. | |
""" | |
if x1 == x2 and y1 == y2: | |
return False | |
if x1 == x2 or y1 == y2: | |
return True | |
if (x1 + y1) % 2 == 0: | |
if (x2 - x1) == (y2 - y1): | |
return True | |
if (x2 - x1) == -(y2 - y1): | |
return True | |
return False | |
count = 0 | |
for x1, y1 in itertools.product(range(CX), range(CY)): | |
for x2, y2, direction in neighbours(x1, y1): | |
for x3, y3, _ in neighbours(x2, y2, direction): | |
if is_neighbour(x1, y1, x3, y3): | |
count += 1 | |
print(count // 6) # each triangle is counted in every permutation |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment