1
0
Fork 0

Compare commits

...

2 Commits

Author SHA1 Message Date
Florent DENEF 0d7097dbf8 WAiting for approval of this function. 2022-12-02 16:06:00 +01:00
Florent DENEF c90d69fdb9 Finished programming a round. 2022-12-02 16:04:40 +01:00
1 changed files with 33 additions and 5 deletions

View File

@ -55,10 +55,38 @@ impl Into<i32> for ResultPlay {
}
}
pub fn winning(play: Play) -> char {
match play {
Play::Rock => Play::Paper.into(),
Play::Paper => Play::Scissors.into(),
Play::Scissors => Play::Rock.into(),
pub fn round_score(play: char, counter_play: char) -> i32 {
let coup = Play::from(play); // translate to the play
let counter = Play::from(counter_play); // translate to the play
if coup == counter {
(ResultPlay::Draw as i32) + (counter_play as i32)
} else {
use Play::*;
use ResultPlay::*;
match (&coup, &counter) {
(Rock, Paper) => (Win as i32) + (counter as i32),
(Rock, Scissors) => (Loss as i32) + (counter as i32),
(Paper, Rock) => (Loss as i32) + (counter as i32),
(Paper, Scissors) => (Win as i32) + (counter as i32),
(Scissors, Rock) => (Win as i32) + (counter as i32),
(Scissors, Paper) => (Loss as i32) + (counter as i32),
_ => panic!("Case not covered, impossible to reach."),
}
}
}
pub fn solve_part1(file_path: &str) -> i32 {
let contents =
std::fs::read_to_string(file_path).expect("Please provide a text file as an argument.");
let lines: Vec<&str> = contents.split('\n').collect();
lines
.iter()
.map(|line| {
let first: char = line.chars().nth(0).unwrap_or('R');
let last: char = line.chars().nth(2).unwrap_or('R');
round_score(first, last)
})
.collect::<Vec<i32>>()
.iter()
.sum()
}