Created
April 14, 2019 04:31
-
-
Save srabuini/135cd04c34b7d434b939c25b1d8f44cc to your computer and use it in GitHub Desktop.
Generates a formula to scramble a Rubik's cube
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
#!/usr/bin/env ruby | |
module Scrambler | |
FACES = %w[B L D R U F].freeze | |
def self.run(steps) | |
directions = [] | |
faces = [] | |
steps.times do | |
source = FACES - last_and_parallel(faces.last) | |
faces.push(source[rand(source.size)]) | |
directions.push(["'", nil][rand(2)]) | |
end | |
faces.zip(directions) | |
end | |
def self.last_and_parallel(value) | |
pairs = [%w[B F], %w[D U], %w[L R]] | |
pairs.reduce { |memo, pair| pair.include?(value) ? pair : memo } | |
end | |
end | |
scramble = Scrambler.run(20) | |
print scramble.map { |e| e.compact.join }.join(' ') + "\n" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment