Skip to content

Instantly share code, notes, and snippets.

@srabuini
Created April 14, 2019 04:31
Show Gist options
  • Save srabuini/135cd04c34b7d434b939c25b1d8f44cc to your computer and use it in GitHub Desktop.
Save srabuini/135cd04c34b7d434b939c25b1d8f44cc to your computer and use it in GitHub Desktop.
Generates a formula to scramble a Rubik's cube
#!/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