|
|
@ -1,11 +1,25 @@
|
|
|
|
use std::{fmt::Display, future::Future, ops::Index, time::Duration};
|
|
|
|
use std::{
|
|
|
|
|
|
|
|
fmt::Display,
|
|
|
|
|
|
|
|
future::Future,
|
|
|
|
|
|
|
|
ops::{Index, IndexMut},
|
|
|
|
|
|
|
|
time::Duration,
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
use rand::Rng;
|
|
|
|
use rand::Rng;
|
|
|
|
use tokio::{signal, time};
|
|
|
|
use tokio::{signal, time};
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy)]
|
|
|
|
#[derive(Debug, Clone, Copy, Default)]
|
|
|
|
struct State([bool; 8]);
|
|
|
|
struct State([bool; 8]);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl State {
|
|
|
|
|
|
|
|
fn rule_iterator(&self, rule: u8) -> impl Iterator<Item = State> {
|
|
|
|
|
|
|
|
StateIterator {
|
|
|
|
|
|
|
|
rule: rule,
|
|
|
|
|
|
|
|
state: *self,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl Index<usize> for State {
|
|
|
|
impl Index<usize> for State {
|
|
|
|
type Output = bool;
|
|
|
|
type Output = bool;
|
|
|
|
|
|
|
|
|
|
|
@ -14,17 +28,35 @@ impl Index<usize> for State {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn map_state(rule: u8, state: &State) -> State {
|
|
|
|
impl IndexMut<usize> for State {
|
|
|
|
let mut new_state: [bool; 8] = [false; 8];
|
|
|
|
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
|
|
|
|
|
|
|
|
&mut self.0[index]
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct StateIterator {
|
|
|
|
|
|
|
|
rule: u8,
|
|
|
|
|
|
|
|
state: State,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl Iterator for StateIterator {
|
|
|
|
|
|
|
|
type Item = State;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
|
|
|
|
let old_state = self.state;
|
|
|
|
|
|
|
|
let mut new_state = State::default();
|
|
|
|
|
|
|
|
|
|
|
|
for i in 0..8 {
|
|
|
|
for i in 0..8 {
|
|
|
|
let pattern = ((i != 0 && state[i - 1]) as u8 * 4)
|
|
|
|
let pattern = ((i != 0 && self.state[i - 1]) as u8 * 4)
|
|
|
|
| (state[i] as u8 * 2)
|
|
|
|
| (self.state[i] as u8 * 2)
|
|
|
|
| ((i != 7 && state[i + 1]) as u8);
|
|
|
|
| ((i != 7 && self.state[i + 1]) as u8);
|
|
|
|
new_state[i] = (rule >> pattern & 1) == 1;
|
|
|
|
new_state[i] = (self.rule >> pattern & 1) == 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
State(new_state)
|
|
|
|
self.state = new_state;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Some(old_state)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl Display for State {
|
|
|
|
impl Display for State {
|
|
|
@ -72,14 +104,11 @@ fn send_status(
|
|
|
|
|
|
|
|
|
|
|
|
fn generate(seed: u16) -> String {
|
|
|
|
fn generate(seed: u16) -> String {
|
|
|
|
let rule: u8 = (seed & 0xff) as u8;
|
|
|
|
let rule: u8 = (seed & 0xff) as u8;
|
|
|
|
let init_state = ((seed >> 8 & 0xff) as u8).into();
|
|
|
|
let init_state: State = ((seed >> 8 & 0xff) as u8).into();
|
|
|
|
|
|
|
|
|
|
|
|
(0..8)
|
|
|
|
init_state
|
|
|
|
.scan(init_state, |state, _i| {
|
|
|
|
.rule_iterator(rule)
|
|
|
|
let old_state = *state;
|
|
|
|
.take(8)
|
|
|
|
*state = map_state(rule, state);
|
|
|
|
|
|
|
|
Some(old_state)
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
.map(|state| state.to_string())
|
|
|
|
.map(|state| state.to_string())
|
|
|
|
.collect::<Vec<String>>()
|
|
|
|
.collect::<Vec<String>>()
|
|
|
|
.join("\n")
|
|
|
|
.join("\n")
|
|
|
|