feat(nandu): implemented input file parsing

This commit is contained in:
antifallobst 2023-09-17 23:44:57 +02:00
parent 80764ac26a
commit 18f419dfe5
Signed by: antifallobst
GPG Key ID: 2B4F402172791BAF
6 changed files with 344 additions and 3 deletions

230
nandu/Cargo.lock generated Normal file
View File

@ -0,0 +1,230 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "anstream"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b1f58811cfac344940f1a400b6e6231ce35171f614f26439e80f8c1465c5cc0c"
dependencies = [
"anstyle",
"anstyle-parse",
"anstyle-query",
"anstyle-wincon",
"colorchoice",
"utf8parse",
]
[[package]]
name = "anstyle"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b84bf0a05bbb2a83e5eb6fa36bb6e87baa08193c35ff52bbf6b38d8af2890e46"
[[package]]
name = "anstyle-parse"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "938874ff5980b03a87c5524b3ae5b59cf99b1d6bc836848df7bc5ada9643c333"
dependencies = [
"utf8parse",
]
[[package]]
name = "anstyle-query"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5ca11d4be1bab0c8bc8734a9aa7bf4ee8316d462a08c6ac5052f888fef5b494b"
dependencies = [
"windows-sys",
]
[[package]]
name = "anstyle-wincon"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "58f54d10c6dfa51283a066ceab3ec1ab78d13fae00aa49243a45e4571fb79dfd"
dependencies = [
"anstyle",
"windows-sys",
]
[[package]]
name = "anyhow"
version = "1.0.75"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a4668cab20f66d8d020e1fbc0ebe47217433c1b6c8f2040faf858554e394ace6"
[[package]]
name = "clap"
version = "4.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "84ed82781cea27b43c9b106a979fe450a13a31aab0500595fb3fc06616de08e6"
dependencies = [
"clap_builder",
"clap_derive",
]
[[package]]
name = "clap_builder"
version = "4.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2bb9faaa7c2ef94b2743a21f5a29e6f0010dff4caa69ac8e9d6cf8b6fa74da08"
dependencies = [
"anstream",
"anstyle",
"clap_lex",
"strsim",
]
[[package]]
name = "clap_derive"
version = "4.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0862016ff20d69b84ef8247369fabf5c008a7417002411897d40ee1f4532b873"
dependencies = [
"heck",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "clap_lex"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cd7cc57abe963c6d3b9d8be5b06ba7c8957a930305ca90304f24ef040aa6f961"
[[package]]
name = "colorchoice"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7"
[[package]]
name = "heck"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8"
[[package]]
name = "nandu"
version = "0.1.0"
dependencies = [
"anyhow",
"clap",
]
[[package]]
name = "proc-macro2"
version = "1.0.67"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d433d9f1a3e8c1263d9456598b16fec66f4acc9a74dacffd35c7bb09b3a1328"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.33"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae"
dependencies = [
"proc-macro2",
]
[[package]]
name = "strsim"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
[[package]]
name = "syn"
version = "2.0.33"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9caece70c63bfba29ec2fed841a09851b14a235c60010fa4de58089b6c025668"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "unicode-ident"
version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
[[package]]
name = "utf8parse"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a"
[[package]]
name = "windows-sys"
version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9"
dependencies = [
"windows-targets",
]
[[package]]
name = "windows-targets"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c"
dependencies = [
"windows_aarch64_gnullvm",
"windows_aarch64_msvc",
"windows_i686_gnu",
"windows_i686_msvc",
"windows_x86_64_gnu",
"windows_x86_64_gnullvm",
"windows_x86_64_msvc",
]
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8"
[[package]]
name = "windows_aarch64_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc"
[[package]]
name = "windows_i686_gnu"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e"
[[package]]
name = "windows_i686_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406"
[[package]]
name = "windows_x86_64_gnu"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc"
[[package]]
name = "windows_x86_64_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538"

View File

@ -6,3 +6,5 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
anyhow = "1.0"
clap = { version = "4.4.2", features = ["derive"] }

View File

@ -4,4 +4,4 @@ X W W X
r R R r r R R r
X B B X X B B X
X W W X X W W X
X L1 L2 X X L1 L2 X

View File

@ -1,3 +1,24 @@
fn main() { mod nandu;
println!("Hello, world!");
use anyhow::Result;
use clap::Parser;
use nandu::Environment;
use std::path::Path;
#[derive(Parser, Debug)]
#[command(author, version, long_about = None)]
struct Args {
/// The file that contains the Nandu environment that should be simulated
file: String,
}
fn main() -> Result<()> {
println!("BWINF 42.1 - <Name Censored> - Aufgabe 4: Nandu");
let args = Args::parse();
let env = Environment::new(Path::new(&args.file))?;
println!("{:#?}", env);
Ok(())
} }

21
nandu/src/nandu/gate.rs Normal file
View File

@ -0,0 +1,21 @@
#[derive(Debug, Clone)]
pub enum Gate {
Nand,
NotLeft,
NotRight,
Passthrough,
Source,
Led,
}
impl Gate {
pub fn compute(&self, s1: bool, s2: bool) -> (bool, bool) {
match self {
Self::Nand => (!(s1 && s2), !(s1 && s2)),
Self::NotLeft => (!s1, !s1),
Self::NotRight => (!s2, !s2),
Self::Passthrough => (s1, s2),
_ => (false, false),
}
}
}

67
nandu/src/nandu/mod.rs Normal file
View File

@ -0,0 +1,67 @@
mod gate;
use crate::nandu::gate::Gate;
use crate::nandu::gate::Gate::Passthrough;
use anyhow::{Error, Result};
use std::path::Path;
#[derive(Debug)]
pub struct Environment {
state: Vec<bool>, // A bitmap would be more efficient, but nah, we have these resources xD
logic: Vec<Vec<Option<Gate>>>,
sources: Vec<(usize, usize)>,
}
impl Environment {
pub fn new(path: &Path) -> Result<Self> {
let file_content = std::fs::read_to_string(path)?;
let (width, height, data) = {
let (header, data) = file_content.split_once(|c| c == '\n' || c == '\r').unwrap();
let raw = header
.split(" ")
.filter(|x| !x.is_empty())
.collect::<Vec<&str>>();
(raw[0].parse::<usize>()?, raw[1].parse::<usize>()?, data)
};
let mut env = Self {
state: vec![false; width],
logic: vec![Vec::new(); height],
sources: Vec::new(),
};
for (y, line) in data.lines().filter(|x| !x.is_empty()).enumerate() {
let mut skip = false;
for (x, field) in line.split(" ").filter(|x| !x.is_empty()).enumerate() {
if field.contains("Q") {
env.logic[y].push(Some(Gate::Source));
skip = false;
} else if field.contains("L") {
env.logic[y].push(Some(Gate::Led));
skip = false;
} else if field.contains("X") {
env.logic[y].push(None);
skip = false;
} else if skip {
skip = false;
} else {
skip = true;
match field {
"" => continue,
"r" => env.logic[y].push(Some(Gate::NotLeft)),
"R" => env.logic[y].push(Some(Gate::NotRight)),
"W" => env.logic[y].push(Some(Gate::Nand)),
"B" => env.logic[y].push(Some(Passthrough)),
c => {
return Err(Error::msg(format!(
"Failed to parse nandu file -> unknown character {c}"
)));
}
}
}
}
}
Ok(env)
}
}