-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Application can compile and run, next steps are validation
- Loading branch information
Showing
5 changed files
with
81 additions
and
2 deletions.
There are no files selected for viewing
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
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
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] |
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
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. | ||
|
||
 | ||
*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 | ||
``` |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
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); | ||
} | ||
} |