first?
commit
365a59f91f
@ -0,0 +1,3 @@
|
||||
/target
|
||||
|
||||
.vscode
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,11 @@
|
||||
[package]
|
||||
name = "rulebot"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
reqwest = { version = "0.11.16", features = ["blocking"] }
|
||||
exitcode = "1.1.2"
|
||||
rand = "0.8.5"
|
@ -0,0 +1,4 @@
|
||||
RuleBot
|
||||
=======
|
||||
|
||||
Bot that runs a random ![cellular automaton](https://en.wikipedia.org/wiki/Elementary_cellular_automaton) on an 8-bit input.
|
@ -0,0 +1,71 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue