diff options
author | Justin Worthe <justin@worthe-it.co.za> | 2018-11-17 21:52:51 +0200 |
---|---|---|
committer | Justin Worthe <justin@worthe-it.co.za> | 2018-11-17 21:52:51 +0200 |
commit | 87e2140a5d46e95dc05526dc03cc2f55f195842c (patch) | |
tree | 94a106f1156ba612fa3e2cac016de55cbb67275e /src |
Initial scaffolding for doing advent of code challenges
Mostly just having a project and a convenient way of reading in a
file.
Diffstat (limited to 'src')
-rw-r--r-- | src/bin/day_1.rs | 18 | ||||
-rw-r--r-- | src/lib.rs | 23 |
2 files changed, 41 insertions, 0 deletions
diff --git a/src/bin/day_1.rs b/src/bin/day_1.rs new file mode 100644 index 0000000..7c9cdbf --- /dev/null +++ b/src/bin/day_1.rs @@ -0,0 +1,18 @@ +extern crate advent_of_code_2018; +use advent_of_code_2018::*; + +use std::error::Error; +use std::path::PathBuf; + +// cargo watch -cs "cargo run --bin day_1" + +fn main() -> Result<(), Box<Error>> { + let input = read_file(&PathBuf::from("inputs/1.txt"))?; + + println!("Input: {:?}", input); + + + + + Ok(()) +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..01d6242 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,23 @@ +use std::path::PathBuf; +use std::io::BufReader; +use std::io::prelude::*; +use std::fs::File; + +/// Reads a specified file into a vector of strings, one line of the +/// file per string. Fails if any part of reading the file fails. +pub fn read_file(file: &PathBuf) -> Result<Vec<String>, std::io::Error> { + let file = File::open(file)?; + let file_reader = BufReader::new(file); + file_reader.lines() + .collect::<Result<Vec<_>, _>>() + .map(preprocess_file_lines) +} + +/// Removes any empty lines and makes sure that lines don't have +/// problematic whitespace. +pub fn preprocess_file_lines(lines: Vec<String>) -> Vec<String> { + lines.iter() + .filter(|line| line.len() > 0) + .map(|line| line.trim_right().to_string()) + .collect() +} |