Make restructuring work

This commit is contained in:
LordMathis 2023-12-18 22:45:49 +01:00
parent 439b15d11e
commit e74ff52755
27 changed files with 180 additions and 33 deletions

View File

@ -1,6 +1,21 @@
pub mod year_2023; mod utils;
pub mod utils; use std::env;
mod year_2023;
fn main() { fn main() {
println!("Hello, world!"); // Parse command-line arguments
} let args: Vec<String> = env::args().collect();
if args.len() != 3 {
panic!("Usage: {} <year> <day>", args[0]);
}
let year = &args[1];
let day = &args[2];
match year.as_str() {
"2023" => year_2023::run(day),
_ => panic!("Invalid year specified"),
};
}

View File

@ -1 +1,2 @@
pub mod input_reader; pub mod input_reader;
pub mod solution;

4
src/utils/solution.rs Normal file
View File

@ -0,0 +1,4 @@
pub trait Solution {
fn part_one(&self) -> String;
fn part_two(&self) -> String;
}

View File

@ -1,2 +1,20 @@
pub mod part_one; pub mod part_one;
pub mod part_two; pub mod part_two;
use crate::utils::solution::Solution;
pub struct Day1Solution;
impl Solution for Day1Solution {
fn part_one(&self) -> String{
// Implementation for part one of day 1
// ...
part_one::part_one()
}
fn part_two(&self) -> String {
// Implementation for part two of day 1
// ...
part_two::part_two()
}
}

View File

@ -1,14 +1,16 @@
use crate::utils::input_reader; use crate::utils::input_reader;
pub fn part_one() { pub fn part_one() -> String {
if let Ok(lines) = input_reader::read_lines("./input/input.txt") { if let Ok(lines) = input_reader::read_lines("./input/2023/day_01/input.txt") {
let mut result = 0; let mut result = 0;
for line in lines { for line in lines {
result += parse_line(&line.unwrap()); result += parse_line(&line.unwrap());
} }
println!("{}", result); result.to_string()
} else {
panic!("Failed to read lines from input file");
} }
} }

View File

@ -2,15 +2,17 @@ use std::collections::HashMap;
use crate::utils::input_reader; use crate::utils::input_reader;
pub fn part_two() { pub fn part_two() -> String {
if let Ok(lines) = input_reader::read_lines("./input.txt") { if let Ok(lines) = input_reader::read_lines("./input/2023/day_01/input.txt") {
let mut result = 0; let mut result = 0;
for line in lines { for line in lines {
result += parse_line(&line.unwrap()); result += parse_line(&line.unwrap());
} }
println!("{}", result); result.to_string()
} else {
panic!("Failed to read lines from input file");
} }
} }

View File

@ -1,2 +1,20 @@
pub mod part_one; pub mod part_one;
pub mod part_two; pub mod part_two;
use crate::utils::solution::Solution;
pub struct Day2Solution;
impl Solution for Day2Solution {
fn part_one(&self) -> String{
// Implementation for part one of day 1
// ...
part_one::part_one()
}
fn part_two(&self) -> String {
// Implementation for part two of day 1
// ...
part_two::part_two()
}
}

View File

@ -1,14 +1,16 @@
use crate::utils::input_reader; use crate::utils::input_reader;
pub fn part_one() { pub fn part_one() -> String{
if let Ok(lines) = input_reader::read_lines("./input.txt") { if let Ok(lines) = input_reader::read_lines("./input/2023/day_02/input.txt") {
let mut result = 0; let mut result = 0;
for line in lines { for line in lines {
result += process_line(&line.unwrap()); result += process_line(&line.unwrap());
} }
println!("{}", result); result.to_string()
} else {
panic!("Failed to read lines from input file");
} }
} }

View File

@ -8,15 +8,17 @@ struct Game {
blue: u32, blue: u32,
} }
pub fn part_two() { pub fn part_two() -> String {
if let Ok(lines) = input_reader::read_lines("./input.txt") { if let Ok(lines) = input_reader::read_lines("./input/2023/day_02/input.txt") {
let mut result = 0; let mut result = 0;
for line in lines { for line in lines {
result += process_line(&line.unwrap()); result += process_line(&line.unwrap());
} }
println!("{}", result); result.to_string()
} else {
panic!("Failed to read lines from input file");
} }
} }
@ -25,9 +27,10 @@ pub fn part_two() {
fn process_line(s: &str) -> u32 { fn process_line(s: &str) -> u32 {
let v: Vec<&str> = s.split(':').collect(); let v: Vec<&str> = s.split(':').collect();
let game_id: u32 = v[0].split("Game ").collect::<Vec<&str>>()[1] v[0].split("Game ").collect::<Vec<&str>>()[1]
.parse::<u32>() .parse::<u32>()
.unwrap(); .unwrap();
let games: Vec<&str> = v[1].split(';').collect(); let games: Vec<&str> = v[1].split(';').collect();
let mut min_game = Game { let mut min_game = Game {

View File

@ -1 +1,20 @@
pub mod part_one; pub mod part_one;
use crate::utils::solution::Solution;
pub struct Day3Solution;
impl Solution for Day3Solution {
fn part_one(&self) -> String{
// Implementation for part one of day 1
// ...
part_one::part_one()
}
fn part_two(&self) -> String {
// Implementation for part two of day 1
// ...
String::from("Not implemented")
}
}

View File

@ -8,12 +8,14 @@ enum Engine {
Empty, Empty,
} }
pub fn part_one() { pub fn part_one() -> String {
if let Ok(lines) = input_reader::read_lines("./input.txt") { if let Ok(lines) = input_reader::read_lines("./input/2023/day_03/input.txt") {
let matrix = create_matrix(lines); let matrix = create_matrix(lines);
let result = process_matrix(&matrix); let result = process_matrix(&matrix);
println!("{}", result); result.to_string()
} else {
panic!("Failed to read lines from input file");
} }
} }

View File

@ -1 +1,20 @@
pub mod part_one; pub mod part_one;
use crate::utils::solution::Solution;
pub struct Day4Solution;
impl Solution for Day4Solution {
fn part_one(&self) -> String{
// Implementation for part one of day 1
// ...
part_one::part_one()
}
fn part_two(&self) -> String {
// Implementation for part two of day 1
// ...
String::from("Not implemented")
}
}

View File

@ -2,15 +2,17 @@ use std::collections::HashSet;
use crate::utils::input_reader; use crate::utils::input_reader;
pub fn part_one() { pub fn part_one() -> String {
if let Ok(lines) = input_reader::read_lines("./input.txt") { if let Ok(lines) = input_reader::read_lines("./input/2023/day_04/input.txt") {
let mut result = 0; let mut result = 0;
for line in lines { for line in lines {
result += process_line(&line.unwrap()); result += process_line(&line.unwrap());
} }
println!("{}", result); result.to_string()
} else {
panic!("Failed to read lines from input file");
} }
} }

View File

@ -1,2 +1,21 @@
pub mod part_one; pub mod part_one;
pub mod part_two; pub mod part_two;
use crate::utils::solution::Solution;
pub struct Day6Solution;
impl Solution for Day6Solution {
fn part_one(&self) -> String{
// Implementation for part one of day 1
// ...
part_one::part_one()
}
fn part_two(&self) -> String {
// Implementation for part two of day 1
// ...
part_two::part_two()
}
}

View File

@ -1,7 +1,7 @@
use crate::utils::input_reader; use crate::utils::input_reader;
pub fn part_one() { pub fn part_one() -> String {
if let Ok(lines) = input_reader::read_lines("./input.txt") { if let Ok(lines) = input_reader::read_lines("./input/2023/day_06/input.txt") {
let mut margin = 1; let mut margin = 1;
let mut race_results: Vec<Vec<u32>> = Vec::new(); let mut race_results: Vec<Vec<u32>> = Vec::new();
@ -14,7 +14,9 @@ pub fn part_one() {
margin *= process_race_result(race_results[0][i], race_results[1][i]); margin *= process_race_result(race_results[0][i], race_results[1][i]);
} }
println!("{:?}", margin); margin.to_string()
} else {
panic!("Failed to read lines from input file");
} }
} }

View File

@ -1,7 +1,7 @@
use crate::utils::input_reader; use crate::utils::input_reader;
pub fn part_two() { pub fn part_two() -> String {
if let Ok(lines) = input_reader::read_lines("./input.txt") { if let Ok(lines) = input_reader::read_lines("./input/2023/day_06/input.txt") {
let mut race_result: Vec<u64> = Vec::new(); let mut race_result: Vec<u64> = Vec::new();
for line in lines { for line in lines {
@ -10,7 +10,9 @@ pub fn part_two() {
let win_count = process_race_result(race_result[0], race_result[1]); let win_count = process_race_result(race_result[0], race_result[1]);
println!("{:?}", win_count); win_count.to_string()
} else {
panic!("Failed to read lines from input file");
} }
} }

View File

@ -1,5 +1,22 @@
use crate::utils::solution::Solution;
pub mod day_01_trebuchet; pub mod day_01_trebuchet;
pub mod day_02_cube_conundrum; pub mod day_02_cube_conundrum;
pub mod day_03_gear_ratios; pub mod day_03_gear_ratios;
pub mod day_04_scratchcards; pub mod day_04_scratchcards;
pub mod day_06_wait_for_it; pub mod day_06_wait_for_it;
pub fn run(day: &str) {
let solution: Box<dyn Solution> = match day {
"1" => Box::new(day_01_trebuchet::Day1Solution),
"2" => Box::new(day_02_cube_conundrum::Day2Solution),
"3" => Box::new(day_03_gear_ratios::Day3Solution),
"4" => Box::new(day_04_scratchcards::Day4Solution),
"6" => Box::new(day_06_wait_for_it::Day6Solution),
_ => panic!("Invalid day specified"),
};
// Call part_one and part_two on the solution instance
println!("Part One Result: {}", solution.part_one());
println!("Part Two Result: {}", solution.part_two());
}