Merge pull request 'restructuring' (#1) from restructuring into main

Reviewed-on: #1
This commit is contained in:
Mathis 2023-12-18 21:49:45 +00:00
commit fe14fed68c
40 changed files with 233 additions and 175 deletions

6
.gitignore vendored
View File

@ -11,4 +11,8 @@ Cargo.lock
**/*.rs.bk **/*.rs.bk
# MSVC Windows builds of rustc generate these, which store debugging information # MSVC Windows builds of rustc generate these, which store debugging information
*.pdb *.pdb
# Added by cargo
/target

6
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,6 @@
{
"rust-analyzer.linkedProjects": [
"./Cargo.toml",
"./Cargo.toml"
]
}

View File

@ -1,7 +0,0 @@
mod part_one;
mod part_two;
fn main() {
//part_one::part_one();
part_two::part_two();
}

View File

@ -1,8 +0,0 @@
[package]
name = "cube-conundrum"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

View File

@ -1,7 +0,0 @@
mod part_one;
mod part_two;
fn main() {
//part_one::part_one();
part_two::part_two();
}

View File

@ -1,8 +0,0 @@
[package]
name = "gear-ratios"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

View File

@ -1,7 +0,0 @@
mod part_one;
//mod part_two;
fn main() {
part_one::part_one();
//part_two::part_two();
}

View File

@ -1,8 +0,0 @@
[package]
name = "scratchcards"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

View File

@ -1,5 +0,0 @@
mod part_one;
fn main() {
part_one::part_one();
}

View File

@ -1,8 +0,0 @@
[package]
name = "wait-for-it"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

View File

@ -1,7 +0,0 @@
//mod part_one;
mod part_two;
fn main() {
//part_one::part_one();
part_two::part_two();
}

View File

@ -1,5 +1,5 @@
[package] [package]
name = "trebuchet" name = "advent-of-code"
version = "0.1.0" version = "0.1.0"
edition = "2021" edition = "2021"

21
src/main.rs Normal file
View File

@ -0,0 +1,21 @@
mod utils;
use std::env;
mod year_2023;
fn main() {
// 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"),
};
}

13
src/utils/input_reader.rs Normal file
View File

@ -0,0 +1,13 @@
// https://doc.rust-lang.org/stable/rust-by-example/std_misc/file/read_lines.html
use std::fs::File;
use std::io::{self, BufRead};
use std::path::Path;
pub fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
where
P: AsRef<Path>,
{
let file = File::open(filename)?;
Ok(io::BufReader::new(file).lines())
}

2
src/utils/mod.rs Normal file
View File

@ -0,0 +1,2 @@
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

@ -0,0 +1,20 @@
pub mod part_one;
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,28 +1,19 @@
// https://doc.rust-lang.org/stable/rust-by-example/std_misc/file/read_lines.html use crate::utils::input_reader;
use std::fs::File;
use std::io::{self, BufRead};
use std::path::Path;
pub fn part_one() { pub fn part_one() -> String {
if let Ok(lines) = 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");
} }
} }
fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
where
P: AsRef<Path>,
{
let file = File::open(filename)?;
Ok(io::BufReader::new(file).lines())
}
fn parse_line(s: &str) -> u32 { fn parse_line(s: &str) -> u32 {
const RADIX: u32 = 10; const RADIX: u32 = 10;

View File

@ -1,27 +1,21 @@
use std::collections::HashMap; use std::collections::HashMap;
use std::fs::File;
use std::io::{self, BufRead};
use std::path::Path;
pub fn part_two() { use crate::utils::input_reader;
if let Ok(lines) = read_lines("./input.txt") {
pub fn part_two() -> String {
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");
} }
} }
fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
where
P: AsRef<Path>,
{
let file = File::open(filename)?;
Ok(io::BufReader::new(file).lines())
}
fn parse_line(s: &str) -> u32 { fn parse_line(s: &str) -> u32 {
const RADIX: u32 = 10; const RADIX: u32 = 10;

View File

@ -0,0 +1,20 @@
pub mod part_one;
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,27 +1,19 @@
use std::fs::File; use crate::utils::input_reader;
use std::io::{self, BufRead};
use std::path::Path;
pub fn part_one() { pub fn part_one() -> String{
if let Ok(lines) = 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");
} }
} }
fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
where
P: AsRef<Path>,
{
let file = File::open(filename)?;
Ok(io::BufReader::new(file).lines())
}
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();

View File

@ -1,7 +1,6 @@
use std::cmp::max; use std::cmp::max;
use std::fs::File;
use std::io::{self, BufRead}; use crate::utils::input_reader;
use std::path::Path;
struct Game { struct Game {
red: u32, red: u32,
@ -9,32 +8,29 @@ struct Game {
blue: u32, blue: u32,
} }
pub fn part_two() { pub fn part_two() -> String {
if let Ok(lines) = 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");
} }
} }
fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
where
P: AsRef<Path>,
{
let file = File::open(filename)?;
Ok(io::BufReader::new(file).lines())
}
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

@ -0,0 +1,20 @@
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

@ -1,6 +1,6 @@
use std::fs::File; use std::{fs::File, io};
use std::io::{self, BufRead};
use std::path::Path; use crate::utils::input_reader;
enum Engine { enum Engine {
Symbol(char), Symbol(char),
@ -8,22 +8,17 @@ enum Engine {
Empty, Empty,
} }
pub fn part_one() { pub fn part_one() -> String {
if let Ok(lines) = 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");
} }
} }
fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
where
P: AsRef<Path>,
{
let file = File::open(filename)?;
Ok(io::BufReader::new(file).lines())
}
fn create_matrix(lines: io::Lines<io::BufReader<File>>) -> Vec<Vec<Engine>> { fn create_matrix(lines: io::Lines<io::BufReader<File>>) -> Vec<Vec<Engine>> {
let mut matrix: Vec<Vec<Engine>> = Vec::new(); let mut matrix: Vec<Vec<Engine>> = Vec::new();

View File

@ -0,0 +1,20 @@
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

@ -1,17 +1,18 @@
use std::fs::File;
use std::io::{self, BufRead};
use std::path::Path;
use std::collections::HashSet; use std::collections::HashSet;
pub fn part_one() { use crate::utils::input_reader;
if let Ok(lines) = read_lines("./input.txt") {
pub fn part_one() -> String {
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");
} }
} }
@ -46,10 +47,3 @@ fn process_line(s: &str) -> u32 {
} }
fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
where
P: AsRef<Path>,
{
let file = File::open(filename)?;
Ok(io::BufReader::new(file).lines())
}

View File

@ -0,0 +1,21 @@
pub mod part_one;
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,9 +1,7 @@
use std::fs::File; use crate::utils::input_reader;
use std::io::{self, BufRead};
use std::path::Path;
pub fn part_one() { pub fn part_one() -> String {
if let Ok(lines) = 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();
@ -16,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");
} }
} }
@ -41,10 +41,4 @@ fn process_race_result(time: u32, distance: u32) -> u32 {
return win_count; return win_count;
} }
fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
where
P: AsRef<Path>,
{
let file = File::open(filename)?;
Ok(io::BufReader::new(file).lines())
}

View File

@ -1,9 +1,7 @@
use std::fs::File; use crate::utils::input_reader;
use std::io::{self, BufRead};
use std::path::Path;
pub fn part_two() { pub fn part_two() -> String {
if let Ok(lines) = 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 {
@ -12,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");
} }
} }
@ -38,10 +38,4 @@ fn process_race_result(time: u64, distance: u64) -> u64 {
return win_count; return win_count;
} }
fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
where
P: AsRef<Path>,
{
let file = File::open(filename)?;
Ok(io::BufReader::new(file).lines())
}

22
src/year_2023/mod.rs Normal file
View File

@ -0,0 +1,22 @@
use crate::utils::solution::Solution;
pub mod day_01_trebuchet;
pub mod day_02_cube_conundrum;
pub mod day_03_gear_ratios;
pub mod day_04_scratchcards;
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());
}