diff --git a/nandu/src/main.rs b/nandu/src/main.rs index a777ed2..28e76de 100644 --- a/nandu/src/main.rs +++ b/nandu/src/main.rs @@ -3,6 +3,7 @@ mod nandu; use anyhow::Result; use clap::Parser; use nandu::Environment; +use std::arch::x86_64::_mm256_stream_pd; use std::path::Path; #[derive(Parser, Debug)] @@ -18,7 +19,9 @@ fn main() -> Result<()> { let env = Environment::new(Path::new(&args.file))?; - println!("{:#?}", env); + let state = env.simulate(vec![false, false])?; + + println!("State: {:?}", state); Ok(()) } diff --git a/nandu/src/nandu/mod.rs b/nandu/src/nandu/mod.rs index a8ddc41..9e7d2ff 100644 --- a/nandu/src/nandu/mod.rs +++ b/nandu/src/nandu/mod.rs @@ -7,9 +7,9 @@ use std::path::Path; #[derive(Debug)] pub struct Environment { - state: Vec, // A bitmap would be more efficient, but nah, we have these resources xD logic: Vec>>, - sources: Vec<(usize, usize)>, + sources: usize, + width: usize, } impl Environment { @@ -25,9 +25,9 @@ impl Environment { }; let mut env = Self { - state: vec![false; width], logic: vec![Vec::new(); height], - sources: Vec::new(), + sources: 0, + width, }; for (y, line) in data.lines().filter(|x| !x.is_empty()).enumerate() { @@ -35,6 +35,7 @@ impl Environment { for (x, field) in line.split(" ").filter(|x| !x.is_empty()).enumerate() { if field.contains("Q") { env.logic[y].push(Some(Gate::Source)); + env.sources += 1; skip = false; } else if field.contains("L") { env.logic[y].push(Some(Gate::Led)); @@ -48,8 +49,8 @@ impl Environment { skip = true; match field { "" => continue, - "r" => env.logic[y].push(Some(Gate::NotLeft)), - "R" => env.logic[y].push(Some(Gate::NotRight)), + "r" => env.logic[y].push(Some(Gate::NotRight)), + "R" => env.logic[y].push(Some(Gate::NotLeft)), "W" => env.logic[y].push(Some(Gate::Nand)), "B" => env.logic[y].push(Some(Passthrough)), c => { @@ -61,7 +62,40 @@ impl Environment { } } } - Ok(env) } + + pub fn sources(&self) -> usize { + self.sources + } + + pub fn simulate(&self, sources: Vec) -> Result> { + let mut state = vec![false; self.width]; + + let mut source_index = 0usize; + + for row in &self.logic { + let mut x = 0usize; + for field in row { + match field { + Some(g) => match g { + Gate::Source => { + state[x] = sources[source_index]; + source_index += 1; + } + Gate::Led => (), + _ => { + (state[x], state[x + 1]) = g.compute(state[x], state[x + 1]); + x += 1; + } + }, + None => state[x] = false, + } + x += 1; + } + // println!("{:?}", state); + } + + Ok(state) + } }