-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
86 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters