From 87e2140a5d46e95dc05526dc03cc2f55f195842c Mon Sep 17 00:00:00 2001 From: Justin Worthe Date: Sat, 17 Nov 2018 21:52:51 +0200 Subject: Initial scaffolding for doing advent of code challenges Mostly just having a project and a convenient way of reading in a file. --- src/bin/day_1.rs | 18 ++++++++++++++++++ src/lib.rs | 23 +++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 src/bin/day_1.rs create mode 100644 src/lib.rs (limited to 'src') 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> { + 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, std::io::Error> { + let file = File::open(file)?; + let file_reader = BufReader::new(file); + file_reader.lines() + .collect::, _>>() + .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) -> Vec { + lines.iter() + .filter(|line| line.len() > 0) + .map(|line| line.trim_right().to_string()) + .collect() +} -- cgit v1.2.3