Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
scipy.spatial.transform.Rotation solution in 3rd party category for Rolling 🎲! by juestr
from scipy.spatial.transform import Rotation
import numpy as np
rx = Rotation.from_euler('X', 90, degrees=True)
ry = Rotation.from_euler('Y', 90, degrees=True)
move2r = {'N': rx, 'S': rx.inv(), 'E': ry, 'W': ry.inv()}.__getitem__
moves2rs = np.vectorize(move2r, otypes='O')
initial_faces = np.array([
[0, 0, 1], [0, 1, 0], [1, 0, 0], [-1, 0, 0], [0, -1, 0], [0, 0, -1],
])
def rolling_dice(moves: str) -> int:
rotations = moves2rs(list(moves))
combined_rotation = np.product(rotations[::-1], initial=Rotation.identity())
new_faces = combined_rotation.apply(initial_faces).round().astype(int)
new_faces_up = np.logical_and.reduce(np.equal(new_faces, initial_faces[0]), axis=1)
return np.flatnonzero(new_faces_up)[0] + 1
April 28, 2023
Comments: