Skip to content

Commit

Permalink
Application can compile and run, next steps are validation
Browse files Browse the repository at this point in the history
  • Loading branch information
reecewayt committed Jan 19, 2025
1 parent 3d307cd commit 26c259e
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 2 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@ Cargo.lock
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
#.idea/

# Added by cargo

/target
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "rust-rule110"
version = "0.1.0"
edition = "2021"

[dependencies]
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,19 @@
# Rust-Rule110
A simple rust application for assignment 1 of CS 523
Author: Reece Wayt
Contact: [email protected]
Date: 1/18/2025
---
## Algorithm Description
A simple rust application for assignment 1 of CS 523.

Rule 110 is a elementary cellular automata which means each cell is binary 0 or 1. Each generation of a string of binary is created iteratively by cycling through individual cells, considering each cells neighboring cells (i.e left and right) to determine what the next cell value will be in the next generation. Its important to note that in the case of 8-bits, where each bit is a cell, the 7th bit's right neighbor wraps around to bit position 0; vis-avis the 0th bit's left neighbor wraps around which would be the 7th bit. There are many rule sets to chose from, but this implementation uses rule 110 as summarized pictorally below.

![Rule 110 Pattern](./docs/images/rule-110-wolfram-ref.png)
*Image source: [Wolfram MathWorld - Rule 110](https://mathworld.wolfram.com/Rule110.html)*

Here I'm using **u8** method for encapsulating generational data. Another method could use an array instead.

## Building and running the application
```bash
cargo run
```
Binary file added docs/images/rule-110-wolfram-ref.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
52 changes: 52 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@


fn rule110(bits: u8) -> u8 {
let mut next_gen = 0;

for i in 0..8 {
// Get the left bit
let left = if i == 7 {
bits & 0b0000_0001 // mask all but the lsb
} else {
(bits >> (i + 1)) & 0b0000_0001
};

// Get the center bit
let center = (bits >> i) & 0b0000_0001;

// Get the right bit
let right = if i == 0 {
(bits & 0b1000_0000) >> 7 // mask all but the msb
} else {
(bits >> (i - 1)) & 0b0000_0001
};

// Apply rule 110 pattern
let pattern = (left << 2) | (center << 1) | right;
let temp_bit = match pattern {
0b111 => 0,
0b110 => 1,
0b101 => 1,
0b100 => 0,
0b011 => 1,
0b010 => 1,
0b001 => 1,
0b000 => 0,
_ => unreachable!(),
};
// Set the next generation bit
next_gen |= temp_bit << i;
};
next_gen
}

fn main() {
// Test with some initial pattern
let mut current = 0b1010_0100u8;

// Print several generations
for _ in 0..8 {
println!("{:08b}", current);
current = rule110(current);
}
}

0 comments on commit 26c259e

Please sign in to comment.