Skip to content

Commit

Permalink
Add day 1 part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr-Ker committed Dec 1, 2024
1 parent c3da9e9 commit 88442aa
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 4 deletions.
6 changes: 6 additions & 0 deletions input_examples/day01.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
3 4
4 3
2 5
1 3
3 9
3 3
16 changes: 16 additions & 0 deletions src/days.rs
Original file line number Diff line number Diff line change
@@ -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)
}
61 changes: 61 additions & 0 deletions src/days/day01.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use std::fs::read_to_string;

pub struct Day01 {
pub input_file: String,
first_list: Vec<i64>,
second_list: Vec<i64>,
}

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<i64> = self.first_list.clone();
let mut sorted_second_list: Vec<i64> = 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);
}
}
7 changes: 3 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 88442aa

Please sign in to comment.