diff --git a/input_examples/day01.txt b/input_examples/day01.txt new file mode 100644 index 0000000..b8af9ad --- /dev/null +++ b/input_examples/day01.txt @@ -0,0 +1,6 @@ +3 4 +4 3 +2 5 +1 3 +3 9 +3 3 diff --git a/src/days.rs b/src/days.rs new file mode 100644 index 0000000..15951b2 --- /dev/null +++ b/src/days.rs @@ -0,0 +1,16 @@ +mod day01; + +pub fn run_day(day: u8) { + let mut result_part1: i64 = -1; + let mut result_part2: i64 = -1; + match day { + 1 => { + let day01: day01::Day01 = day01::Day01::new(String::from("/workspaces/advent-of-code-2024/input/day01.txt")); + result_part1 = day01.part1(); + result_part2 = day01.part2(); + } + _ => println!("Day not implemented yet"), + } + println!("Result of part 1 is {}", result_part1); + println!("Result of part 2 is {}", result_part2) +} diff --git a/src/days/day01.rs b/src/days/day01.rs new file mode 100644 index 0000000..375db3b --- /dev/null +++ b/src/days/day01.rs @@ -0,0 +1,61 @@ +use std::fs::read_to_string; + +pub struct Day01 { + pub input_file: String, + first_list: Vec, + second_list: Vec, +} + +impl Day01 { + pub fn new(input_file: String) -> Day01 { + let mut day01: Day01 = Day01 { + input_file: input_file, + first_list: Vec::new(), + second_list: Vec::new(), + }; + day01.open_input(); + day01 + } + fn open_input(&mut self) { + for line in read_to_string(self.input_file.clone()).unwrap().lines() { + let values: Vec<&str> = line.split_whitespace().collect(); + self.first_list.push(values[0].parse().unwrap()); + self.second_list.push(values[1].parse().unwrap()); + } + } + pub fn part1(&self) -> i64 { + let mut sorted_first_list: Vec = self.first_list.clone(); + let mut sorted_second_list: Vec = self.second_list.clone(); + sorted_first_list.sort(); + sorted_second_list.sort(); + let mut result: i64 = 0; + let len = sorted_first_list.len(); + for i in 0..len { + let distance: i64 = sorted_first_list[i] - sorted_second_list[i]; + result += distance.abs(); + } + result + } + + pub fn part2(&self) -> i64 { + 23 + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn result_part1() { + let day01: Day01 = Day01::new(String::from("/workspaces/advent-of-code-2024/input_examples/day01.txt")); + let result = day01.part1(); + assert_eq!(result, 11); + } + + #[test] + fn result_part2() { + let day01: Day01 = Day01::new(String::from("input_examples/day01.txt")); + assert_eq!(day01.part2(), 23); + } +} diff --git a/src/main.rs b/src/main.rs index 680a0a0..8665db1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,14 +3,13 @@ use clap::Parser; #[derive(Parser, Debug)] #[command(version, about, long_about = None)] struct Args { - #[arg(short, long, default_value_t = String::from("input_examples"))] - input_folder: String, #[arg(short, long, default_value_t = 1)] day: u8, } +mod days; + fn main() { let args = Args::parse(); - - println!("Running day {} with input folder <{}>!", args.day, args.input_folder); + days::run_day(args.day) } \ No newline at end of file