Compare commits

...

3 Commits

Author SHA1 Message Date
22c7a9b737 day02 solution 2023-01-05 01:58:08 +01:00
b571cd3146 fix gitignore 2023-01-05 01:57:46 +01:00
c46397de9e move gitignore 2023-01-05 01:57:08 +01:00
7 changed files with 2675 additions and 1 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
target/

1
day01/.gitignore vendored
View File

@ -1 +0,0 @@
/target

7
day02/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 = "day02"
version = "0.1.0"

8
day02/Cargo.toml Normal file
View File

@ -0,0 +1,8 @@
[package]
name = "day02"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

3
day02/exampleInput.txt Normal file
View File

@ -0,0 +1,3 @@
A Y
B X
C Z

2500
day02/input.txt Normal file

File diff suppressed because it is too large Load Diff

156
day02/src/main.rs Normal file
View File

@ -0,0 +1,156 @@
use std::fs;
const DRAW_SCORE: i32 = 3;
const WIN_SCORE: i32 = 6;
const LOSE_SCORE: i32 = 0;
fn read_file_to_string_list(file_path: &str) -> Vec<String> {
println!("Reading file {}", file_path);
let contents = fs::read_to_string(file_path).expect("Should have been able to read the file");
let mut splitted: Vec<String> = contents
.split("\n")
.into_iter()
.map(|s| s.to_string())
.collect();
splitted.pop();
return splitted;
}
enum WinState {
Win,
Loss,
Draw,
Error,
}
enum Shape {
Rock,
Paper,
Scissors,
Error,
}
fn is_win_for_right(left: &str, right: &str) -> WinState {
use crate::WinState::*;
match left {
// Rock
"A" => match right {
"X" => Draw,
"Y" => Win,
"Z" => Loss,
_ => Error,
},
// Paper
"B" => match right {
"X" => Loss,
"Y" => Draw,
"Z" => Win,
_ => Error,
},
// Scissors
"C" => match right {
"X" => Win,
"Y" => Loss,
"Z" => Draw,
_ => Error,
},
_ => return Error,
}
}
fn find_needed_shape_for_right(left: &str, win_state: WinState) -> String {
let shape = match win_state {
WinState::Loss => match left {
"A" => "Z",
"B" => "X",
"C" => "Y",
_ => "",
},
WinState::Win => match left {
"A" => "Y",
"B" => "Z",
"C" => "X",
_ => "",
},
WinState::Draw => match left {
"A" => "X",
"B" => "Y",
"C" => "Z",
_ => "",
},
WinState::Error => todo!(),
};
return String::from(shape);
}
fn find_complicated_result(left: &str, right: &str) -> i32 {
use crate::WinState::*;
let predetermined_win_state: WinState = match right {
"X" => Loss,
"Y" => Draw,
"Z" => Win,
_ => Error,
};
let needed_shape_for_right = find_needed_shape_for_right(left, predetermined_win_state);
return find_result(left, needed_shape_for_right.as_str());
}
fn find_result(left: &str, right: &str) -> i32 {
let shape_score = shape_score(string_to_shape(right));
match is_win_for_right(left, right) {
WinState::Win => return WIN_SCORE + shape_score,
WinState::Loss => return LOSE_SCORE + shape_score,
WinState::Draw => return DRAW_SCORE + shape_score,
WinState::Error => return 0,
}
}
fn shape_score(shape: Shape) -> i32 {
return match shape {
Shape::Rock => 1,
Shape::Paper => 2,
Shape::Scissors => 3,
Shape::Error => 0,
};
}
fn string_to_shape(shapestring: &str) -> Shape {
return match shapestring {
"A" | "X" => Shape::Rock,
"B" | "Y" => Shape::Paper,
"C" | "Z" => Shape::Scissors,
_ => Shape::Error,
};
}
fn main() {
let file_content = read_file_to_string_list("input.txt");
let mut results_first_part: Vec<i32> = vec![];
let mut results_second_part: Vec<i32> = vec![];
for line in &file_content {
let left_and_right: Vec<&str> = line.split(" ").collect();
results_first_part.push(find_result(left_and_right[0], left_and_right[1]));
results_second_part.push(find_complicated_result(
left_and_right[0],
left_and_right[1],
));
}
let final_result_first_part: i32 = results_first_part.into_iter().sum();
println!("Final Result: {}", final_result_first_part);
let final_result_second_part: i32 = results_second_part.into_iter().sum();
println!("Final Result: {}", final_result_second_part)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_logic() {
assert_eq!(find_result("A", "Y"), 8);
assert_eq!(find_result("B", "X"), 1);
assert_eq!(find_result("C", "Z"), 6);
}
#[test]
fn test_second_part() {
assert_eq!(find_complicated_result("A", "Y"), 4);
assert_eq!(find_complicated_result("B", "X"), 1);
assert_eq!(find_complicated_result("C", "Z"), 7);
}
}