Adds Day 5 solution 1.

This commit is contained in:
Dennis Brentjes
2023-12-06 11:10:20 +01:00
parent d721d0753e
commit 2946a6a212
5 changed files with 415 additions and 2 deletions

View File

@@ -1,4 +1,4 @@
use std::{io::{BufReader, BufRead}, fs::File, str::FromStr, path::{Path, PathBuf}};
use std::{io::{BufReader, BufRead, Read}, fs::File, str::FromStr, path::{Path, PathBuf}};
use crate::error::AdventError;
@@ -8,4 +8,13 @@ pub fn read_into_vec<T:FromStr>(file_path : &Path) -> Result<Vec<T>, AdventError
let file = File::open(path).expect("no such file");
let buf = BufReader::new(file);
buf.lines().map(|line| T::from_str(line?.as_str()).map_err(Into::into)).collect::<Result<Vec<T>, AdventError>>()
}
pub fn read_into<T:FromStr>(file_path: &Path) -> Result<T, AdventError> where AdventError: From<<T as FromStr>::Err> {
let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
path.push(file_path);
let mut file = File::open(path).expect("no such file");
let mut str: String = "".to_string();
file.read_to_string(&mut str)?;
Ok(T::from_str(str.as_str())?)
}