diff options
author | Justin Worthe <justin@worthe-it.co.za> | 2017-11-30 21:23:08 +0200 |
---|---|---|
committer | Justin Worthe <justin@worthe-it.co.za> | 2017-11-30 21:23:08 +0200 |
commit | 2fa7e467549d9c4499932a48663034e173ec02b1 (patch) | |
tree | e62f8f114dff19d52ce18c8a0e667e22332c3533 |
Set up base repo with common file reading and CLI
-rw-r--r-- | .gitignore | 3 | ||||
-rw-r--r-- | Cargo.toml | 9 | ||||
-rw-r--r-- | src/bin/day_1.rs | 8 | ||||
-rw-r--r-- | src/lib.rs | 61 |
4 files changed, 81 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6aa1064 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/target/ +**/*.rs.bk +Cargo.lock diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..814e77e --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "advent_of_code_2017" +version = "0.1.0" +authors = ["Justin Worthe <justin@worthe-it.co.za>"] + +[dependencies] +regex = "0.2.3" +structopt = "0.1.6" +structopt-derive = "0.1.6"
\ No newline at end of file diff --git a/src/bin/day_1.rs b/src/bin/day_1.rs new file mode 100644 index 0000000..7bcfcee --- /dev/null +++ b/src/bin/day_1.rs @@ -0,0 +1,8 @@ +extern crate advent_of_code_2017; +use advent_of_code_2017::*; + +fn main() { + let args = AdventArgs::init(); + + println!("{:?} {:?}", args.part, args.input); +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..c5a0f16 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,61 @@ +extern crate structopt; +#[macro_use] +extern crate structopt_derive; +use structopt::StructOpt; + +use std::path::PathBuf; +use std::io::BufReader; +use std::io::prelude::*; +use std::fs::File; +use std::process; + +#[derive(StructOpt, Debug)] +#[structopt(name = "AOC2017", about = "An Advent of Code CLI arguments object.")] +struct AdventCli { + #[structopt(help = "Which part of the puzzle you are solving")] + part: u32, + + #[structopt(help = "Input file", parse(from_os_str))] + input: PathBuf +} + +pub struct AdventArgs { + pub part: u32, + pub input: Vec<String> +} + +impl AdventArgs { + pub fn init() -> AdventArgs { + let opt = AdventCli::from_args(); + let input = match AdventArgs::read_file(&opt.input) { + Ok(input) => input, + Err(error) => { + // Typically I would think of exiting the program like + // this to be bad form, but in this case I'm matching the + // interface of StructOpt: if the input parameters were + // invalid, just quit now with a nice message. + eprintln!("Error reading file: {}", error); + process::exit(1); + } + }; + AdventArgs { + part: opt.part, + input: input + } + } + + 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(AdventArgs::preprocess_file_lines) + } + + fn preprocess_file_lines(lines: Vec<String>) -> Vec<String> { + lines.iter() + .filter(|line| line.len() > 0) + .map(|line| line.trim().to_string()) + .collect() + } +} |