diff --git a/day1/answer1 b/day1/answer1 new file mode 100644 index 0000000..52c04f7 --- /dev/null +++ b/day1/answer1 @@ -0,0 +1 @@ +1110981 diff --git a/day1/p1.rs b/day1/p1.rs new file mode 100644 index 0000000..c66d1b5 --- /dev/null +++ b/day1/p1.rs @@ -0,0 +1,56 @@ +use std::env; +use std::fs; +use std::process; +use std::{thread, time}; + +fn main() { + //println!("Hello world!"); + + let args: Vec = env::args().collect(); + let mut path = String::from(""); + + if args.len() > 1 { + path = args[1].clone(); + } else { + println!("Specify an input file!"); + process::exit(1); + } + + if ! fs::metadata(path.clone()).is_ok() { + println!("Path does not exist!"); + process::exit(1); + } + + //let rawInput = fs::read_to_string(path.clone()).unwrap(); + //let splitInput: Vec<&str> = rawInput.lines().collect(); + + let mut left: Vec = vec![]; + let mut right: Vec = vec![]; + + for line in fs::read_to_string(path.clone()).unwrap().lines() { + //let line = splitInput[i]; + let split: Vec<&str> = line.split(" ").collect(); + + //println!("{}",split[0]); + + left.push(split[0].parse().unwrap()); + right.push(split[1].parse().unwrap()); + } + + left.sort(); + //left.dedup(); + + right.sort(); + //right.dedup(); + + let mut accum: i32 = 0; + for i in 0..right.len() { + println!("{}, {}", left[i], right[i]); + println!("Distance is {}", (right[i] - left[i]).abs()); + accum += (right[i] - left[i]).abs(); + + //thread::sleep(time::Duration::from_millis(1000)); + } + + println!("{}", accum); +}