diff --git a/.gitignore b/.gitignore index ada8be9..ccf6d7f 100644 --- a/.gitignore +++ b/.gitignore @@ -11,4 +11,8 @@ Cargo.lock **/*.rs.bk # MSVC Windows builds of rustc generate these, which store debugging information -*.pdb \ No newline at end of file +*.pdb + +# Added by cargo + +/target diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..e47d9bb --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,6 @@ +{ + "rust-analyzer.linkedProjects": [ + "./Cargo.toml", + "./Cargo.toml" + ] +} \ No newline at end of file diff --git a/2023/01-trebuchet/src/main.rs b/2023/01-trebuchet/src/main.rs deleted file mode 100644 index e072d03..0000000 --- a/2023/01-trebuchet/src/main.rs +++ /dev/null @@ -1,7 +0,0 @@ -mod part_one; -mod part_two; - -fn main() { - //part_one::part_one(); - part_two::part_two(); -} diff --git a/2023/02-cube-conundrum/Cargo.toml b/2023/02-cube-conundrum/Cargo.toml deleted file mode 100644 index e660896..0000000 --- a/2023/02-cube-conundrum/Cargo.toml +++ /dev/null @@ -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] diff --git a/2023/02-cube-conundrum/src/main.rs b/2023/02-cube-conundrum/src/main.rs deleted file mode 100644 index e072d03..0000000 --- a/2023/02-cube-conundrum/src/main.rs +++ /dev/null @@ -1,7 +0,0 @@ -mod part_one; -mod part_two; - -fn main() { - //part_one::part_one(); - part_two::part_two(); -} diff --git a/2023/03-gear-ratios/Cargo.toml b/2023/03-gear-ratios/Cargo.toml deleted file mode 100644 index e44941d..0000000 --- a/2023/03-gear-ratios/Cargo.toml +++ /dev/null @@ -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] diff --git a/2023/03-gear-ratios/src/main.rs b/2023/03-gear-ratios/src/main.rs deleted file mode 100644 index 71d7c6a..0000000 --- a/2023/03-gear-ratios/src/main.rs +++ /dev/null @@ -1,7 +0,0 @@ -mod part_one; -//mod part_two; - -fn main() { - part_one::part_one(); - //part_two::part_two(); -} diff --git a/2023/04-scratchcards/Cargo.toml b/2023/04-scratchcards/Cargo.toml deleted file mode 100644 index 89ee6ef..0000000 --- a/2023/04-scratchcards/Cargo.toml +++ /dev/null @@ -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] diff --git a/2023/04-scratchcards/src/main.rs b/2023/04-scratchcards/src/main.rs deleted file mode 100644 index d52adb4..0000000 --- a/2023/04-scratchcards/src/main.rs +++ /dev/null @@ -1,5 +0,0 @@ -mod part_one; - -fn main() { - part_one::part_one(); -} diff --git a/2023/06-wait-for-it/Cargo.toml b/2023/06-wait-for-it/Cargo.toml deleted file mode 100644 index 49c02f8..0000000 --- a/2023/06-wait-for-it/Cargo.toml +++ /dev/null @@ -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] diff --git a/2023/06-wait-for-it/src/main.rs b/2023/06-wait-for-it/src/main.rs deleted file mode 100644 index df0b383..0000000 --- a/2023/06-wait-for-it/src/main.rs +++ /dev/null @@ -1,7 +0,0 @@ -//mod part_one; -mod part_two; - -fn main() { - //part_one::part_one(); - part_two::part_two(); -} diff --git a/2023/01-trebuchet/Cargo.toml b/Cargo.toml similarity index 86% rename from 2023/01-trebuchet/Cargo.toml rename to Cargo.toml index 5d49fb2..cb161c1 100644 --- a/2023/01-trebuchet/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "trebuchet" +name = "advent-of-code" version = "0.1.0" edition = "2021" diff --git a/2023/01-trebuchet/input.txt b/input/2023/day_01/input.txt similarity index 100% rename from 2023/01-trebuchet/input.txt rename to input/2023/day_01/input.txt diff --git a/2023/01-trebuchet/input_test.txt b/input/2023/day_01/input_test.txt similarity index 100% rename from 2023/01-trebuchet/input_test.txt rename to input/2023/day_01/input_test.txt diff --git a/2023/02-cube-conundrum/input.txt b/input/2023/day_02/input.txt similarity index 100% rename from 2023/02-cube-conundrum/input.txt rename to input/2023/day_02/input.txt diff --git a/2023/02-cube-conundrum/input_test.txt b/input/2023/day_02/input_test.txt similarity index 100% rename from 2023/02-cube-conundrum/input_test.txt rename to input/2023/day_02/input_test.txt diff --git a/2023/03-gear-ratios/input.txt b/input/2023/day_03/input.txt similarity index 100% rename from 2023/03-gear-ratios/input.txt rename to input/2023/day_03/input.txt diff --git a/2023/03-gear-ratios/input_test.txt b/input/2023/day_03/input_test.txt similarity index 100% rename from 2023/03-gear-ratios/input_test.txt rename to input/2023/day_03/input_test.txt diff --git a/2023/04-scratchcards/input.txt b/input/2023/day_04/input.txt similarity index 100% rename from 2023/04-scratchcards/input.txt rename to input/2023/day_04/input.txt diff --git a/2023/04-scratchcards/input_test.txt b/input/2023/day_04/input_test.txt similarity index 100% rename from 2023/04-scratchcards/input_test.txt rename to input/2023/day_04/input_test.txt diff --git a/2023/06-wait-for-it/input.txt b/input/2023/day_06/input.txt similarity index 100% rename from 2023/06-wait-for-it/input.txt rename to input/2023/day_06/input.txt diff --git a/2023/06-wait-for-it/input_test.txt b/input/2023/day_06/input_test.txt similarity index 100% rename from 2023/06-wait-for-it/input_test.txt rename to input/2023/day_06/input_test.txt diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..26be4a0 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,21 @@ +mod utils; +use std::env; + +mod year_2023; + +fn main() { + // Parse command-line arguments + let args: Vec = env::args().collect(); + + if args.len() != 3 { + panic!("Usage: {} ", args[0]); + } + + let year = &args[1]; + let day = &args[2]; + + match year.as_str() { + "2023" => year_2023::run(day), + _ => panic!("Invalid year specified"), + }; +} \ No newline at end of file diff --git a/src/utils/input_reader.rs b/src/utils/input_reader.rs new file mode 100644 index 0000000..e7b2fb4 --- /dev/null +++ b/src/utils/input_reader.rs @@ -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

(filename: P) -> io::Result>> +where + P: AsRef, +{ + let file = File::open(filename)?; + Ok(io::BufReader::new(file).lines()) +} \ No newline at end of file diff --git a/src/utils/mod.rs b/src/utils/mod.rs new file mode 100644 index 0000000..b777308 --- /dev/null +++ b/src/utils/mod.rs @@ -0,0 +1,2 @@ +pub mod input_reader; +pub mod solution; \ No newline at end of file diff --git a/src/utils/solution.rs b/src/utils/solution.rs new file mode 100644 index 0000000..7bd1d8c --- /dev/null +++ b/src/utils/solution.rs @@ -0,0 +1,4 @@ +pub trait Solution { + fn part_one(&self) -> String; + fn part_two(&self) -> String; +} diff --git a/src/year_2023/day_01_trebuchet/mod.rs b/src/year_2023/day_01_trebuchet/mod.rs new file mode 100644 index 0000000..7575fc9 --- /dev/null +++ b/src/year_2023/day_01_trebuchet/mod.rs @@ -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() + } +} \ No newline at end of file diff --git a/2023/01-trebuchet/src/part_one.rs b/src/year_2023/day_01_trebuchet/part_one.rs similarity index 54% rename from 2023/01-trebuchet/src/part_one.rs rename to src/year_2023/day_01_trebuchet/part_one.rs index 98fa680..d6a90c6 100644 --- a/2023/01-trebuchet/src/part_one.rs +++ b/src/year_2023/day_01_trebuchet/part_one.rs @@ -1,28 +1,19 @@ -// 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; +use crate::utils::input_reader; -pub fn part_one() { - if let Ok(lines) = read_lines("./input.txt") { +pub fn part_one() -> String { + if let Ok(lines) = input_reader::read_lines("./input/2023/day_01/input.txt") { let mut result = 0; for line in lines { result += parse_line(&line.unwrap()); } - println!("{}", result); + result.to_string() + } else { + panic!("Failed to read lines from input file"); } } -fn read_lines

(filename: P) -> io::Result>> -where - P: AsRef, -{ - let file = File::open(filename)?; - Ok(io::BufReader::new(file).lines()) -} - fn parse_line(s: &str) -> u32 { const RADIX: u32 = 10; diff --git a/2023/01-trebuchet/src/part_two.rs b/src/year_2023/day_01_trebuchet/part_two.rs similarity index 76% rename from 2023/01-trebuchet/src/part_two.rs rename to src/year_2023/day_01_trebuchet/part_two.rs index 37c7ce0..8b60128 100644 --- a/2023/01-trebuchet/src/part_two.rs +++ b/src/year_2023/day_01_trebuchet/part_two.rs @@ -1,27 +1,21 @@ use std::collections::HashMap; -use std::fs::File; -use std::io::{self, BufRead}; -use std::path::Path; -pub fn part_two() { - if let Ok(lines) = read_lines("./input.txt") { +use crate::utils::input_reader; + +pub fn part_two() -> String { + if let Ok(lines) = input_reader::read_lines("./input/2023/day_01/input.txt") { let mut result = 0; for line in lines { result += parse_line(&line.unwrap()); } - println!("{}", result); + result.to_string() + } else { + panic!("Failed to read lines from input file"); } } -fn read_lines

(filename: P) -> io::Result>> -where - P: AsRef, -{ - let file = File::open(filename)?; - Ok(io::BufReader::new(file).lines()) -} fn parse_line(s: &str) -> u32 { const RADIX: u32 = 10; diff --git a/src/year_2023/day_02_cube_conundrum/mod.rs b/src/year_2023/day_02_cube_conundrum/mod.rs new file mode 100644 index 0000000..47dedb7 --- /dev/null +++ b/src/year_2023/day_02_cube_conundrum/mod.rs @@ -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() + } +} \ No newline at end of file diff --git a/2023/02-cube-conundrum/src/part_one.rs b/src/year_2023/day_02_cube_conundrum/part_one.rs similarity index 74% rename from 2023/02-cube-conundrum/src/part_one.rs rename to src/year_2023/day_02_cube_conundrum/part_one.rs index f14bcbf..0ec7a14 100644 --- a/2023/02-cube-conundrum/src/part_one.rs +++ b/src/year_2023/day_02_cube_conundrum/part_one.rs @@ -1,27 +1,19 @@ -use std::fs::File; -use std::io::{self, BufRead}; -use std::path::Path; +use crate::utils::input_reader; -pub fn part_one() { - if let Ok(lines) = read_lines("./input.txt") { +pub fn part_one() -> String{ + if let Ok(lines) = input_reader::read_lines("./input/2023/day_02/input.txt") { let mut result = 0; for line in lines { result += process_line(&line.unwrap()); } - println!("{}", result); + result.to_string() + } else { + panic!("Failed to read lines from input file"); } } -fn read_lines

(filename: P) -> io::Result>> -where - P: AsRef, -{ - let file = File::open(filename)?; - Ok(io::BufReader::new(file).lines()) -} - fn process_line(s: &str) -> u32 { let v: Vec<&str> = s.split(':').collect(); diff --git a/2023/02-cube-conundrum/src/part_two.rs b/src/year_2023/day_02_cube_conundrum/part_two.rs similarity index 76% rename from 2023/02-cube-conundrum/src/part_two.rs rename to src/year_2023/day_02_cube_conundrum/part_two.rs index 671eb80..64b3e51 100644 --- a/2023/02-cube-conundrum/src/part_two.rs +++ b/src/year_2023/day_02_cube_conundrum/part_two.rs @@ -1,7 +1,6 @@ use std::cmp::max; -use std::fs::File; -use std::io::{self, BufRead}; -use std::path::Path; + +use crate::utils::input_reader; struct Game { red: u32, @@ -9,32 +8,29 @@ struct Game { blue: u32, } -pub fn part_two() { - if let Ok(lines) = read_lines("./input.txt") { +pub fn part_two() -> String { + if let Ok(lines) = input_reader::read_lines("./input/2023/day_02/input.txt") { let mut result = 0; for line in lines { result += process_line(&line.unwrap()); } - println!("{}", result); + result.to_string() + } else { + panic!("Failed to read lines from input file"); } } -fn read_lines

(filename: P) -> io::Result>> -where - P: AsRef, -{ - let file = File::open(filename)?; - Ok(io::BufReader::new(file).lines()) -} + fn process_line(s: &str) -> u32 { let v: Vec<&str> = s.split(':').collect(); - let game_id: u32 = v[0].split("Game ").collect::>()[1] + v[0].split("Game ").collect::>()[1] .parse::() .unwrap(); + let games: Vec<&str> = v[1].split(';').collect(); let mut min_game = Game { diff --git a/src/year_2023/day_03_gear_ratios/mod.rs b/src/year_2023/day_03_gear_ratios/mod.rs new file mode 100644 index 0000000..4f5eb87 --- /dev/null +++ b/src/year_2023/day_03_gear_ratios/mod.rs @@ -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") + } +} \ No newline at end of file diff --git a/2023/03-gear-ratios/src/part_one.rs b/src/year_2023/day_03_gear_ratios/part_one.rs similarity index 89% rename from 2023/03-gear-ratios/src/part_one.rs rename to src/year_2023/day_03_gear_ratios/part_one.rs index e7c5ad1..e43544c 100644 --- a/2023/03-gear-ratios/src/part_one.rs +++ b/src/year_2023/day_03_gear_ratios/part_one.rs @@ -1,6 +1,6 @@ -use std::fs::File; -use std::io::{self, BufRead}; -use std::path::Path; +use std::{fs::File, io}; + +use crate::utils::input_reader; enum Engine { Symbol(char), @@ -8,22 +8,17 @@ enum Engine { Empty, } -pub fn part_one() { - if let Ok(lines) = read_lines("./input.txt") { +pub fn part_one() -> String { + if let Ok(lines) = input_reader::read_lines("./input/2023/day_03/input.txt") { let matrix = create_matrix(lines); let result = process_matrix(&matrix); - println!("{}", result); + result.to_string() + } else { + panic!("Failed to read lines from input file"); } } -fn read_lines

(filename: P) -> io::Result>> -where - P: AsRef, -{ - let file = File::open(filename)?; - Ok(io::BufReader::new(file).lines()) -} fn create_matrix(lines: io::Lines>) -> Vec> { let mut matrix: Vec> = Vec::new(); diff --git a/src/year_2023/day_04_scratchcards/mod.rs b/src/year_2023/day_04_scratchcards/mod.rs new file mode 100644 index 0000000..a2378e4 --- /dev/null +++ b/src/year_2023/day_04_scratchcards/mod.rs @@ -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") + } +} \ No newline at end of file diff --git a/2023/04-scratchcards/src/part_one.rs b/src/year_2023/day_04_scratchcards/part_one.rs similarity index 72% rename from 2023/04-scratchcards/src/part_one.rs rename to src/year_2023/day_04_scratchcards/part_one.rs index 6c5bcdc..aab305f 100644 --- a/2023/04-scratchcards/src/part_one.rs +++ b/src/year_2023/day_04_scratchcards/part_one.rs @@ -1,17 +1,18 @@ -use std::fs::File; -use std::io::{self, BufRead}; -use std::path::Path; use std::collections::HashSet; -pub fn part_one() { - if let Ok(lines) = read_lines("./input.txt") { +use crate::utils::input_reader; + +pub fn part_one() -> String { + if let Ok(lines) = input_reader::read_lines("./input/2023/day_04/input.txt") { let mut result = 0; for line in lines { 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

(filename: P) -> io::Result>> -where - P: AsRef, -{ - let file = File::open(filename)?; - Ok(io::BufReader::new(file).lines()) -} \ No newline at end of file diff --git a/src/year_2023/day_06_wait_for_it/mod.rs b/src/year_2023/day_06_wait_for_it/mod.rs new file mode 100644 index 0000000..d9ed005 --- /dev/null +++ b/src/year_2023/day_06_wait_for_it/mod.rs @@ -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() + } +} \ No newline at end of file diff --git a/2023/06-wait-for-it/src/part_one.rs b/src/year_2023/day_06_wait_for_it/part_one.rs similarity index 69% rename from 2023/06-wait-for-it/src/part_one.rs rename to src/year_2023/day_06_wait_for_it/part_one.rs index 93d1e1e..392b441 100644 --- a/2023/06-wait-for-it/src/part_one.rs +++ b/src/year_2023/day_06_wait_for_it/part_one.rs @@ -1,9 +1,7 @@ -use std::fs::File; -use std::io::{self, BufRead}; -use std::path::Path; +use crate::utils::input_reader; -pub fn part_one() { - if let Ok(lines) = read_lines("./input.txt") { +pub fn part_one() -> String { + if let Ok(lines) = input_reader::read_lines("./input/2023/day_06/input.txt") { let mut margin = 1; let mut race_results: Vec> = Vec::new(); @@ -16,7 +14,9 @@ pub fn part_one() { 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; } -fn read_lines

(filename: P) -> io::Result>> -where - P: AsRef, -{ - let file = File::open(filename)?; - Ok(io::BufReader::new(file).lines()) -} + diff --git a/2023/06-wait-for-it/src/part_two.rs b/src/year_2023/day_06_wait_for_it/part_two.rs similarity index 65% rename from 2023/06-wait-for-it/src/part_two.rs rename to src/year_2023/day_06_wait_for_it/part_two.rs index dfa2ab6..d4905f1 100644 --- a/2023/06-wait-for-it/src/part_two.rs +++ b/src/year_2023/day_06_wait_for_it/part_two.rs @@ -1,9 +1,7 @@ -use std::fs::File; -use std::io::{self, BufRead}; -use std::path::Path; +use crate::utils::input_reader; -pub fn part_two() { - if let Ok(lines) = read_lines("./input.txt") { +pub fn part_two() -> String { + if let Ok(lines) = input_reader::read_lines("./input/2023/day_06/input.txt") { let mut race_result: Vec = Vec::new(); for line in lines { @@ -12,7 +10,9 @@ pub fn part_two() { 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; } -fn read_lines

(filename: P) -> io::Result>> -where - P: AsRef, -{ - let file = File::open(filename)?; - Ok(io::BufReader::new(file).lines()) -} + diff --git a/src/year_2023/mod.rs b/src/year_2023/mod.rs new file mode 100644 index 0000000..9005afc --- /dev/null +++ b/src/year_2023/mod.rs @@ -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 = 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()); +} \ No newline at end of file