Compare commits
No commits in common. "22c7a9b737cb03c15e643dbc08b82aa54ae978f8" and "21e32ff968d75592655d0a2025c408801ececbaa" have entirely different histories.
22c7a9b737
...
21e32ff968
1
.gitignore
vendored
1
.gitignore
vendored
@ -1 +0,0 @@
|
|||||||
target/
|
|
1
day01/.gitignore
vendored
Normal file
1
day01/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
/target
|
7
day02/Cargo.lock
generated
7
day02/Cargo.lock
generated
@ -1,7 +0,0 @@
|
|||||||
# This file is automatically @generated by Cargo.
|
|
||||||
# It is not intended for manual editing.
|
|
||||||
version = 3
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "day02"
|
|
||||||
version = "0.1.0"
|
|
@ -1,8 +0,0 @@
|
|||||||
[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]
|
|
@ -1,3 +0,0 @@
|
|||||||
A Y
|
|
||||||
B X
|
|
||||||
C Z
|
|
2500
day02/input.txt
2500
day02/input.txt
File diff suppressed because it is too large
Load Diff
@ -1,156 +0,0 @@
|
|||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user