diff --git a/day_1/hint1.txt b/day_1/hint.txt similarity index 100% rename from day_1/hint1.txt rename to day_1/hint.txt diff --git a/day_1/part2 b/day_1/part2 new file mode 100755 index 0000000..b9618c6 Binary files /dev/null and b/day_1/part2 differ diff --git a/day_1/part2.rs b/day_1/part2.rs new file mode 100644 index 0000000..f344123 --- /dev/null +++ b/day_1/part2.rs @@ -0,0 +1,43 @@ +use std::fs::File; +use std::io::{self, BufRead}; +use std::path::Path; +use std::collections::HashMap; +use std::env; + +fn read_lines

(filename: P) -> io::Result>> +where P: AsRef, { + let file = File::open(filename)?; + Ok(io::BufReader::new(file).lines()) +} + +fn main() { + let mut occurrences: HashMap = HashMap::new(); + let mut left_list: Vec = vec![]; + let mut right_list: Vec = vec![]; + + let args: Vec<_> = env::args().collect(); + + if let Ok(lines) = read_lines(args[1].clone()) { + for line in lines.flatten() { + let parts = line.split(" ").collect::>(); + let lnum = parts[0].parse::().unwrap(); + let rnum = parts[1].parse::().unwrap(); + left_list.push(lnum); + right_list.push(rnum); + } + for left_n in left_list { + let entry = occurrences.entry(left_n).or_insert(0); + for right_n in &right_list { + if left_n == *right_n { + *entry += 1; + } + } + } + + let mut sum = 0; + for (num, count) in &occurrences { + sum += num * count; + } + println!("{}", sum) + } +}