feat(nandu): added testing of all possibilities

This commit is contained in:
antifallobst 2023-09-20 16:03:52 +02:00
parent 2c379dcfd8
commit 2d9959954e
Signed by: antifallobst
GPG Key ID: 2B4F402172791BAF
3 changed files with 76 additions and 7 deletions

View File

@ -2,8 +2,8 @@ mod nandu;
use anyhow::Result; use anyhow::Result;
use clap::Parser; use clap::Parser;
use nandu::output::Output;
use nandu::Environment; use nandu::Environment;
use std::arch::x86_64::_mm256_stream_pd;
use std::path::Path; use std::path::Path;
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
@ -19,9 +19,18 @@ fn main() -> Result<()> {
let env = Environment::new(Path::new(&args.file))?; let env = Environment::new(Path::new(&args.file))?;
let state = env.simulate(vec![false, false])?; let mut output = Output::new();
println!("State: {:?}", state); for i in 0..2usize.pow(env.sources() as u32) {
let mut sources = vec![false; env.sources()];
for j in 0..env.sources() {
sources[j] = (i & (1 << j)) > 0;
}
let leds = env.simulate(&sources)?;
output.push(sources, leds);
}
println!("{}", output);
Ok(()) Ok(())
} }

View File

@ -1,4 +1,5 @@
mod gate; mod gate;
pub mod output;
use crate::nandu::gate::Gate; use crate::nandu::gate::Gate;
use crate::nandu::gate::Gate::Passthrough; use crate::nandu::gate::Gate::Passthrough;
@ -32,7 +33,7 @@ impl Environment {
for (y, line) in data.lines().filter(|x| !x.is_empty()).enumerate() { for (y, line) in data.lines().filter(|x| !x.is_empty()).enumerate() {
let mut skip = false; let mut skip = false;
for (x, field) in line.split(" ").filter(|x| !x.is_empty()).enumerate() { for field in line.split(" ").filter(|x| !x.is_empty()) {
if field.contains("Q") { if field.contains("Q") {
env.logic[y].push(Some(Gate::Source)); env.logic[y].push(Some(Gate::Source));
env.sources += 1; env.sources += 1;
@ -69,9 +70,11 @@ impl Environment {
self.sources self.sources
} }
pub fn simulate(&self, sources: Vec<bool>) -> Result<Vec<bool>> { pub fn simulate(&self, sources: &Vec<bool>) -> Result<Vec<bool>> {
let mut state = vec![false; self.width]; let mut state = vec![false; self.width];
let mut leds = Vec::new();
let mut source_index = 0usize; let mut source_index = 0usize;
for row in &self.logic { for row in &self.logic {
@ -83,7 +86,7 @@ impl Environment {
state[x] = sources[source_index]; state[x] = sources[source_index];
source_index += 1; source_index += 1;
} }
Gate::Led => (), Gate::Led => leds.push(state[x]),
_ => { _ => {
(state[x], state[x + 1]) = g.compute(state[x], state[x + 1]); (state[x], state[x + 1]) = g.compute(state[x], state[x + 1]);
x += 1; x += 1;
@ -93,9 +96,10 @@ impl Environment {
} }
x += 1; x += 1;
} }
// println!("{:?}", row);
// println!("{:?}", state); // println!("{:?}", state);
} }
Ok(state) Ok(leds)
} }
} }

56
nandu/src/nandu/output.rs Normal file
View File

@ -0,0 +1,56 @@
use std::fmt::{Display, Formatter};
pub struct Output {
sources: Vec<Vec<bool>>,
leds: Vec<Vec<bool>>,
n: usize,
}
impl Display for Output {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
for x in 0..self.sources[0].len() {
write!(f, "| Q{:<2}", x + 1)?;
}
write!(f, "|")?;
for x in 0..self.leds[0].len() {
write!(f, "| L{:<2}", x + 1)?;
}
write!(f, "|\n")?;
for _ in 0..self.sources[0].len() {
write!(f, "|{:-<4}", "-")?;
}
write!(f, "|")?;
for _ in 0..self.leds[0].len() {
write!(f, "|{:-<4}", "-")?;
}
write!(f, "|\n")?;
for y in 0..self.n {
for x in 0..self.sources[y].len() {
write!(f, "| {:b} ", self.sources[y][x] as u8)?;
}
write!(f, "|")?;
for x in 0..self.leds[y].len() {
write!(f, "| {:b} ", self.leds[y][x] as u8)?;
}
write!(f, "|\n")?;
}
Ok(())
}
}
impl Output {
pub fn new() -> Self {
Self {
sources: Vec::new(),
leds: Vec::new(),
n: 0,
}
}
pub fn push(&mut self, sources: Vec<bool>, leds: Vec<bool>) {
self.sources.push(sources);
self.leds.push(leds);
self.n += 1;
}
}