You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
72 lines
1.7 KiB
Rust
72 lines
1.7 KiB
Rust
2 years ago
|
use rand::Rng;
|
||
|
|
||
|
type State = [bool; 8];
|
||
|
|
||
|
fn map_state(rule: u8, state: &State) -> State {
|
||
|
let mut new_state: [bool; 8] = [false; 8];
|
||
|
|
||
|
for i in 0..8 {
|
||
|
let pattern = ((i != 0 && state[i - 1]) as u8 * 4)
|
||
|
| (state[i] as u8 * 2)
|
||
|
| ((i != 7 && state[i + 1]) as u8);
|
||
|
new_state[i] = (rule >> pattern & 1) == 1;
|
||
|
}
|
||
|
|
||
|
new_state
|
||
|
}
|
||
|
|
||
|
fn state_to_string(state: State) -> String {
|
||
|
state.map(|c| if c { "⬛" } else { "⬜" }).concat()
|
||
|
}
|
||
|
|
||
|
fn num_to_state(num: u8) -> State {
|
||
|
let mut state = [false; 8];
|
||
|
|
||
|
for i in 0..8 {
|
||
|
state[i] = (num >> i & 1) == 1;
|
||
|
}
|
||
|
|
||
|
state
|
||
|
}
|
||
|
|
||
|
fn main() {
|
||
|
let mut rng = rand::thread_rng();
|
||
|
let seed: u16 = rng.gen();
|
||
|
|
||
|
let rule: u8 = (seed & 0xff) as u8;
|
||
|
let init_state = num_to_state((seed >> 8 & 0xff) as u8);
|
||
|
|
||
|
let states: String = (0..8)
|
||
|
.scan(init_state, |state, _i| {
|
||
|
let old_state = *state;
|
||
|
*state = map_state(rule, state);
|
||
|
Some(old_state)
|
||
|
})
|
||
|
.map(state_to_string)
|
||
|
.collect::<Vec<String>>()
|
||
|
.join("\n");
|
||
|
|
||
|
println!("Rule: {}\n{}", rule, states);
|
||
|
|
||
|
let client = reqwest::blocking::Client::new();
|
||
|
let res = client
|
||
|
.post("https://mastodon.raptorpond.com/api/v1/statuses")
|
||
|
.header(
|
||
|
"Authorization",
|
||
|
format!("Bearer {}", std::env::var("MASTODON_TOKEN").unwrap()),
|
||
|
)
|
||
|
.form(&[
|
||
|
("status", format!("Rule: {}\n{}", rule, states)),
|
||
|
("visibility", String::from("private")),
|
||
|
])
|
||
|
.send();
|
||
|
|
||
|
match res {
|
||
|
Ok(_) => std::process::exit(exitcode::OK),
|
||
|
Err(error) => {
|
||
|
println!("{:?}", error);
|
||
|
std::process::exit(exitcode::IOERR);
|
||
|
}
|
||
|
}
|
||
|
}
|