Règle 62 en C++

mail@pastecode.io avatar
unknown
c_cpp
3 months ago
1.5 kB
4
Indexable
Never
#include <iostream>
#include <string>

// Function to apply Rule 62 to a cell based on its neighbors
bool applyRule62(char left, char center, char right) {
    if (left == '#' && center == '#' && right == '#') return false;
    if (left == '#' && center == '#' && right == '_') return false;
    if (left == '_' && center == '_' && right == '_') return false;

    return true; // every other combination is true
}

int main() {
    const int num_iterations = 65; // Number of iterations to perform
    const int width = 50; // Width of the cellular automaton
    std::string current_row(width, '_'); // Current row of the cellular automaton

    // Initialize the current row with the given first row
    current_row = "_#___###___##__##__###_##____#_#__####_#__#_###___";

    // Execute iterations
    for (int iteration = 0; iteration < num_iterations; ++iteration) {
        // Print the current row
        std::cout << current_row << std::endl;

        // Calculate the next row based on Rule 62
        std::string next_row(width, '_');
        for (int i = 1; i < width - 1; ++i) {
            next_row[i] = applyRule62(current_row[i - 1], current_row[i], current_row[i + 1]) ? '#' : '_';
        }

        // Update the current row with the newly calculated row
        current_row = next_row;
    }

    // Print the result at row 50
    std::cout << "Result at row 50: " << current_row << std::endl;

    return 0;
}
Leave a Comment