#game-of-life #cellular-automata #parser

ca-formats

Parsing pattern files for Conway’s Game of Life

8 releases

0.2.3 Feb 20, 2021
0.2.2 Oct 31, 2020
0.1.3 Sep 24, 2020
0.1.2 May 19, 2020
0.1.1 Jan 26, 2020

#354 in Algorithms

30 downloads per month

MIT license

51KB
1K SLoC

CA formats parsers

Travis (.com) Crates.io Docs.rs 中文

Parsing pattern files for Conway's Game of Life.

The parsers read a string and return an iterator of coordinates of living cells.

Supported formats

Example

Reading from a string:

use ca_formats::rle::Rle;

const GLIDER: &str = r"#N Glider
#O Richard K. Guy
#C The smallest, most common, and first discovered spaceship. Diagonal, has period 4 and speed c/4.
#C www.conwaylife.com/wiki/index.php?title=Glider
x = 3, y = 3, rule = B3/S23
bob$2bo$3o!";

let glider = Rle::new(GLIDER).unwrap();
assert_eq!(glider.header_data().unwrap().x, 3);
assert_eq!(glider.header_data().unwrap().y, 3);
assert_eq!(glider.header_data().unwrap().rule, Some(String::from("B3/S23")));

let cells = glider.map(|cell| cell.unwrap().position).collect::<Vec<_>>();
assert_eq!(cells, vec![(1, 0), (2, 1), (0, 2), (1, 2), (2, 2)]);

Reading from a file:

use std::fs::File;
use ca_formats::rle::Rle;

let file = File::open("tests/sirrobin.rle").unwrap();
let sirrobin = Rle::new_from_file(file).unwrap();

assert_eq!(sirrobin.count(), 282);

See also

Dependencies

~1.4–2.2MB
~60K SLoC