Impl: Simple Parsing
This commit is contained in:
45
Cargo.lock
generated
45
Cargo.lock
generated
@@ -2,6 +2,15 @@
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "aho-corasick"
|
||||
version = "1.1.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b2969dcb958b36655471fc61f7e416fa76033bdd4bfed0678d8fee1e2d07a1f0"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anstream"
|
||||
version = "0.6.11"
|
||||
@@ -102,6 +111,12 @@ version = "0.4.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8"
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
version = "2.7.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "523dc4f511e55ab87b694dc30d0f820d60906ef06413f93d4d7a1385599cc149"
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.78"
|
||||
@@ -120,6 +135,35 @@ dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex"
|
||||
version = "1.10.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b62dbe01f0b06f9d8dc7d49e05a0785f153b00b2c227856282f671e0318c9b15"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"memchr",
|
||||
"regex-automata",
|
||||
"regex-syntax",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex-automata"
|
||||
version = "0.4.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5bb987efffd3c6d0d8f5f89510bb458559eab11e4f869acb20bf845e016259cd"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"memchr",
|
||||
"regex-syntax",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex-syntax"
|
||||
version = "0.8.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f"
|
||||
|
||||
[[package]]
|
||||
name = "strsim"
|
||||
version = "0.11.0"
|
||||
@@ -131,6 +175,7 @@ name = "sudoku"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"clap",
|
||||
"regex",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
||||
@@ -7,3 +7,4 @@ edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
clap = { version = "4.5.1", features = ["derive"] }
|
||||
regex = "1.10.3"
|
||||
|
||||
47
src/main.rs
47
src/main.rs
@@ -1,5 +1,7 @@
|
||||
use clap::Parser;
|
||||
|
||||
use regex::Regex;
|
||||
|
||||
#[derive(Parser)]
|
||||
#[command(version, about, long_about = None)]
|
||||
struct Cli {
|
||||
@@ -12,16 +14,16 @@ struct Cli {
|
||||
field_size: usize,
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Debug)]
|
||||
#[derive(PartialEq, Debug, Clone)]
|
||||
struct Playfield {
|
||||
fields: Vec<Vec<Field>>,
|
||||
open_fields: Vec<u8>,
|
||||
open_fields: Vec<u32>,
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq, Debug)]
|
||||
struct Field {
|
||||
possible_values: Vec<u8>,
|
||||
value: Option<u8>,
|
||||
possible_values: Vec<u32>,
|
||||
value: Option<u32>,
|
||||
}
|
||||
|
||||
impl Playfield {
|
||||
@@ -29,31 +31,30 @@ impl Playfield {
|
||||
let fields = vec![
|
||||
vec![
|
||||
Field {
|
||||
possible_values: Vec::<u8>::new(),
|
||||
possible_values: Vec::<u32>::new(),
|
||||
value: None
|
||||
};
|
||||
size
|
||||
];
|
||||
size
|
||||
];
|
||||
let open_fields = Vec::<u8>::new();
|
||||
Self {
|
||||
fields,
|
||||
open_fields,
|
||||
open_fields: Vec::<u32>::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Field {
|
||||
fn new(value: u8) -> Field {
|
||||
fn new(value: u32) -> Field {
|
||||
Self {
|
||||
possible_values: Vec::<u8>::new(),
|
||||
possible_values: Vec::<u32>::new(),
|
||||
value: Some(value),
|
||||
}
|
||||
}
|
||||
fn default() -> Field {
|
||||
Self {
|
||||
possible_values: Vec::<u8>::new(),
|
||||
possible_values: Vec::<u32>::new(),
|
||||
value: None,
|
||||
}
|
||||
}
|
||||
@@ -63,12 +64,30 @@ fn main() {
|
||||
let cli = Cli::parse();
|
||||
|
||||
let playfield = parse_playfield(&cli.field, cli.field_size);
|
||||
|
||||
println!("Hello, world!");
|
||||
}
|
||||
|
||||
fn parse_playfield(field: &String, field_size: usize) -> Playfield {
|
||||
Playfield::new(field_size)
|
||||
let playfield = &mut Playfield::new(field_size);
|
||||
|
||||
let chars = field.split(" ").collect::<Vec<&str>>();
|
||||
|
||||
for row in 0..field_size {
|
||||
for col in 0..field_size {
|
||||
let field = &mut playfield.fields[row][col];
|
||||
let char_field = chars.get(row * field_size + col).expect(&format!(
|
||||
"Input must have dimension of size {size}x{size}",
|
||||
size = field_size
|
||||
));
|
||||
|
||||
if *char_field == "0" {
|
||||
continue;
|
||||
}
|
||||
|
||||
field.value = Some(char_field.to_string().parse::<u32>().unwrap());
|
||||
}
|
||||
}
|
||||
|
||||
return playfield.clone();
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -78,7 +97,7 @@ mod tests {
|
||||
#[test]
|
||||
fn test_parse_field() {
|
||||
let input = "1 2 3 0 0 0 3 2 1";
|
||||
let field_size = 2;
|
||||
let field_size = 3;
|
||||
|
||||
let playfield = parse_playfield(&input.to_string(), field_size);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user