diff --git a/README.md b/README.md new file mode 100644 index 0000000..b10d03e --- /dev/null +++ b/README.md @@ -0,0 +1,7 @@ +# mkz_aoc + +## Modules + +- file: Open files & prepare inputs +- parse: Parse contents (moslty from Strings to Vec/integers +) \ No newline at end of file diff --git a/src/file.rs b/src/file.rs index 133fe4c..8d6384d 100644 --- a/src/file.rs +++ b/src/file.rs @@ -1,7 +1,18 @@ +//! This module proposes reading file helper functions. + use std::fs; use crate::parse::string_to_numbers; +/// Read a file & return its contents in a Vec structure +/// +/// ``` +/// use mkz_aoc::file; +/// +/// fn main() { +/// let lines = file ::read_to_numbers("input.txt"); +/// } +/// ``` pub fn read_to_lines(_filename: &str) -> Result, String> { let _contents = fs::read_to_string(_filename); if let Err(v) = _contents { @@ -13,6 +24,17 @@ pub fn read_to_lines(_filename: &str) -> Result, String> { Ok(lines) } +/// Read a file & return its contents in a Vec structure +pub fn read_to_numbers(_filename: &str) -> Result, String> { + let _lines = read_to_lines(_filename); + if let Err(v) = _lines { + return Err(v); + } + + Ok(_lines.unwrap().iter().map(|x| x.parse::().unwrap()).collect::>()) +} + +/// Read a file & return its first line in a String pub fn read_to_string(_filename: &str) -> Result { let _lines = read_to_lines(_filename); @@ -26,6 +48,7 @@ pub fn read_to_string(_filename: &str) -> Result { } } +/// Read a file for a matrix & return it in a Vec> data structure. pub fn read_to_matrix(_filename: &str) -> Result>, String> { let _lines = read_to_lines(_filename); diff --git a/src/lib.rs b/src/lib.rs index 99e3888..a873ff8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,2 +1,5 @@ +#![deny(missing_docs)] +//! This create implements some useful helper functions to solve Advent of Code problems. + pub mod file; pub mod parse; \ No newline at end of file diff --git a/src/parse.rs b/src/parse.rs index d246b5b..5c81529 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -1,7 +1,11 @@ +//! Parsing helper functions + +/// Transform a String with only digits to a number array. pub fn string_to_usize_vec(input: String) -> Vec { input.chars().map(|x| x.to_digit(10).unwrap() as usize).collect() } +/// Transform a string to a i128 array. pub fn string_to_numbers(input: String) -> Vec { input .split_whitespace()