reorganize

This commit is contained in:
CherryKitten 2023-12-02 14:54:33 +01:00
parent 489f76161e
commit f544d8bd34
Signed by: sammy
GPG key ID: 98D8F75FB0658276
5 changed files with 128 additions and 282 deletions

View file

@ -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"] }

38
src/bin/day_1.rs Normal file
View file

@ -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::<String>()
})
.collect::<Vec<String>>()
{
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::<usize>().unwrap()).sum();
println!("{result:?}");
}

90
src/bin/day_2.rs Normal file
View file

@ -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<Game> = 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::<Vec<&str>>())
.collect::<Vec<Vec<&str>>>();
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}")
}

View file

@ -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::<String>()
})
.collect::<Vec<String>>()
{
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::<usize>().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<Game> = 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::<Vec<&str>>())
.collect::<Vec<Vec<&str>>>();
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!()
}

View file

@ -1,7 +0,0 @@
use clap::Parser;
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
pub struct Args {
pub day: usize,
}