summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorReiner Herrmann <reiner@reiner-h.de>2023-12-01 11:59:01 +0100
committerReiner Herrmann <reiner@reiner-h.de>2023-12-01 11:59:01 +0100
commit1dd56a0261bb5d56825168cf8bb4774c32dc437a (patch)
treef5799dfe76f2f8adc020c0bccabcc96ee6c7f53e
parentc6bcd6346786524b91f4a4ff945ce7bccbc1e9eb (diff)
Add helper lib
-rw-r--r--src/lib.rs19
1 files changed, 19 insertions, 0 deletions
diff --git a/src/lib.rs b/src/lib.rs
new file mode 100644
index 0000000..f0199c7
--- /dev/null
+++ b/src/lib.rs
@@ -0,0 +1,19 @@
+use std::str::FromStr;
+use std::fmt::Debug;
+
+pub fn read_file(day: u8) -> String {
+ let filename = format!("inputs/day{}", day);
+ std::fs::read_to_string(filename).unwrap()
+}
+
+pub fn read_lines(day: u8) -> Vec<String> {
+ read_file(day).split_terminator('\n')
+ .map(String::from)
+ .collect()
+}
+
+pub fn read_numbers<T: FromStr>(day: u8) -> Vec<T> where <T as FromStr>::Err: Debug {
+ read_lines(day).iter()
+ .map(|n| n.parse::<T>().unwrap())
+ .collect::<Vec<T>>()
+}