1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
use std::error::Error;
use std::fmt;
use std::str::FromStr;
const TRUTH_TABLE_SIZE: usize = 9;
/// A representation of a rule of [Life-like cellular automata](https://conwaylife.com/wiki/Life-like_cellular_automaton).
///
/// The following operations are supported:
///
/// - Constructing from a pair of truth tables
/// - Parsing a string into a value of this type, e.g., `"B3/S23"`.
/// The following notations are supported, see [Rulestring](https://conwaylife.com/wiki/Rulestring):
/// - The birth/survival notation (e.g., `"B3/S23"`). Lowercase `'b'` or `'s'` are also allowed in the notation instead of `'B'` or `'S'`
/// - S/B notation (e.g., `"23/3"`)
/// - Determining whether a new cell will be born from the specified number of alive neighbors
/// - Determining whether a cell surrounded by the specified number of alive neighbors will survive
/// - Converting into a [`String`] value, e.g., `"B3/S23"`.
/// This operation only supports the birth/survival notation
///
/// [`String`]: std::string::String
///
/// # Examples
///
/// ```
/// use life_backend::Rule;
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let rule = "B3/S23".parse::<Rule>()?;
/// for i in 0..=8 {
/// assert_eq!(rule.is_born(i), [3].iter().any(|&x| x == i));
/// assert_eq!(rule.is_survive(i), [2, 3].iter().any(|&x| x == i));
/// }
/// assert_eq!(format!("{rule}"), "B3/S23");
/// # Ok(())
/// # }
/// ```
///
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct Rule {
birth: [bool; TRUTH_TABLE_SIZE],
survival: [bool; TRUTH_TABLE_SIZE],
}
// Inherent methods
impl Rule {
/// Creates a new rule based on the specified pair of truth tables.
///
/// # Examples
///
/// ```
/// use life_backend::Rule;
/// let rule = Rule::new(
/// &[false, false, false, true, false, false, false, false, false],
/// &[false, false, true, true, false, false, false, false, false],
/// );
/// let b = [3];
/// let s = [2, 3];
/// for i in 0..=8 {
/// assert_eq!(rule.is_born(i), b.iter().any(|&x| x == i));
/// assert_eq!(rule.is_survive(i), s.iter().any(|&x| x == i));
/// }
/// ```
///
pub const fn new(birth: &[bool; 9], survival: &[bool; 9]) -> Self {
Self {
birth: *birth,
survival: *survival,
}
}
/// Returns whether a new cell will be born from the specified number of alive neighbors.
///
/// # Panics
///
/// Panics if the argument `count` is greater than 8.
///
/// # Examples
///
/// ```
/// use life_backend::Rule;
/// let rule = Rule::conways_life();
/// let b = [3];
/// for i in 0..=8 {
/// assert_eq!(rule.is_born(i), b.iter().any(|&x| x == i));
/// }
/// ```
///
#[inline]
pub const fn is_born(&self, count: usize) -> bool {
self.birth[count]
}
/// Returns whether a cell surrounded by the specified number of alive neighbors will survive.
///
/// # Panics
///
/// Panics if the argument `count` is greater than 8.
///
/// # Examples
///
/// ```
/// use life_backend::Rule;
/// let rule = Rule::conways_life();
/// let s = [2, 3];
/// for i in 0..=8 {
/// assert_eq!(rule.is_survive(i), s.iter().any(|&x| x == i));
/// }
/// ```
///
#[inline]
pub const fn is_survive(&self, count: usize) -> bool {
self.survival[count]
}
/// Returns the rule of [Conway's Game of Life](https://conwaylife.com/wiki/Conway%27s_Game_of_Life).
///
/// # Examples
///
/// ```
/// use life_backend::Rule;
/// let rule = Rule::conways_life();
/// let b = [3];
/// let s = [2, 3];
/// for i in 0..=8 {
/// assert_eq!(rule.is_born(i), b.iter().any(|&x| x == i));
/// assert_eq!(rule.is_survive(i), s.iter().any(|&x| x == i));
/// }
/// ```
///
pub const fn conways_life() -> Self {
Self::new(
&[false, false, false, true, false, false, false, false, false],
&[false, false, true, true, false, false, false, false, false],
)
}
}
// Trait implementations
impl fmt::Display for Rule {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fn count_slice_numbers(slice: &[bool]) -> usize {
slice.iter().filter(|x| **x).count()
}
fn convert_slice_to_string(slice: &[bool]) -> String {
slice
.iter()
.enumerate()
.filter_map(|(i, &x)| if x { Some(i) } else { None })
.map(|n| char::from_digit(n as u32, TRUTH_TABLE_SIZE as u32).unwrap()) // this unwrap never panic because `n < TRUTH_TABLE_SIZE` is always guaranteed
.collect()
}
let mut buf = String::with_capacity(count_slice_numbers(&self.birth) + count_slice_numbers(&self.survival));
buf += "B";
buf += &convert_slice_to_string(&self.birth);
buf += "/S";
buf += &convert_slice_to_string(&self.survival);
f.write_str(&buf)?;
Ok(())
}
}
#[derive(Debug)]
pub struct ParseRuleError;
impl Error for ParseRuleError {}
impl fmt::Display for ParseRuleError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str("cannot parse rule from the string")
}
}
impl FromStr for Rule {
type Err = ParseRuleError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
fn convert_numbers_to_slice(numbers: &str) -> Option<[bool; TRUTH_TABLE_SIZE]> {
numbers.chars().try_fold([false; TRUTH_TABLE_SIZE], |mut buf, c| {
let n = c.to_digit(TRUTH_TABLE_SIZE as u32)? as usize;
buf[n] = true;
Some(buf)
})
}
let fields_splitted: Vec<_> = s.split('/').collect();
if fields_splitted.len() != 2 {
return Err(ParseRuleError);
}
let (labels, numbers): (Vec<_>, Vec<_>) = fields_splitted
.iter()
.map(|s| s.split_at(s.find(|c: char| c.is_ascii_digit()).unwrap_or(s.len())))
.unzip();
let numbers = if labels.iter().zip(["B", "S"]).all(|(lhs, rhs)| lhs.eq_ignore_ascii_case(rhs)) {
// the birth/survival notation, e.g., "B3/S23"
numbers
} else if labels.iter().all(|s| s.is_empty()) {
// S/B notation, e.g., "23/3"
vec![numbers[1], numbers[0]]
} else {
return Err(ParseRuleError);
};
let Some(slices) = numbers
.into_iter()
.map(convert_numbers_to_slice)
.collect::<Option<Vec<_>>>() else {
return Err(ParseRuleError);
};
Ok(Self {
birth: slices[0],
survival: slices[1],
})
}
}
// Unit tests
#[cfg(test)]
mod tests {
use super::*;
use anyhow::Result;
const RULE_HIGHLIFE: Rule = Rule::new(
&[false, false, false, true, false, false, true, false, false],
&[false, false, true, true, false, false, false, false, false],
);
fn check_value(target: &Rule, expected_birth: &[usize], expected_survival: &[usize]) {
for i in 0..=8 {
assert_eq!(target.is_born(i), expected_birth.iter().any(|&x| x == i));
assert_eq!(target.is_survive(i), expected_survival.iter().any(|&x| x == i));
}
}
#[test]
fn new_conways_life() {
let target = Rule::new(
&[false, false, false, true, false, false, false, false, false],
&[false, false, true, true, false, false, false, false, false],
);
check_value(&target, &[3], &[2, 3]);
}
#[test]
fn new_highlife() {
let target = Rule::new(
&[false, false, false, true, false, false, true, false, false],
&[false, false, true, true, false, false, false, false, false],
);
check_value(&target, &[3, 6], &[2, 3]);
}
#[test]
fn conways_life() {
let target = Rule::conways_life();
check_value(&target, &[3], &[2, 3]);
}
#[test]
fn display_conways_life() {
let target = Rule::conways_life();
assert_eq!(target.to_string(), "B3/S23");
}
#[test]
fn display_highlife() {
let target = RULE_HIGHLIFE;
assert_eq!(target.to_string(), "B36/S23");
}
#[test]
fn from_str_birth_survival_notation() -> Result<()> {
let target: Rule = "B3/S23".parse()?;
check_value(&target, &[3], &[2, 3]);
Ok(())
}
#[test]
fn from_str_s_b_notation() -> Result<()> {
let target: Rule = "23/3".parse()?;
check_value(&target, &[3], &[2, 3]);
Ok(())
}
#[test]
fn from_str_birth_survival_notation_without_birth_number() -> Result<()> {
let target: Rule = "B/S23".parse()?;
check_value(&target, &[], &[2, 3]);
Ok(())
}
#[test]
fn from_str_birth_survival_notation_without_survival_number() -> Result<()> {
let target: Rule = "B3/S".parse()?;
check_value(&target, &[3], &[]);
Ok(())
}
#[test]
fn from_str_birth_survival_notation_lowercase_b() -> Result<()> {
let target: Rule = "b3/S23".parse()?;
check_value(&target, &[3], &[2, 3]);
Ok(())
}
#[test]
fn from_str_birth_survival_notation_lowercase_s() -> Result<()> {
let target: Rule = "B3/s23".parse()?;
check_value(&target, &[3], &[2, 3]);
Ok(())
}
#[test]
fn from_str_no_separator() {
let target = "B0S0".parse::<Rule>();
assert!(target.is_err());
}
#[test]
fn from_str_too_many_separators() {
let target = "B0/S0/C0".parse::<Rule>();
assert!(target.is_err());
}
#[test]
fn from_str_no_label_birth() {
let target = "0/S0".parse::<Rule>();
assert!(target.is_err());
}
#[test]
fn from_str_no_label_survival() {
let target = "B0/0".parse::<Rule>();
assert!(target.is_err());
}
#[test]
fn from_str_birth_survival_notation_too_large_number() {
let target = "B9/S0".parse::<Rule>();
assert!(target.is_err());
}
}