From f544d8bd34f4b24151fed1df3e742eeb396f5e74 Mon Sep 17 00:00:00 2001 From: CherryKitten Date: Sat, 2 Dec 2023 14:54:33 +0100 Subject: [PATCH] reorganize --- Cargo.toml | 8 -- src/bin/day_1.rs | 38 +++++++ src/bin/day_2.rs | 90 ++++++++++++++++ src/bin/run.rs | 267 ----------------------------------------------- src/lib.rs | 7 -- 5 files changed, 128 insertions(+), 282 deletions(-) create mode 100644 src/bin/day_1.rs create mode 100644 src/bin/day_2.rs delete mode 100644 src/bin/run.rs delete mode 100644 src/lib.rs diff --git a/Cargo.toml b/Cargo.toml index c47c7c6..a43aeb3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,13 +5,5 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html -[lib] -name = "aoc_2023" -path = "src/lib.rs" - -[[bin]] -name = "run" -path = "src/bin/run.rs" - [dependencies] clap = { version = "4.4.10", features = ["derive"] } diff --git a/src/bin/day_1.rs b/src/bin/day_1.rs new file mode 100644 index 0000000..fc25046 --- /dev/null +++ b/src/bin/day_1.rs @@ -0,0 +1,38 @@ +fn main() { + let input = std::fs::read_to_string("./inputs/1.txt").unwrap(); + let mut result = vec![]; + + // Yes this is very ugly, I don't care lol + let input = input.replace("one", "o1e"); + let input = input.replace("two", "t2o"); + let input = input.replace("three", "t3e"); + let input = input.replace("four", "f4r"); + let input = input.replace("five", "f5e"); + let input = input.replace("six", "s6x"); + let input = input.replace("seven", "s7n"); + let input = input.replace("eight", "e8t"); + let input = input.replace("nine", "n9e"); + + for value in input + .lines() + .map(|line| { + line.chars() + .filter(|char| char.is_numeric()) + .map(|char| char.to_string()) + .collect::() + }) + .collect::>() + { + let first = value.chars().next().unwrap(); + let second = match value.chars().last() { + None => first, + Some(v) => v, + }; + + result.push(format!("{first}{second}")); + } + + let result: usize = result.iter().map(|v| v.parse::().unwrap()).sum(); + + println!("{result:?}"); +} diff --git a/src/bin/day_2.rs b/src/bin/day_2.rs new file mode 100644 index 0000000..d6aa24d --- /dev/null +++ b/src/bin/day_2.rs @@ -0,0 +1,90 @@ +#[derive(Default, Debug)] +struct Game { + id: usize, + red: usize, + green: usize, + blue: usize, + max_red: usize, + max_green: usize, + max_blue: usize, +} + +impl Game { + fn new(id: usize) -> Self { + Game { + id, + max_red: 12, + max_blue: 14, + max_green: 13, + ..Default::default() + } + } + + fn check(&self) -> bool { + self.red <= self.max_red && self.blue <= self.max_blue && self.green <= self.max_green + } + + fn add_red(&mut self, count: usize) -> &mut Self { + if count > self.red { + self.red = count; + } + + self + } + fn add_blue(&mut self, count: usize) -> &mut Self { + if count > self.blue { + self.blue = count; + } + + self + } + fn add_green(&mut self, count: usize) -> &mut Self { + if count > self.green { + self.green = count; + } + + self + } +} + +fn main() { + let input = std::fs::read_to_string("./inputs/2.txt").unwrap(); + let result: Vec = input + .lines() + .map(|line| { + let mut line = line.strip_prefix("Game ").unwrap().split(':'); + let id = line.next().unwrap().to_string().parse().unwrap(); + let draws = line + .next() + .unwrap() + .split(';') + .map(|v| v.split(',').collect::>()) + .collect::>>(); + + let mut game = Game::new(id); + + for mut draw in draws { + while let Some(balls) = draw.pop() { + let mut balls = balls.split_whitespace(); + let count = balls.next().unwrap().to_string().parse().unwrap(); + let color = balls.next().unwrap(); + + match color { + "red" => game.add_red(count), + "blue" => game.add_blue(count), + "green" => game.add_green(count), + _ => unreachable!(), + }; + } + } + + game + }) + .collect(); + + let result1: usize = result.iter().filter(|g| g.check()).map(|v| v.id).sum(); + let result2: usize = result.iter().map(|g| g.red * g.blue * g.green).sum(); + + println!("{result1}"); + println!("{result2}") +} diff --git a/src/bin/run.rs b/src/bin/run.rs deleted file mode 100644 index 97c4d04..0000000 --- a/src/bin/run.rs +++ /dev/null @@ -1,267 +0,0 @@ -#![allow(unused)] - -use std::fmt::format; - -use aoc_2023::Args; -use clap::Parser; - -fn main() { - let args = Args::parse(); - let day = args.day; - if day == 0 || day >= 25 { - println!("That day doesn't make sense"); - return; - } - - let path = format!("./inputs/{day}.txt"); - let input = std::fs::read_to_string(path).unwrap(); - - match day { - 1 => day_1(input), - 2 => day_2(input), - 3 => day_3(input), - 4 => day_4(input), - 5 => day_5(input), - 6 => day_6(input), - 7 => day_7(input), - 8 => day_8(input), - 9 => day_9(input), - 10 => day_10(input), - 11 => day_11(input), - 12 => day_12(input), - 13 => day_13(input), - 14 => day_14(input), - 15 => day_15(input), - 16 => day_16(input), - 17 => day_17(input), - 18 => day_18(input), - 19 => day_19(input), - 20 => day_20(input), - 21 => day_21(input), - 22 => day_22(input), - 23 => day_23(input), - 24 => day_24(input), - 25 => day_25(input), - _ => (), - } -} - -fn day_1(input: String) { - let mut result = vec![]; - - // Yes this is very ugly, I don't care lol - let input = input.replace("one", "o1e"); - let input = input.replace("two", "t2o"); - let input = input.replace("three", "t3e"); - let input = input.replace("four", "f4r"); - let input = input.replace("five", "f5e"); - let input = input.replace("six", "s6x"); - let input = input.replace("seven", "s7n"); - let input = input.replace("eight", "e8t"); - let input = input.replace("nine", "n9e"); - - for value in input - .lines() - .map(|line| { - line.chars() - .filter(|char| char.is_numeric()) - .map(|char| char.to_string()) - .collect::() - }) - .collect::>() - { - let first = value.chars().next().unwrap(); - let second = match value.chars().last() { - None => first, - Some(v) => v, - }; - - result.push(format!("{first}{second}")); - } - - let result: usize = result.iter().map(|v| v.parse::().unwrap()).sum(); - - println!("{result:?}"); -} -/// Struct for Day 2 -#[derive(Default, Debug)] -struct Game { - id: usize, - red: usize, - green: usize, - blue: usize, - max_red: usize, - max_green: usize, - max_blue: usize, -} - -impl Game { - fn new(id: usize) -> Self { - Game { - id, - max_red: 12, - max_blue: 14, - max_green: 13, - ..Default::default() - } - } - - fn check(&self) -> bool { - self.red <= self.max_red && self.blue <= self.max_blue && self.green <= self.max_green - } - - fn add_red(&mut self, count: usize) -> &mut Self { - if count > self.red { - self.red = count; - } - - self - } - fn add_blue(&mut self, count: usize) -> &mut Self { - if count > self.blue { - self.blue = count; - } - - self - } - fn add_green(&mut self, count: usize) -> &mut Self { - if count > self.green { - self.green = count; - } - - self - } -} - -fn day_2(input: String) { - let result: Vec = input - .lines() - .map(|line| { - let mut line = line.strip_prefix("Game ").unwrap().split(':'); - let id = line.next().unwrap().to_string().parse().unwrap(); - let draws = line - .next() - .unwrap() - .split(';') - .map(|v| v.split(',').collect::>()) - .collect::>>(); - - let mut game = Game::new(id); - - for mut draw in draws { - while let Some(balls) = draw.pop() { - let mut balls = balls.split_whitespace(); - let count = balls.next().unwrap().to_string().parse().unwrap(); - let color = balls.next().unwrap(); - - match color { - "red" => game.add_red(count), - "blue" => game.add_blue(count), - "green" => game.add_green(count), - _ => unreachable!(), - }; - } - } - - game - }) - .collect(); - - let result1: usize = result.iter().filter(|g| g.check()).map(|v| v.id).sum(); - let result2: usize = result.iter().map(|g| g.red * g.blue * g.green).sum(); - - println!("{result1}"); - println!("{result2}") -} - -fn day_3(input: String) { - todo!() -} - -fn day_4(input: String) { - todo!() -} - -fn day_5(input: String) { - todo!() -} - -fn day_6(input: String) { - todo!() -} - -fn day_7(input: String) { - todo!() -} - -fn day_8(input: String) { - todo!() -} - -fn day_9(input: String) { - todo!() -} - -fn day_10(input: String) { - todo!() -} - -fn day_11(input: String) { - todo!() -} - -fn day_12(input: String) { - todo!() -} - -fn day_13(input: String) { - todo!() -} - -fn day_14(input: String) { - todo!() -} - -fn day_15(input: String) { - todo!() -} - -fn day_16(input: String) { - todo!() -} - -fn day_17(input: String) { - todo!() -} - -fn day_18(input: String) { - todo!() -} - -fn day_19(input: String) { - todo!() -} - -fn day_20(input: String) { - todo!() -} - -fn day_21(input: String) { - todo!() -} - -fn day_22(input: String) { - todo!() -} - -fn day_23(input: String) { - todo!() -} - -fn day_24(input: String) { - todo!() -} - -fn day_25(input: String) { - todo!() -} diff --git a/src/lib.rs b/src/lib.rs deleted file mode 100644 index 27ae5bd..0000000 --- a/src/lib.rs +++ /dev/null @@ -1,7 +0,0 @@ -use clap::Parser; - -#[derive(Parser, Debug)] -#[command(author, version, about, long_about = None)] -pub struct Args { - pub day: usize, -}