Created
November 19, 2020 10:35
-
-
Save monkstone/f0c880c9aa2fbdd0a3252280eacf2010 to your computer and use it in GitHub Desktop.
Wolfram Two Color Rule
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
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