Skip to content

Instantly share code, notes, and snippets.

@japm48
Last active January 8, 2022 01:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save japm48/62f00a7e5323db730860cf2c03e279aa to your computer and use it in GitHub Desktop.
Save japm48/62f00a7e5323db730860cf2c03e279aa to your computer and use it in GitHub Desktop.
AoC'21 orientation generation
import numpy as np
import itertools
def gen_valid_orientations_data():
# Out of all 48 choices, half of them result in a right-handed system (right is right [in this context...])
# and the other half results in a left-handed system (wrong).
# We check this via cross product: Z axis should have the same orientation as X cross Y.
#
# We only keep right-handed ones.
axis_signs_comb = list(itertools.product((+1, -1), repeat=3)) # 8 options
axis_perms = list(itertools.permutations(range(3))) # 6 options
# signature of each permutation (+1 or -1).
# https://en.wikipedia.org/wiki/Parity_of_a_permutation
perm_signatures = [
# Convert from 0/1 to +1/-1 without powers.
-1 + 2 * (1 & ((perm[2] > perm[1]) + (perm[1] > perm[0]) + (perm[2] > perm[0])))
for perm in axis_perms]
def _generator():
for signs in axis_signs_comb:
for (axis_perm, perm_signature) in zip(axis_perms, perm_signatures):
if np.prod(signs) == perm_signature:
# print(axis_perm, signs)
yield signs, axis_perm
return tuple(_generator())
VALID_ORIENTATIONS_DATA = gen_valid_orientations_data()
def generate_orientations(base_coord_list):
# NOTE: base_coord_list is a numpy array with 3 columns
for ax_signs, axis_perm in VALID_ORIENTATIONS_DATA:
permuted = base_coord_list[:, axis_perm]
yield permuted * ax_signs
# Note:
# VALID_ORIENTATIONS_DATA contains the following:
#
# (((1, 1, 1), (0, 1, 2)),
# ((1, 1, 1), (1, 2, 0)),
# ((1, 1, 1), (2, 0, 1)),
# ((1, 1, -1), (0, 2, 1)),
# ((1, 1, -1), (1, 0, 2)),
# ((1, 1, -1), (2, 1, 0)),
# ((1, -1, 1), (0, 2, 1)),
# ((1, -1, 1), (1, 0, 2)),
# ((1, -1, 1), (2, 1, 0)),
# ((1, -1, -1), (0, 1, 2)),
# ((1, -1, -1), (1, 2, 0)),
# ((1, -1, -1), (2, 0, 1)),
# ((-1, 1, 1), (0, 2, 1)),
# ((-1, 1, 1), (1, 0, 2)),
# ((-1, 1, 1), (2, 1, 0)),
# ((-1, 1, -1), (0, 1, 2)),
# ((-1, 1, -1), (1, 2, 0)),
# ((-1, 1, -1), (2, 0, 1)),
# ((-1, -1, 1), (0, 1, 2)),
# ((-1, -1, 1), (1, 2, 0)),
# ((-1, -1, 1), (2, 0, 1)),
# ((-1, -1, -1), (0, 2, 1)),
# ((-1, -1, -1), (1, 0, 2)),
# ((-1, -1, -1), (2, 1, 0)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment