Created
December 26, 2019 20:12
-
-
Save Zulko/3c9b65abd4415a859e1be4b25cab5e8d to your computer and use it in GitHub Desktop.
A cube puzzle resolution script
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
import numpy as np | |
segments_lengths = [3, 2, 2, 2, 1, 1, 1, 2, 2, 1, 1, 2, 1, 2, 1, 1, 2] | |
assert sum(segments_lengths) == 27 | |
def solve(cube, position, direction, segment_number): | |
if segment_number == len(segments_lengths): | |
return cube | |
x, y, z = direction | |
a1, a2 = np.array([y, z, x]), np.array([z, x, y]) | |
perpendicular_directions = [a1, -a1, a2, -a2] | |
for new_direction in perpendicular_directions: | |
new_cube, new_position = +cube, +position # copies | |
for _ in range(segments_lengths[segment_number]): | |
new_position = new_position + new_direction | |
x, y, z = new_position | |
in_cube = all([0 <= e < 3 for e in new_position]) | |
if not in_cube or new_cube[x, y, z]: | |
break | |
new_cube[x, y, z] = segment_number + 1 | |
else: | |
new_cube = solve( | |
new_cube, new_position, new_direction, segment_number + 1 | |
) | |
if new_cube is not None: | |
return new_cube | |
return None | |
final_cube = solve( | |
cube=np.zeros((3, 3, 3), dtype="int"), | |
position=np.array([0, 0, -1]), | |
direction=np.array([1, 0, 0]), | |
segment_number=0, | |
) | |
print(final_cube) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment