Compare commits

..

No commits in common. "fe14fed68cecf9e1b4b1b117d691c614b43c8091" and "dae33e4a4b1177607879bc69ee86f3421c166161" have entirely different histories.

40 changed files with 175 additions and 233 deletions

4
.gitignore vendored
View File

@ -12,7 +12,3 @@ Cargo.lock
# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb
# Added by cargo
/target

View File

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

View File

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

View File

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

View File

@ -1,19 +1,28 @@
use crate::utils::input_reader;
// 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 part_one() -> String {
if let Ok(lines) = input_reader::read_lines("./input/2023/day_01/input.txt") {
pub fn part_one() {
if let Ok(lines) = read_lines("./input.txt") {
let mut result = 0;
for line in lines {
result += parse_line(&line.unwrap());
}
result.to_string()
} else {
panic!("Failed to read lines from input file");
println!("{}", result);
}
}
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 {
const RADIX: u32 = 10;

View File

@ -1,21 +1,27 @@
use std::collections::HashMap;
use std::fs::File;
use std::io::{self, BufRead};
use std::path::Path;
use crate::utils::input_reader;
pub fn part_two() -> String {
if let Ok(lines) = input_reader::read_lines("./input/2023/day_01/input.txt") {
pub fn part_two() {
if let Ok(lines) = read_lines("./input.txt") {
let mut result = 0;
for line in lines {
result += parse_line(&line.unwrap());
}
result.to_string()
} else {
panic!("Failed to read lines from input file");
println!("{}", result);
}
}
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 {
const RADIX: u32 = 10;

View File

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

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

View File

@ -1,19 +1,27 @@
use crate::utils::input_reader;
use std::fs::File;
use std::io::{self, BufRead};
use std::path::Path;
pub fn part_one() -> String{
if let Ok(lines) = input_reader::read_lines("./input/2023/day_02/input.txt") {
pub fn part_one() {
if let Ok(lines) = read_lines("./input.txt") {
let mut result = 0;
for line in lines {
result += process_line(&line.unwrap());
}
result.to_string()
} else {
panic!("Failed to read lines from input file");
println!("{}", result);
}
}
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 {
let v: Vec<&str> = s.split(':').collect();

View File

@ -1,6 +1,7 @@
use std::cmp::max;
use crate::utils::input_reader;
use std::fs::File;
use std::io::{self, BufRead};
use std::path::Path;
struct Game {
red: u32,
@ -8,29 +9,32 @@ struct Game {
blue: u32,
}
pub fn part_two() -> String {
if let Ok(lines) = input_reader::read_lines("./input/2023/day_02/input.txt") {
pub fn part_two() {
if let Ok(lines) = read_lines("./input.txt") {
let mut result = 0;
for line in lines {
result += process_line(&line.unwrap());
}
result.to_string()
} else {
panic!("Failed to read lines from input file");
println!("{}", result);
}
}
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 {
let v: Vec<&str> = s.split(':').collect();
v[0].split("Game ").collect::<Vec<&str>>()[1]
let game_id: u32 = v[0].split("Game ").collect::<Vec<&str>>()[1]
.parse::<u32>()
.unwrap();
let games: Vec<&str> = v[1].split(';').collect();
let mut min_game = Game {

View File

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

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

View File

@ -1,6 +1,6 @@
use std::{fs::File, io};
use crate::utils::input_reader;
use std::fs::File;
use std::io::{self, BufRead};
use std::path::Path;
enum Engine {
Symbol(char),
@ -8,17 +8,22 @@ enum Engine {
Empty,
}
pub fn part_one() -> String {
if let Ok(lines) = input_reader::read_lines("./input/2023/day_03/input.txt") {
pub fn part_one() {
if let Ok(lines) = read_lines("./input.txt") {
let matrix = create_matrix(lines);
let result = process_matrix(&matrix);
result.to_string()
} else {
panic!("Failed to read lines from input file");
println!("{}", result);
}
}
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>> {
let mut matrix: Vec<Vec<Engine>> = Vec::new();

View File

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

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

View File

@ -1,18 +1,17 @@
use std::fs::File;
use std::io::{self, BufRead};
use std::path::Path;
use std::collections::HashSet;
use crate::utils::input_reader;
pub fn part_one() -> String {
if let Ok(lines) = input_reader::read_lines("./input/2023/day_04/input.txt") {
pub fn part_one() {
if let Ok(lines) = read_lines("./input.txt") {
let mut result = 0;
for line in lines {
result += process_line(&line.unwrap());
}
result.to_string()
} else {
panic!("Failed to read lines from input file");
println!("{}", result);
}
}
@ -47,3 +46,10 @@ 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,8 @@
[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

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

View File

@ -1,7 +1,9 @@
use crate::utils::input_reader;
use std::fs::File;
use std::io::{self, BufRead};
use std::path::Path;
pub fn part_one() -> String {
if let Ok(lines) = input_reader::read_lines("./input/2023/day_06/input.txt") {
pub fn part_one() {
if let Ok(lines) = read_lines("./input.txt") {
let mut margin = 1;
let mut race_results: Vec<Vec<u32>> = Vec::new();
@ -14,9 +16,7 @@ pub fn part_one() -> String {
margin *= process_race_result(race_results[0][i], race_results[1][i]);
}
margin.to_string()
} else {
panic!("Failed to read lines from input file");
println!("{:?}", margin);
}
}
@ -41,4 +41,10 @@ fn process_race_result(time: u32, distance: u32) -> u32 {
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,7 +1,9 @@
use crate::utils::input_reader;
use std::fs::File;
use std::io::{self, BufRead};
use std::path::Path;
pub fn part_two() -> String {
if let Ok(lines) = input_reader::read_lines("./input/2023/day_06/input.txt") {
pub fn part_two() {
if let Ok(lines) = read_lines("./input.txt") {
let mut race_result: Vec<u64> = Vec::new();
for line in lines {
@ -10,9 +12,7 @@ pub fn part_two() -> String {
let win_count = process_race_result(race_result[0], race_result[1]);
win_count.to_string()
} else {
panic!("Failed to read lines from input file");
println!("{:?}", win_count);
}
}
@ -38,4 +38,10 @@ fn process_race_result(time: u64, distance: u64) -> u64 {
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,21 +0,0 @@
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"),
};
}

View File

@ -1,13 +0,0 @@
// 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())
}

View File

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

View File

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

View File

@ -1,20 +0,0 @@
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,20 +0,0 @@
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,20 +0,0 @@
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,20 +0,0 @@
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,21 +0,0 @@
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,22 +0,0 @@
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());
}