Skip to content

Instantly share code, notes, and snippets.

@monkstone
Created November 19, 2020 10:35
Show Gist options
  • Save monkstone/f0c880c9aa2fbdd0a3252280eacf2010 to your computer and use it in GitHub Desktop.
Save monkstone/f0c880c9aa2fbdd0a3252280eacf2010 to your computer and use it in GitHub Desktop.
Wolfram Two Color Rule
class TwoColorRule
attr_reader :rule, :colors
def initialize(number:, colors:)
@colors = colors
@rule = create_rule_from_int(number)
end
def apply_rule(input)
input.each_cons(3) do |triple|
left, center, right = *triple
sum = triple.inject(:+)
white = colors[0]
black = colors[1]
idx = 0
case sum
when 3 * white
idx = 7
when 2 * white + black
idx = left == center ? 5 : left == right ? 4 : 6
when 2 * black + white
idx = left == center ? 5 : left == right ? 4 : 6
end
end
end
private
def create_rule_from_int(number)
format("%08b", number).split('').map { |s| s.to_i }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment