Skip to content

Commit 88442aa

Browse files
committed
Add day 1 part 1
1 parent c3da9e9 commit 88442aa

File tree

4 files changed

+86
-4
lines changed

4 files changed

+86
-4
lines changed

input_examples/day01.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
3 4
2+
4 3
3+
2 5
4+
1 3
5+
3 9
6+
3 3

src/days.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
mod day01;
2+
3+
pub fn run_day(day: u8) {
4+
let mut result_part1: i64 = -1;
5+
let mut result_part2: i64 = -1;
6+
match day {
7+
1 => {
8+
let day01: day01::Day01 = day01::Day01::new(String::from("/workspaces/advent-of-code-2024/input/day01.txt"));
9+
result_part1 = day01.part1();
10+
result_part2 = day01.part2();
11+
}
12+
_ => println!("Day not implemented yet"),
13+
}
14+
println!("Result of part 1 is {}", result_part1);
15+
println!("Result of part 2 is {}", result_part2)
16+
}

src/days/day01.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
use std::fs::read_to_string;
2+
3+
pub struct Day01 {
4+
pub input_file: String,
5+
first_list: Vec<i64>,
6+
second_list: Vec<i64>,
7+
}
8+
9+
impl Day01 {
10+
pub fn new(input_file: String) -> Day01 {
11+
let mut day01: Day01 = Day01 {
12+
input_file: input_file,
13+
first_list: Vec::new(),
14+
second_list: Vec::new(),
15+
};
16+
day01.open_input();
17+
day01
18+
}
19+
fn open_input(&mut self) {
20+
for line in read_to_string(self.input_file.clone()).unwrap().lines() {
21+
let values: Vec<&str> = line.split_whitespace().collect();
22+
self.first_list.push(values[0].parse().unwrap());
23+
self.second_list.push(values[1].parse().unwrap());
24+
}
25+
}
26+
pub fn part1(&self) -> i64 {
27+
let mut sorted_first_list: Vec<i64> = self.first_list.clone();
28+
let mut sorted_second_list: Vec<i64> = self.second_list.clone();
29+
sorted_first_list.sort();
30+
sorted_second_list.sort();
31+
let mut result: i64 = 0;
32+
let len = sorted_first_list.len();
33+
for i in 0..len {
34+
let distance: i64 = sorted_first_list[i] - sorted_second_list[i];
35+
result += distance.abs();
36+
}
37+
result
38+
}
39+
40+
pub fn part2(&self) -> i64 {
41+
23
42+
}
43+
}
44+
45+
#[cfg(test)]
46+
mod tests {
47+
use super::*;
48+
49+
#[test]
50+
fn result_part1() {
51+
let day01: Day01 = Day01::new(String::from("/workspaces/advent-of-code-2024/input_examples/day01.txt"));
52+
let result = day01.part1();
53+
assert_eq!(result, 11);
54+
}
55+
56+
#[test]
57+
fn result_part2() {
58+
let day01: Day01 = Day01::new(String::from("input_examples/day01.txt"));
59+
assert_eq!(day01.part2(), 23);
60+
}
61+
}

src/main.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@ use clap::Parser;
33
#[derive(Parser, Debug)]
44
#[command(version, about, long_about = None)]
55
struct Args {
6-
#[arg(short, long, default_value_t = String::from("input_examples"))]
7-
input_folder: String,
86
#[arg(short, long, default_value_t = 1)]
97
day: u8,
108
}
119

10+
mod days;
11+
1212
fn main() {
1313
let args = Args::parse();
14-
15-
println!("Running day {} with input folder <{}>!", args.day, args.input_folder);
14+
days::run_day(args.day)
1615
}

0 commit comments

Comments
 (0)