add library to load data

This commit is contained in:
unintendedfraud
2023-12-03 18:11:29 +01:00
parent bad5f5e70e
commit 15fb43e600
62 changed files with 52 additions and 18 deletions
+6
View File
@@ -0,0 +1,6 @@
# Generated files
**/target/
# Well, this depends... See below!
Cargo.lock
+7
View File
@@ -5,3 +5,10 @@ version = 3
[[package]]
name = "day1"
version = "0.1.0"
dependencies = [
"util",
]
[[package]]
name = "util"
version = "0.1.0"
+1
View File
@@ -6,3 +6,4 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
util = { path = "../util/" }
+1 -17
View File
@@ -1,4 +1,4 @@
use std::{fs::File, io::Read};
use util::get_data;
fn main() {
let word_numbers: Vec<String> = vec![
@@ -123,22 +123,6 @@ fn get_first_digit(word_numbers: &Vec<String>, line: &String) -> char {
}
}
fn get_data(path: &str) -> Vec<String> {
let mut data = match File::open(path) {
Ok(file) => file,
Err(err) => panic!("failed to read the data file: {:?}", err),
};
let mut content = String::new();
data.read_to_string(&mut content)
.ok()
.expect("failed to parse file");
let lines: Vec<String> = content.split("\n").map(|l| l.to_string()).collect();
return lines;
}
fn get_digit_from_str(word: &str) -> char {
match word {
"zero" => return '0',
Binary file not shown.
+1 -1
View File
@@ -1 +1 @@
/home/mimu/projects/aoc2023/day1/target/debug/day1: /home/mimu/projects/aoc2023/day1/src/main.rs
/home/mimu/projects/aoc2023/day1/target/debug/day1: /home/mimu/projects/aoc2023/day1/src/main.rs /home/mimu/projects/aoc2023/util/src/lib.rs
Binary file not shown.
+8
View File
@@ -0,0 +1,8 @@
[package]
name = "day2"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
+3
View File
@@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
}
+8
View File
@@ -0,0 +1,8 @@
[package]
name = "util"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
+17
View File
@@ -0,0 +1,17 @@
use std::{fs::File, io::Read};
pub fn get_data(path: &str) -> Vec<String> {
let mut data = match File::open(path) {
Ok(file) => file,
Err(err) => panic!("failed to read the data file: {:?}", err),
};
let mut content = String::new();
data.read_to_string(&mut content)
.ok()
.expect("failed to parse file");
let lines: Vec<String> = content.split("\n").map(|l| l.to_string()).collect();
return lines;
}