Olyol95 5 years ago
commit
1929b97c54
6 changed files with 111 additions and 0 deletions
  1. 1 0
      .gitignore
  2. 12 0
      Cargo.lock
  3. 7 0
      Cargo.toml
  4. 1 0
      input/day_1
  5. 3 0
      src/day1.rs
  6. 87 0
      src/main.rs

+ 1 - 0
.gitignore

@@ -0,0 +1 @@
+target

+ 12 - 0
Cargo.lock

@@ -0,0 +1,12 @@
+[root]
+name = "aoc_2016"
+version = "0.1.0"
+dependencies = [
+ "getopts 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "getopts"
+version = "0.2.14"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+

+ 7 - 0
Cargo.toml

@@ -0,0 +1,7 @@
+[package]
+name = "aoc_2016"
+version = "0.1.0"
+authors = ["Olyol95 <youle.oliver@gmail.com>"]
+
+[dependencies]
+getopts = "0.2"

+ 1 - 0
input/day_1

@@ -0,0 +1 @@
+day 1

+ 3 - 0
src/day1.rs

@@ -0,0 +1,3 @@
+pub fn main( input: &str ) {
+    println!( "{}", input );
+}

+ 87 - 0
src/main.rs

@@ -0,0 +1,87 @@
+extern crate getopts;
+use getopts::Options;
+
+use std::env;
+use std::error::Error;
+use std::fs::File;
+use std::io::prelude::*;
+use std::path::Path;
+use std::process::exit;
+
+mod day1;
+
+fn main() {
+    let args: Vec<String> = env::args().collect();
+    parse_opt( &args );
+}
+
+fn parse_opt( args: &Vec<String> ) {
+    let mut options = Options::new();
+    options.optopt( "d", "day", "specify the day number", "<day_number>" );
+    options.optopt( "i", "input", "specify an input file", "<file_name>" );
+    options.optflag( "h", "help", "print this help menu" );
+    
+    let matches = match options.parse( &args[1..] ) {
+        Ok ( m ) => { m },
+        Err( f ) => { 
+            println!( "{}", f.to_string() );
+            exit( 1 );
+        },
+    };
+
+    if matches.opt_present( "h" ) {
+        print_usage( &args[0], options );
+        exit( 0 );
+    }
+
+    let input_file = matches.opt_str( "i" );
+    let mut input = String::new();
+    match input_file {
+        Some( x ) => { input = read_input( &x ) },
+        None      => {
+            println!( "No input file specified!" );
+            exit( 1 );
+        },
+    }
+    
+    let day = matches.opt_str( "d" );
+    match day {
+        Some( x ) => {
+            match x.as_ref() {
+                "1"  => day1::main( &input ),
+                _    => { 
+                    println!( "Unknown day number {}", x );
+                    exit( 1 );
+                },
+            }
+        },
+        None => {
+            println!( "No day specified!" );
+            exit( 1 );
+        },
+    }
+}
+
+fn print_usage( prog_name: &str, options: Options ) {
+    let brief = format!( "Usage: {} -d <day_number> -i <input_file> [-h]", prog_name );
+    print!( "{}", options.usage( &brief ) );
+}
+
+fn read_input( input: &str ) -> String {
+    let path = Path::new( input );
+    let mut file = match File::open( &path ) {
+        Ok ( f ) => { f },
+        Err( f ) => {
+            println!( "Error opening file {}: {}", path.display(), f.description() );
+            exit( 1 );
+        },
+    };
+    let mut data = String::new();
+    match file.read_to_string( &mut data ) {
+        Ok ( _ ) => { }
+        Err( f ) => {
+            println!( "Error reading from file {}: {}", path.display(), f.description() );
+        },
+    }
+    data
+}