Last active
February 4, 2024 03:41
-
-
Save ParadoxV5/71ca53b9e4b5efe1692cd767486bc5d1 to your computer and use it in GitHub Desktop.
Ruby Discord Valentine’s Challenge
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
# frozen_string_literal: true | |
class CellularAutomaton | |
def initialize(grid) | |
@grid = grid | |
# Back Buffer | |
@grid2 = grid.map(&:dup) | |
@rules = yield self | |
end | |
def self.load(file, ...) = new(file.each_line(chomp: true).map(&:chars), ...) | |
def [](x, y) | |
return nil if x.negative? or y.negative? | |
@grid.dig y, x | |
end | |
# @return a flat `nil`-less array of the 3×3 region surrounding the given coörds | |
def flat_get_33(x, y) | |
Enumerator.product( | |
[x.pred, x, x.succ], | |
[y.pred, y, y.succ] | |
).filter_map { self[*_1] } | |
end | |
def tick | |
@grid.each_with_index do|row, y| | |
row.each_with_index do|cell, x| | |
@grid2.fetch(y)[x] = @rules[cell]&.[](x, y) || cell | |
end | |
end | |
@grid, @grid2 = @grid2, @grid | |
end | |
def display = @grid.each { puts _1.join } | |
def run(steps: nil, sleep: 1) | |
system('clear') or system('cls') | |
(steps ? steps.times : loop).each do | |
print "\e[H" # Position cursor at the beginning of the console | |
display | |
tick | |
sleep sleep | |
end | |
end | |
# [Conway’s Game of Life](https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life) | |
#load(DATA) do|ca| | |
# live = '🟩' | |
# dead = '🟪' | |
# { | |
# live => ->(x, y) { dead unless [3, 4].include? ca.flat_get_33(x, y).count(live) }, | |
# dead => ->(x, y) { live if 3.equal? ca.flat_get_33(x, y).count(live) } | |
# } | |
#end.run | |
# [Wireworld](https://en.wikipedia.org/wiki/Wireworld) | |
load(DATA) do|ca| | |
head = '🟥' | |
tail = '🟨' | |
conductor = '🟪' | |
{ | |
head => proc { tail }, | |
tail => proc { conductor }, | |
conductor => ->(x, y) { head if [1, 2].include? ca.flat_get_33(x, y).count(head) } | |
} | |
end.run | |
end | |
__END__ | |
🟫🟪🟪🟪🟥🟨🟪🟪🟪🟪🟪🟪🟪🟪🟥🟨🟪🟪🟪🟪🟪🟪🟪🟪🟥🟨🟪🟪🟪🟫 | |
🟪🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟪 | |
🟪🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟪 | |
🟪🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟪 | |
🟪🟫🟫🟫🟫🟫🟫🟫🟫🟪🟪🟪🟫🟫🟫🟫🟫🟫🟪🟪🟪🟫🟫🟫🟫🟫🟫🟫🟫🟪 | |
🟪🟫🟫🟫🟫🟫🟫🟫🟪🟦🟦🟦🟪🟫🟫🟫🟫🟪🟦🟦🟦🟪🟫🟫🟫🟫🟫🟫🟫🟪 | |
🟨🟫🟫🟫🟫🟫🟫🟪🟦🟦🟦🟦🟦🟪🟫🟫🟪🟦🟦🟦🟦🟦🟪🟫🟫🟫🟫🟫🟫🟥 | |
🟥🟫🟫🟫🟫🟫🟫🟪🟦🟦🟦🟦🟦🟪🟫🟫🟪🟦🟦🟦🟦🟦🟪🟫🟫🟫🟫🟫🟫🟨 | |
🟪🟫🟫🟫🟫🟫🟫🟪🟦🟦🟦🟦🟦🟦🟪🟪🟦🟦🟦🟦🟦🟦🟪🟫🟫🟫🟫🟫🟫🟪 | |
🟪🟫🟫🟫🟫🟫🟫🟪🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟪🟫🟫🟫🟫🟫🟫🟪 | |
🟪🟫🟫🟫🟫🟫🟫🟪🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟪🟫🟫🟫🟫🟫🟫🟪 | |
🟪🟫🟫🟫🟫🟫🟫🟪🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟪🟫🟫🟫🟫🟫🟫🟪 | |
🟪🟫🟫🟫🟫🟫🟫🟪🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟪🟫🟫🟫🟫🟫🟫🟪 | |
🟪🟫🟫🟫🟫🟫🟫🟪🟪🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟪🟪🟫🟫🟫🟫🟫🟫🟪 | |
🟪🟫🟫🟫🟫🟫🟫🟫🟫🟪🟦🟦🟦🟦🟦🟦🟦🟦🟦🟦🟪🟫🟫🟫🟫🟫🟫🟫🟫🟪 | |
🟪🟫🟫🟫🟫🟫🟫🟫🟫🟪🟪🟦🟦🟦🟦🟦🟦🟦🟦🟪🟪🟫🟫🟫🟫🟫🟫🟫🟫🟪 | |
🟨🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟪🟦🟦🟦🟦🟦🟦🟪🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟥 | |
🟥🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟪🟪🟦🟦🟦🟦🟪🟪🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟨 | |
🟪🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟪🟦🟦🟪🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟪 | |
🟪🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟪🟪🟪🟪🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟪 | |
🟪🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟪🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟪 | |
🟪🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟪🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟪 | |
🟪🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟥🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟫🟪 | |
🟫🟪🟪🟪🟨🟥🟪🟪🟪🟪🟪🟪🟪🟪🟨🟥🟪🟪🟪🟪🟪🟪🟪🟪🟨🟥🟪🟪🟪🟫 |
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 CellularAutomaton[C < _ToS] | |
type grid[C] = Array[Array[C]] | |
@grid: grid[C] | |
@grid2: grid[C] | |
interface _Rule[C] | |
def []: (Integer x, Integer y) -> C | |
end | |
type rules[C] = Hash[C, _Rule[C]] | |
@rules: rules[C] | |
def initialize: (grid[C]) { (self) -> rules[C] } -> void | |
def self.load: (IO file) { (instance) -> rules[String] } -> instance | |
def []: (Integer x, Integer y) -> C? | |
def flat_get_33: (Integer x, Integer y) -> Array[C] | |
def tick: () -> void | |
def display: () -> void | |
def run: (steps: Integer?, sleep: Integer | Float) -> void | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment