feat: Implement day01 und day02 of aoc

This commit is contained in:
Lennard Brinkhaus 2022-12-02 14:43:20 +01:00
parent 4417549eef
commit 1802867d05
Signed by: lennard.brinkhaus
GPG Key ID: 286421EC53998B22
7 changed files with 4961 additions and 1 deletions

7
Cargo.lock generated Normal file
View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "aoc-2022"
version = "0.1.0"

2262
input/day01/question01 Normal file

File diff suppressed because it is too large Load Diff

2500
input/day02/question01 Normal file

File diff suppressed because it is too large Load Diff

45
src/day01.rs Normal file
View File

@ -0,0 +1,45 @@
use crate::utils;
pub fn execute_task01(content: &str) {
let vector = utils::convert_to_string_slice(&content);
let mut sum_vector = collect_all_calories(vector);
sort_calories_descending(&mut sum_vector);
let max: i32 = sum_vector[0];
println!("Day01 - Task01 - Biggest Calories {}", max)
}
pub fn execute_task02(content: &str) {
let vector = utils::convert_to_string_slice(&content);
let mut sum_vector = collect_all_calories(vector);
sort_calories_descending(&mut sum_vector);
let max_three: i32 = sum_vector.iter().take(3).sum();
println!("Day01 - Task02 - Biggest Calories of top three: {}", max_three);
}
fn collect_all_calories(list: Vec<&str>) -> Vec<i32> {
let mut calories: i32 = 0;
let mut calories_list: Vec<i32> = vec![];
for var in list {
if var.is_empty() {
calories_list.push(calories);
calories = 0;
continue;
}
calories += var.parse::<i32>().unwrap_or(0);
}
return calories_list;
}
fn sort_calories_descending(vec: &mut Vec<i32>) {
vec.sort_by_key(|a|
std::cmp::Reverse(*a));
}

115
src/day02.rs Normal file
View File

@ -0,0 +1,115 @@
use crate::day02::Move::{Paper, Rock, Scissors};
use crate::utils;
/*
First Colum (Opponent):
A = Rock
B = Paper
C = Scissors
Second Colum (You):
X = Rock
Y = Paper
Z = Scissors
Rock > Scissors
Scissors > Paper
Paper > Rock
Score:
Win -> 6
Draw -> 3
Loos -> 0
Rock (X) -> 1
Paper (Y) -> 2
Scissors (Z) -> 3
*/
#[derive(Clone, Copy)]
enum Move {
Rock,
Paper,
Scissors
}
pub fn execute_task01(content: &str) {
let vector = utils::convert_to_string_slice(&content);
let score: i32 = vector.into_iter().map(|line| {
let opp_char: char = line.chars().nth(0).unwrap();
let you_char: char = line.chars().nth(2).unwrap();
calculate_score_from_game(convert_to_move(opp_char), convert_to_move(you_char))
}).sum();
println!("Day02 - Task01 - Score: {}", score);
}
pub fn execute_task02(content: &str) {
let vector = utils::convert_to_string_slice(&content);
let score: i32 = vector.into_iter().map(|line| {
let opp_char: char = line.chars().nth(0).unwrap();
let you_char: char = line.chars().nth(2).unwrap();
let opp_move: Move = convert_to_move(opp_char);
calculate_score_from_game(opp_move, convert_to_conditional_move(opp_move ,you_char))
}).sum();
println!("Day02 - Task01 - Score: {}", score);
}
fn convert_to_conditional_move(opp: Move, you: char) -> Move {
match you {
'X' => match opp { // need to loose
Rock => Scissors,
Paper => Rock,
Scissors => Paper,
},
'Y' => opp, // need to draw
'Z' => match opp { // need to win
Rock => Paper,
Paper => Scissors,
Scissors => Rock,
},
_ => panic!("error"),
}
}
fn convert_to_move(c: char) -> Move {
match c {
'A' | 'X' => Rock,
'B' | 'Y' => Paper,
'C' | 'Z' => Scissors,
_ => panic!("error"),
}
}
fn calculate_score_from_game(opp: Move, you: Move) -> i32 {
match opp {
Rock => { // Opp == Rock
match you {
Rock => 3 + 1, // You Draw with Rock
Paper => 6 + 2, // You Win with Paper
Scissors => 0 + 3, // You Lose with Scissors
}
},
Paper => { // Opp == Paper
match you {
Rock => 0 + 1, // You Lose with Rock
Paper => 3 + 2, // You Draw with Paper
Scissors => 6 + 3, // You Win with Scissors
}
},
Scissors => { // Opp == Scissors
match you {
Rock => 6 + 1, // You Win with Rock
Paper => 0 + 2, // You Lose with Paper
Scissors => 3 + 3, // You Draw with Scissors
}
},
}
}

View File

@ -1,3 +1,23 @@
mod utils;
mod day01;
mod day02;
fn main() { fn main() {
println!("Hello, world!"); let path01 = "input/day01/question01";
let content01: String = match utils::load_file_as_string(path01) {
Ok(content) => content,
Err(error) => panic!("Problem opening the file: {:?}", error),
};
let path02 = "input/day02/question01";
let content02: String = match utils::load_file_as_string(path02) {
Ok(content) => content,
Err(error) => panic!("Problem opening the file: {:?}", error),
};
day01::execute_task01(&content01);
day01::execute_task02(&content01);
day02::execute_task01(&content02);
day02::execute_task02(&content02);
} }

11
src/utils.rs Normal file
View File

@ -0,0 +1,11 @@
use std::fs;
use std::error::Error;
pub fn load_file_as_string(path: &str) -> Result<String, Box<dyn Error>> {
let content = fs::read_to_string(path)?;
Ok(content)
}
pub fn convert_to_string_slice(content: &str) -> Vec<&str> {
content.lines().collect()
}